<?php

class LanguageHandler extends Handler {

	function __construct(){
		parent::__construct('LanguageHandler',true);
	}

	function setSearch($request = array()) {
		global $Server;
		$Server->settings['LanguageSearch'] = array();
		foreach($request as $key => $value) $Server->settings['LanguageSearch'][$key] = $value;
		return array();
	}

	function getSearch($request = array()) {
		global $Server;
		return $Server->settings['LanguageSearch'];
	}

	function getConditions($request = array(),$search = array()) {
		if(isset($search['var']) && validate($search['var']))
			$request['conditions']['ci.var'] = $search['var'];
		if(!empty($search['type'])) {
			foreach($search['type'] as $k=>$v) {
				if(validate($v)) $request['conditions']['ci.contenttype'] = $v;
			}
		}
		if(!empty($search['module'])) {
			foreach($search['module'] as $k=>$v) {
				if(validate($v)) $request['conditions']['ci.module'] = $v;
			}
		}
		return $request;
	}

	function selectModules($request) {
		if(empty($request['fields'])) $request['fields']='m.id, m.sys_name as name, m.path, ml.name as modulename';
		if(empty($request['curPageNumber'])) $request['curPageNumber'] = '1';
		if(empty($request['pageRecordsQty'])) $request['pageRecordsQty'] = '100';
		$request['conditions']['ml.lang'] = $request['lang'];
		$request['conditions']['m.id'] = 'ml.ownerid';
		$request['sortColumn'] = 'm.id asc';
		$selected = $this->DB->SelectArray('module m, module_lang ml','count(*) as qty',$request['conditions']);
		$request['recordsTotal'] = $selected[0]['qty'];
		$request['pagesTotal'] = ceil($request['recordsTotal'] / $request['pageRecordsQty']);
		$request['curPage']=$this->DB->SelectArray('module m, module_lang ml',$request['fields'],$request['conditions'],$this->getOrderBy($request),$this->getLimit($request));
		return $request;
	}

	function saveContentItem($request){
		$contentitem=$this->createContentItem();
		$this->parseRequest($request,$contentitem);
		$this->DB->processObject($contentitem);
		$this->DB->commit();
		return $contentitem;
	}

	function openContentItem($request){
		$contentitem=$this->createContentItem();
		$this->parseRequest($request,$contentitem);
		$this->DB->initObject($contentitem);
		return $contentitem;
	}

	function selectContentItems($request){
		$search = $this->getSearch();
		$request = $this->getConditions($request,$search);
		$request['pageRecordsQty'] = !empty($search['amount'])? $search['amount'] : (!empty($request['pageRecordsQty'])? $request['pageRecordsQty'] : 100);
		if(empty($request['fields'])) $request['fields']='ci.id,ci.module,ci.contenttype,ci.var,cit.name as contenttype_name,m.sys_name as module_name';
		if(empty($request['sortColumn'])) { $request['sortColumn']='module asc, contenttype_name asc, var asc';}
		if(empty($request['curPageNumber'])) $request['curPageNumber']='1';
		if(!empty($request['curModule']) && $request['curModule']!='') $request['conditions']['m.id'] = $request['curModule'];
		$request['conditions']['m.id']='ci.module';
		$request['conditions']['cit.id']='ci.contenttype';
		$selected=$this->DB->SelectArray('contentitem ci,contenttype cit, module m','count(*) as qty',$request['conditions']);
		$request['recordsTotal']=$selected[0]['qty'];
		$request['pagesTotal'] = ceil($request['recordsTotal']/$request['pageRecordsQty']);
		$request['curPage']=$this->DB->SelectArray(Array('contentitem ci,contenttype cit, module m'),$request['fields'],$request['conditions'],$this->getOrderBy($request),$this->getLimit($request));
		return $request;
	}

	function selectContentType($request=array()){
		$types=array();
		$types = $this->DB->SelectGuide(array('contenttype'),array('id','name'));
		return $types;
	}

	function selectContentModules($request=array()){
		$modules = array();
		empty($request['lang']) and $request['lang'] = 1;
		$modules = $this->DB->SelectGuide(array('module g','module_lang gl'),array('gl.ownerid as id','gl.name'),array('g.id' => 'gl.ownerid','gl.lang' => $request['lang']));
		return $modules;
	}

	function createContentItem(){
		$contentitem = new ContentItem();
		$contentitem->contenttype = new ContentItemType();
		$contentitem->item_langs = new ObjectsList();
		$contentitem->item_langs->type = new ContentItem_Lang();
		return $contentitem;
	}
}
?>