<?php
require_once 'GenericAdmin/gui/view/ListView.php';
require_once 'GenericAdmin/gui/model/GenadClassList.php';

class DetailView extends AdminView {
	
	// Detail view can be viewed in following modes:
	const DV_NEW = 1;
	const DV_READ_ONLY = 2;
	const DV_EDIT = 3;
	
	public function DetailView($app) {
		$this->app = $app;
		include "name_map.php";
	}
	
	public function view(GenadInstance $inst, $mode, $requestAppclass = null) {
		$this->printHeader();
		$className = $inst->getName();
		$appclass = $this->app." ".$inst->getClassId();
		if (!$requestAppclass) $requestAppclass = $appclass;
		$id = $inst->getId();
		$mod = $inst->getMod();
		echo <<<DELETE_QUERY
  <!-- check box for delete query -->
  <div id="deleteQuery" class="overlay">
	 <div class="popup">
		<p>Really delete this instance?</p>
		<a class="button" href="#">No</a>&nbsp;&nbsp;&nbsp;<a class="button" href="ListView.php?event=Delete&appclass=$appclass&Id=$id&mod=$mod">Yes</a>
	 </div>
  </div>
DELETE_QUERY;
		
		echo "<h1>$className</h1>";
		if ($mode == self::DV_NEW)
			echo '<form method="POST" action="DetailView.php?event=create&appclass='."$requestAppclass&Id=$id&mod=$mod".'">';
		elseif ($mode == self::DV_EDIT)
			echo '<form method="POST" action="DetailView.php?event=submit&appclass='."$requestAppclass&Id=$id&mod=$mod".'">';
			
		echo "\n<table>";

		$specialAttributeExists = false;
		$atts = $inst->getAttributeList();
		foreach ($atts as $att) {
			if ($att->isList() || $att->isFK())
				$specialAttributeExists = true;
			else { // it's a scalar attribute
				if ((($att->isEditable()) || ($mode == self::DV_NEW)) && (! $att->hasSystemInit())) {
					if ($mode == self::DV_READ_ONLY) {
						
						// attribute in principle editable, but no editing mode
						if ($att->isBool()) {
							if ($att->getValue() == "true") $checked = " checked "; else $checked = "";
							echo "<tr><th>".$this->getGuiString($att->getName()).'</th><td class="edit"><input type="checkbox" name="'.
								$att->getName().'" value="true" disabled '.$checked.'/></td></tr>';
						}
						else {
							echo "<tr><th>".$this->getGuiString($att->getName()).'</th><td class="edit">'.$att."</td></tr>\n";
						}						
					} 
					else {
						// check type class
						if ( ($typ_cl = $att->getTypeClass()) != null 
							&& $typ_cl->hasProperty("defd_vals")) { // a enum attribute
							$defd_vals = new ReflectionProperty($typ_cl->name, 'defd_vals');
							echo "<tr><th>".$this->getGuiString($att->getName()).
								'</th><td class="edit"><select name="'.$att->getName().'">';
							foreach ($defd_vals->getValue() as $key => $val) {
								$selected = "";
								if ($val == $att) $selected = ' selected="yes"';
								echo '<option value="'.$val."\"$selected>".$val.'</option>';
							}
							echo '</select></td></tr>';
						}
						else if ($att->isBool()) {
							if ($att->getValue() == "true") $checked = " checked "; else $checked = "";
							echo "<tr><th>".$this->getGuiString($att->getName()).'</th><td class="edit"><input type="checkbox" name="'.
								$att->getName().'" value="true"'.$checked.'/></td></tr>';
						}
						else {
							echo "<tr><th>".$this->getGuiString($att->getName()).'</th><td class="edit"><input type="text" name="'.
								$att->getName().'" value="'.$att.'"/></td></tr>';
						}
					}
				}
				else { // a not editable attribute and not CREATE mode
					if ($att->isBool()) {
						if ($att->getValue() == "true") $checked = " checked "; else $checked = "";
						echo "<tr><th>".$this->getGuiString($att->getName()).'</th><td"><input type="checkbox" name="'.
							$att->getName().'" value="true" disabled '.$checked.'/></td></tr>';
					}
					else
						echo "<tr><th>".$this->getGuiString($att->getName())."</th><td>".$att."</td></tr>\n";
				}
				
			}
				
		}			

		echo "</table>\n";
		
		if ($specialAttributeExists) {
			// process simple relation attributes
			foreach ($atts as $att) {
				if ($att->isFK() && !$att->isList()) {
					$idList = array($att->getValue());
					$rel_moc = $att->getRelatedClass();
					$instList = new GenadInstanceList($this->app, $rel_moc, $idList);
					echo "<h3>Related ".$att->getName()."</h3>";
					$listView = new ListView($this->app);
					if (($att->isEditable() && ($mode == self::DV_EDIT)) || ($mode == self::DV_NEW)) {
						$totalList = new GenadInstanceList($this->app, $rel_moc);
						$att_nam = $this->getGuiString($att->getName());
						$listView->viewList($totalList, false, $instList, $att_nam, false);
					}
					else $listView->viewList($instList, false);
				}
			}
			
			// process simple lists
			foreach ($atts as $att) {
				if (!$att->isFK() && $att->isList()) {
					$att_nam = $this->getGuiString($att->getName());
					echo "<h3>$att_nam List</h3>\n";
					echo "<table>";
					
					$list = $att->getList();
					foreach ($list as $elem) {
						echo "<tr><td>$elem</td></tr>\n";
					}
					echo "</table>";
				}
			}
				
			// process foreign key lists
			foreach ($atts as $att) {
				if ($att->isFK() && $att->isList()) {
					$idList = $att->getList();
					if ($idList === null) $idList = array(); // this happens in empty create
					$rel_moc = $att->getRelatedClass();
					$instList = new GenadInstanceList($this->app, $rel_moc, $idList);
					echo "<h3>Related ".$att->getName()." List</h3>";
					$listView = new ListView("AlarmManagement");
					if (($att->isEditable() && ($mode == self::DV_EDIT)) || ($mode == self::DV_NEW)) {
						$totalList = new GenadInstanceList($this->app, $rel_moc);
						$att_nam = $this->getGuiString($att->getName());
						$listView->viewList($totalList, false, $instList, $att_nam, true);
					}
					else $listView->viewList($instList, false);
/*					$att_nam = $this->getGuiString($att->getName());
					echo "<h3>Related $att_nam List</h3><p>\n";
					$idList = $att->getList();
					$rel_moc = $att->getRelatedClass();
					$instList = new GenadInstanceList($this->app, $rel_moc, $idList);
					$listView = new ListView($this->app);
					$listView->viewList($instList, false);
					if ($mode == self::DV_EDIT) {
						echo "<input type=\"submit\" name=\"EditRelList_$rel_moc\" value=\"Edit this list\"/>";
					}
					echo "</p>\n"; */
				}
			}
		}

		if ($mode == self::DV_READ_ONLY) {
			echo <<<END
<p/>
END;
		$classList = GenadClassList::getInstance($this->app);
		$classId = $inst->getClassId();
		if ($classList->hasProperty($classId, new ClassProperty(ClassProperty::CP_SET)))
 	 		echo "<a title=\"Edit\" href=\"DetailView.php?event=Edit&appclass=$appclass&Id=$id\"><img src=\"view/gfx/edit.png\" alt=\"Edit\"></a>";
 	 	if ($classList->hasProperty($classId, new ClassProperty(ClassProperty::CP_CREATE)))
    		echo "<a title=\"Copy\" href=\"ListView.php?event=Copy&appclass=$appclass&Id=$id\"><img src=\"view/gfx/copy.png\" alt=\"Copy\"></a>";
 	 	if ($classList->hasProperty($classId, new ClassProperty(ClassProperty::CP_DELETE)))
    		echo "<a title=\"Delete\" href=\"ListView.php?event=Info&appclass=$appclass&Id=$id#deleteQuery\"><img src=\"view/gfx/delete.png\" alt=\"Delete\"></a>";
  		echo "</td>";
/*			
<a title="Edit" href="DetailView.php?event=Edit&appclass=$appclass&Id=$id"><img src="view/gfx/edit.png" alt="Edit"></a>
<a title="Copy" href="ListView.php?event=Copy&appclass=$appclass&Id=$id"><img src="view/gfx/copy.png" alt="Copy"></a>
<a title="Delete" href="#deleteQuery"><img src="view/gfx/delete.png" alt="Delete"></a>
END;
*/
		}
		else {
			if ( $mode == self::DV_NEW ) {
				$label = $this->getGuiString("Create");
				$req = "create";
			} else {
				$label = $this->getGuiString("Submit changes");
				$req = "save";
			}
			echo '<input type="submit" name="' . $req . '" value="' . $label . '"/>';
			echo "</form>\n";
		}
		$back = $this->getGuiString("Back");
		echo<<<END1
<p/>
<form method="GET" action="DetailView.php">
<input type="hidden" name="event" value="List"/>
<input type="hidden" name="appclass" value="$requestAppclass"/>
<input type="submit" value="$back"/>
</form>
</body>
</html>
END1;
	}
}