<?php
class UnusedObjectsReportView extends Slot {

	var $ModuleID = 4;
	var $SectionID = 40;
	var $target = array();
	var $request = array ();
	var $order = array('Id', 'Name');

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

	function init() {	
		global $ROOT_PATH, $REGISTRY;
		include($ROOT_PATH.'/engine/Tools/common.inc');
		$this->request['pageRecordsQty'] = 50;
		$this->request['curPageNumber'] = isset($this->LocalTarget[3]) ? $this->LocalTarget[3] + 1 : 1;
		switch(@$this->LocalTarget[0]) {
			default: case 'Id': $this->target[0] = 'Id'; $this->request['order'] = 'id'; break;
			case 'Name': $this->target[0] = 'Name'; $this->request['order'] = 'name'; break;
		}
		switch(@$this->LocalTarget[1]) {
			default: case 'ASC': $this->target[1] = 'ASC'; $this->request['direction'] = 'ASC'; break;
			case 'DESC': $this->target[1] = 'DESC'; $this->request['direction'] = 'DESC'; break;
		}
		switch(@$this->LocalTarget[2]) {
			default: case 'Files': {
				$this->target[2] = 'Files';
				$this->request['type'] = 'files';
				$dir = eregi_replace(getenv('SCRIPT_NAME'), '', getenv('SCRIPT_FILENAME'));
				$dir2 = $REGISTRY['Core']['TemplatesDirDefault'];
				$this->request['files'] = $this->parseDir($dir,$Path);
				$this->request['layouts'] = $this->parseDir($dir2,$Path);
				break;
			}
			case 'Layouts': $this->target[2] = 'Layouts'; $this->request['type'] = 'layouts'; break;
			case 'Templates': $this->target[2] = 'Templates'; $this->request['type'] = 'templates'; break;
			case 'Menus': $this->target[2] = 'Menus'; $this->request['type'] = 'menus'; break;
		}
		$this->Action->processEvent($this->SectionID,'select_unused_objects', $this->request, $this);
		$this->getPageControl($this->Home.'/Registar/UnusedObjectsReport/'.$this->target[0].'/'.$this->target[1].'/'.$this->target[2]);
	}

	function fillElements() {
		global $REGISTRY;
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'select_unused_objects');
		$this->TempFillEls['order'] = $this->target[0];
		$this->TempFillEls['direction'] = $this->target[1];
		$this->TempFillEls['title'] = $this->target[2];
		$this->TempFillEls['cur'] = isset($this->LocalTarget[3]) ? '/'.$this->LocalTarget[3] : '';
		foreach ($this->order as $value)
			$this->TempFillEls['to_'.strtolower($value).'_direction'] = $this->target[1];
		$this->TempFillEls['to_'.strtolower($this->target[0]).'_direction'] = $this->target[1] == 'ASC' ? 'DESC' : 'ASC';
		if ($this->target[2] != 'Files')
			$this->TempFillEls[strtolower($this->target[0]).'_direction'] = $this->target[1] == 'ASC' ? '<img src="'.$REGISTRY['Globals']['static']['adminpicPath'].'/arrow_up.gif">' : '<img src="'.$REGISTRY['Globals']['static']['adminpicPath'].'/arrow_down.gif">';
		switch ($this->PreparedTemplates['type']){
			case 'files': {
				foreach($this->PreparedTemplates['curPage'] as $ind => $val)
					$this->TempFillEls['logs'][] = array(
						'number' => ($this->PreparedTemplates['pageRecordsQty']*($this->PreparedTemplates['curPageNumber']-1))+($ind+1),
						'name' => $val
					);
				break;
			}
			case 'menus':
				foreach($this->PreparedTemplates['curPage'] as $ind => $val)
					$this->TempFillEls['logs'][] = array(
						'number' => ($this->PreparedTemplates['pageRecordsQty']*($this->PreparedTemplates['curPageNumber']-1))+($ind+1),
						'id' => $val['id'],
						'name' => '<a href="/Registar/MenuManager/'.$val['type'].'Menu/Update/'.$val['id'].'">'.$val['name'].'</a>'
					);
				break;
			case 'layouts':
				foreach($this->PreparedTemplates['curPage'] as $ind => $val)
					$this->TempFillEls['logs'][] = array(
						'number' => ($this->PreparedTemplates['pageRecordsQty']*($this->PreparedTemplates['curPageNumber']-1))+($ind+1),
						'id' => $val['id'],
						'name' => '<a href="/Registar/LayoutManager/Update/'.$val['id'].'">'.$val['name'].'</a>'
					);
				break;
			case 'templates':
				foreach($this->PreparedTemplates['curPage'] as $ind => $val)
					$this->TempFillEls['logs'][] = array(
						'number' => ($this->PreparedTemplates['pageRecordsQty']*($this->PreparedTemplates['curPageNumber']-1))+($ind+1),
						'id' => $val['id'],
						'name' => '<a href="/Registar/TemplateManager/Update/'.$val['id'].'">'.$val['name'].'</a>'
					);
				break;
		}
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'select_unused_objects');
	}
	
	function parseDir($dir = '', $Path){
		$t = eregi_replace(getenv('SCRIPT_NAME'), '', getenv('SCRIPT_FILENAME'));
		$files = array();
		//print '<br>--- DIR --- : '.$dir.'<br>';
		if ($handle = opendir($dir)) {
			while($item = readdir($handle)){
				//print '--- Item: '.pathinfo($item, PATHINFO_DIRNAME).'<br>';
				if (is_file($dir.'/'.pathinfo($item, PATHINFO_BASENAME))){
					//print 'File: '.$dir.'/'.pathinfo($item, PATHINFO_BASENAME).'<br>';
					if (in_array(".".pathinfo($item, PATHINFO_EXTENSION), $Path['unignored_file_types']) || (pathinfo($item, PATHINFO_EXTENSION) == 'js')){
						$files[] = eregi_replace($t,'',$dir).'/'.pathinfo($item, PATHINFO_BASENAME);
					}
				}
				else if ((is_dir($dir.'/'.pathinfo($item, PATHINFO_BASENAME))) && ((!in_array($item, $Path['ignored_folders']) and (pathinfo($item, PATHINFO_BASENAME) != 'tinymce')) || ($item == 'js'))){
					//print 'Folder: '.eregi_replace($t,'',$dir).'/'.pathinfo($item, PATHINFO_BASENAME).'<br>';
					//$files[] = $this->parseDir($dir.'/'.pathinfo($item, PATHINFO_BASENAME),$Path);
					$temp = $this->parseDir($dir.'/'.pathinfo($item, PATHINFO_BASENAME),$Path);
					if (!empty($temp)) {foreach ($temp as $k => $v)	$files[] = $v;}
				}
			}
			closedir($handle);
		}
		if (!empty($files1)){
			if (!empty($files)) foreach ($files as $k => $v)	$files1[] = $v;
		}
		else
			$files1 = $files;
		return $files1;
	}
}
?>