<?php

class ProductMetaManagerView extends Slot {

	var $SectionID = 26;
	var $ModuleID = 4;
	var $moduletype = '';
	var $ownerid = '';
	var $meta_type = '';
	var $meta_name = '';
	var $reqfield = '<img src="/i/must.gif">&nbsp;';
	var $colors = array(
		'red' => '#B00000',
		'yellow' => '#FF8C00',
		'green' => '#008000',
		'black' => '#000000',
	);
	var $default_js = array(
		'1' => 'Do not execute Default JS for this area',
		'2' => 'Execute Default JS before this area',
		'3' => 'Execute Default JS after this area',
	);
	var $terminated = false;

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

	function init() {
		switch(@$this->LocalTarget[0]) {
			default: {
				$this->Action->processEvent($this->SectionID,'tree',array('full' => 1,'admin' => 1,'parent' => 0),$this);
				$this->Action->processEvent($this->SectionID,'list',array('full' => 1),$this);
				$this->Scheme = 'default';
			break;
			}
			case 'Product': {
				switch(@$this->LocalTarget[1]) {
					default: header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
					case 'Update':
						if($this->server_type=='dev') {
							$products = $this->Db->SelectGuide(array('parentitem i','product g'),array('g.id','g.name'),array('g.item' => 'i.id','i.itemtype' => 1));
							if(validate($this->LocalTarget[2]) && in_array($this->LocalTarget[2],array_keys($products))) {
								$array = array('id' => $this->LocalTarget[2],'meta' => 1);
								$this->Action->processEvent($this->SectionID,'getlock',array('product' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
								if(!empty($this->PreparedTemplates['access_flag']) && $this->PreparedTemplates['access_flag'] != $this->Action->Security->currentuser->id) {
									header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
									$this->terminated = true;
									return;
								}
								else {
									if(empty($this->PreparedTemplates['access_flag']))
										$this->Action->processEvent($this->SectionID,'lock',array('product' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
									$this->Action->processEvent($this->SectionID,'open_product',$array,$this);
									if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
									else {
										$this->moduletype = 3;
										$this->ownerid = $this->LocalTarget[2];
										$this->meta_type = 'Product';
										$this->Scheme = 'action';
									}
								}
							}
							else header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
						}
						else header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
					break;
					case 'Unlock':
						if($this->server_type=='dev') {
							$this->Action->processEvent($this->SectionID,'getlock',array('product' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
							if(!empty($this->PreparedTemplates)){
								$this->Action->processEvent($this->SectionID,'unlock',array('product' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
							}	
							header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
						}
						else $this->Scheme = 'default';
					break;
				}
			break;
			}
			case 'Category': {
				switch(@$this->LocalTarget[1]) {
					default: header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
					case 'Update':
						if($this->server_type=='dev') {
							$categories = $this->Db->SelectGuide(array('category g'),array('g.id','g.name'),array(1 => 1));
							if(validate($this->LocalTarget[2]) && in_array($this->LocalTarget[2],array_keys($categories))) {
								$array = array('id' => $this->LocalTarget[2],'meta' => 1,'parent' => 1,'getlinks' => 0);
								$this->Action->processEvent($this->SectionID,'getlock',array('category' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
								if(!empty($this->PreparedTemplates['access_flag']) && $this->PreparedTemplates['access_flag'] != $this->Action->Security->currentuser->id) {
									header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
									$this->terminated = true;
									return;
								}
								else {
									if(empty($this->PreparedTemplates['access_flag']))
										$this->Action->processEvent($this->SectionID,'lock',array('category' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
									$this->Action->processEvent($this->SectionID,'open_category',$array,$this);
									if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
									else {
										$this->moduletype = 2;
										$this->ownerid = $this->LocalTarget[2];
										$this->meta_type = 'Category';
										$this->Scheme = 'action';
									}
								}
							}
							else header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
						}
						else header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
					break;
					case 'Unlock':
						if($this->server_type=='dev') {
							$this->Action->processEvent($this->SectionID,'getlock',array('category' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
							if(!empty($this->PreparedTemplates)){
								$this->Action->processEvent($this->SectionID,'unlock',array('category' => $this->LocalTarget[2],'member' => $this->Action->Security->currentuser->id),$this);
							}	
							header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
						}
						else $this->Scheme = 'default';
					break;
				}
			break;
			}
		}
		if(!empty($this->Request)) {
			if($this->server_type=='dev') {
				//printArray($this->Request);
				switch($this->getRequestField('step')) {
					case 'unlock':
						$this->Request['form']['member'] = $this->Action->Security->currentuser->id;
						$this->Action->processEvent($this->SectionID,'unlock',$this->getRequestField('form'),$this);
						header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
					break;
					default:
						$this->Request['form']['js_type'] = strtolower($this->meta_type);
						$this->validateForm()
						and $this->Action->processEvent($this->SectionID,'save',$this->getRequestField('form'),$this)
						and header('Location: '.$this->Home.'/Registar/ProductMetaManager'.$this->SID);
					break;
				}
			}
			else header('Location: '.$this->Home.'/Registar/ProductMetaManager/'.$this->SID);
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function validateForm() {
		if(!is_AdvCleanText($this->Request['form']['meta']['title'],3,512)) $this->errors['title'] = $this->meta_type.'\'s Title is empty or is not valid';
		if(!is_AdvCleanText($this->Request['form']['meta']['keywords'],3,512)) $this->errors['keywords'] = $this->meta_type.'\'s Keywords is empty or is not valid';
		if(!is_AdvCleanText($this->Request['form']['meta']['description'],3,512)) $this->errors['description'] = $this->meta_type.'\'s Description is empty or is not valid';
		if(count($this->errors) > 0) return FALSE;
		return TRUE;
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'action': $this->fillInfo(); break;
			default: $this->fillSelect(); break;
		}
	}

	function fillInfo($array = array()) {
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open_'.strtolower($this->meta_type));
		//printArray($this->PreparedTemplates);
		$this->TempFillEls['sec_name'] = $this->PreparedTemplates['name'];
		$this->TempFillEls['ownerid'] = $this->ownerid;
		$this->TempFillEls['module'] = $this->ownerid;
		$this->TempFillEls['moduletype'] = $this->moduletype;
		$this->TempFillEls['meta_type'] = $this->meta_type;
		empty($this->PreparedTemplates['metas']) and $this->PreparedTemplates['metas'] = array();
		empty($this->PreparedTemplates['productjszones']) and $this->PreparedTemplates['productjszones'] = array();
		foreach($this->PreparedTemplates['metas'] as $value) {
			switch($value['metatype']['id']) {
				case 1: $this->TempFillEls['title'] = inputTextArea('form[meta][title]',(!empty($this->Request)? $this->Request['form']['meta']['title'] : @$value['value']),80,1,'style="width:870px;"'); break;
				case 2: $this->TempFillEls['description'] = inputTextArea('form[meta][description]',(!empty($this->Request)? $this->Request['form']['meta']['description'] : @$value['value']),80,3,'style="width:430px;"'); break;
				case 3: $this->TempFillEls['keywords'] = inputTextArea('form[meta][keywords]',(!empty($this->Request)? $this->Request['form']['meta']['keywords'] : @$value['value']),80,3,'style="width:430px;"'); break;
			}
		}
		empty($this->TempFillEls['title']) and $this->TempFillEls['title'] = inputTextArea('form[meta][title]',(!empty($this->Request)? $this->Request['form']['meta']['title'] : ''),80,1,'style="width: 870px;"');
		empty($this->TempFillEls['description']) and $this->TempFillEls['description'] = inputTextArea('form[meta][description]',(!empty($this->Request)? $this->Request['form']['meta']['description'] : ''),80,3,'style="width: 430px;"');
		empty($this->TempFillEls['keywords']) and $this->TempFillEls['keywords'] = inputTextArea('form[meta][keywords]',(!empty($this->Request)? $this->Request['form']['meta']['keywords'] : ''),80,3,'style="width: 430px;"');
		foreach($this->PreparedTemplates[strtolower($this->meta_type).'jszones'] as $value) {
			switch($value['type']['id']) {
				case 1:
					$this->TempFillEls['head'] = inputTextArea('form[js][head]',(!empty($this->Request)? $this->Request['form']['js']['head'] : @$value['value']),80,10,'style="width:880px;"');
					$this->TempFillEls['head_use_default'] = inputSelect('form[js][head_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['head_use_default'] : (empty($value['use_default'])? 3 : $value['use_default'])));
				break;
				case 2:
					$this->TempFillEls['body_before'] = inputTextArea('form[js][body_before]',(!empty($this->Request)? $this->Request['js']['body_before'] : @$value['value']),80,10,'style="width:880px;"');
					$this->TempFillEls['before_use_default'] = inputSelect('form[js][before_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['before_use_default'] : (empty($value['use_default'])? 2 : $value['use_default'])));
				break;
				case 3:
					$this->TempFillEls['body_after'] = inputTextArea('form[js][body_after]',(!empty($this->Request)? $this->Request['form']['js']['body_after'] : @$value['value']),80,10,'style="width:880px;"');
					$this->TempFillEls['after_use_default'] = inputSelect('form[js][after_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['after_use_default'] : (empty($value['use_default'])? 3 : $value['use_default'])));
				break;
			}
		}
		empty($this->TempFillEls['head']) and $this->TempFillEls['head'] = inputTextArea('form[js][head]',(!empty($this->Request)? $this->Request['form']['js']['head'] : ''),80,10,'style="width:880px;"');
		empty($this->TempFillEls['head_use_default']) and $this->TempFillEls['head_use_default'] = inputSelect('form[js][head_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['head_use_default'] : 3));
		empty($this->TempFillEls['body_before']) and $this->TempFillEls['body_before'] = inputTextArea('form[js][body_before]',(!empty($this->Request)? $this->Request['form']['js']['body_before'] : ''),80,10,'style="width:880px;"');
		empty($this->TempFillEls['before_use_default']) and $this->TempFillEls['before_use_default'] = inputSelect('form[js][before_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['before_use_default'] : 2));
		empty($this->TempFillEls['body_after']) and $this->TempFillEls['body_after'] = inputTextArea('form[js][body_after]',(!empty($this->Request)? $this->Request['form']['js']['body_after'] : ''),80,10,'style="width:880px;"');
		empty($this->TempFillEls['after_use_default']) and $this->TempFillEls['after_use_default'] = inputSelect('form[js][after_use_default]',$this->default_js,(!empty($this->Request)? $this->Request['form']['js']['after_use_default'] : 3));
		foreach($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $k=='header'? $this->printError($v).'<p>' : $this->printError($v);
		//printArray($this->TempFillEls);
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open_product');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open_category');
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'save');
	}

	function fillSelect() {
		if($this->terminated) return;
		$this->TempFillEls['addBut'] = $this->server_type == 'dev' ? 'visible' : 'hidden';
		$this->TempFillEls['pubBut'] = $this->server_type == 'pro' ? 'hidden' : 'visible';
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'tree');
		$this->setTargetArray($this->PreparedTemplates,0.01);
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'list');
		$this->setProductArray($this->PreparedTemplates);
	}

	function setProductArray($array = array()) {
		$btnEdit_vis = $this->server_type == 'dev' ? 'visible' : 'hidden';
		foreach($array as $value) {
			$locked = empty($value['access_flag']) || $value['access_flag'] == $this->Action->Security->currentuser->id ? false : true;
			$presets_count = explode('/', $value['published_count']);
			$c = count($value['publish']);
			$this->TempFillEls['products'][$value['id']] = array(
				'id' => $value['id'],
				'name' => $value['name'],
				'path' => $value['path'],
				'name_color' => $this->colors[($value['unmodified'] > 30 ? 'black' : ($value['unmodified'] > 14 && $value['unmodified'] <= 30 ? 'green' : ($value['unmodified'] > 7 && $value['unmodified'] <= 14 ? 'yellow' : 'red')))],
				'published_count' => $value['published_count'],
				'published_count_cursor' => $c ? 'pointer' : 'auto',
				'published_count_color' => $this->colors[($presets_count[0] >= $presets_count[1] ? 'green' : (empty($presets_count[0]) ? 'red' : 'blue'))],
				'chbAdds_disabled' => $locked ? 'disabled' : '',
				'date_changed' => $value['date_changed'],
				'btnUnlock_vis' => $value['access_flag'] == $this->Action->Security->currentuser->id ? 'visible' : 'hidden',
				'btnEdit_vis' => $btnEdit_vis,
				'chbAdds_vis' => $this->TempFillEls['pubBut'],
				'btnUnlock_disp' => $locked ? 'none' : '',
				'btnEdit_disp' => $locked ? 'none' : '',
				'locked_message_disp' => $locked ? '' : 'none',
				'user_name' => $value['username'],
				'date_locked' => $value['date_locked'],
			);
		}
	}

	function setTargetArray($array = array(),$level = 1,$levels = array()) {
		$i = 0;
		$btnEdit_vis = $this->server_type == 'dev' ? 'visible' : 'hidden';
		foreach($array as $value) {
			$levels[$level] = $i==sizeof($array)-1? 1 : 0;
			$locked = empty($value['access_flag']) || $value['access_flag'] == $this->Action->Security->currentuser->id ? false : true;
			$presets_count = explode('/', $value['published_count']);
			$c = count($value['publish']);
			$this->TempFillEls['pages'][$value['id']] = array(
				'id' => $value['id'],
				'name' => $value['name'],
				'name_color' => $this->colors[($value['unmodified'] > 30 ? 'black' : ($value['unmodified'] > 14 && $value['unmodified'] <= 30 ? 'green' : ($value['unmodified'] > 7 && $value['unmodified'] <= 14 ? 'yellow' : 'red')))],
				'published_count' => $value['published_count'],
				'published_count_cursor' => $c ? 'pointer' : 'auto',
				'published_count_color' => $this->colors[($presets_count[0] >= $presets_count[1] ? 'green' : (empty($presets_count[0]) ? 'red' : 'blue'))],
				'chbAdds_disabled' => $locked ? 'disabled' : '',
				'date_changed' => $value['date_changed'],
				'btnUnlock_vis' => $value['access_flag'] == $this->Action->Security->currentuser->id ? 'visible' : 'hidden',
				'btnEdit_vis' => $btnEdit_vis,
				'btnView_vis' => $value['is_active']? 'visible' : 'hidden',
				'chbAdds_vis' => $this->TempFillEls['pubBut'],
				'btnUnlock_disp' => $locked ? 'none' : '',
				'btnEdit_disp' => $locked ? 'none' : '',
				'btnView_disp' => $locked ? 'none' : '',
				'locked_message_disp' => $locked ? '' : 'none',
				'user_name' => $value['username'],
				'date_locked' => $value['date_locked'],
				'path' => $this->path_sticker($value['id']),
				'width' => intval($level*25),
				'width2' => 419 - intval($level*25),
				'start_div' => '<div id=page'.$value['id'].(!empty($value['subcat'])? ' style="display: none;"' : '').'>',
				'minus' => !empty($value['subcat'])? '_minus' : '',
			);
			if(!empty($value['subcat'])) {
				$this->TempFillEls['pages'][$value['id']]['header'] = array(array(
					'id' => $value['id'],
					'name' => $value['name'],
					'name_color' => $this->colors[($value['unmodified'] > 30 ? 'black' : ($value['unmodified'] > 14 && $value['unmodified'] <= 30 ? 'green' : ($value['unmodified'] > 7 && $value['unmodified'] <= 14 ? 'yellow' : 'red')))],
					'published_count' => $value['published_count'],
					'published_count_cursor' => $c ? 'pointer' : 'auto',
					'published_count_color' => $this->colors[($presets_count[0] >= $presets_count[1] ? 'green' : (empty($presets_count[0]) ? 'red' : 'blue'))],
					'chbAdds_disabled' => $locked ? 'disabled' : '',
					'date_changed' => $value['date_changed'],
					'btnUnlock_vis' => $value['access_flag'] == $this->Action->Security->currentuser->id ? 'visible' : 'hidden',
					'btnEdit_vis' => $btnEdit_vis,
					'chbAdds_vis' => $this->TempFillEls['pubBut'],
					'btnUnlock_disp' => $locked ? 'none' : '',
					'btnEdit_disp' => $locked ? 'none' : '',
					'locked_message_disp' => $locked ? '' : 'none',
					'user_name' => $value['username'],
					'date_locked' => $value['date_locked'],
					'path' => $this->path_sticker($value['id']),
					'width' => intval($level*25),
					'width2' => 419 - intval($level*25),
				));
				$this->TempFillEls['pages'][$value['id']]['onclick'] = array(array('id' => $value['id']));
				$this->setTargetArray($value['subcat'],$level+1,$levels);
			}
			else {
				if($level==1) $this->TempFillEls['pages'][$value['id']]['end_div'] = '</div>';
				else $this->TempFillEls['pages'][$value['id']]['end_div'] = !empty($levels[$level])? (!empty($levels[$level-1])? (!empty($levels[$level-2])? str_repeat('</div>',4) : str_repeat('</div>',3)) : str_repeat('</div>',2)) : '</div>';
			}
			$i++;
		}
	}
	function getPath($id,$arr,$path=Array()){
		foreach($arr as $value){
			if($value['id']==$id){
				$path[]=$value['path'];
				return $this->getPath($value['parent'],$arr,$path);
			}
		}
		return '/'.implode('/',array_reverse($path));
	}

	function getTime($str){
		$time=explode('.',$str);
		return $time[0];
	}

	function path_sticker($id) {
		global $DatabaseBroker, $Paths;
		if(empty($Paths)) {
			$query = 'SELECT id, sys_name, ownerid FROM category';
			$selected = $DatabaseBroker->SelectAny($query);
			foreach($selected as $value)
				$Paths[$value['id']] = array(
					'path' => $value['sys_name'],
					'parent' => $value['ownerid'],
				);
		}
		$p = '';
		while(1){
			if(empty($Paths[$id])) return null;
			if(empty($Paths[$id]['parent'])) return '/'.$Paths[$id]['path'].$p;
			$p = '/'.$Paths[$id]['path'].$p;
			$id = $Paths[$id]['parent'];
		}
	}
}
?>