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