<?php

class RobotsTxtManagerView extends Slot {

	var $ModuleID = 4;
	var $SectionID = 39;
	var $new_log;

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

	function init() {
		/*insert shipping
		  	digital_night_vision		+ 6000
			direct_view					+ 5000
			illumination				+ 7000
			night_vision				+ 3000
			thermal_imaging				+ 4000  		  
//SELECT `product_id` , `delivery_id` , `price_zone1` , `price_zone2` , `price_zone3` FROM `shipping_states_products` ssp RIGHT JOIN `ti_products` cp ON ssp.product_id = cp.id where catalogue_id = ssp.type_id  
$products = array(
  array('product_id' => '191','delivery_id' => '1','price_zone1' => '70.60','price_zone2' => '81.30','price_zone3' => '83.20'),
  array('product_id' => '191','delivery_id' => '5','price_zone1' => '21.00','price_zone2' => '24.50','price_zone3' => '27.60'),
  array('product_id' => '253','delivery_id' => '3','price_zone1' => '111.50','price_zone2' => '111.50','price_zone3' => '111.50')
);
			foreach($products as $key => $value) $this->Db->SelectAny(" INSERT INTO shipcost  VALUES (nextval('shipcost_id_seq'::regclass), ".($value['product_id']+4000).",  ".$value['delivery_id'].",  '".$value['price_zone1']."', '".$value['price_zone2']."', '".$value['price_zone3']."');   ");
	exit;*/
		
		
		
		
		
		
		
		
		global $TOOLS_PATH;
		switch(@$this->LocalTarget[0]) {
			default: $this->Scheme = 'default'; break;
			case 'Edit': 
				if($this->server_type=='dev') $this->Scheme = 'edit';
				else $this->Scheme = 'default';
			break;
		}
		if(!empty($this->Request)) {
			if($this->server_type=='dev') {
				switch(@$this->LocalTarget[0]) {
					default:
						$this->fillGenerate()
						and $this->Action->processEvent($this->SectionID,'log',array('type' => 2,'entity' => 11,'event' => 8,'value' => 'Generate robots.txt file'.(validate($this->getRequestField('domain'))? ' for subdomain "'.$this->getRequestField('domain').'"' : '')),$this)
						and header('Location: '.$this->sHome.'/Registar/RobotsTxtManager');
					break;
					case 'Edit':
						$this->new_log = $this->getRequestFormField('file');
						$this->fillGenerate()
						and $this->Action->processEvent($this->SectionID,'log',array('type' => 2,'entity' => 11,'event' => 12,'value' => 'Edit robots.txt file'.(validate($this->getRequestField('domain'))? ' for subdomain "'.$this->getRequestField('domain').'"' : '')),$this)
						and header('Location: '.$this->sHome.'/Registar/RobotsTxtManager');
					break;
				}
			}
			else header('Location: '.$this->sHome.'/Registar/RobotsTxtManager');
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'edit': $this->fillEdit(); break;
			default: $this->fillDefault(); break;
		}
	}

	function fillEdit() {
		global $ROOT_PATH, $REGISTRY, $subdomains, $signature;
		if(empty($subdomains)) $subdomains = $this->Db->SelectList('domain','subdomain',array('domain'));
		if(!empty($this->LocalTarget[1]) && in_array($this->LocalTarget[1],$subdomains)) $domain = $this->LocalTarget[1];
		else $domain = '';
		$this->TempFillEls['domain_url'] = !empty($domain)? $domain.'.'.$signature['domain'][0] : $REGISTRY['Core']['SiteName'];
		$this->TempFillEls['domain'] = $domain;
		if(file_exists($ROOT_PATH.(!empty($domain)? '/subdomains/'.$domain : '').'/robots.txt'))
			$data = file_get_contents($ROOT_PATH.(!empty($domain)? '/subdomains/'.$domain : '').'/robots.txt');
		else $data = '';
		$this->TempFillEls['file'] = inputTextArea('form[file]',(!empty($this->Request)? $this->getRequestFormField('file') : $data),100,40,'style="width:700px;height:300px;"');
		if(count($this->errors) > 0) $this->errors['header'] = 'There were some errors.<br>Please see below for details';
		foreach($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $this->printError($v).($k=='header'? '<p>' : '');
	}

	function fillDefault() {
		global $ROOT_PATH, $REGISTRY, $signature;
		$this->TempFillEls['addBut'] = $this->server_type=='dev'? 'visible' : 'hidden';
		$this->TempFillEls['pubBut'] = $this->server_type=='pro'? 'hidden' : 'visible';
		$array = array();
		if(file_exists($ROOT_PATH.'/robots.txt')) $array = stat($ROOT_PATH.'/robots.txt');
		$this->TempFillEls['lineitems'][0] = array(
			'size' => !empty($array['7'])? $array['7'] : ' - ',
			'ctime' => !empty($array['10'])? date("D, d M Y H:i:s",$array['10']) : ' - ',
			'domain_url' => $signature['domain'][0],
			'domain_url2' => $REGISTRY['Core']['SiteName'],
			'domain_name' => '',
			'domain_name2' => '',
			'publish_type' => '5',
			'publish_value' => '/robots.txt',
			'addBut' => $this->server_type=='dev'? 'visible' : 'hidden',
			'pubBut' => $this->server_type=='pro'? 'hidden' : 'visible',
		);
		$subdomains = $this->Db->SelectList('domain','subdomain',array('domain'),array(),'id');
		foreach($subdomains as $key => $value) {
			$array = array();
			if(file_exists($ROOT_PATH.'/subdomains/'.$value.'/robots.txt')) $array = stat($ROOT_PATH.'/subdomains/'.$value.'/robots.txt');
			$this->TempFillEls['lineitems'][$key+1] = array(
				'size' => !empty($array['7'])? $array['7'] : ' 0 ',
				'ctime' => !empty($array['10'])? date("D, d M Y H:i:s",$array['10']) : ' - ',
				'domain_url' => $value.'.'.$signature['domain'][0],
				'domain_url2' => $value.'.'.$signature['domain'][0],
				'domain_name' => '/'.$value,
				'domain_name2' => $value,
				'publish_type' => '5',
				'publish_value' => '/subdomains/'.$value.'/robots.txt',
				'addBut' => $this->server_type=='dev'? 'visible' : 'hidden',
				'pubBut' => $this->server_type=='pro'? 'hidden' : 'visible',
			);
		}
	}

	function fillGenerate() {
		global $ROOT_PATH;
		//$this->TempFillEls['body'] = !empty($this->new_log)? $this->new_log : ' ';
		//$parser = new XTemplate(readCacheFile($this->Templates['generate'],templatePath(array('RobotsTxtManagerView/'))));
		//$els = &$parser->getElements();
		$data = !empty($this->new_log)? $this->new_log : ' ';
		$file = fopen($ROOT_PATH.(validate($this->getRequestField('domain'))? '/subdomains/'.$this->getRequestField('domain') : '').'/robots.txt','w');
		flock($file,LOCK_EX);
		fwrite($file,$data);
		chmod($ROOT_PATH.(validate($this->getRequestField('domain'))? '/subdomains/'.$this->getRequestField('domain') : '').'/robots.txt',0664);
		flock($file,LOCK_UN);
		fclose($file);
		return true;
	}
}
?>