<?php 

class PackagesAlertsReportView extends Slot {

	var $SectionID = 41;
	var $ModuleID = 4;
	var $presets = array();
	var $publish_type = array();
	var $report_type = array();
	var $colors = array(
		0 => 'white',
		1 => '#287BBB',
		2 => '#D4EBF8',
		3 => '#D4EBF8',
		4 => '#E8F2F7',
	);

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

	function init() {
		GLOBAL $ROOT_PATH, $TOOLS_PATH, $CORE_PATH, $COMMON_VARS, $MailBroker;
		$this->Scheme = 'default';
		$this->Action->processEvent($this->SectionID, 'select_presets', array('changed_urls_check' => true,), $this);
		$this->presets = $this->PreparedTemplates;
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'default': $this->fillDefault(); break;
		}
	}
	
	function fillDefault() {
		GLOBAL $REGISTRY;
		$keys = array_keys($this->presets);
		$count = count($keys);
		$index = 0;
		for ($i = 0; $i < $count; $i++){
			$c = count($this->presets[$keys[$i]]['presets']);
			if($keys[$i] == 1){
				for ($j = 0; $j < $c; $j++){
					$this->TempFillEls['preset'][$index]['type'] = '[SINGLE]';
					$this->TempFillEls['preset'][$index]['name'] = '<a href="'.$this->presets[$keys[$i]]['presets'][$j]['path'].'">['.$this->presets[$keys[$i]]['presets'][$j]['id'].'] '.$this->presets[$keys[$i]]['presets'][$j]['name'].'</a>';
					$deleted_count = count($this->presets[$keys[$i]]['presets'][$j]['deleted_objects']);
					$changed_urls_count = count($this->presets[$keys[$i]]['presets'][$j]['changed_urls']);
					if(!$deleted_count && !$changed_urls_count){
						$this->TempFillEls['preset'][$index]['alert'][] = array(
							'bgcolor' => $this->colors[0],
							'message' => 'No alerts were found',
							'checkbox' => '&nbsp;',
						);
						$index++;
						continue;
					}
					if($deleted_count){
						$this->TempFillEls['preset'][$index]['alert'][] = array(
							'bgcolor' => $this->colors[1],
							'message' => '<span style="color: white; font-weight: bolder;">1. Do "Deleted Objects Processing" on server <nobr>"'.$this->presets[$keys[$i]]['presets'][$j]['name'].'"</nobr>. Reason:</span>',
							'checkbox' => '&nbsp;',
						);
						$this->TempFillEls['preset'][$index]['alert'][] = array(
							'bgcolor' => $this->colors[2],
							'message' => $deleted_count.' unprocessed deleted page'.($deleted_count > 1 ? 's were' : ' was').' found',
							'checkbox' => inputCheckbox('', false, 'style="visibility: hidden;"', ''),
						);
					}
					if($changed_urls_count)
						$this->TempFillEls['preset'][$index]['alert'][] = array(
							'bgcolor' => $this->colors[1],
							'message' => '<span style="color: white; font-weight: bolder;">'.($deleted_count ? 2 : 1).'. For pages listed below: Add pages to package (use checkboxes), publish package to server <nobr>"'.$this->presets[$keys[$i]]['presets'][$j]['name'].'"</nobr>. Reason(s):</span>',
							'checkbox' => '&nbsp;',
						);
					for ($k = 0; $k < $changed_urls_count; $k++)
						$this->TempFillEls['preset'][$index]['alert'][] = array(
							'bgcolor' => empty($this->TempFillEls['preset'][$index]['alert']) || (count($this->TempFillEls['preset'][$index]['alert']) % 2) == 0 ? $this->colors[3] : $this->colors[4],
							'message' => 'Page ['.$this->presets[$keys[$i]]['presets'][$j]['changed_urls'][$k]['module_id'].'] "'.$this->presets[$keys[$i]]['presets'][$j]['changed_urls'][$k]['module_name'].'" URL was changed',
							'checkbox' => inputCheckbox('form[adds][3][]', false, 'onClick="setSubmitAbility(this)"', $this->presets[$keys[$i]]['presets'][$j]['changed_urls'][$k]['module_id']),
						);
					$index++;
				}
			}
			else{
				if(!$c) continue;
				$this->TempFillEls['preset'][$index]['type'] = '[GROUP]';
				$this->TempFillEls['preset'][$index]['name'] = $this->presets[$keys[$i]]['name'];
				$list = array();
				$temp_deleted = array();
				$unique_changed_urls = array();
				for ($j = 0; $j < $c; $j++){
					$list[] = '<a href="'.$this->presets[$keys[$i]]['presets'][$j]['path'].'"><nobr>['.$this->presets[$keys[$i]]['presets'][$j]['id'].'] '.$this->presets[$keys[$i]]['presets'][$j]['name'].'</nobr></a>';
					$deleted_count = count($this->presets[$keys[$i]]['presets'][$j]['deleted_objects']);
					for ($k = 0; $k < $deleted_count; $k++) $temp_deleted[$this->presets[$keys[$i]]['presets'][$j]['deleted_objects'][$k]['uid']] = true;
					$changed_urls_count = count($this->presets[$keys[$i]]['presets'][$j]['changed_urls']);
					for ($k = 0; $k < $changed_urls_count; $k++) $unique_changed_urls[$this->presets[$keys[$i]]['presets'][$j]['changed_urls'][$k]['module_id']] = $this->presets[$keys[$i]]['presets'][$j]['changed_urls'][$k];
				}
				if(!empty($list)) $this->TempFillEls['preset'][$index]['list'] = ':<br>'.implode(',<br>', $list);
				$deleted_unique_count = count($temp_deleted);
				$unique_changed_urls = array_values($unique_changed_urls);
				$unique_changed_urls_count = count($unique_changed_urls);
				if(!$deleted_unique_count && !$unique_changed_urls_count){
					$this->TempFillEls['preset'][$index]['alert'][] = array(
						'bgcolor' => $this->colors[0],
						'message' => 'No alerts were found',
						'checkbox' => '&nbsp;',
					);
					$index++;
					continue;
				}
				if($deleted_unique_count){
					$this->TempFillEls['preset'][$index]['alert'][] = array(
						'bgcolor' => $this->colors[1],
						'message' => '<span style="color: white; font-weight: bolder;">1. Do "Deleted Objects Processing" on server group <nobr>"'.$this->presets[$keys[$i]]['name'].'"</nobr>. Reason:</span>',
						'checkbox' => '&nbsp;',
					);
					$this->TempFillEls['preset'][$index]['alert'][] = array(
						'bgcolor' => $this->colors[2],
						'message' => $deleted_unique_count.' unprocessed deleted page'.($deleted_unique_count > 1 ? 's were' : ' was').' found',
						'checkbox' => inputCheckbox('', false, 'style="visibility: hidden;"', ''),
					);
				}
				if($unique_changed_urls_count)
					$this->TempFillEls['preset'][$index]['alert'][] = array(
						'bgcolor' => $this->colors[1],
						'message' => '<span style="color: white; font-weight: bolder;">'.($deleted_unique_count ? 2 : 1).'. For pages listed below: Add pages to package (use checkboxes), publish package to server group <nobr>"'.$this->presets[$keys[$i]]['name'].'"</nobr>. Reason(s):</span>',
						'checkbox' => '&nbsp;',
					);
				for ($j = 0; $j < $unique_changed_urls_count; $j++) $this->TempFillEls['preset'][$index]['alert'][] = array(
					'bgcolor' => empty($this->TempFillEls['preset'][$index]['alert']) || (count($this->TempFillEls['preset'][$index]['alert']) % 2) == 0 ? $this->colors[3] : $this->colors[4],
					'message' => 'Page ['.$unique_changed_urls[$j]['module_id'].']"'.$unique_changed_urls[$j]['module_name'].'" URL was changed',
					'checkbox' => inputCheckbox('form[adds][3][]', false, 'onClick="setSubmitAbility(this)"', $unique_changed_urls[$j]['module_id']),
				);
				$index++;
			}
		}
		$this->clearAllTemplates();
	}
	
	function terminateAction(){
		$this->clearAllTemplates();
		header('Location: '.$this->Home.'/Registar/PackagesAlertsReport'.$this->SID);
		return true;
	}
	
	function clearAllTemplates(){
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select_presets');
	}
}
?>