Просмотр файла system/classes/comments.php

Размер файла: 3.6Kb
<?php
/**********************************
*	@package: PerfCMS			  *
*	@year: 2012					  *
*	@author: Artas				  *
*	@link: http://perfcms.ru	  *
**********************************/
class Comments {
	protected $module;
	protected $module_id;
	protected $params;
	protected $ret;
	protected $mod;
	
	public function __construct($module = '', $module_id = '', $params = '', $ret='', $mod='') {
		if($module != '' && $module_id != '') {
			$this->module = $module;
			$this->module_id = $module_id;
			$this->params = $params;
			$this->ret = $ret;
			$this->mod = $mod;
		}
		else {
			echo "<b>Undefined Comments module or comments parametr id!</b>\n<br/>
					Change string \$module and \$module_id\n";
		}
	}
	
	public function view() {
		global $db;
		global $user;
		global $ames;
		global $start;
		global $lang;
		global $tpl;
		global $settings;
		$comments_r = $db->query("SELECT * FROM `". $this->module ."_comms` WHERE `". $this->module ."_id` = '".$this->module_id ."' ". ($this->params != "" ? $this->params : NULL) ."")->rowCount();
		$pages = new Paginator($comments_r, $ames);
		if($comments_r == 0) {
			echo $tpl->div('menu', $lang->word('no_posts'));
		}
		else {
			$comments_q = $db->query("SELECT * FROM `". $this->module ."_comms` WHERE `". $this->module ."_id` = '".$this->module_id ."' ". ($this->params != "" ? $this->params : NULL) ." ORDER BY time DESC LIMIT $start, $ames");
			while($comments = $comments_q->fetch()) {
				echo '<div class="post">'. nick($comments['user_id']) . ($user['level'] >=5 || $comments['user_id'] == $user['id'] ? '<a href="/'. $this->module .'/delete_comment/'. $_GET[$this->module .'_id'] .'/?post_id='. $comments['id'] .'">'. img('delete.png') .'</a>' : NULL) .'<br/>
				'. output($comments['text']) .'<br/>
				<small>('.rtime($comments['time']).')</small></div>';
			}
			$pages->view('/'. $this->module .'/'. $_GET[$this->module .'_id'] .'/comments/?');
		}
		if($settings['fast_mess'] == 'yes') { 
		$tpl->div('post', '<form action="/'.$this->module.'/add_comment/?act=create&amp;'. $this->module .'_id='. $this->module_id .'" method="post">
							<textarea name="text" rows="5" cols="26">'.(isset($_GET['reply_to']) ? '[b]'.tnick($_GET['reply_to']).'[/b], ' : NULL).'</textarea>
							<br/>
							<input type="submit" name="create" value="'. $lang->word('send') .'" /><br/>
							</form>');
		}
	}
	
	public function add($text) {
		global $db;
		global $user;
		if(!empty($text) && !empty($this->module_id)) {
			$text = substr(input($text), 0, 3000);
			$module_id = abs(intval($this->module_id));
			if($db->query("SELECT * FROM `". $this->module ."` WHERE `id` = '". $module_id ."' LIMIT 1")->rowCount() != 0) {
				$db->query("INSERT INTO `". (!empty($this->ret) ? $this->ret ."_comms`":$this->module."_comms`")." (`". (!empty($this->ret) ? $this->ret ."_id`":$this->module."_id`").", `text`, `time`, `user_id`) VALUES('".$module_id ."', '". $text ."', '". time() ."', '". $user['id'] ."')");
				// print_r($db->errorInfo());
				if(!empty($this->ret)) { $this->module = $this->ret; }
				header("Location: /". $this->module ."/".$module_id ."/comments/");
				
			} else {
				header("Location: /". $this->module ."/".$module_id ."/");
			}
		} else {
				header("Location: /". $this->module ."/".$module_id ."/");
		}
	}
	
	public function delete($post_id) {
		global $db;
		if(!empty($this->ret)) { $this->module = $this->ret; }
		$db->query("DELETE FROM `".$this->module."_comms` WHERE `id` = '". $post_id ."'");
		// print_r($db->errorInfo());
		header('Location: /'. $this->module .'/'. $this->module_id .'/comments/');
	}
	
}