<?php
	defined( "_VALID_PAGE_MONITOR" ) or die (_NODIRECTACCESS);
	
	class ProductList extends Module{
		private $folderList;
			// PreviewBrowser module
			
		private $selectedDate;
			// $_GET["date"]
		private $selectedProduct;
			// $_GET["prod"];
		private $selectedPage;
			// $_GET["page"];
		private $allKey;
		private $onlyReadyPages;
			// $_GET["onlyreadypages"];
		
		private $params;
			
		/* Constructor */
		function ProductList($params){
			//if( !is_a($params, "PreviewBrowser") ){
			//	die( _PRODUCTLISTMODULE_WRONGPARAM );
			//}
			
			// Super constructor
			parent::Module();
			
			$this->params = new Params;
			
			$this->folderList = new FolderList($GLOBALS["pm_config"]["ftp_folder"]);
			$this->selectedDate = $this->params->selectedDate;
			$this->selectedProduct = $this->params->selectedProduct;
			$this->selectedPage = $this->params->selectedPage;
			$this->onlyReadyPages = $this->params->onlyReadyPages;
			$this->allKey = "_all_";
		}
		
		public function getSelectedDate(){
			return $this->selectedDate;
		}
		
		public function getSelectedPage(){
			return $this->selectedPage;
		}
		
		public function getAllKey(){
			return $this->allKey;
		}
		
		/*
		 *	getPath()
		 *	Return the path to date/product [and if selected] selected page. Otherwise it's just
		 *	a path to a folder conatining all the pages folders.
		 */
		public function getPath(){
			$ret = $this->selectedDate . DIRECTORY_SEPARATOR . $this->selectedProduct;
			if( $this->selectedPage != "_all_" )
				$ret .= DIRECTORY_SEPARATOR . $this->selectedPage;
				
			return $ret;
		}
		
		/*
		 *	out()
		 *	Prints out module content.
		 */
		public function out(){
		/*
			if( !$this->params->showPageHistory ){
				echo '<h1>Page list</h1>';
			}else{
				echo '<h1>Page history</h1>';
			}
			
			<div id="browser">
<table border="0">
	<tbody>
		<tr>
			<td class="text"><?=_DATE?></td>
		</tr>
	</tbody>
</table>

		*/
			// Prepare a form with first elements selected.
			?>
			<div id="browser">
			
			<form action="index.php">
			<?php
				if( $this->params->showPageHistory ){
				?>
					<input type="hidden" name="<?=_PARAM_SHOWPAGEHISTORY?>" value="1"/>
				<?php
				}
			?>
			<?php
				if( $this->params->selectedPage ){
				?>
					<input type="hidden" name="<?=_PARAM_PAGE?>" value="<?=$this->params->selectedPage?>"/>
				<?php
				}
			?>
			
			<table border="0" cellpadding="0" cellspacing="0">
			<tbody>
			<tr>
			<td class="text" style="width: 40px;"><?=_DATE?></td>
			<td class="nottext"><select name="date" onchange="submit();">
			<?php
				$arrayOfDates = $this->folderList->getListOfDates();
				$found = false;
				
				if( $this->selectedDate != NULL ){
					foreach( $arrayOfDates as $key => $value ){
							if( $value["filename"] == $this->selectedDate ){
								$found = true;
								break;
							}
					}
				}
				
				if( false == $found )
					if( count($arrayOfDates) )
						$this->selectedDate = $arrayOfDates[0]["filename"];

				foreach( $arrayOfDates as $datestringarray ){
					$selected = $this->selectedDate == $datestringarray["filename"];
				?>
					<option value="<?=$datestringarray["filename"]?>" <?php if( $selected == true ) echo ' selected="selected" ';?> ><?=$datestringarray["string"]?></option>
				<?php
				}
				?>
				</select>
			</td>
			<td class="text" style="width: 60px;"><?=_PRODUCT?></td>
			<td class="nottext" style=""><select name="prod" onchange="submit();">
			<?php
				$arrayOfProducts = $this->folderList->getListOfProducts($this->selectedDate);

				if( !$this->selectedProduct && count($arrayOfProducts) )
					$this->selectedProduct = $arrayOfProducts[0];
										
				if( NULL != $this->selectedProduct && !in_array($this->selectedProduct, $arrayOfProducts) ){
					if( count($arrayOfDates) )
						$this->selectedProduct = $arrayOfProducts[0];
					else
						$this->selectedProduct = NULL;
				}
				foreach( $arrayOfProducts as $prod ){
					$selected = $this->selectedProduct == $prod;
				?>
					<option value="<?=$prod?>" <?php if( $selected == true ) echo ' selected="selected" ';?>><?=$prod?></option>
				<?php
				}
				?>
			</select>
			</td>
			<?php
			/*
			<!--pages: <select name="page" onchange="submit();">-->
			<!--<option value="<?=allKey?>"><?=_ALLPAGES?></option>-->
			<?php
				$arrayOfPages = $this->folderList->getListOfPages($this->selectedDate, $this->selectedProduct);
				if( !$this->selectedPage && count($arrayOfPages) )
					$this->selectedPage = $this->allKey;
					
				if( NULL != $this->selectedPage && !in_array($this->selectedPage, $arrayOfPages) ){
					if( count($arrayOfDates) )
						$this->selectedPage = $this->allKey;
				}

				//foreach( $arrayOfPages as $page ){
					//$selected = $this->selectedPage == $page;
				?>
					<!--<option value="<?=$page?>" <?php if( $selected == true ) echo ' selected ';?> ><?=$page?></option>-->
				<?php
				//}
				?>
			?>
*/
			$arrayOfPages = $this->folderList->getListOfPages($this->selectedDate, $this->selectedProduct);
			if( !$this->selectedPage && count($arrayOfPages) )
				$this->selectedPage = $this->allKey;
				
			if( NULL != $this->selectedPage && !in_array($this->selectedPage, $arrayOfPages) ){
				if( count($arrayOfDates) )
					$this->selectedPage = $this->allKey;
			}
			?>
			
			<td class="nottext" style="text-align: right; width: 50px;"><input type="checkbox" name="<?=_PARAM_ONLY_READY_PAGES?>" onchange="submit();" <?php if( $this->params->onlyReadyPages ) echo "checked"; ?>/></td>
			<td class="text" style="width: 150px;text-align: left;"><?=_SHOWONLYREADYPAGES?></td>

			<td class="notext" style="padding-left: 10px;"><input type="submit" value="<?=_REFRESH?>"/></td>
			
			</tr>
			</tbody>
			</table>
			</form>			
			</div>
			<?php
		}
	}
	
?>