1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die();
10
11 12 13 14 15 16
17 class FOFConfigDomainTables implements FOFConfigDomainInterface
18 {
19 20 21 22 23 24 25 26
27 public function parseDomain(SimpleXMLElement $xml, array &$ret)
28 {
29
30 $ret['tables'] = array();
31
32
33 $tableData = $xml->xpath('table');
34
35
36 if (empty($tableData))
37 {
38 return;
39 }
40
41 foreach ($tableData as $aTable)
42 {
43 $key = (string) $aTable['name'];
44
45 $ret['tables'][$key]['behaviors'] = (string) $aTable->behaviors;
46 $ret['tables'][$key]['tablealias'] = $aTable->xpath('tablealias');
47 $ret['tables'][$key]['fields'] = array();
48 $ret['tables'][$key]['relations'] = array();
49
50 $fieldData = $aTable->xpath('field');
51
52 if (!empty($fieldData))
53 {
54 foreach ($fieldData as $field)
55 {
56 $k = (string) $field['name'];
57 $ret['tables'][$key]['fields'][$k] = (string) $field;
58 }
59 }
60
61 $relationsData = $aTable->xpath('relation');
62
63 if (!empty($relationsData))
64 {
65 foreach ($relationsData as $relationData)
66 {
67 $type = (string)$relationData['type'];
68 $itemName = (string)$relationData['name'];
69
70 if (empty($type) || empty($itemName))
71 {
72 continue;
73 }
74
75 $tableClass = (string)$relationData['tableClass'];
76 $localKey = (string)$relationData['localKey'];
77 $remoteKey = (string)$relationData['remoteKey'];
78 $ourPivotKey = (string)$relationData['ourPivotKey'];
79 $theirPivotKey = (string)$relationData['theirPivotKey'];
80 $pivotTable = (string)$relationData['pivotTable'];
81 $default = (string)$relationData['default'];
82
83 $default = !in_array($default, array('no', 'false', 0));
84
85 $relation = array(
86 'type' => $type,
87 'itemName' => $itemName,
88 'tableClass' => empty($tableClass) ? null : $tableClass,
89 'localKey' => empty($localKey) ? null : $localKey,
90 'remoteKey' => empty($remoteKey) ? null : $remoteKey,
91 'default' => $default,
92 );
93
94 if (!empty($ourPivotKey) || !empty($theirPivotKey) || !empty($pivotTable))
95 {
96 $relation['ourPivotKey'] = empty($ourPivotKey) ? null : $ourPivotKey;
97 $relation['theirPivotKey'] = empty($theirPivotKey) ? null : $theirPivotKey;
98 $relation['pivotTable'] = empty($pivotTable) ? null : $pivotTable;
99 }
100
101 $ret['tables'][$key]['relations'][] = $relation;
102 }
103 }
104 }
105 }
106
107 108 109 110 111 112 113 114 115
116 public function get(&$configuration, $var, $default)
117 {
118 $parts = explode('.', $var);
119
120 $view = $parts[0];
121 $method = 'get' . ucfirst($parts[1]);
122
123 if (!method_exists($this, $method))
124 {
125 return $default;
126 }
127
128 array_shift($parts);
129 array_shift($parts);
130
131 $ret = $this->$method($view, $configuration, $parts, $default);
132
133 return $ret;
134 }
135
136 137 138 139 140 141 142 143 144 145
146 protected function getField($table, &$configuration, $params, $default = '')
147 {
148 $fieldmap = array();
149
150 if (isset($configuration['tables']['*']) && isset($configuration['tables']['*']['fields']))
151 {
152 $fieldmap = $configuration['tables']['*']['fields'];
153 }
154
155 if (isset($configuration['tables'][$table]) && isset($configuration['tables'][$table]['fields']))
156 {
157 $fieldmap = array_merge($fieldmap, $configuration['tables'][$table]['fields']);
158 }
159
160 $map = $default;
161
162 if (empty($params[0]))
163 {
164 $map = $fieldmap;
165 }
166 elseif (isset($fieldmap[$params[0]]))
167 {
168 $map = $fieldmap[$params[0]];
169 }
170
171 return $map;
172 }
173
174 175 176 177 178 179 180 181 182 183
184 protected function getTablealias($table, &$configuration, $params, $default = '')
185 {
186 $tablealias = $default;
187
188 if (isset($configuration['tables']['*'])
189 && isset($configuration['tables']['*']['tablealias'])
190 && isset($configuration['tables']['*']['tablealias'][0]))
191 {
192 $tablealias = (string) $configuration['tables']['*']['tablealias'][0];
193 }
194
195 if (isset($configuration['tables'][$table])
196 && isset($configuration['tables'][$table]['tablealias'])
197 && isset($configuration['tables'][$table]['tablealias'][0]))
198 {
199 $tablealias = (string) $configuration['tables'][$table]['tablealias'][0];
200 }
201
202 return $tablealias;
203 }
204
205 206 207 208 209 210 211 212 213 214
215 protected function getBehaviors($table, &$configuration, $params, $default = '')
216 {
217 $behaviors = $default;
218
219 if (isset($configuration['tables']['*'])
220 && isset($configuration['tables']['*']['behaviors']))
221 {
222 $behaviors = (string) $configuration['tables']['*']['behaviors'];
223 }
224
225 if (isset($configuration['tables'][$table])
226 && isset($configuration['tables'][$table]['behaviors']))
227 {
228 $behaviors = (string) $configuration['tables'][$table]['behaviors'];
229 }
230
231 return $behaviors;
232 }
233
234 235 236 237 238 239 240 241 242 243
244 protected function getRelations($table, &$configuration, $params, $default = '')
245 {
246 $relations = $default;
247
248 if (isset($configuration['tables']['*'])
249 && isset($configuration['tables']['*']['relations']))
250 {
251 $relations = $configuration['tables']['*']['relations'];
252 }
253
254 if (isset($configuration['tables'][$table])
255 && isset($configuration['tables'][$table]['relations']))
256 {
257 $relations = $configuration['tables'][$table]['relations'];
258 }
259
260 return $relations;
261 }
262 }
263