class.staticPages.php 8.3 KB
<?php
/**
* @author:  Bunzia Alexander <nifus@mail.ru> <http://www.weblancer.net/users/nifus/>
* @copyright: Copyright (c) 2010, Bunzia Alexander
* @version: 1.0
* @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @package: HiLo CMS
*/
include_once(MAIN_SOURCE_PATH.'/inc/class.query.php');
include_once(MAIN_SOURCE_PATH.'/inc/class.objects.php');
include_once(MAIN_SOURCE_PATH.'/modules/admin/lang/'.MAIN_LANG.'/lang.php');


class filedsSetStaticPages
{
  //static $id = false; //  адрес новости
  
  
	static function p_type_content($v){
		if ( empty($v) ){
			return true;
		}
		
		$values = array('HTML','PHP','TMPL');
		if ( !in_array($v,$values) ){
		  throw new Exception( M_ADMIN_BLOCKS_FALSE_TYPE_CONTENT );
		}
		return $v;
	}
	
	static function p_title($v){
	  $v = trim($v);
		if ( empty($v) ){
			 throw new Exception( 'Ошибка: нужно заполнить поле "Название страницы"!' );
		}
		return $v;
	
	}
	
	

	
	static function p_url($v,$obj){
		if ( empty($v) ){
		   throw new Exception( M_ADMIN_SP_EMPTY_URL );
		}
		if ( false!==staticPages::isExistsUrl($v,$obj-> id() ) ){
		  throw new Exception( 'Ошибка: адрес "'.$v.'" уже используется!' );
		}
		
		return $v;
	}
	
	static function t_id($v){
		$v = intval($v);
		if ( empty($v) ){
		  throw new Exception( M_ADMIN_SP_EMPTY_TMPL );
			
		}
		
		return $v;
	}
	static function free_access($v){
    return !empty($v) ? '1' : '0';
  }
  
}


class filedsGetStaticPages
{

  static $__var = array();  //  для хранения временных переменных
  
  
  static function free_access($v){
    return !empty($v) ? 'checked' : '';
  }
  
  static function tmpl_list($v){
		include_once(MAIN_SOURCE_PATH.'/modules/admin/inc/class.tmpl.php');
		$p = new tmpl_q('f');
		$p -> orderby_tid('DESC');
		$p -> get('f.t_id, f.t_name');
		$s = new select_box();
		$s -> selected_id($v);
		$s -> empty_record(false);
		while( list($t_id,$t_name) = $p -> row() ){
			$s -> set($t_id,$t_name );
		}		
		return $s -> get();
	}
  static function type_content_php($v){
		if ( $v=='PHP'){
			return true;
		}else{
			return false;
		}
	}
	static function type_content_html($v){
		if ( $v=='HTML'){
			return true;
		}else{
			return false;
		}
	}
	static function type_content_tmpl($v){
		if ( $v=='TMPL'){
			return true;
		}else{
			return false;
		}
	}
	
		//	возвращаем список групп допущенных к просмотру страницы
	static function groups($v){
	global $MAIN_DB;
	
			//	группы
		$sql = "SELECT g_id,g_name FROM ".$MAIN_DB -> prefix("system_groups")." WHERE hidden='0'";
		$res = $MAIN_DB -> query($sql);
		$groups = array();
		while( $row = $MAIN_DB -> fetch_array($res) )
		{
		  $groups[$row['g_id']]=$row;
		}
		if ( empty($v) ){
		  return $groups;
		}
		  //  допущенные 
		$sql = "SELECT g_id FROM ".$MAIN_DB -> prefix("system_stat_pages_groups_link")." WHERE p_id='$v'";
		$res = $MAIN_DB -> query($sql);
		while(list($g_id) = $MAIN_DB -> fetch_array($res) ){
			$groups[$g_id]['checked']=true;
		}
	
		return $groups;
	}
	
		//	проверяем доступ группы к странице v
	static function check_access($v,$row){
	global $MAIN_DB,$MAIN_USER;
	  
	  if ( ($row['free_access']==1) )
	  {
	    return true;
	  }
	    //  если перед нами языковая копия, то пользуемся правами оригинала
	  if ( $row['p_id']!=$row['link_id'] ){
	    $v = $row['link_id'];
	  }
	 
		$g_id = $MAIN_USER -> group();
		$sql = "
			SELECT count(*) 
			FROM ".$MAIN_DB -> prefix("system_stat_pages_groups_link")."
			WHERE p_id='$v' AND g_id='$g_id'";
			
		list($count) = $MAIN_DB -> fetch_array( $MAIN_DB -> query($sql) );
		if ( $count>0 ){
			return true;
		}else{
			return false;
		}
	}
	
	
	
