<?php

class SubscribeManagerView extends Slot {

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

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

	function init() { 
		$subscriptions = $this->Db->SelectAny("Select * from subscribe");
		ini_set("zlib.output_compression", "Off"); 
		header('Content-Type: text/csv; charset=utf-8');
		header('Content-Disposition: attachment; filename=subscriptions.csv');
		header('Expires: 0');
		header('Pragma: no-cache');

		print "Name,Email\n";
		foreach($subscriptions as $key => $value)
		{
			print $value['name'].",".$value['email']."\n";
		}
		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;
	}
}
?>