File: 0.01.1a/core/DataDictionary.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:  * Data Dictionary Types of Field
 25: **/
 26: abstract class DD_FieldType
 27: {
 28: 	/**
 29: 	 * Integer
 30: 	**/
 31: 	const Int = 0;
 32: 	/**
 33: 	 * Unsigned Int
 34: 	**/
 35: 	const UnsignedInt = 1;
 36: 	/**
 37: 	 * Char
 38: 	**/
 39: 	const Char = 2;
 40: 	/**
 41: 	 * Float
 42: 	**/
 43: 	const Float = 3;
 44: 	/**
 45: 	 * Datetime
 46: 	**/
 47: 	const DateTime = 4;
 48: 	/**
 49: 	 * Text (large multi-line block)
 50: 	**/
 51: 	const Text = 5;
 52: 	/**
 53: 	 * Password (textual)
 54: 	**/
 55: 	const Password = 6;
 56: }
 57: 
 58: /**
 59:  * Data Dictionary Types of Relationship
 60: **/
 61: abstract class DD_RelationshipType
 62: {
 63: 	/**
 64: 	 * One to One
 65: 	**/
 66: 	const OTO = 0;
 67: 	/**
 68: 	 * One to Many
 69: 	**/
 70: 	const OTM = 2;
 71: 	/**
 72: 	 * Many to Many
 73: 	**/
 74: 	const MTM = 3;
 75: }
 76: 
 77: /**
 78:  * Data Dictionary Table class holds information about a table
 79: **/
 80: class DD_Table
 81: {
 82: 	/**
 83: 	 * Table name (human form)
 84: 	**/
 85: 	var $name = "";
 86: 	/**
 87: 	 * Table entity name (database)
 88: 	**/
 89: 	var $entity = "";
 90: 	/**
 91: 	 * Fields
 92: 	**/
 93: 	var $fields = array();
 94: 	/**
 95: 	 * Entity class - for EntityFactory use
 96: 	**/
 97: 	var $class = "";
 98: 	/**
 99: 	 * Keyfield
100: 	**/
101: 	var $keyfield = "";
102: 	/**
103: 	 * Entity Directly Editable
104: 	**/
105: 	var $editable = false;
106: 	/**
107: 	 * Add a field to the table
108: 	 * @param mixed $field Field data of type DD_Field
109: 	**/
110: 	function Add($field)
111: 	{
112: 		$this->fields[$field->field] = $field;
113: 		if ($field->keyfield)
114: 			$this->keyfield = $field->field;
115: 	}
116: 	/**
117: 	 * Get a field or return boolean false if not found
118: 	 * @param string $field Name of field
119: 	 * @return mixed False on fail or DD_Field of field
120: 	**/
121: 	function GetField($field)
122: 	{
123: 		if (isset($this->fields[$field]))
124: 			return $this->fields[$field];
125: 		else
126: 			return false;
127: 	}
128: 	
129: 	/**
130: 	 * Set a field value
131: 	 * @param string $field Field name
132: 	 * @param string $item Item/member name
133: 	 * @param mixed $value Value to set item to
134: 	**/
135: 	function SetVal($field, $item, $value)
136: 	{
137: 		if (isset($fields[$field]))
138: 			$fields[$field]->$item = $value;
139: 	}
140: }
141: 
142: /**
143:  * Data Dictionary Field class holds information about a field
144: **/
145: class DD_Field
146: {
147: 	/**
148: 	 * Field name (human)
149: 	**/
150: 	var $name = "";
151: 	/**
152: 	 * Field column name (database)
153: 	**/
154: 	var $field = "";
155: 	/**
156: 	 * Field type (int, char, text, datetime)
157: 	**/
158: 	var $type = "";
159: 	/**
160: 	 * Size (length for char/text or range for int)
161: 	**/
162: 	var $size = 0;
163: 	/**
164: 	 * Searchable (bool)
165: 	**/
166: 	var $searchable = false;
167: 	/**
168: 	 * Keyfield flag (bool)
169: 	**/
170: 	var $keyfield = false;
171: 	/**
172: 	 * Display in standard results flag (bool)
173: 	**/
174: 	var $display = true;
175: 	/**
176: 	 * Is a foreign key flag (bool)
177: 	**/
178: 	var $foreignkey = false;
179: 	/**
180: 	 * Binding for foreign key - entity
181: 	**/
182: 	var $foreignentity = "";
183: 	/**
184: 	 * Binding for foreign key - field
185: 	**/
186: 	var $foreignfield = "";
187: 	/**
188: 	 * Is read-only
189: 	**/
190: 	var $readonly = false;
191: }
192: 
193: /**
194:  * Data Dictionary Relationship class holds information about inter-entity relationships
195: **/
196: class DD_Relationship
197: {
198: 	/**
199: 	 * Relationship type (1t1, 1tm, m2m)
200: 	**/
201: 	var $type = "";
202: 	/**
203: 	 * First entity
204: 	**/
205: 	var $firstentity = "";
206: 	/**
207: 	 * First field
208: 	**/
209: 	var $firstfield = "";
210: 	/**
211: 	 * Second entity
212: 	**/
213: 	var $secondentity = "";
214: 	/**
215: 	 * Second field
216: 	**/
217: 	var $secondfield = "";
218: 	/**
219: 	 * Link Table (for m2m)
220: 	**/
221: 	var $linktable = "";
222: 	/**
223: 	 * Link table first field (blank uses same name as firstfield)
224: 	**/
225: 	var $linkfirst = "";
226: 	/**
227: 	 * Link table second field (blank uses same name as secondfield)
228: 	**/
229: 	var $linksecond = "";
230: }
231: 
232: /**
233:  * DataDictionary class for FreeDESK - holds all DD information for the system
234: **/
235: class DataDictionary
236: {
237: 	/**
238: 	 * FreeDESK instance
239: 	**/
240: 	private $DESK = null;
241: 	/**
242: 	 * Tables
243: 	**/
244: 	var $Tables = array();
245: 	/**
246: 	 * Relationships
247: 	**/
248: 	var $Relationships = array();
249: 	
250: 	/**
251: 	 * Constructor
252: 	 * @param mixed $freeDESK FreeDESK instance
253: 	**/
254: 	function DataDictionary(&$freeDESK)
255: 	{
256: 		$this->DESK = &$freeDESK;
257: 		$this->DESK->PluginManager->Register(new Plugin(
258: 			"Data Dictionary", "0.01", "Core" ));
259: 	}
260: 	
261: 	/**
262: 	 * Add a table
263: 	 * @param mixed $table Table of type DD_Table
264: 	**/
265: 	function AddTable($table)
266: 	{
267: 		$this->Tables[$table->entity] = $table;
268: 		$perm = "entity_".$table->entity;
269: 		$this->DESK->PermissionManager->Register($perm, false);
270: 	}
271: 	
272: 	/**
273: 	 * Get a table
274: 	 * @param string $table Entity name
275: 	 * @return mixed DD_Table if set otherwise bool false
276: 	**/
277: 	function GetTable($table)
278: 	{
279: 		if (isset($this->Tables[$table]))
280: 			return $this->Tables[$table];
281: 		else
282: 			return false;
283: 	}
284: 	
285: 	/**
286: 	 * Add TO a table (add a field to an existing table)
287: 	 * @param string $table Table entity
288: 	 * @param mixed $field Field (of form DD_Field)
289: 	**/
290: 	function AddToTable($table, $field)
291: 	{
292: 		if (isset($this->Tables[$table]))
293: 			$this->Tables[$table]->Add($field);
294: 	}
295: 	
296: 	/**
297: 	 * Set a field value in a table
298: 	 * @param string $table Table entity
299: 	 * @param string $field Field name
300: 	 * @param string $item Item to set
301: 	 * @param mixed $value Value to set item to
302: 	**/
303: 	function SetFieldVal($table, $field, $item, $value)
304: 	{
305: 		if (isset($this->Tables[$table]))
306: 			$this->Tables[$table]->SetVal($field, $item, $value);
307: 	}
308: 	
309: 	/**
310: 	 * Set a table value
311: 	 * @param string $table Table entity
312: 	 * @param string $item Item to set
313: 	 * @param mixed $value Value to set item to
314: 	**/
315: 	function SetTableVal($table, $item, $value)
316: 	{
317: 		if (isset($this->Tables[$table]))
318: 			$this->Tables[$table]->$item = $value;
319: 	}
320: 	
321: 	/**
322: 	 * Add a relationship
323: 	 * @param mixed $relationship Data of type DD_Relationship
324: 	**/
325: 	function AddRelationship($relationship)
326: 	{
327: 		$this->Relationships[] = $relationship;
328: 	}
329: 	
330: 	/**
331: 	 * Add an item - uses RTTI to discover if a table or a relationship
332: 	 * @param mixed $data Data (of type DD_Table or DD_Relationship)
333: 	**/
334: 	function Add($data)
335: 	{
336: 		$class = get_class($data);
337: 		if ($class == "DD_Table")
338: 			$this->AddTable($data);
339: 		else if ($class == "DD_Relationship")
340: 			$this->AddRelationship($data);
341: 		// Otherwise we have no idea...
342: 	}
343: }
344: 
345: 
346: ?>
347: 	
348: