<?php
/******************************************************************************/
//                                                                            //
//                             InstantCMS v1.8                                //
//                        http://www.instantcms.ru/                           //
//                                                                            //
//                   written by InstantCMS Team, 2007-2010                    //
//                produced by InstantSoft, (www.instantsoft.ru)               //
//                                                                            //
//                        LICENSED BY GNU/GPL v2                              //
//                                                                            //
/******************************************************************************/
if(!defined('VALID_CMS')) { die('ACCESS DENIED'); }

function rew($target='', $target_id=0, $labels=array()){

    $inCore     = cmsCore::getInstance();
    $inPage     = cmsPage::getInstance();
    $inDB       = cmsDatabase::getInstance();
    $inUser     = cmsUser::getInstance();

    $inCore->loadModel('rew');
    $model = new cms_model_rew();

    //Загружаем настройки компонента
	$cfg = $inCore->loadComponentConfig('rew');

    // Проверяем включени ли компонент
	if(!$cfg['component_enabled']) { return false; }

    if (!isset($cfg['bbcode'])) { $cfg['bbcode'] = 1; }
    if (!isset($cfg['regcap'])) { $cfg['regcap'] = 1; }
	if (!isset($cfg['min_karma'])) { $cfg['min_karma'] = 0; }
	if (!isset($cfg['min_karma_add'])) { $cfg['min_karma_add'] = 0; }
	if (!isset($cfg['min_karma_show'])) { $cfg['min_karma_show'] = 0; }
	if(!isset($cfg['cmm_ajax'])) { $cfg['cmm_ajax']=0;	}
    if(!isset($cfg['max_level'])) { $cfg['max_level']=5;       }

    global $_LANG;
    global $_CFG;
    $inCore->loadLanguage('components/rew');

	// Значения по умолчанию для надписей
	$def_labels = array('rew' => $_LANG['COMMENTS'], 'add' => $_LANG['ADD_COMM'], 'rss' => $_LANG['RSS_COMM'], 'not_comments' => $_LANG['NOT_COMMENT_TEXT']);
	if (!$labels) { $labels = $def_labels; }

    //Определяем адрес для редиректа назад
    $back   = $inCore->getBackURL();
    
    $do     = $inCore->request('do', 'str', 'view');

//========================================================================================================================//
//========================================================================================================================//
    if ($do == 'view' && !$target && !$target_id){
		
		$inPage->addHeadJS('components/rew/js/comments.js');
		
	        //Подключаем CSS к странице
	        $inPage->addHeadCSS('templates/'.$_CFG['template'].'/css/torg_rew.css');
		
		// Загружаем функции профиля
		$inCore->includeFile('components/users/includes/usercore.php');

		//  Заголовки и глубиномер
		$inPage->setTitle($_LANG['COMMENTS']);
		$inPage->addPathway($_LANG['COMMENTS']);
		$inPage->backButton(false);
		$inCore->loadModel('torg');
                $model_torg = new cms_model_torg();

		$cat_id    = $inCore->request('cat_id', 'int');
		
		// Пагинация
		$perpage = $cfg['perpage'] ? $cfg['perpage'] : 20;
		$page    = $inCore->request('page', 'int', 1);

		$is_admin = $inCore->userIsAdmin($inUser->id);

		//Загружаем комментарии
		$comments = array();
		// Считаем общее число количество комментариев
		$total = $inDB->rows_count('cms_torg_rew', 'published=1');
		
		// Если есть комментарии, выбираем и обрабатываем
		if ($total){

			$comments_list  = $model->getRewAll($page, $perpage, $cat_id);
			
			foreach($comments_list as $comment){
				$next = sizeof($comments);
				$comments[$next] = $comment;
			$comments[$next]['rating'] = $model_torg->buildWallRating($comments[$next]['points']);
		     	$comments[$next]['rating2'] = $model_torg->buildWallRating($comments[$next]['points2']);
		      	$comments[$next]['rating3'] = $model_torg->buildWallRating($comments[$next]['points3']);
			$comments[$next]['rating4'] = $model_torg->buildWallRating($comments[$next]['points4']);
			$comments[$next]['total_comments'] = $inDB->rows_count('cms_torg_rew', 'target_id='.$comments[$next]['target_id'].' AND published=1 AND parent_id!=0');
				if ($comments[$next]['guestname']) {
					$comments[$next]['author']      = $comments[$next]['guestname'];
					$comments[$next]['is_profile']  =false;
					$comments[$next]['ip']  		= (($cfg['cmm_ip'] == 1 || $cfg['cmm_ip'] == 2) && $comments[$next]['ip']) ? '('.$comments[$next]['ip'].')' : false;
				} else {
					$comments[$next]['author']['nickname'] = $comments[$next]['nickname'];
					$comments[$next]['author']['login'] = $comments[$next]['login'];
					$comments[$next]['is_profile'] 	= true;
					$comments[$next]['user_image'] 	= usrImageNOdb($comments[$next]['user_id'], 'small', $comments[$next]['imageurl'], $comments[$next]['is_deleted']);
					$comments[$next]['ip']  		= ($cfg['cmm_ip'] == 2 && $comments[$next]['ip']) ? '('.$comments[$next]['ip'].')' : false;
				}
				$comments[$next]['show'] 	   	= ((!$cfg['min_karma'] || $comments[$next]['votes']>=$cfg['min_karma_show']) || $inCore->userIsAdmin($comments[$next]['user_id']));
				if ($comments[$next]['votes']>0){
					$comments[$next]['votes'] = '<span class="cmm_good">+'.$comments[$next]['votes'].'</span>';
				} elseif ($comments[$next]['votes']<0){
					$comments[$next]['votes'] = '<span class="cmm_bad">'.$comments[$next]['votes'].'</span>';
				}
			}
		}
		
		// Отдаем в шаблон
		$smarty = $inCore->initSmarty('components', 'com_torg_rew_list_all.tpl');

		$smarty->assign('comments_count', $total);
		$smarty->assign('comments', $comments);
		$smarty->assign('pagebar', cmsPage::getPagebar($total, $page, $perpage, '/rew/page-%page%'));
		$smarty->assign('is_user', $inUser->id);
		$smarty->assign('cfg', $cfg);
		$smarty->assign('is_admin', $is_admin);
		$smarty->assign('url', $_SERVER['REQUEST_URI']);
		
	        
		
		$smarty->assign('cat_title', $comment['cat_title']);
		$smarty->assign('cat_id', $cat_id);
		$smarty->assign('rew_cats',  $model->rew_cats());

		$smarty->display('com_torg_rew_list_all.tpl');

    }
//========================================================================================================================//
//========================================================================================================================//
    if ($do == 'item' && !$target && !$target_id){
	
	  $inPage->addHeadJS('includes/jquery/autogrow/jquery.autogrow.js');
     
     
        if ($cfg['bbcode'] || $cfg['smiles']){
            $inPage->addHeadJS('core/js/smiles.js');
        }

    
        $cm_message     = isset($_SESSION['cm_message']) ? $_SESSION['cm_message'] : '';
        $cm_error       = isset($_SESSION['cm_error']) ? $_SESSION['cm_error'] : '';

        unset($_SESSION['cm_message']);
        unset($_SESSION['cm_error']);
		
		$inPage->addHeadJS('components/rew/js/comments.js');
		
	        //Подключаем CSS к странице
	        $inPage->addHeadCSS('templates/'.$_CFG['template'].'/css/torg_rew.css');
		
		// Загружаем функции профиля
		$inCore->includeFile('components/users/includes/usercore.php');

		//  Заголовки и глубиномер
		$inPage->setTitle($_LANG['COMMENTS']);
		$inPage->addPathway($_LANG['COMMENTS']);
		$inPage->backButton(false);
		$inCore->loadModel('torg');
                $model_torg = new cms_model_torg();
		
                // ID Товара
		$id    = $inCore->request('id', 'int');
		$target = "rew";
		// Пагинация
		$perpage = $cfg['perpage'] ? $cfg['perpage'] : 20;
		$page    = $inCore->request('page', 'int', 1);

		$is_admin = $inCore->userIsAdmin($inUser->id);
                $user_can_delete    = $inCore->isUserCan('comments/delete');
		$user_can_moderate  = $inCore->isUserCan('comments/moderate');
		//Загружаем комментарии
		$comments = array();
		// Считаем общее число количество комментариев
		$total = $inDB->rows_count('cms_torg_rew', 'published=1');
		// Если есть комментарии, выбираем и обрабатываем
		if ($total){
		    

			$comments_list  = $model->getItem($id);
			
			foreach($comments_list as $comment){
			$next = sizeof($comments);
			$comments[$next] = $comment;
			$comments[$next]['rating'] = $model_torg->buildWallRating($comments[$next]['points']);
		     	$comments[$next]['rating2'] = $model_torg->buildWallRating($comments[$next]['points2']);
		      	$comments[$next]['rating3'] = $model_torg->buildWallRating($comments[$next]['points3']);
			$comments[$next]['rating4'] = $model_torg->buildWallRating($comments[$next]['points4']);
			
			//плагин похожие товары
			$item['title']=$comments[$next]['target_title'];
		        $item['description']=$comments[$next]['target_title'];
			
			$comments[$next]['total_comments'] = $inDB->rows_count('cms_torg_rew', 'target_id='.$comments[$next]['target_id'].' AND published=1 AND parent_id!=0');
				if ($comments[$next]['guestname']) {
					$comments[$next]['author']      = $comments[$next]['guestname'];
					$comments[$next]['is_profile']  =false;
					$comments[$next]['ip']  		= (($cfg['cmm_ip'] == 1 || $cfg['cmm_ip'] == 2) && $comments[$next]['ip']) ? '('.$comments[$next]['ip'].')' : false;
				} else {
					$comments[$next]['author']['nickname'] = $comments[$next]['nickname'];
					$comments[$next]['author']['login'] = $comments[$next]['login'];
					$comments[$next]['is_profile'] 	= true;
					$comments[$next]['user_image'] 	= usrImageNOdb($comments[$next]['user_id'], 'small', $comments[$next]['imageurl'], $comments[$next]['is_deleted']);
					$comments[$next]['ip']  		= ($cfg['cmm_ip'] == 2 && $comments[$next]['ip']) ? '('.$comments[$next]['ip'].')' : false;
				}
				$comments[$next]['show'] 	   	= ((!$cfg['min_karma'] || $comments[$next]['votes']>=$cfg['min_karma_show']) || $inCore->userIsAdmin($comments[$next]['user_id']));
				if ($comments[$next]['votes']>0){
					$comments[$next]['votes'] = '<span class="cmm_good">+'.$comments[$next]['votes'].'</span>';
				} elseif ($comments[$next]['votes']<0){
					$comments[$next]['votes'] = '<span class="cmm_bad">'.$comments[$next]['votes'].'</span>';
				}
			}
		}
		
		
		//плагин похожие товары
		$item['id']=$target_id;
		$item = cmsCore::callEvent('GET_TORG_ITEM', $item);
		
		// Отдаем в шаблон
		$smarty = $inCore->initSmarty('components', 'com_torg_rew.tpl');

		$smarty->assign('comments_count', $total);
		$smarty->assign('comments', $comments);
		$smarty->assign('pagebar', cmsPage::getPagebar($total, $page, $perpage, '/rew/page-%page%'));
		$smarty->assign('is_user', $inUser->id);
		$smarty->assign('cfg', $cfg);
		$smarty->assign('item', $item);
		$smarty->assign('is_admin', $is_admin);
		$smarty->assign('url', $_SERVER['REQUEST_URI']);
		$smarty->assign('cat_title', $comment['cat_title']);
		$smarty->assign('cat_id', $comment['cat_id']);
		
	
        $smarty->assign('cm_message', $cm_message);
        $smarty->assign('cm_error', $cm_error);
        $smarty->assign('target', $target);
        $smarty->assign('target_id', $id);
	$smarty->assign('labels', $labels);
	$smarty->assign('html', $html);
        $smarty->assign('add_comment_js', "addTorgRew('".md5(session_id())."', '".$target."', '".$id."', 0)");
        $smarty->assign('user_subscribed', cmsUser::isSubscribed($inUser->id, $target, $id));
        $smarty->assign('user_can_moderate', $user_can_moderate);
	$smarty->assign('user_can_delete', $user_can_delete);
		
		
		
		$smarty->display('com_torg_rew.tpl');

    }

//========================================================================================================================//
//========================================================================================================================//
    if ($do!='add' && $do!='edit' && $do!='delete' && $target && $target_id){

        $inPage->addHeadJS('includes/jquery/autogrow/jquery.autogrow.js');
        $inPage->addHeadJS('components/rew/js/comments.js');
	
	//Подключаем CSS к странице
	$inPage->addHeadCSS('templates/'.$_CFG['template'].'/css/torg_rew.css');
	
        $inCore->loadModel('torg');
        $model_torg = new cms_model_torg();
    
        if ($cfg['bbcode'] || $cfg['smiles']){
            $inPage->addHeadJS('core/js/smiles.js');
        }

    
        $cm_message     = isset($_SESSION['cm_message']) ? $_SESSION['cm_message'] : '';
        $cm_error       = isset($_SESSION['cm_error']) ? $_SESSION['cm_error'] : '';

        unset($_SESSION['cm_message']);
        unset($_SESSION['cm_error']);

		$comments_count = $model->getRewCount($target, $target_id);
		
		$is_admin           = $inCore->userIsAdmin($inUser->id);
		$user_can_delete    = $inCore->isUserCan('comments/delete');
		$user_can_moderate  = $inCore->isUserCan('comments/moderate');

		if (!$cfg['cmm_ajax']){
			
			//activate profiles support
			$inCore->includeFile('components/users/includes/usercore.php');	
			
			//LIST COMMENTS
	
			$comments = array();
			$tree     = array();			
			
			$comments_list  = $model->getRews($target, $target_id, $cfg);
			
			foreach($comments_list as $comment){
				$next = sizeof($comments);
				$comments[$next] = $comment;
				$comments[$next]['level'] = 0;
					
		      	$comments[$next]['rating'] = $model_torg->buildWallRating($comments[$next]['points']);
		     	$comments[$next]['rating2'] = $model_torg->buildWallRating($comments[$next]['points2']);
		      	$comments[$next]['rating3'] = $model_torg->buildWallRating($comments[$next]['points3']);
			$comments[$next]['rating4'] = $model_torg->buildWallRating($comments[$next]['points4']);
				if ($comments[$next]['guestname']) {
					$comments[$next]['author']      = $comments[$next]['guestname'];
					$comments[$next]['is_profile']  = false;
					$comments[$next]['ip']  		= (($cfg['cmm_ip'] == 1 || $cfg['cmm_ip'] == 2) && $comments[$next]['ip']) ? '('.$comments[$next]['ip'].')' : false;
				} else {
					$comments[$next]['author']['nickname'] = $comments[$next]['nickname'];
					$comments[$next]['author']['login'] = $comments[$next]['login'];
					$comments[$next]['is_profile'] 	= true;
					$comments[$next]['user_image'] 	= usrImageNOdb($comments[$next]['user_id'], 'small', $comments[$next]['imageurl'], $comments[$next]['is_deleted']);
					$comments[$next]['ip']  		= ($cfg['cmm_ip'] == 2 && $comments[$next]['ip']) ? '('.$comments[$next]['ip'].')' : false;
		    
				}
				$comments[$next]['show'] 	   	= ((!$cfg['min_karma'] || $comments[$next]['votes']>=$cfg['min_karma_show']) || $inCore->userIsAdmin($comments[$next]['user_id']));
				if ($comments[$next]['votes']>0){
					$comments[$next]['votes'] = '<span class="cmm_good">+'.$comments[$next]['votes'].'</span>';
				} elseif ($comments[$next]['votes']<0){
					$comments[$next]['votes'] = '<span class="cmm_bad">'.$comments[$next]['votes'].'</span>';
				}
				$comments[$next]['is_my'] = ($inUser->id==$comments[$next]['user_id']);
				if ($inUser->id){
					$comments[$next]['is_voted'] = ($comments[$next]['is_my'] || $inDB->rows_count('cms_ratings', 'item_id='.$comments[$next]['id'].' AND target=\'rew\' AND user_id='.$inUser->id, 1));
				}
            }

			$model->buildTree(0, 0, $comments, $tree);
			
			ob_start();
		
			$smarty = $inCore->initSmarty('components', 'com_torg_rew_list.tpl');
			$smarty->assign('comments_count', $comments_count);
			$smarty->assign('comments', $tree);
			$smarty->assign('user_can_moderate', $user_can_moderate);
			$smarty->assign('user_can_delete', $user_can_delete);
			$smarty->assign('is_admin', $is_admin);
			$smarty->assign('is_user', $inUser->id);
			$smarty->assign('cfg', $cfg);
			$smarty->assign('labels', $labels);
			$smarty->assign('target', $target);
			$smarty->assign('target_id', $target_id);
			$smarty->assign('url', $_SERVER['REQUEST_URI']);
		
			$smarty->display('com_torg_rew_list.tpl');
		
			$html = ob_get_clean();
		}

        $smarty = $inCore->initSmarty('components', 'com_torg_rew_view.tpl');
        $smarty->assign('cm_message', $cm_message);
        $smarty->assign('cm_error', $cm_error);
		$smarty->assign('comments_count', $comments_count);
        $smarty->assign('target', $target);
        $smarty->assign('target_id', $target_id);
        $smarty->assign('is_admin', $is_admin);
		$smarty->assign('labels', $labels);
        $smarty->assign('is_user', $inUser->id);
        $smarty->assign('cfg', $cfg);
		$smarty->assign('html', $html);
        $smarty->assign('add_comment_js', "addTorgRew('".md5(session_id())."', '".$target."', '".$target_id."', 0)");
        $smarty->assign('user_subscribed', cmsUser::isSubscribed($inUser->id, $target, $target_id));
        $smarty->display('com_torg_rew_view.tpl');

    }

//========================================================================================================================//
//========================================================================================================================//
	if ($do=='add'){

        //Удаляем сообщение об ошибках комментариев
        unset($_SESSION['cm_error']);

        $error          = '';

        $captha_code    = $inCore->request('code', 'str', '');
        $guestname      = $inCore->request('guestname', 'str', '');
        $user_id        = $inCore->request('user_id', 'int', 0);
		
$points      = $inCore->request('points', 'int');
$points2        = $inCore->request('points2', 'int');
$points3          = $inCore->request('points3', 'int');
$points4         = $inCore->request('points4', 'int');

$plusi         = $inCore->request('plusi', 'str');
$minusi         = $inCore->request('minusi', 'str');
$zametka         = $inCore->request('zametka', 'str');


		
		if ($inCore->isUserCan('comments/bbcode') && ($cfg['bbcode'] || $cfg['smiles'])) {	
            $content        = $inCore->request('content', 'html', '');
            $content_bb     = $inDB->escape_string($content);
            $content        = $inCore->parseSmiles($content, true);
            $content        = $inDB->escape_string($content);
		} else {
			$content        = $inCore->request('content', 'str', '');
            $content_bb     = '';
		}

        $need_captcha   = (!$inUser->id || ($inUser->id && $cfg['regcap']==1));

        $target         = $inCore->request('target', 'str', '');
        $target_id      = $inCore->request('target_id', 'int', 0);
		$ip             = $_SERVER['REMOTE_ADDR'];
		
          
  
        if (!$target || !$target_id) { $error = $_LANG['ERR_UNKNOWN_TARGET']; }

        //Проверяем ошибки
        if ($user_id != $inUser->id) { $error = $_LANG['ERR_DEFINE_USER']; }
        if (!$guestname && !$user_id) { $error = $_LANG['ERR_USER_NAME']; }
        if (!$content) { $error = $_LANG['ERR_COMMENT_TEXT']; }
        if ($need_captcha && !$inCore->checkCaptchaCode($inCore->request('code', 'str'))) { $error = $_LANG['ERR_CAPTCHA']; }

        // получаем массив со ссылкой и заголовком цели комментария
        // для этого:

        //  1. узнаем ответственный компонент из cms_comment_targets
        $target_component = 'torg';
        if (!$target_component) { $error = $_LANG['ERR_UNKNOWN_TARGET'] . ' #1'; }

        //  2. подключим модель этого компонента
        $inCore->loadModel($target_component);
        $model_class  = 'cms_model_'.$target_component;
	    $target_model = new $model_class();

        if (!$target_model) { $error = $_LANG['ERR_UNKNOWN_TARGET'] . ' #2'; }

        //  3. запросим массив $target[link, title] у метода getUcCommentTarget модели
        $target_data = $target_model->getRewTarget($target, $target_id);
        if (!$target_data) { $error = $_LANG['ERR_UNKNOWN_TARGET'] . ' #3'; }

		//Если ошибок не было,
        //добавляем комментарий в базу
        if(!$error){
																
            $parent_id = $inCore->request('parent_id', 'int', 0);

			//помещаем комментарий в базу
            $comment_id = $model->addRew(array(
                                                    'parent_id'=>$parent_id,
                                                    'user_id'=>$user_id,
                                                    'target'=>$target,
                                                    'target_id'=>$target_id,
                                                    'guestname'=>$guestname,
                                                    'content'=>$content,
                                                    'content_bbcode'=>$content_bb,
                                                    'published'=>$cfg['publish'],
                                                    'target_title'=>$target_data['title'], 
                                                    'target_link'=>$target_data['link'], 
							
							'points'=>$points,
							'points2'=>$points2,
							'points3'=>$points3,
							'points4'=>$points4,
							
						    'plusi'=>$plusi,
						      'minusi'=>$minusi,
						        'zametka'=>$zametka,
													
															
                                                    'ip'=>$ip
                                                  ));

            //регистрируем событие
            cmsActions::log('add_torg_rew', array(
                'object' => 'комментарий',
                'object_url' => $target_data['link'] . '#c' . $comment_id,
                'object_id' => $comment_id,
                'target' => $target_data['title'],
                'target_url' => $target_data['link'],
                'target_id' => 0, 
                'description' => strip_tags( strlen(strip_tags($content))>140 ? substr($content, 0, 140) : $content )
            ));

			//подписываем пользователя на обновления, если нужно
            if ($inUser->id && $inCore->inRequest('subscribe')){
                cmsUser::isSubscribed($inUser->id, $target, $target_id);
			}

			//рассылаем уведомления о новом комменте
			cmsUser::sendUpdateNotify($target, $target_id);

			//проверяем и выдаем награду если нужно
            cmsUser::checkAwards( $inUser->id );

			//смотрим настройки модерации
			if (!$cfg['publish']){
				$_SESSION['cm_message'] = $_LANG['COMM_PREMODER_TEXT'];
			}
			$inConf = cmsConfig::getInstance();
			//отправляем админу уведомление о комментарии на e-mail, если нужно
			if($cfg['email']) {
				$mailmsg = $_LANG['DATE'].": ".date('d m Y (H:i)')."\n";
				$mailmsg .= $_LANG['NEW_COMMENT'].': http://'.$_SERVER['HTTP_HOST'].$target_data['link'].'#c'. $comment_id . "\n";
                $mailmsg .= "-------------------------------------------------------\n";
				$mailmsg .= strip_tags($content);
				$mailmsg = wordwrap($mailmsg, 70);
				$email_subj = str_replace('{sitename}', $inConf->sitename, $_LANG['EMAIL_SUDJECT_NEW_COMM']);
				$inCore->mailText($cfg['email'], $email_subj, $mailmsg);
			}

			//если коммент для блога или фотографии, отправляем автору уведомление на e-mail
			if($target=='torg'){
				
			
						$table      = 'cms_torg_items';
						$subj       = "Ваш обзор";
						$targetlink = 'http://'.$_SERVER['HTTP_HOST'].'/torg/item'.$target_id.'.html#c'.$comment_id;
			

				//получаем ID и e-mail автора
                $author = $model->getTargetAuthor($table, $target_id);

				if ($author){
					$needmail   = $model->isAuthorNeedMail($author['id']);
					if ($needmail && $user_id != $author['id']){

							$postdate   = date('d/m/Y H:i:s');
							$to_email   = $author['email'];
                            $from_nick  = $user_id ? dbGetField('cms_users', "id='{$user_id}'", 'nickname') : $guestname;

							$targetlink = str_replace('%author_id%', $author['id'], $targetlink);

						

							$letter_path    = PATH.'/includes/letters/newpostcomment.txt';
							$letter         = file_get_contents($letter_path);

							$letter = str_replace('{sitename}', $inConf->sitename, $letter);
							$letter = str_replace('{subj}', $subj, $letter);
							$letter = str_replace('{subjtitle}', $author['title'], $letter);
							$letter = str_replace('{targetlink}', $targetlink, $letter);
							$letter = str_replace('{date}', $postdate, $letter);
							$letter = str_replace('{from}', $from_nick, $letter);
							$inCore->mailText($to_email, $_LANG['NEW_COMMENT'].'! - '.$inConf->sitename, $letter);
					}
				}
			}
		}

        if ($error) { $_SESSION['cm_error'] = $error; }

        $inCore->redirect($back.'#c');

	}

//========================================================================================================================//
//========================================================================================================================//
	if ($do=='edit'){

        //Удаляем сообщение об ошибках комментариев
        unset($_SESSION['cm_error']);

        $error          = '';

		if ($inCore->isUserCan('comments/bbcode') && ($cfg['bbcode'] || $cfg['smiles'])) {
            $content        = $inCore->request('content', 'html', '');
            $content_bb     = $inDB->escape_string($content);
            $content        = $inCore->parseSmiles($content, true);
            $content        = $inDB->escape_string($content);
		} else {
			$content        = $inCore->request('content', 'str', '');
            $content_bb     = '';
		}

        $comment_id     = $inCore->request('comment_id', 'int', 0);

        //Проверяем ошибки
        if (!$comment_id) { $error = $_LANG['ERR_UNKNOWN_TARGET']; }
        if (!$content) { $error = $_LANG['ERR_COMMENT_TEXT']; }

        $comment = $model->getRew($comment_id, $cfg);

        if ((!$comment['is_editable'] || $comment['user_id']!=$inUser->id) && !$inUser->is_admin) {
            $error = $_LANG['ERR_UNKNOWN_TARGET'];
        }

		//Если ошибок не было,
        //обновляем комментарий в базе
        if(!$error){

			//помещаем комментарий в базу
            $model->updateRew($comment_id, array(
                'content'=>$content,
                'content_bbcode'=>$content_bb
            ));

		}

        if ($error) { $_SESSION['cm_error'] = $error; }

        $inCore->redirect($back.'#c'.$comment_id);

	}

//========================================================================================================================//
//========================================================================================================================//
    if ($do == 'delete'){

        $id = $inCore->request('id', 'int', 0);

		if($id){

			$comment_user   = $model->getRewAuthorId($id);

			$is_admin       = $inCore->userIsAdmin( $inUser->id );
			$is_my          = ($inUser->id == $comment_user);

			if($is_admin || ($is_my&&$inCore->isUserCan('comments/delete')) || $inCore->isUserCan('comments/moderate')){
				$model->deleteRew($id);
				$_SESSION['cm_message'] = $_LANG['COMM_SUC_DELETE'];
			}
		}

        $inCore->redirectBack($back.'#c');

	}

//========================================================================================================================//
} //function
?>