class.modules.php 6.85 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
*/


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 filedsSetModules
{

  static function m_name($v){
		if ( empty($v) ){
			throw new Exception('Ошибка: название модуля не указано');
		}
		return $v;
	}
	
	static function m_path($v){
		if ( empty($v) ){
			throw new Exception('Ошибка: адрес модуля не указан');
			$this -> make_error(ERROR_MESSAGE,'');
		}
		return $v;
	}
	

}


class filedsGetModules
{

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


 
  

}




class modulesQuery extends query{
	
	var $fields_get = 'filedsGetModules';
		
	public function __construct($alias){
		$this -> set_table(MAIN_MODULES_TBL);
		$this -> set_alias($alias);
	}
	
	public function include_pages($alias,$cols=''){
		$this -> include_table(MAIN_PAGES_TBL,$alias,'m_id',$cols );
		return true;
	}
	
	public function where_id($id){
		$id = intval($id);
		if ( empty($id) ){
			return false;
		}
		return $this -> set_where( $this -> alias.'.m_id',$id);
	}
	
	public function where_active($id){
		$id = intval($id);
		if ( empty($id) ){
			return false;
		}
		return $this -> set_where( $this -> alias.'.m_status',$id);
	}
	
	public function where_path($path){
		if ( !modules::is_path($path) ){
			return false;
		}
		return $this -> set_where( $this -> alias.'.m_path',$path);
	}
	
	public function orderby_pos($v){
		return 	$this -> set_orderby($this -> alias.'.m_pos',$v);
	}
	
	


	


} //  end modulesQuery


class modules extends objects{
	
	public $table = "system_modules";
  public $key = "m_id";
	public $fields = 'filedsSetModules';
	
	
		//	загрузка модуля по его id
	static function load_id($id){
		$m = modules::query('m');
		if ( false===$m -> where_id($id) ){
			return FALSE;
		}
		$m -> get('*');
		if (  $m -> get_count_rows()==0 ){
			return FALSE;
		}
		return new modules($id,$m -> row());
	}
	
	static function load_path($id){
		$m =  modules::query('m');
		if ( !$m -> where_path($id) ){
			return false;
		}
		$m -> get('*');
		if (  $m -> get_count_rows()==0 ){
			return FALSE;
		}
		$row = $m -> row();
		return new modules($row['m_id'],$row);
	}


	static function create(){
		return new modules(0, array('f_id'=>0) );
	}

	static function query($alias){
		return new modulesQuery( $alias );
	}

	
	public function insert(){

			//	так как запись новая, нужно проверить уникальные ключи на повторение
		if ( self::exists_module($this -> set_info['m_path']) ){
			throw new Exception(ERROR_MESSAGE, sprintf(A_EXISTS_MODULE,$this -> set_info['m_path']));
		} 
		
		return parent::insert();
	}
	

	public function delete(){

		$id = $this -> id();
		if ( empty($id) ){
			throw new Exception(A_EMPTY_ID);
		}
		if ( !file_exists(MAIN_SOURCE_PATH.'/modules/'.$this -> get('m_path').'/info.php') ){
        throw new Exception( 'Ошибка: модуль "'.$this -> get('m_path').'" не содержит системного файла info.php!' );
    }
		include_once(MAIN_SOURCE_PATH.'/modules/admin/inc/class.pages.php');
		$p = new pagesQuery('f');
		$p -> where_mid($id);
		$p -> get('f.f_id');
		while( list($f_id) = $p -> row() ){
			$page = pages::load_id($f_id);
			if ( false===$page ){
				die('continue');
			}
			$page -> delete();
		}


		return parent::delete();
	}

	public function id(){
		return $this -> info['m_id']; 
	}
	public function path(){
		return $this -> info['m_path']; 
	}
	public function name(){
		return $this -> info['m_name']; 
	}

	static function is_path($p){
		if ( empty($p) ){
			return false;
		}
		if ( preg_match('#[^a-z_0-9]#i',$p) ){
			return false;
		}
		return true;
	}
	

	
	static function exists_module($v){
		$u = modules::query('m');
		//$u -> set_debug(1);
		if ( false===$u -> where_path($v) )
		{
			throw new Exception('Это не путь к модулю!');
		}
		$u -> get('COUNT(*)');
		list($count) = $u -> row();
		if ( $count>0 ){
			return true;
		}
		return false;
	}
	
	
	/**
	* получаем список модулей
	*
	*/
	static function list_active(){
	  $modules = array();
	  $m = modules::query('m');
	  $m -> where_active(1);
	  $m -> get('m_path');
	  while( list($m_path) = $m -> row() ){
	    $modules[]=$m_path;
	  }
		return $modules;
	}
	

	public function installPages($pages){

    foreach( $pages as $k=>$v ){
    	$p = pages::load_pname($k);
      if ( false!==$p){
      	throw new Exception( 'Страница "'.$k.'" уже существует');  
      }
      $p = pages::create();
      
      $p -> set('f_active',1);
      $p -> set('f_name',$v);
      $p -> set('p_name',$k);
      $p -> set('m_dirname', $this -> get('m_path') );
      $p -> set('m_id', $this -> key_value  );
      $p -> set('f_func',$k);
      $p -> set('lang',MAIN_LANG);
      $p_id = $p -> insert();
      if ( false===$p_id ){
        throw new Exception( $p -> error() );  
      }
      $p -> set('link_id',$p_id);
      $p -> update();
      pages::add_langs($p_id);
    }
    return true;

	}

	public function installBlocks($blocks){
		//$setup_blocks = array();

	  foreach( $blocks as $block ){
    	$b = blocks::query('b');
    	 // $b -> set_debug(1);
      if ( false===$b -> where_parser_name($block['parser_name']) ){
      	throw new Exception( 'Ошибка: неверно указан parser_name блока "'.$block['parser_name'].'"');
      }
      if ( false===$b -> where_path( $this -> get('m_path') ) ){
      	throw new Exception( 'Ошибка: неверно указан путь к блоку "'.$this -> get('m_path').'"');
      }
      $b -> get('COUNT(*)',false);
      list($count) = $b -> row();
      if ( $count>0 ){
        $setup_blocks[]=$block['parser_name'];
        continue; 
      }
      $p =  blocks::create();
      //$p -> set_debug(1);
      $p -> set('b_name',$block['name']);
      $p -> set('m_path',$this -> get('m_path') );
      $p -> set('b_func',$block['file']);
      $p -> set('b_parser_name',$block['parser_name']);
      if ( false===$p -> insert() ){
        throw new Exception( $p -> error()  );
      }
     // $setup_blocks[]=$block['parser_name'];
    }//foreach
    return true;//$setup_blocks;
	}

  /**
  * Выпадающий список модулей
  *
  */
  static function listSelect($v){
    $s = new select_box();
    $s -> empty_record(true,0);
    $s -> selected_id($v);
    $m = self::query('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();
  }



}