* @copyright: Copyright (c) 2010, Bunzia Alexander * @version: 1.0 * @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL */ include_once(MAIN_SOURCE_PATH.'/modules/admin/lang/'.MAIN_LANG.'/lang.php'); /** * p_query * * Класс формирования sql запросов к таблице system_pages * * @package: HiLo CMS */ class p_query extends query{ public function __construct($alias){ $this -> set_table(MAIN_PAGES_TBL ); $this -> set_alias($alias); } public function include_modules($alias,$cols=''){ $this -> include_table(MAIN_MODULES_TBL,$alias,'m_id',$cols ); return true; } public function include_tmpl($alias,$cols=''){ $this -> include_table(MAIN_TMPL_TBL,$alias,'t_id',$cols ); return true; } public function where_id($v){ $v = intval($v); if ( empty($v) ){ return FALSE; } return $this -> set_where( $this -> alias.'.f_id',$v); } public function where_pname($v){ if ( !pages::check_page($v) ){ return FALSE; } return $this -> set_where( $this -> alias.'.p_name',$v); } public function where_ffunc($v){ return $this -> set_where( $this -> alias.'.f_func',$v); } public function where_link($v){ $v = intval($v); if ( empty($v) ){ return false; } return $this -> set_where( $this -> alias.'.link_id',$v); } public function where_lang($v){ if ( empty($v) ){ return false; } return $this -> set_where( $this -> alias.'.lang',$v); } public function where_mpath($m_path){ if ( empty($m_path) || !sys_check_module_name($m_path) ){ return false; } return $this -> set_where( $this -> alias.'.m_dirname',$m_path); } public function where_mid($v){ $v = intval($v); if ( empty($v) ){ return FALSE; } return $this -> set_where( $this -> alias.'.m_id',$v); } public function orderby_mid($v){ $a = $this -> get_alias(MAIN_MODULES_TBL); return $this -> set_orderby($a.'.m_id',$v); } public function orderby_active($v){ return $this -> set_orderby($this -> alias.'.f_active',$v); } public function orderby_fname($v){ return $this -> set_orderby($this -> alias.'.f_name',$v); } public function orderby_ftitle($v){ return $this -> set_orderby($this -> alias.'.f_title',$v); } public function orderby_fid($v){ return $this -> set_orderby($this -> alias.'.f_id',$v); } /*** Функции - обработчики столбцов таблицы ***/ // посчитаем количество блоков protected function count_blocks($v,$row){ global $MAIN_DB; $sql = "SELECT COUNT(*) FROM ".MAIN_PLB_TBL." WHERE t_id='$v'"; list($count) = $MAIN_DB -> fetch_array( $MAIN_DB -> query($sql) ); return $count; } // вернём список групп для которых разрешён доступ к файлу v protected function groups($v,$row){ global $MAIN_DB; $groups = array(); $sql = "SELECT g_id FROM ".MAIN_GRP_PAGES_LINK_TBL." WHERE f_id='$v'"; $res = $MAIN_DB -> query($sql); while( list($g_id) = $MAIN_DB -> fetch_array($res) ){ $groups[]=$g_id; } return $groups; } } // end p_query /** * pages * * Класс управления записями таблицы system_pages * * @package: HiLo CMS */ class pages{ private $debug = false; private $info = array(); private $last_error = false; private $main_key = false; private $set_info = array(); private function __construct( $array=array() ){ $this -> info = $array; $this -> main_key = $array['f_id']; } public function error(){ return $this -> last_error; } public function get($k){ if ( isset($this -> info[$k]) ){ return $this -> info[$k]; } $this -> make_error(ERROR_500, sprintf(A_NOT_VAR,$k)); } public function set($k,$v=''){ if ( method_exists($this,'set_'.$k) ){ $f = 'set_'.$k; return $this -> $f($v); } $this -> set_info[$k] = $v; return true; } /******************** * SET-функции ********************/ private function set_f_name($v){ if ( empty($v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка: название страницы не указано'); return false; } $this -> set_info['f_name']=$v; return true; } private function set_p_name($v){ if ( empty($v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка: программный файл не указан'); return false; } if ( !pages::check_page($v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка: программный файл имеет неправильное название'); return FALSE; } $this -> set_info['p_name']=$v; return true; } private function set_m_dirname($v){ if ( empty($v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка: каталог не указан'); return false; } $this -> set_info['m_dirname']=$v; return true; } private function set_m_id($v){ $v = intval($v); if ( empty($v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка: номер модуля не указан'); return false; } $this -> set_info['m_id']=$v; return true; } private function set_f_func($v){ if ( empty($v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка:не указано название функции'); return false; } $this -> set_info['f_func']=$v; return true; } private function set_f_access($v){ $this -> set_info['f_access']=!empty($v) ? 1 : 0; return true; } private function set_f_active($v){ $this -> set_info['f_active']=!empty($v) ? 1 : 0; return true; } private function set_f_tmpl($v){ if ( empty($v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка: шаблон не выбран'); return false; } if ( !file_exists(MAIN_SOURCE_PATH.'/tmpl/files/'.$v) ){ $this -> make_error(ERROR_MESSAGE,'Ошибка: шаблон не существует'); return false; } $this -> set_info['f_tmpl']=$v; return true; } /**************** * GET *****************/ public function id(){ return $this -> info['f_id']; } public function module(){ return $this -> info['m_dirname']; } public function set_debug($flag){ $this -> debug = $flag; } /** * Удаление страницы * * @return: bool */ public function delete(){ global $MAIN_DB; $id = $this -> id(); if ( empty($id) ){ sys_error(ERROR_500,A_EMPTY_ID); } $sql = "DELETE FROM ".MAIN_PAGES_TBL." WHERE f_id='$id'"; $MAIN_DB -> query($sql); // чистим таблицу связей страницы и групп пользователей $sql = "DELETE FROM ".MAIN_GRP_PAGES_LINK_TBL." WHERE f_id='$id'"; $MAIN_DB -> query($sql); return TRUE; } public function update(){ global $MAIN_DB; if ( false===$this -> main_key ){ $this -> make_error(ERROR_500,A_EMPTY_ID); } if ( !empty($this -> error) ){ return $this -> error; } if ( sizeof($this -> set_info)==0 ){ $this -> make_error(ERROR_500,A_EMPTY_ARRAY); } $sql = array(); foreach( $this -> set_info as $k=>$v ){ $sql[]="`$k`='".sys_in_sql($v)."'"; } $sql = " UPDATE ".MAIN_PAGES_TBL." SET ".implode(',',$sql)." WHERE f_id='".$this -> main_key."'"; if ( !empty($this -> debug) ){ die($sql); } $MAIN_DB -> query($sql); return true; } public function insert(){ global $MAIN_DB; // при инициализации значений произошла ошибка if ( false!==$this -> error() ){ return $this -> error(); } if ( sizeof($this -> set_info)==0 ){ $this -> make_error(ERROR_500,A_EMPTY_ARRAY); } // так как запись новая, нужно проверить уникальные ключи на повторение if ( pages::exists_page($this -> set_info['p_name']) ){ $this -> make_error(ERROR_MESSAGE, sprintf(A_EXISTS_PAGE,$this -> set_info['p_name'])); return false; } if ( pages::exists_func($this -> set_info['f_func']) ){ $this -> make_error(ERROR_MESSAGE, sprintf(A_EXISTS_FUNC,$this -> set_info['f_func'])); return false; } $sql = array(); foreach( $this -> set_info as $k=>$v ){ $sql[]="`$k`='".sys_in_sql($v)."'"; } $sql = " INSERT INTO ".MAIN_PAGES_TBL." SET ".implode(',',$sql).""; if ( !empty($this -> debug) ){ die($sql); } $MAIN_DB -> query($sql); return $MAIN_DB -> insert_id(); } public function set_group($array){ global $MAIN_DB; if ( $this -> main_key ===false ){ $this -> make_error(ERROR_500); exit(); } if ( !is_array($array) ){ $array = array(); } $sql = "DELETE FROM ".MAIN_GRP_PAGES_LINK_TBL." WHERE f_id='".$this -> main_key."'"; $MAIN_DB -> query($sql); foreach( $array AS $g_id ){ $g_id = intval($g_id); if ( empty($g_id) ){ continue; } $sql = " INSERT INTO ".MAIN_GRP_PAGES_LINK_TBL." SET f_id='".$this -> main_key."', g_id='$g_id'"; $MAIN_DB -> query($sql); } } static function exists_page($v){ global $MAIN_DB; $u = new p_query('m'); $u -> where_pname($v); $u -> get('COUNT(*)'); list($count) = $u -> row(); if ( $count>0 ){ return true; } return false; } static function exists_func($v){ global $MAIN_DB; $u = new p_query('m'); $u -> where_ffunc($v); $u -> get('COUNT(*)'); list($count) = $u -> row(); if ( $count>0 ){ return true; } return false; } private function make_error($type,$msg){ $info = debug_backtrace(); $line = $info[0]['line']; $file = $info[0]['file']; switch($type){ case(ERROR_500): sys_error(ERROR_500,$msg,$file,$line); break; case(ERROR_MESSAGE): $this -> last_error = $msg; break; } } //STATIC static function check_page($text) { if ( empty($text) ){ return FALSE; } if ( preg_match('#[^a-z0-9-_]#iU',$text) ){ return FALSE; } return TRUE; } static function load_id($id){ $m = new p_query('m'); if ( !$m -> where_id($id) ){ return false; } $m -> get('*'); if ( !$m -> get_count_rows() ){ return false; } return new pages($m -> row()); } static function load_pname($id){ $m = new p_query('m'); if (!$m -> where_pname($id) ){ return false; } $m -> get('*'); if ( !$m -> get_count_rows() ){ return false; } return new pages($m -> row()); } static function create(){ return new pages( array('f_id'=>0) ); } // добавляем языки static function add_langs($pages_id) { if ( false===MAIN_MULTILANG ) { return false; } $p = pages::load_id($pages_id); if ( false===$p ) { return false; } foreach( MAIN_LANGS as $l ){ // основной язык уже добавлен if ( $l==MAIN_LANG ) { continue; } //проверяем, вдруг язык уже добавлен $pq = new p_query('p'); $pq -> where_link($p -> link_id); $pq -> where_lang($l); $pq -> get('COUNT(*)'); list($count) = $pq -> row(); if ( $count>0 ){ continue; } $new_page = pages::create(); $new_page -> set('lang',$l); $new_page -> set('link_id',$p -> link_id ); $new_page -> set('p_name',$p -> p_name ); $new_page -> set('m_dirname',$p -> m_dirname ); $new_page -> set('f_func',$p -> f_func ); $new_page -> set('f_encoding',$p -> f_encoding ); $new_page -> set('f_locale',$p -> f_locale ); $new_page -> set('f_active',$p -> f_active ); $new_page -> set('m_id',$p -> m_id ); $new_page -> set('f_access',$p -> f_access ); $new_page -> set('t_id',$p -> t_id); $new_page -> set('t_id',$p -> t_id); $new_page -> insert(); } } } ?>