File: 0.00.0a/entity.php (View as HTML)

  1: <?php 
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeDESK is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: /**
 24:  * Entity Interface - Search, Edit, Create
 25: **/
 26: 
 27: 
 28: // Output buffer on and start FreeDESK then discard startup whitespace-spam
 29: ob_start();
 30: include("core/FreeDESK.php");
 31: $DESK = new FreeDESK("./");
 32: $DESK->Start();
 33: ob_end_clean();
 34: 
 35: 
 36: if (!isset($_REQUEST['sid']) || !$DESK->ContextManager->Open(ContextType::User, $_REQUEST['sid']))
 37: {
 38: 	$data=array("title"=>$DESK->Lang->Get("welcome"));
 39: 	$DESK->Skin->IncludeFile("min_header.php",$data);
 40: 
 41: 	echo "\n<noscript>\n";
 42: 	echo "<h1>Sorry you must have Javascript enabled to use FreeDESK analyst portal</h1>\n";
 43: 	echo "</noscript>\n";
 44: 
 45: 	echo "<h3>".$DESK->Lang->Get("login_invalid").":</h3>\n";
 46: 
 47: 	
 48: 	$DESK->Skin->IncludeFile("min_footer.php");
 49: 	exit();
 50: }
 51: 
 52: 
 53: // So we're authenticated let's view the main page
 54: $data=array("title"=>"FreeDESK");
 55: $DESK->Skin->IncludeFile("min_header.php",$data);
 56: 
 57: if (!isset($_REQUEST['mode']))
 58: 	$mode="";
 59: else
 60: 	$mode=$_REQUEST['mode'];
 61: if (!isset($_REQUEST['entity']))
 62: 	$entity="";
 63: else
 64: 	$entity=$_REQUEST['entity'];
 65: 
 66: $table = $DESK->DataDictionary->GetTable($entity);
 67: 
 68: if ( ($table === false) || (!$table->editable) )
 69: {
 70: 	echo "<h3>".$DESK->Lang->Get("entity_not_found")."</h3>";
 71: }
 72: else if (!$DESK->ContextManager->Permission("entity_view.".$entity))
 73: {
 74: 	echo "<h3>".$DESK->Lang->Get("permission_denied")."</h3>";
 75: }
 76: else if ($mode == "search")
 77: {
 78: 	echo "<script type=\"text/javascript\">\n";
 79: 	echo "DESKSearch.entity = \"".$entity."\";\n";
 80: 	echo "</script>\n";
 81: 	echo "<div id=\"searchfields\">\n";
 82: 	echo "<table class=\"search\">\n";
 83: 	echo "<form id=\"entitysearch\" onsubmit=\"return false;\">\n";
 84: 	
 85: 	$searchnow=false;
 86: 	foreach($table->fields as $id => $field)
 87: 	{
 88: 		if ($field->searchable)
 89: 		{
 90: 			echo "<tr><td>".$field->name."</td>\n";
 91: 			$val="";
 92: 			if (isset($_REQUEST[$field->field]))
 93: 			{
 94: 				$val=$_REQUEST[$field->field];
 95: 				if ($val!="")
 96: 					$searchnow=true;
 97: 			}
 98: 			echo "<td><input type=\"text\" name=\"".$field->field."\" value=\"".$val."\"></td></tr>\n";
 99: 		}
100: 	}
101: 	echo "<tr><td>&nbsp;</td>\n";
102: 	echo "<td><input type=\"submit\" value=\"".$DESK->Lang->Get("search")."\" onclick=\"DESKSearch.search();\"></td>\n";
103: 	echo "</tr>";
104: 	echo "</form></table>\n";
105: 	echo "</div>\n";
106: 	
107: 	echo "<div id=\"searchresults\">\n";
108: 	echo "</div>";
109: 	
110: 	
111: 	echo "<script type=\"text/javascript\">\n";
112: 	if (isset($_REQUEST['callback']) && $_REQUEST['callback']!="")
113: 		echo "DESKSearch.callback = window.opener.".$_REQUEST['callback'].";\n";
114: 	else
115: 		echo "DESKSearch.callback = null;\n";
116: 	if (isset($_REQUEST['onereturn']) && $_REQUEST['onereturn']==1)
117: 		echo "DESKSearch.callbackOnSingle = true;\n";
118: 	else
119: 		echo "DESKSearch.callbackOnSingle = false;\n";
120: 	if ($searchnow)
121: 		echo "DESKSearch.search();\n";
122: 	echo "</script>\n";
123: 	
124: }
125: else if ($mode == "edit")
126: {
127: 	$loaded = $DESK->EntityManager->Load($entity, $_REQUEST['value']);
128: 	if ($loaded !== false)
129: 	{
130: 		$data = $loaded->GetData();
131: 		echo "<table class=\"edit\">\n";
132: 		echo "<form id=\"entity_edit\" onsubmit=\"return false;\">\n";
133: 		echo "<input type=\"hidden\" name=\"mode\" value=\"entity_save\">\n";
134: 		echo "<input type=\"hidden\" name=\"entity\" value=\"".$entity."\">\n";
135: 		foreach($table->fields as $id => $field)
136: 		{
137: 			echo "<tr>\n";
138: 			echo "<td>".$field->name."</td>\n";
139: 			echo "<td>\n";
140: 			// TODO: Different field types
141: 			$i="<input type=\"text\" name=\"".$id."\" value=\"".$data[$id]."\"";
142: 			if ($field->readonly)
143: 				$i.=" readonly";
144: 			$i.=">";
145: 			echo $i;
146: 			echo "</td>\n";
147: 			echo "</tr>\n";
148: 		}
149: 		
150: 		echo "<tr><td>\n";
151: 		echo "<a href=\"#\" onclick=\"window.close();\">Cancel</a>";
152: 		echo "</td><td>";
153: 		echo "<input type=\"submit\" value=\"".$DESK->Lang->Get("save")."\" onclick=\"DESK.formAPI('entity_edit');\"> ";
154: 		echo "<input type=\"submit\" value=\"".$DESK->Lang->Get("save_close")."\" onclick=\"DESK.formAPI('entity_edit',true);\">";
155: 		echo "</td></tr>\n";
156: 		
157: 		echo "</form></table>\n";
158: 	}
159: 	else
160: 	{
161: 		echo "Entity Load Failed";
162: 	}
163: }
164: else if ($mode == "create")
165: {
166: 	echo "<table class=\"create\">\n";
167: 	echo "<form id=\"entity_create\" onsubmit=\"return false;\">\n";
168: 	echo "<input type=\"hidden\" name=\"mode\" value=\"entity_create\">\n";
169: 	echo "<input type=\"hidden\" name=\"entity\" value=\"".$entity."\">\n";
170: 	foreach($table->fields as $id => $field)
171: 	{
172: 		echo "<tr>\n";
173: 		echo "<td>".$field->name."</td>\n";
174: 		echo "<td>\n";
175: 		// TODO: Different field types
176: 		echo "<input type=\"text\" name=\"".$id."\" value=\"\">\n";
177: 		echo "</td></tr>";
178: 	}
179: 	echo "<tr><td>\n";
180: 	echo "<a href=\"#\" onclick=\"window.close();\">Cancel</a>";
181: 	echo "</td><td>";
182: 	echo "<input type=\"submit\" value=\"".$DESK->Lang->Get("save")."\" onclick=\"DESK.formAPI('entity_create');\"> ";
183: 	echo "<input type=\"submit\" value=\"".$DESK->Lang->Get("save_close")."\" onclick=\"DESK.formAPI('entity_create',true);\">";
184: 	echo "</td></tr>\n";
185: 	
186: 	echo "</form></table>\n";
187: }
188: 
189: else
190: {
191: 	echo "<h3>".$DESK->Lang->Get("action_invalid")."</h3>";
192: }
193: 			
194: 
195: $DESK->Skin->IncludeFile("min_footer.php");
196: 
197: 
198: ?>
199: