<?php

class LogsManagerView extends Slot {

	var $ModuleID = 4;
	var $SectionID = 40;

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {
		$this->Action->processEvent($this->SectionID,'select_filters',array(),$this);
		$this->filters = $this->PreparedTemplates;
		$request = $this->LocalTarget;
		$request['pageRecordsQty'] = 50;
		$request['curPageNumber'] = isset($this->LocalTarget[0])? $this->LocalTarget[0]+1 : 1;
		if(!empty($this->Request)) {
			$or_line = ''; $ents = array();
			foreach($this->Request['form']['user'] as $k => $v)
				if($v == 0) $filtusers[] = $k;
			foreach($this->Request['form']['type'] as $k => $v) {
				if($v['id'] == 0)	$filttypes[] = $k;
				else{
					foreach($v['events'] as $k1 => $v1) if($v1 == 0) $filtevents[] = $k1;
					foreach($v['entities'] as $k1 => $v1) {
						if($v1 == 0) $filtents[] = $k1;
						else{
							if(!empty($v['ids'][$k1]) && $v['ids'][$k1] != 0){
								$or_line .= '(object = '.$v['ids'][$k1].' and entity = '.$k1.') or ';
							}
							else $ents[] = $k1;
						}
					}
				}
			}
			if($or_line != '') {
				$or_line = (empty($ents)) ? preg_replace('/or\s$/',' ',$or_line) : $or_line.'entity in ('.implode(',',$ents).')';
				$request['additional']['query'] = 'select id from log where '.$or_line;
			}
			if(!empty($filtusers))	$request['conditions']['m.id'] = array('not in' => $filtusers);
			if(!empty($filttypes))	$request['conditions']['l.type'] = array('not in' => $filttypes);
			if(!empty($filtevents)) $request['conditions']['l.event'] = array('not in' => $filtevents);
			if(!empty($filtents)) $request['conditions']['le.id'] = array('not in' => $filtents);
			$time1 = date("Y-m-d",mktime(0, 0, 0, $this->Request['month'], $this->Request['day'], $this->Request['year']));
			$time2 = date("Y-m-d",mktime(0, 0, 0, $this->Request['month2'], $this->Request['day2']+1, $this->Request['year2']));
			$request['conditions']['l.date'] = array('between' => array ($time1,$time2));
		}
		$request = $this->Action->processEvent($this->SectionID,'select_logs',$request,$this);
		//printArray($this->PreparedTemplates);
		$this->getPageControl($this->Home.'/Registar/LogsManager');
	}

	function fillElements() {
		GLOBAL $REGISTRY, $signature;
		$this->TempFillEls['yearStart']='2007';
		$this->TempFillEls['monthStart']='0';
		$this->TempFillEls['dayStart']='01';
		$this->TempFillEls['yearNow']=date('Y');
		$this->TempFillEls['dayNow']=date('d');
		$this->TempFillEls['monthNow']=date('m')-1;
		$this->TempFillEls['yearSelect1']=(!empty($this->Request)? $this->Request['year'] : '2007');
		$this->TempFillEls['monthSelect1']=(!empty($this->Request)? $this->Request['month']-1 : '0');
		$this->TempFillEls['daySelect1']=(!empty($this->Request)? $this->Request['day'] : '01');
		$this->TempFillEls['yearSelect2']=(!empty($this->Request)? $this->Request['year2'] : date('Y'));
		$this->TempFillEls['daySelect2']=(!empty($this->Request)? $this->Request['day2'] : date('d'));
		$this->TempFillEls['monthSelect2']=(!empty($this->Request)? $this->Request['month2']-1 : date('m')-1);
		$i = 1;
		foreach($this->filters['types'] as $key => $value) {
			$this->TempFillEls['log_filters'][$value['type']] = array(
				'number' => $i,
				'id' => inputHidden('form[type]['.$key.'][id]',0).inputCheckbox('form[type]['.$key.'][id]',(!empty($this->Request)? $this->Request['form']['type'][$key]['id'] : 1),'onclick="allSelect(this)"',1),
				'type' => $value['type'],
			);
			$n = 0;
			foreach($value['events'] as $k => $v) {
				$this->TempFillEls['log_filters'][$value['type']]['events'][] = array(
					'id' => inputHidden('form[type]['.$key.'][events]['.$k.']',0).inputCheckbox('form[type]['.$key.'][events]['.$k.']',(!empty($this->Request)? $this->Request['form']['type'][$key]['events'][$k] : 1),'',1),
					'name' => $v,
				);
				$n++;
			}
			$j = 0;
			foreach($value['entities'] as $k => $v) {
				if (!empty($v['ids'])) $v['ids'][0] = 'All';
				$this->TempFillEls['log_filters'][$value['type']]['entities'][] = array(
					'id' => inputHidden('form[type]['.$key.'][entities]['.$k.']',0).inputCheckbox('form[type]['.$key.'][entities]['.$k.']',(!empty($this->Request)? $this->Request['form']['type'][$key]['entities'][$k] : 1),'',1),
					'name' => $v['ent_name'].' '.inputSelect('form[type]['.$key.'][ids]['.$k.']',$v['ids'],(!empty($this->Request['form']['type'][$key]['ids'][$k])? $this->Request['form']['type'][$key]['ids'][$k] : 0),''),
				);
				$j++;
			}
			$i++;
		}
		$u = 1;
		foreach($this->filters['users'] as $key => $value){
			$this->TempFillEls['users'][] = array(
				'id' => inputHidden('form[user]['.$key.']',0).inputCheckbox('form[user]['.$key.']',(!empty($this->Request)? $this->Request['form']['user'][$key] : 1),'',1),
				'name' => $value,
			);
			$u++;
		}
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'select_logs');
		$pubVis = $this->server_type == 'pro' ? 'hidden' : 'visible';
		$this->TempFillEls['pubBut'] = $pubVis;
		$subdomains = $this->Db->SelectList('domain','subdomain',array('domain'));
		$count = count($this->PreparedTemplates['curPage']);
		for ($i = 0; $i < $count; $i++){
			$view = '';
			$edit = '';
			$publish_chb = '';
			$symbols = 32;
			if(($this->PreparedTemplates['curPage'][$i]['object'] != 0 || $this->PreparedTemplates['curPage'][$i]['entity'] == 'xml sitemap' || $this->PreparedTemplates['curPage'][$i]['entity'] == 'css') && $this->PreparedTemplates['curPage'][$i]['event'] != 13){
				$view_event = '';
				$edit_event = '';
				$publish_chb_name = '';
				switch($this->PreparedTemplates['curPage'][$i]['entity']){
					case 'page': {
						if(empty($this->PreparedTemplates['curPage'][$i]['page_path']) || $this->PreparedTemplates['curPage'][$i]['object'] == 60) break;
						if(preg_match("/^(\/subdomains)/i", $this->PreparedTemplates['curPage'][$i]['page_path'])){
							$paths = explode('/',strtolower($this->PreparedTemplates['curPage'][$i]['page_path']));
							array_shift($paths);
							$view_event = in_array($paths[1], $subdomains) ? 'http://'.(isset($REGISTRY['typedomain']) ? $REGISTRY['typedomain'].'.' : '').$paths[1].'.'.$signature['domain'][0].preg_replace('/^(\/subdomains\/'.$paths[1].')/i', '', $this->PreparedTemplates['curPage'][$i]['page_path']) : 'http://'.(isset($REGISTRY['typedomain']) ? $REGISTRY['typedomain'].'.' : '').$signature['domain'][0].$this->PreparedTemplates['curPage'][$i]['page_path'];
						}
						else $view_event = $this->PreparedTemplates['curPage'][$i]['page_path'] == ' ' ? '' : $REGISTRY['Globals']['static']['homePath'].($this->PreparedTemplates['curPage'][$i]['object'] == 10 ? '/'.md5(microtime()) : $this->PreparedTemplates['curPage'][$i]['page_path']);
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/'.($this->PreparedTemplates['curPage'][$i]['object'] == 10 ? 'DefaultPage' : 'ContentManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object']);
						$publish_chb_name = 'form[adds]['.($this->PreparedTemplates['curPage'][$i]['object'] == 10 ? 10 : 3).'][]';
						break;
					}
					case 'preset': $edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/PreSetManager/Update/'.$this->PreparedTemplates['curPage'][$i]['presetgroup'].'/'.$this->PreparedTemplates['curPage'][$i]['object']; break;
					case 'presetgroup': $edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/PreSetManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object']; break;
					case 'xml sitemap':{
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/SiteMapXMLGenerator';
						$publish_chb_name = 'form[adds][4][]';
						break;
					}
					case 'css':{
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/CSSManager';
						$publish_chb_name = 'form[adds][0][]';
						break;
					}
					case 'user': $edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/SecurityManager/View/'.$this->PreparedTemplates['curPage'][$i]['object']; break;
					case 'meta & js zones':{
						if (!empty($this->PreparedTemplates['curPage'][$i]['page_path'])){
							if ($this->PreparedTemplates['curPage'][$i]['object'] == 30){
								$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/ScriptEditor';
								$publish_chb_name = 'form[adds][11][]';
							}
							else $edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/MetaManager/Page/'.$this->PreparedTemplates['curPage'][$i]['object']['object'];
						}
						break;
					}
					case 'layout':{
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/LayoutManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][1][]';
						break;
					}
					case 'template':{
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/TemplateManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/TemplateManager/View/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][6][]';
						break;
					}
					case 'menu': { 
						switch ($this->PreparedTemplates['curPage'][$i]['menutype']){
							case '1': $menu_path = 'MenuManager/SiteMap'; break;
							case '3': $menu_path = 'MenuManager/BottomMenu/Update/'.$this->PreparedTemplates['curPage'][$i]['object']; break;
							case '4': $menu_path = 'MenuManager/LeftMenu/Update/'.$this->PreparedTemplates['curPage'][$i]['object']; break;
							case '6': $menu_path = 'MenuManager/RightMenu/Update/'.$this->PreparedTemplates['curPage'][$i]['object']; break;
							case '7': $menu_path = 'MenuManager/TabsMenu/Update/'.$this->PreparedTemplates['curPage'][$i]['object']; break;
							default: $menu_path = 'MenuManager'; break;
						}
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/'.$menu_path;
						$publish_chb_name = 'form[adds][2][]';
						break;
					}
					case 'subdomain': {
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/SubdomainManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/SubdomainManager/View/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][12][]';
						break;
					}
					case 'package': {
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/PackageManager/Look/'.$this->PreparedTemplates['curPage'][$i]['object'];
						break;
					}
					case 'engine packet': {
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/EnginePacketManager/View/'.$this->PreparedTemplates['curPage'][$i]['object'];
						break;
					}
					case 'deleted objects': {
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/DeletedObjectsProcessingReport/View/'.$this->PreparedTemplates['curPage'][$i]['object'];
						break;
					}
					case 'imported feed': {
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/ImportedFeedManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/ImportedFeedManager/View/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][13][]';
						break;
					}
					case 'feed': {
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/FeedManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/FeedManager/View/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][15][]';
						break;
					}
					case 'city': {
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/CitiesManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$view_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/CitiesManager/View/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][14][]';
						break;
					}
					case 'category':{
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/CategoriesManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][16][]';
						break;
					}
					case 'parent item':{
						$edit_event = $REGISTRY['Globals']['static']['homePath'].'/Registar/ParentItemsManager/Update/'.$this->PreparedTemplates['curPage'][$i]['object'];
						$publish_chb_name = 'form[adds][17][]';
						break;
					}
				}
				if(!empty($view_event)) $view = '<a href="'.$view_event.'" class=adminlink>View</a>';
				if(!empty($edit_event)) $edit = '<a href="'.$edit_event.'" class=adminlink>Edit</a>';
				if(!empty($publish_chb_name)) $publish_chb = inputCheckbox($publish_chb_name, FALSE, ' id="'.$this->PreparedTemplates['curPage'][$i]['id'].'" onClick="setSubmitAbility(\''.$this->PreparedTemplates['curPage'][$i]['id'].'\')" style="visibility: '.$pubVis.'"', $this->PreparedTemplates['curPage'][$i]['object']);
			}
			else $symbols = 35;
			$tok = explode(".", $this->PreparedTemplates['curPage'][$i]['date']);
			$this->TempFillEls['logs'][] = array(
				'number' => $this->PreparedTemplates['curPage'][$i]['id'],
				'date' => $tok[0],
				'author' => $this->PreparedTemplates['curPage'][$i]['member'],
				'value' => wordwrap($this->PreparedTemplates['curPage'][$i]['value'], $symbols, " &nbsp;", 1),
				'entity' => $this->PreparedTemplates['curPage'][$i]['entity'],
				'view' => $view,
				'edit' => $edit,
				'publish_chb' => $publish_chb,
			);
		}
	}
}
?>