File:
0.00.1a/core/entity/EntityBase.php (
View as Code)
1: 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 base is the entity base class
25: **/
26: abstract class EntityBase
27: {
28: /**
29: * FreeDESK instance
30: **/
31: protected $DESK = null;
32:
33: /**
34: * Data Elements
35: **/
36: protected $data = array();
37:
38: /**
39: * Entity (table name)
40: **/
41: protected $entity = "";
42:
43: /**
44: * Data dictionary entry for entity
45: **/
46: protected $table = null;
47:
48: /**
49: * Constructor
50: * @param mixed $freeDESK FreeDESK instance
51: **/
52: function EntityBase(&$freeDESK)
53: {
54: $this->DESK = &$freeDESK;
55: }
56:
57: /**
58: * Set a data field to a value
59: * @param string $field Field ID
60: * @param string $data Data
61: **/
62: function Set($field, $data)
63: {
64: $this->data[$field]=$data;
65: }
66:
67: /**
68: * Load from an asociative array
69: * @param array &$assoc Array
70: **/
71: function LoadAssoc(&$assoc)
72: {
73: foreach($assoc as $key => $val)
74: $this->Set($key, $val);
75: }
76:
77: /**
78: * Set the entity
79: * @param string $entity Entity
80: **/
81: function SetEntity($entity)
82: {
83: $this->entity = $entity;
84: }
85:
86: /**
87: * Get entity
88: * @return string Entity
89: **/
90: function GetEntity()
91: {
92: return $this->entity;
93: }
94:
95: /**
96: * Output a data array as XML
97: * @param mixed &$xml XML creator
98: * @param array &$data Data object
99: * @return mixed XML creator
100: **/
101: protected function XMLData(&$xml)
102: {
103: //TODO: Deal with entity lists
104:
105: foreach($this->data as $key=>$item)
106: {
107: if (is_object($item))
108: {
109: if (is_subclass_of($item, "EntityBase"))
110: {
111: $subdata=array("entity"=>$key);
112: $xml->startElement("subentity",$subdata);
113: $xml->charData($item->XML());
114: $xml->endElement("subentity");
115: }
116: else if (get_class($data) == "EntityList")
117: {
118: // TODO
119: }
120: }
121: else
122: {
123: $head=array("field"=>$key);
124: $xml->startElement("field",$head);
125: $xml->charData($item, true);
126: $xml->endElement("field");
127: }
128: }
129: return $xml;
130: }
131:
132: /**
133: * Output as XML - can be overriden
134: * @param bool $header Put an XML header in (optional, default false)
135: * @return string XML for data
136: **/
137: function XML($header=false)
138: {
139: $xml = new xmlCreate();
140: $data=array("entity"=>$this->entity);
141: $xml->startElement("entity",$data);
142: $this->XMLData($xml);
143: $xml->endElement("entity");
144:
145: return $xml->getXML($header);
146: }
147:
148:
149: /**
150: * Get all the raw data
151: * @return array Data array
152: **/
153: function GetData()
154: {
155: return $this->data;
156: }
157:
158:
159: }
160:
161: ?>
162:
163: