class.pages.php 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
<?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
*/


include_once(MAIN_SOURCE_PATH.'/modules/admin/lang/'.MAIN_LANG.'/lang.php');
include_once(MAIN_SOURCE_PATH.'/inc/class.objects.php');
include_once(MAIN_SOURCE_PATH.'/inc/class.query.php');

class filedsSetPages
{
  //static $id = false; //  адрес новости
  
  
	static function f_name($v){
		if ( empty($v) ){
		  throw new Exception('Ошибка: название страницы не указано');
		}
		return $v;
	}
	
	static function p_name($v){
		if ( empty($v) ){
		  throw new Exception('Ошибка: программный файл не указан');
		}
		if (  !pages::check_page($v) ){
		  throw new Exception('Ошибка: программный файл имеет неправильное название');
		}
		return $v;
	}
	
	static function m_dirname($v){
		if ( empty($v) ){
		  throw new Exception('Ошибка: каталог не указан');
		}
		return $v;
	}
	
	static function m_id($v){
		$v = intval($v);
		if ( empty($v) ){
		  throw new Exception('Ошибка: номер модуля не указан');
		}
		return $v;
	}
	
	static function f_func($v){
		if ( empty($v) ){
		  throw new Exception('Ошибка:не указано название функции');
		}
		return $v;
	}
	
	
	static function f_access($v){
		 return !empty($v) ? 1 : 0;
	}
	
	static function f_active($v){
		return !empty($v) ? 1 : 0;
	}
	
	
	static function f_tmpl($v){
		if ( empty($v) ){
		  throw new Exception('Ошибка: шаблон не выбран');
		}
		if ( !file_exists(MAIN_SOURCE_PATH.'/tmpl/files/'.$v) ){
			throw new Exception('Ошибка: шаблон не существует');
		}
		return $v;
	}

}


class filedsGetPages
{

  static $__var = array();  //  для хранения временных переменных

  		//	проверяем, нет ли новых модулей. Если есть, то заносим их в базу	
	static function new_modules(){
		if ( !$handle = opendir(MAIN_SOURCE_PATH.'/modules') ) {
			return false;
		}
    $result = array();
			// перебираем всё в папке modules	
		while (false !== ($path = readdir($handle) ) ) {
      //	ищем модуль path в базе данных
      $m = new modulesQuery('m');
      if ( !$m -> where_path($path) ){
				continue;
			}
			//$m -> set_debug(1);
      $m -> get('COUNT(*)');
      list($exists) = $m -> row();
      if ( !empty($exists) ){
				continue;
			}
			
			if ( !file_exists(MAIN_SOURCE_PATH.'/modules/'.$path.'/info.php') ){
				continue;
			}
			
			include_once(MAIN_SOURCE_PATH.'/modules/'.$path.'/lang/'.MAIN_LANG.'/lang.php');
			include_once(MAIN_SOURCE_PATH.'/modules/'.$path.'/info.php');
			
			if ( !is_array($m_info) || sizeof($m_info)==0 ){
				continue;
			}
      $pages = array();
      foreach( $m_info['page'] as $page => $name ){
        $pages[]=array('name'=>$name);
      }
      $result[]=array('m_name'=>$m_info['name'],'m_path'=>$path,'pages'=>$pages);
   	}
   	return $result;
	}

	static function active_modules_ul($v){
   	include_once(MAIN_SOURCE_PATH.'/modules/admin/inc/class.module.php');
		$s = new select_box();
	  $s -> empty_record(true,0);
	  $m = new modulesQuery('m');
	  $m -> where_active(1);
	  $m -> get('m_path,m_name');
	  while( $row = $m -> row() ){
		  $s -> set($row['m_path'],$row['m_name']);
		}		
		return  $s -> get();
  }

  

  
  static function module_title($v){
    if ( self::$__var['modulte_title']==$v ){
    	return FALSE;
    }else{
    	self::$__var['modulte_title']=$v;
    	return TRUE;
    }
  }


	static function lang_links($v){
    if ( false === MAIN_MULTILANG ){
      return false;
    }
    $langs = explode(',',MAIN_LANGS);
    $result = array();
    foreach( $langs as $l ){
      $q = new pagesQuery('q');
      //$q -> set_debug(1);
      $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;
  }

 
  static function lang_list($v){
    if ( false === MAIN_MULTILANG ){
      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 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
	static 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;	
	}
	
	static function group_list($v){
	  $gps = self::groups($v);
		$groups = users::groups();
		$s = new select_box();
		$s -> selected_id($gps);
		$s -> empty_record(false);
		foreach( $groups AS $g_id => $g_name ){
			$s -> set($g_id,$g_name );
		}
		return $s -> get();
	}
 
  static function tmpl_list($v){
    	//	список папок
		include_once(MAIN_SOURCE_PATH.'/modules/admin/inc/class.tmpl.php');
		$t = new tmpl_q('t');
		//$t -> set_debug(1);
		$t -> get('t.t_id,t.t_name');
		$s = new select_box();
		$s -> selected_id($v);
		$s -> empty_record(false);
		while( list($t_id,$t_name) = $t -> row() ){
			$s -> set($t_id,$t_name);
		}
		return $s -> get();
  }
  

}




class pagesQuery extends query{
	
	var $fields_get = 'filedsGetPages';
		
	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){
	  if ( false===MAIN_MULTILANG ){
	    return false;
	  }
	  $v = intval($v);
	  if ( empty($v) ){
	    return false;
	  }
		return $this -> set_where( $this -> alias.'.link_id',$v);
	}
	
	public function where_lang($v){
	  if ( false===MAIN_MULTILANG ){
	    return false;
	  }
	  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 where_main_rows(){
	if ( false===MAIN_MULTILANG ){
	    return false;
	  }
		return $this -> set_anothe_where( $this -> alias.'.f_id='.$this -> alias.'.link_id');
	}
	
	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);
	}
	

} //  end pagesQuery


class pages extends objects{
	
	public $table = "system_pages";
  public $key = "f_id";
	public $fields = 'filedsSetPages';
	
	
	

	
	public function id(){
		return $this -> info['f_id'];
	}	
	public function module(){
		return $this -> info['m_dirname'];
	}	
	
	
	/**
  * Удаление страницы
  * 
  * @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 -> key_value ===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 -> key_value."'";
		$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 -> key_value."', g_id='$g_id'";
			$MAIN_DB -> query($sql);
		}
	}

	
	

	static function exists_page($v){
	global $MAIN_DB;
		$u = new pagesQuery('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 pagesQuery('m');
		$u -> where_ffunc($v);
		$u -> get('COUNT(*)');
		list($count) = $u -> row();
		if ( $count>0 ){
			return true;
		}
		return false;
	}

	
	
	
	
		//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 pagesQuery('m');
		
		if ( !$m -> where_id($id) ){
			
			return false;
		}
		
		$m -> get('*');
		if ( !$m -> get_count_rows() ){
			return false;
		}
		return new pages($id,$m -> row());
	}
	
	static function load_pname($id){
		$m = new pagesQuery('m');
		if (!$m -> where_pname($id) ){
			return false;
		}
		$m -> get('*');
		if ( !$m -> get_count_rows() ){
			return false;
		}
		$row = $m -> row();
		return new pages($row['f_id'],$m -> row());
	}
	
	static function create(){
		return new pages( array(0,'f_id'=>0) );
	}
	
	  //  добавляем языки
	static function add_langs($page_id)
	{
	 
	  if ( false===MAIN_MULTILANG )
	  {
	    return false;
	  }
	  
	  $p = pages::load_id($page_id);
	  if ( false===$p )
	  {
	    return false;
	  }
	  $langs = explode(',',MAIN_LANGS);
	  foreach( $langs as $l ){
	    
	      //  основной язык уже добавлен
	    if ( $l==MAIN_LANG )
	    {
	      continue;
	    }
	      //проверяем, вдруг язык уже добавлен
	    $pq = new pagesQuery('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();
	  }
	}
}

	
?>