<?php
if(!defined('VALID_CMS')) { die('ACCESS DENIED'); }
function sites(){

    $inCore = cmsCore::getInstance();
    $inPage = cmsPage::getInstance();
    $inDB   = cmsDatabase::getInstance();
    $inUser = cmsUser::getInstance();
    global $_CFG;
 $inPage->addHeadCSS('templates/_default_/css/torgsites.css');
 $do = $inCore->request('do', 'str', 'sites_view');

$tematika = $inCore->request('tematika', 'str', '');
if ($tematika=="torg") {$tematika_title="Торговля"; $sql_tematika="AND ut.tematika_shop=1"; }
elseif ($tematika=="serv") {$tematika_title="Услуги"; $sql_tematika="AND ut.tematika_uslugi=1";}
elseif ($tematika=="info") {$tematika_title="Информация"; $sql_tematika="AND ut.tematika_info=1";}

if ($tematika) {
$inPage->addPathway($tematika_title, '/sites/'.$tematika);
}

$menutitle  = $inCore->menuTitle();
if (!$menutitle) { $menutitle ="Каталог разделов"; }
 if ($tematika) {   
$inPage->setTitle($tematika_title.' - '.$menutitle);
 }
  
    $inCore->loadModel('torg');
    $model = new cms_model_torg();
    

        $cfg        = $inCore->loadComponentConfig('sites');
    
    // Проверяем включени ли компонент
	if(!$cfg['component_enabled']) { cmsCore::error404(); }
    

if ($do == 'sites_view'){

$sql = "SELECT ut.*, u.nickname
                FROM cms_torg_users ut
		LEFT JOIN cms_users u ON u.id = ut.id 
                WHERE ut.is_locked != 1 AND ut.is_deleted != 1 $sql_tematika
		ORDER BY ut.id";
		
$result = $inDB->query($sql);
$count = $inDB->num_rows($result);

if ($count>0){

//pagination
$perpage='10'; 
$page = $inCore->request('page', 'int', 1);

//request items using pagination
$sql .= " LIMIT ".(($page-1)*$perpage).", {$perpage}";
$result = $inDB->query($sql) ;


$users = array();
while($user = $inDB->fetch_assoc($result)){
	$user['page_logo']=($user['page_logo'] ? '/images/torg_plogo/'.$user['page_logo'] : '/images/torg_plogo/small/nopic.jpg');

	$user['email']=$model->hideEmail($user['email']);
  $users[] = $user;

                }
}	
$pagebar = cmsPage::getPagebar($count, $page, $perpage, '/sites/%page%');	




$smarty = $inCore->initSmarty('components', 'com_torg_sites_view.tpl');
$smarty->assign('cfg', $cfg);
$smarty->assign('page', $page);
$smarty->assign('users', $users);
$smarty->assign('tematika', $tematika);
$smarty->assign('tematika_title', $tematika_title);
$smarty->assign('count', $count);
$smarty->assign('pagebar', $pagebar);						
$smarty->display('com_torg_sites_view.tpl');
	
	    
 return true;      
    
 } 


    
    
    
    
}
?>