	 static function lang_list($v){
    if ( !defined('MAIN_LANGS') ){
      return false;
    }
    $langs = explode(',',MAIN_LANGS);
    $s = new select_box();
    $s -> selected_id($v);
    $s -> empty_record(false);
    foreach( $langs as $lng )
    {
      $s -> set($lng,$lng);
    }
    return $s -> get();
  }
  
  
  static function lang_links($v){
    if ( !defined('MAIN_LANGS') ){
      return false;
    }
    $langs = explode(',',MAIN_LANGS);
    $result = array();
    foreach( $langs as $l ){
      $q = new staticPagesQuery('q');
      $q -> where_link($v);
      $q -> where_lang($l);
      $q -> get('COUNT(*)');
      list($count) = $q -> row();
      $result[] = array('lang'=>$l,'exists'=> (( $count ) ? 1 : 0) );
    }
   
    return $result;
  }

}



class staticPagesQuery extends query{

  var $fields_get = 'filedsGetStaticPages';
	
	public function __construct($alias){
	global $MAIN_DB;
		$this -> set_table( $MAIN_DB -> prefix('system_stat_pages') );
		$this -> set_alias($alias);
	}
	
	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.'.p_id',$v);
	}
	
  public function where_link($v){
    if ( false===MAIN_MULTILANG ){
      return $this -> where_id($v);
    }
		$v = intval($v);
		if ( empty($v) ){
			return false;
		}
		return $this -> set_where( $this -> alias.'.link_id',$v);
	}
	
	public function where_main_rows(){
		if ( false===MAIN_MULTILANG ){
      return false;
    }
		return $this -> set_anothe_where( $this -> alias.'.p_id='.$this -> alias.'.link_id');
	}
	
	public function where_url($v){
		if ( empty($v) ){
			return false;
		}
		if ( preg_match('#[^a-z0-9./\-_\#&%]#i',$v) ){
			return false;
		}
		return $this -> set_where( $this -> alias.'.p_url',$v);
	}
	
	public function where_not_id($v){
		$v = intval($v);
		if ( empty($v) ){
			return false;
		}  
		return $this -> set_anothe_where( $this -> alias.'.p_id!="'.$v.'"');
	}
	
	
  public function where_lang($v){
    if ( false===MAIN_MULTILANG ){
      return false;
    }
    $langs = explode(',',MAIN_LANGS);
		if ( !in_array($v,$langs) ){
		  return false;
		}
		return $this -> set_where( $this -> alias.'.lang',$v);
	}
	
	
	public function orderby_id($v){
		return $this -> set_orderby( $this -> alias.'.p_id',$v);
	}


	
}


	//	класс для управления записями таблицы system_staticPages
class staticPages extends objects{

  public $table = "system_stat_pages";
  public $key = "p_id";
	public $fields = 'filedsSetStaticPages';
	
	public function id(){
		return $this -> info['p_id']; 
	}
	public function meta_title(){
		return $this -> info['p_meta_title']; 
	}	
	public function content(){
		return $this -> info['p_content']; 
	}	
	

	
		//	загрузка модуля по его id
	static function load_id($id){
		
		$m = new staticPagesQuery('m');
		if ( !$m -> where_id($id) ){
			return false;
		}
		$m -> get('*');
		if (  $m -> get_count_rows()==0 ){
			return FALSE;
		}
		return new staticPages($id,$m -> row());
	}
	
	static function load_url($v){
		$m = new staticPagesQuery('m');
		if ( !$m -> where_url($v) ){
			return false;
		}
		$m -> get('*');
		if (  $m -> get_count_rows()==0 ){
			return FALSE;
		}
		$row = $m -> row();
		return new staticPages($row['p_id'],$row);
	}
	
	static function create(){
		return new staticPages( array('p_id'=>0));
	}
	
	/**
	* генерируем список действующих страниц
	*
	*/
	static function list_pages(){
		$s  = new staticPagesQuery('s');
		$s -> orderby_id('DESC');
		$s -> get('*');
		$rows = array();
		while( $row = $s -> row() ){
		  $rows[]=$row;
		}
		return $rows;
	}
	

	
	static function groups($id,$groups){
	global $MAIN_DB;
		if ( empty($id) ){
			throw new Exception('Ошибка: не указан id');
		}
		
		$sql = "DELETE FROM ".$MAIN_DB -> prefix("system_stat_pages_groups_link")." WHERE p_id='$id'";
		$MAIN_DB -> query($sql);
		
		if ( !is_array($groups) ){
			return false;
		}
		foreach( $groups as $g_id ){
			$sql = "
			INSERT INTO  ".$MAIN_DB -> prefix("system_stat_pages_groups_link")." 
			SET p_id='$id', g_id='$g_id'";
			$MAIN_DB -> query($sql);
		}
		return true;
	}
		
	static function isExistsUrl($url,$id){
	  $u = new staticPagesQuery('u');
		//$u -> set_debug(1);
		$u -> where_url($url);
	
		$u -> where_not_id($id);
		$u -> get('COUNT(*)');
		list($count) = $u -> row();
		if ( $count>0 ){
			return true;
		}
		return false;
	}
	
}

?>