<?php
if(!defined('VALID_CMS')) { die('ACCESS DENIED'); }
function guestbook(){
global $_LANG;
$inCore = cmsCore::getInstance();
$inPage = cmsPage::getInstance();
$inDB = cmsDatabase::getInstance();
$inUser = cmsUser::getInstance();
$inPage->addPathway(Отзывы, '/guestbook');
$cfg = $inCore->loadComponentConfig('guestbook');
// Проверяем включени ли компонент
if(!$cfg['component_enabled']) { cmsCore::error404(); }
$is_admin = $inCore->userIsAdmin($inUser->id);
$user_can_delete = $inCore->isUserCan('comments/delete');
$user_can_moderate = $inCore->isUserCan('comments/moderate');
$id = $inCore->request('id', 'int', 0);
$do = $inCore->request('do', 'str', 'view');
$page = $inCore->request('page', 'int', '1');
$perpage = $cfg['perpage'];
if ($do=='view'){
$sql = "SELECT * FROM cms_guestbook ";
if (!$is_admin) {$sql .= " where is_show = 1 ";}
$sql .= " ORDER BY pubdate DESC";
$sql .= " LIMIT ".($page-1)*$perpage.", ".$perpage;
$result = $inDB->query($sql) ;
$total = $inDB->rows_count('cms_guestbook', 'is_show = 1');
if ($inDB->num_rows($result)){
while($con = $inDB->fetch_assoc($result)){
$message[] = $con;
}
$is_message = true;
} else {
$is_message = false;
}
$pagebar = cmsPage::getPagebar($total, $page, $perpage, '/guestbook/page-%page%');
$smarty = $inCore->initSmarty('components', 'com_guestbook_view.tpl');
$smarty->assign('admin', $is_admin);
$smarty->assign('delete', $user_can_delete);
$smarty->assign('moderate', $user_can_moderate);
$smarty->assign('pagetitle', $pagetitle);
$smarty->assign('message', $message);
$smarty->assign('pagebar', $pagebar);
$smarty->assign('is_message', $is_message);
$smarty->display('com_guestbook_view.tpl');
}
if ($do=='add'){
$inUser = cmsUser::getInstance();
$inPage->setTitle($_LANG['ASK_QUES']);
$inPage->addPathway($_LANG['ASK_QUES']);
$inPage->addHeadJS('components/guestbook/js/common.js');
if (!$inCore->inRequest('submit')){
$smarty = $inCore->initSmarty('components', 'com_guestbook_add.tpl');
$smarty->assign('user_id', $inUser->id);
$smarty->assign('message', $message);
$smarty->display('com_guestbook_add.tpl');
}
if ($inCore->inRequest('submit') ) {
$error = '';
$show = 0;
$captcha = $inCore->request('code', 'str', '');
$message = $inCore->request('message', 'str', '');
$name = $inCore->request('name', 'str', '');
$email = $inCore->request('email', 'str', '');
$_LANG['ERR_CAPTCHA'] = 'Неверно указан код капчи...';
if (!$inCore->checkCaptchaCode($captcha) && !$inUser->is_admin){ cmsCore::addSessionMessage($_LANG['ERR_CAPTCHA'], 'error'); $errors = true; }
if ($errors){
$inCore->redirect('/guestbook/add.html');
}
if($inUser->id || $cfg['on_moderation']){$show=1;}
$sql = "INSERT INTO cms_guestbook (pubdate, name, message, email,is_show)
VALUES (NOW(), '$name', '$message', '$email','$show')";
$inDB->query($sql);
$inCore->redirect('/guestbook');
}
}
if ($do=='delete'){
$message_id = $inCore->request('message_id', 'int', 0);
$user_id = $inUser->id;
$inDB->query("DELETE FROM cms_guestbook WHERE id={$message_id}");
$inCore->redirect('/guestbook');
}
if ($do=='moder'){
$message_id = $inCore->request('message_id', 'int', 0);
$user_id = $inUser->id;
$inDB->query("UPDATE cms_guestbook SET is_show = 1 WHERE id={$message_id} LIMIT 1");
$inCore->redirect('/guestbook');
}
}
?>