Commit | Line | Data |
---|---|---|
5e434adf ARW |
1 | <?php |
2 | ||
3 | /** | |
4 | * Read the schema specification and parse into internal data structures | |
5 | */ | |
6 | class CRM_Core_CodeGen_Specification { | |
7 | public $tables; | |
8 | public $database; | |
9 | ||
10 | protected $classNames; | |
11 | ||
12 | /** | |
13 | * Read and parse. | |
14 | * | |
77b97be7 | 15 | * @param $schemaPath |
6a0b768e TO |
16 | * @param string $buildVersion |
17 | * Which version of the schema to build. | |
5e434adf | 18 | */ |
00be9182 | 19 | public function parse($schemaPath, $buildVersion) { |
5e434adf ARW |
20 | $this->buildVersion = $buildVersion; |
21 | ||
92fcb95f | 22 | echo "Parsing schema description " . $schemaPath . "\n"; |
5e434adf | 23 | $dbXML = CRM_Core_CodeGen_Util_Xml::parse($schemaPath); |
5e434adf ARW |
24 | |
25 | echo "Extracting database information\n"; | |
26 | $this->database = &$this->getDatabase($dbXML); | |
5e434adf ARW |
27 | |
28 | $this->classNames = array(); | |
29 | ||
30 | # TODO: peel DAO-specific stuff out of getTables, and spec reading into its own class | |
31 | echo "Extracting table information\n"; | |
32 | $this->tables = $this->getTables($dbXML, $this->database); | |
33 | ||
34 | $this->resolveForeignKeys($this->tables, $this->classNames); | |
35 | $this->tables = $this->orderTables($this->tables); | |
36 | ||
37 | // add archive tables here | |
481a74f4 TO |
38 | foreach ($this->tables as $name => $table) { |
39 | if ($table['archive'] == 'true') { | |
5e434adf ARW |
40 | $name = 'archive_' . $table['name']; |
41 | $table['name'] = $name; | |
42 | $table['archive'] = 'false'; | |
481a74f4 | 43 | if (isset($table['foreignKey'])) { |
5e434adf ARW |
44 | foreach ($table['foreignKey'] as $fkName => $fkValue) { |
45 | if ($this->tables[$fkValue['table']]['archive'] == 'true') { | |
46 | $table['foreignKey'][$fkName]['table'] = 'archive_' . $table['foreignKey'][$fkName]['table']; | |
acb1052e WA |
47 | $table['foreignKey'][$fkName]['uniqName'] |
48 | = str_replace('FK_', 'FK_archive_', $table['foreignKey'][$fkName]['uniqName']); | |
5e434adf ARW |
49 | } |
50 | } | |
51 | $archiveTables[$name] = $table; | |
52 | } | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
2558c2b0 EM |
57 | /** |
58 | * @param $dbXML | |
59 | * | |
60 | * @return array | |
61 | */ | |
00be9182 | 62 | public function &getDatabase(&$dbXML) { |
5e434adf ARW |
63 | $database = array('name' => trim((string ) $dbXML->name)); |
64 | ||
65 | $attributes = ''; | |
66 | $this->checkAndAppend($attributes, $dbXML, 'character_set', 'DEFAULT CHARACTER SET ', ''); | |
67 | $this->checkAndAppend($attributes, $dbXML, 'collate', 'COLLATE ', ''); | |
68 | $database['attributes'] = $attributes; | |
69 | ||
70 | $tableAttributes_modern = $tableAttributes_simple = ''; | |
71 | $this->checkAndAppend($tableAttributes_modern, $dbXML, 'table_type', 'ENGINE=', ''); | |
72 | $this->checkAndAppend($tableAttributes_simple, $dbXML, 'table_type', 'TYPE=', ''); | |
73 | $database['tableAttributes_modern'] = trim($tableAttributes_modern . ' ' . $attributes); | |
74 | $database['tableAttributes_simple'] = trim($tableAttributes_simple); | |
75 | ||
76 | $database['comment'] = $this->value('comment', $dbXML, ''); | |
77 | ||
78 | return $database; | |
79 | } | |
80 | ||
2558c2b0 EM |
81 | /** |
82 | * @param $dbXML | |
83 | * @param $database | |
84 | * | |
85 | * @return array | |
86 | */ | |
00be9182 | 87 | public function getTables($dbXML, &$database) { |
5e434adf ARW |
88 | $tables = array(); |
89 | foreach ($dbXML->tables as $tablesXML) { | |
90 | foreach ($tablesXML->table as $tableXML) { | |
91 | if ($this->value('drop', $tableXML, 0) > 0 and $this->value('drop', $tableXML, 0) <= $this->buildVersion) { | |
92 | continue; | |
93 | } | |
94 | ||
95 | if ($this->value('add', $tableXML, 0) <= $this->buildVersion) { | |
96 | $this->getTable($tableXML, $database, $tables); | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | return $tables; | |
102 | } | |
103 | ||
2558c2b0 EM |
104 | /** |
105 | * @param $tables | |
100fef9d | 106 | * @param string $classNames |
2558c2b0 | 107 | */ |
00be9182 | 108 | public function resolveForeignKeys(&$tables, &$classNames) { |
5e434adf ARW |
109 | foreach (array_keys($tables) as $name) { |
110 | $this->resolveForeignKey($tables, $classNames, $name); | |
111 | } | |
112 | } | |
113 | ||
2558c2b0 EM |
114 | /** |
115 | * @param $tables | |
100fef9d CW |
116 | * @param string $classNames |
117 | * @param string $name | |
2558c2b0 | 118 | */ |
00be9182 | 119 | public function resolveForeignKey(&$tables, &$classNames, $name) { |
5e434adf ARW |
120 | if (!array_key_exists('foreignKey', $tables[$name])) { |
121 | return; | |
122 | } | |
123 | ||
124 | foreach (array_keys($tables[$name]['foreignKey']) as $fkey) { | |
125 | $ftable = $tables[$name]['foreignKey'][$fkey]['table']; | |
126 | if (!array_key_exists($ftable, $classNames)) { | |
127 | echo "$ftable is not a valid foreign key table in $name\n"; | |
128 | continue; | |
129 | } | |
130 | $tables[$name]['foreignKey'][$fkey]['className'] = $classNames[$ftable]; | |
131 | $tables[$name]['foreignKey'][$fkey]['fileName'] = str_replace('_', '/', $classNames[$ftable]) . '.php'; | |
132 | $tables[$name]['fields'][$fkey]['FKClassName'] = $classNames[$ftable]; | |
133 | } | |
134 | } | |
135 | ||
2558c2b0 EM |
136 | /** |
137 | * @param $tables | |
138 | * | |
139 | * @return array | |
140 | */ | |
00be9182 | 141 | public function orderTables(&$tables) { |
5e434adf ARW |
142 | $ordered = array(); |
143 | ||
144 | while (!empty($tables)) { | |
145 | foreach (array_keys($tables) as $name) { | |
146 | if ($this->validTable($tables, $ordered, $name)) { | |
147 | $ordered[$name] = $tables[$name]; | |
148 | unset($tables[$name]); | |
149 | } | |
150 | } | |
151 | } | |
152 | return $ordered; | |
153 | } | |
154 | ||
2558c2b0 EM |
155 | /** |
156 | * @param $tables | |
100fef9d CW |
157 | * @param int $valid |
158 | * @param string $name | |
2558c2b0 EM |
159 | * |
160 | * @return bool | |
161 | */ | |
00be9182 | 162 | public function validTable(&$tables, &$valid, $name) { |
5e434adf ARW |
163 | if (!array_key_exists('foreignKey', $tables[$name])) { |
164 | return TRUE; | |
165 | } | |
166 | ||
167 | foreach (array_keys($tables[$name]['foreignKey']) as $fkey) { | |
168 | $ftable = $tables[$name]['foreignKey'][$fkey]['table']; | |
169 | if (!array_key_exists($ftable, $valid) && $ftable !== $name) { | |
170 | return FALSE; | |
171 | } | |
172 | } | |
173 | return TRUE; | |
174 | } | |
175 | ||
2558c2b0 EM |
176 | /** |
177 | * @param $tableXML | |
178 | * @param $database | |
179 | * @param $tables | |
180 | */ | |
00be9182 | 181 | public function getTable($tableXML, &$database, &$tables) { |
5e434adf ARW |
182 | $name = trim((string ) $tableXML->name); |
183 | $klass = trim((string ) $tableXML->class); | |
184 | $base = $this->value('base', $tableXML); | |
185 | $sourceFile = "xml/schema/{$base}/{$klass}.xml"; | |
186 | $daoPath = "{$base}/DAO/"; | |
3cef3c00 | 187 | $baoPath = __DIR__ . '/../../../' . str_replace(' ', '', "{$base}/BAO/"); |
5e434adf ARW |
188 | $pre = str_replace('/', '_', $daoPath); |
189 | $this->classNames[$name] = $pre . $klass; | |
190 | ||
191 | $localizable = FALSE; | |
192 | foreach ($tableXML->field as $fieldXML) { | |
193 | if ($fieldXML->localizable) { | |
194 | $localizable = TRUE; | |
195 | break; | |
196 | } | |
197 | } | |
198 | ||
199 | $table = array( | |
200 | 'name' => $name, | |
201 | 'base' => $daoPath, | |
202 | 'sourceFile' => $sourceFile, | |
203 | 'fileName' => $klass . '.php', | |
204 | 'objectName' => $klass, | |
205 | 'labelName' => substr($name, 8), | |
206 | 'className' => $this->classNames[$name], | |
3cef3c00 | 207 | 'bao' => (file_exists($baoPath . $klass . '.php') ? str_replace('DAO', 'BAO', $this->classNames[$name]) : $this->classNames[$name]), |
208 | 'entity' => $klass, | |
5e434adf ARW |
209 | 'attributes_simple' => trim($database['tableAttributes_simple']), |
210 | 'attributes_modern' => trim($database['tableAttributes_modern']), | |
211 | 'comment' => $this->value('comment', $tableXML), | |
212 | 'localizable' => $localizable, | |
213 | 'log' => $this->value('log', $tableXML, 'false'), | |
214 | 'archive' => $this->value('archive', $tableXML, 'false'), | |
215 | ); | |
216 | ||
217 | $fields = array(); | |
218 | foreach ($tableXML->field as $fieldXML) { | |
219 | if ($this->value('drop', $fieldXML, 0) > 0 and $this->value('drop', $fieldXML, 0) <= $this->buildVersion) { | |
220 | continue; | |
221 | } | |
222 | ||
223 | if ($this->value('add', $fieldXML, 0) <= $this->buildVersion) { | |
224 | $this->getField($fieldXML, $fields); | |
225 | } | |
226 | } | |
227 | ||
228 | $table['fields'] = &$fields; | |
5e434adf ARW |
229 | |
230 | if ($this->value('primaryKey', $tableXML)) { | |
231 | $this->getPrimaryKey($tableXML->primaryKey, $fields, $table); | |
232 | } | |
233 | ||
234 | // some kind of refresh? | |
235 | CRM_Core_Config::singleton(FALSE); | |
236 | if ($this->value('index', $tableXML)) { | |
237 | $index = array(); | |
238 | foreach ($tableXML->index as $indexXML) { | |
239 | if ($this->value('drop', $indexXML, 0) > 0 and $this->value('drop', $indexXML, 0) <= $this->buildVersion) { | |
240 | continue; | |
241 | } | |
242 | ||
243 | $this->getIndex($indexXML, $fields, $index); | |
244 | } | |
6b86d84f | 245 | CRM_Core_BAO_SchemaHandler::addIndexSignature($name, $index); |
5e434adf ARW |
246 | $table['index'] = &$index; |
247 | } | |
248 | ||
249 | if ($this->value('foreignKey', $tableXML)) { | |
250 | $foreign = array(); | |
251 | foreach ($tableXML->foreignKey as $foreignXML) { | |
5e434adf ARW |
252 | |
253 | if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion) { | |
254 | continue; | |
255 | } | |
256 | if ($this->value('add', $foreignXML, 0) <= $this->buildVersion) { | |
257 | $this->getForeignKey($foreignXML, $fields, $foreign, $name); | |
258 | } | |
259 | } | |
260 | $table['foreignKey'] = &$foreign; | |
261 | } | |
262 | ||
263 | if ($this->value('dynamicForeignKey', $tableXML)) { | |
264 | $dynamicForeign = array(); | |
265 | foreach ($tableXML->dynamicForeignKey as $foreignXML) { | |
266 | if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion) { | |
267 | continue; | |
268 | } | |
269 | if ($this->value('add', $foreignXML, 0) <= $this->buildVersion) { | |
270 | $this->getDynamicForeignKey($foreignXML, $dynamicForeign, $name); | |
271 | } | |
272 | } | |
273 | $table['dynamicForeignKey'] = $dynamicForeign; | |
274 | } | |
275 | ||
276 | $tables[$name] = &$table; | |
5e434adf ARW |
277 | } |
278 | ||
2558c2b0 EM |
279 | /** |
280 | * @param $fieldXML | |
281 | * @param $fields | |
282 | */ | |
00be9182 | 283 | public function getField(&$fieldXML, &$fields) { |
353ffa53 | 284 | $name = trim((string ) $fieldXML->name); |
7db08f50 | 285 | $field = array('name' => $name, 'localizable' => ((bool) $fieldXML->localizable) ? 1 : 0); |
353ffa53 | 286 | $type = (string ) $fieldXML->type; |
5e434adf ARW |
287 | switch ($type) { |
288 | case 'varchar': | |
289 | case 'char': | |
290 | $field['length'] = (int) $fieldXML->length; | |
291 | $field['sqlType'] = "$type({$field['length']})"; | |
292 | $field['phpType'] = 'string'; | |
293 | $field['crmType'] = 'CRM_Utils_Type::T_STRING'; | |
294 | $field['size'] = $this->getSize($fieldXML); | |
295 | break; | |
296 | ||
5e434adf ARW |
297 | case 'text': |
298 | $field['sqlType'] = $field['phpType'] = $type; | |
299 | $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type); | |
5e545f38 CW |
300 | // CRM-13497 see fixme below |
301 | $field['rows'] = isset($fieldXML->html) ? $this->value('rows', $fieldXML->html) : NULL; | |
302 | $field['cols'] = isset($fieldXML->html) ? $this->value('cols', $fieldXML->html) : NULL; | |
303 | break; | |
2aa397bc | 304 | |
acb1052e | 305 | break; |
5e434adf ARW |
306 | |
307 | case 'datetime': | |
308 | $field['sqlType'] = $field['phpType'] = $type; | |
309 | $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME'; | |
310 | break; | |
311 | ||
312 | case 'boolean': | |
313 | // need this case since some versions of mysql do not have boolean as a valid column type and hence it | |
314 | // is changed to tinyint. hopefully after 2 yrs this case can be removed. | |
315 | $field['sqlType'] = 'tinyint'; | |
316 | $field['phpType'] = $type; | |
317 | $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type); | |
318 | break; | |
319 | ||
320 | case 'decimal': | |
321 | $length = $fieldXML->length ? $fieldXML->length : '20,2'; | |
322 | $field['sqlType'] = 'decimal(' . $length . ')'; | |
323 | $field['phpType'] = 'float'; | |
324 | $field['crmType'] = 'CRM_Utils_Type::T_MONEY'; | |
ac5f2ccd | 325 | $field['precision'] = $length; |
5e434adf ARW |
326 | break; |
327 | ||
328 | case 'float': | |
329 | $field['sqlType'] = 'double'; | |
330 | $field['phpType'] = 'float'; | |
331 | $field['crmType'] = 'CRM_Utils_Type::T_FLOAT'; | |
332 | break; | |
333 | ||
334 | default: | |
335 | $field['sqlType'] = $field['phpType'] = $type; | |
336 | if ($type == 'int unsigned') { | |
337 | $field['crmType'] = 'CRM_Utils_Type::T_INT'; | |
338 | } | |
339 | else { | |
340 | $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type); | |
341 | } | |
342 | break; | |
343 | } | |
344 | ||
345 | $field['required'] = $this->value('required', $fieldXML); | |
353ffa53 TO |
346 | $field['collate'] = $this->value('collate', $fieldXML); |
347 | $field['comment'] = $this->value('comment', $fieldXML); | |
348 | $field['default'] = $this->value('default', $fieldXML); | |
349 | $field['import'] = $this->value('import', $fieldXML); | |
5e434adf ARW |
350 | if ($this->value('export', $fieldXML)) { |
351 | $field['export'] = $this->value('export', $fieldXML); | |
352 | } | |
353 | else { | |
354 | $field['export'] = $this->value('import', $fieldXML); | |
355 | } | |
356 | $field['rule'] = $this->value('rule', $fieldXML); | |
357 | $field['title'] = $this->value('title', $fieldXML); | |
358 | if (!$field['title']) { | |
359 | $field['title'] = $this->composeTitle($name); | |
360 | } | |
361 | $field['headerPattern'] = $this->value('headerPattern', $fieldXML); | |
362 | $field['dataPattern'] = $this->value('dataPattern', $fieldXML); | |
363 | $field['uniqueName'] = $this->value('uniqueName', $fieldXML); | |
2a5c9b4d | 364 | $field['serialize'] = $this->value('serialize', $fieldXML); |
5e545f38 CW |
365 | $field['html'] = $this->value('html', $fieldXML); |
366 | if (!empty($field['html'])) { | |
367 | $validOptions = array( | |
368 | 'type', | |
24317d89 | 369 | 'formatType', |
5e545f38 CW |
370 | /* Fixme: prior to CRM-13497 these were in a flat structure |
371 | // CRM-13497 moved them to be nested within 'html' but there's no point | |
372 | // making that change in the DAOs right now since we are in the process of | |
373 | // moving to docrtine anyway. | |
374 | // So translating from nested xml back to flat structure for now. | |
375 | 'rows', | |
376 | 'cols', | |
377 | 'size', */ | |
378 | ); | |
379 | $field['html'] = array(); | |
380 | foreach ($validOptions as $htmlOption) { | |
9b873358 | 381 | if (!empty($fieldXML->html->$htmlOption)) { |
5e545f38 CW |
382 | $field['html'][$htmlOption] = $this->value($htmlOption, $fieldXML->html); |
383 | } | |
384 | } | |
385 | } | |
313df5ee SV |
386 | |
387 | // in multilingual context popup, we need extra information to create appropriate widget | |
388 | if ($fieldXML->localizable) { | |
389 | if (isset($fieldXML->html)) { | |
390 | $field['widget'] = (array) $fieldXML->html; | |
391 | } | |
392 | else { | |
393 | // default | |
394 | $field['widget'] = array('type' => 'Text'); | |
395 | } | |
396 | if (isset($fieldXML->required)) { | |
397 | $field['widget']['required'] = $this->value('required', $fieldXML); | |
398 | } | |
399 | } | |
400 | ||
5e434adf | 401 | $field['pseudoconstant'] = $this->value('pseudoconstant', $fieldXML); |
9b873358 | 402 | if (!empty($field['pseudoconstant'])) { |
5e434adf ARW |
403 | //ok this is a bit long-winded but it gets there & is consistent with above approach |
404 | $field['pseudoconstant'] = array(); | |
405 | $validOptions = array( | |
406 | // Fields can specify EITHER optionGroupName OR table, not both | |
407 | // (since declaring optionGroupName means we are using the civicrm_option_value table) | |
408 | 'optionGroupName', | |
409 | 'table', | |
410 | // If table is specified, keyColumn and labelColumn are also required | |
411 | 'keyColumn', | |
412 | 'labelColumn', | |
413 | // Non-translated machine name for programmatic lookup. Defaults to 'name' if that column exists | |
414 | 'nameColumn', | |
76c1631a | 415 | // Where clause snippet (will be joined to the rest of the query with AND operator) |
5e434adf | 416 | 'condition', |
b44e3f84 | 417 | // callback function incase of static arrays |
cc6443c4 | 418 | 'callback', |
14f42e31 CW |
419 | // Path to options edit form |
420 | 'optionEditPath', | |
5e434adf ARW |
421 | ); |
422 | foreach ($validOptions as $pseudoOption) { | |
9b873358 | 423 | if (!empty($fieldXML->pseudoconstant->$pseudoOption)) { |
5e434adf ARW |
424 | $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant); |
425 | } | |
426 | } | |
14f42e31 CW |
427 | if (!isset($field['pseudoconstant']['optionEditPath']) && !empty($field['pseudoconstant']['optionGroupName'])) { |
428 | $field['pseudoconstant']['optionEditPath'] = 'civicrm/admin/options/' . $field['pseudoconstant']['optionGroupName']; | |
429 | } | |
5e434adf ARW |
430 | // For now, fields that have option lists that are not in the db can simply |
431 | // declare an empty pseudoconstant tag and we'll add this placeholder. | |
432 | // That field's BAO::buildOptions fn will need to be responsible for generating the option list | |
433 | if (empty($field['pseudoconstant'])) { | |
434 | $field['pseudoconstant'] = 'not in database'; | |
435 | } | |
436 | } | |
437 | $fields[$name] = &$field; | |
438 | } | |
439 | ||
2558c2b0 | 440 | /** |
100fef9d | 441 | * @param string $name |
2558c2b0 EM |
442 | * |
443 | * @return string | |
444 | */ | |
00be9182 | 445 | public function composeTitle($name) { |
5e434adf ARW |
446 | $names = explode('_', strtolower($name)); |
447 | $title = ''; | |
448 | for ($i = 0; $i < count($names); $i++) { | |
449 | if ($names[$i] === 'id' || $names[$i] === 'is') { | |
450 | // id's do not get titles | |
451 | return NULL; | |
452 | } | |
453 | ||
454 | if ($names[$i] === 'im') { | |
455 | $names[$i] = 'IM'; | |
456 | } | |
457 | else { | |
458 | $names[$i] = ucfirst(trim($names[$i])); | |
459 | } | |
460 | ||
461 | $title = $title . ' ' . $names[$i]; | |
462 | } | |
463 | return trim($title); | |
464 | } | |
465 | ||
2558c2b0 | 466 | /** |
21ca2cb6 | 467 | * @param object $primaryXML |
468 | * @param array $fields | |
469 | * @param array $table | |
2558c2b0 | 470 | */ |
00be9182 | 471 | public function getPrimaryKey(&$primaryXML, &$fields, &$table) { |
5e434adf ARW |
472 | $name = trim((string ) $primaryXML->name); |
473 | ||
5e434adf ARW |
474 | // set the autoincrement property of the field |
475 | $auto = $this->value('autoincrement', $primaryXML); | |
022785d8 JJ |
476 | if (isset($fields[$name])) { |
477 | $fields[$name]['autoincrement'] = $auto; | |
478 | } | |
5e434adf ARW |
479 | $fields[$name]['autoincrement'] = $auto; |
480 | $primaryKey = array( | |
481 | 'name' => $name, | |
482 | 'autoincrement' => $auto, | |
483 | ); | |
022785d8 JJ |
484 | |
485 | // populate fields | |
486 | foreach ($primaryXML->fieldName as $v) { | |
487 | $fieldName = (string) ($v); | |
488 | $length = (string) ($v['length']); | |
489 | if (strlen($length) > 0) { | |
490 | $fieldName = "$fieldName($length)"; | |
491 | } | |
492 | $primaryKey['field'][] = $fieldName; | |
493 | } | |
494 | ||
495 | // when field array is empty set it to the name of the primary key. | |
496 | if (empty($primaryKey['field'])) { | |
497 | $primaryKey['field'][] = $name; | |
498 | } | |
499 | ||
500 | // all fieldnames have to be defined and should exist in schema. | |
501 | foreach ($primaryKey['field'] as $fieldName) { | |
502 | if (!$fieldName) { | |
503 | echo "Invalid field defination for index $name\n"; | |
504 | return; | |
505 | } | |
506 | $parenOffset = strpos($fieldName, '('); | |
507 | if ($parenOffset > 0) { | |
508 | $fieldName = substr($fieldName, 0, $parenOffset); | |
509 | } | |
510 | if (!array_key_exists($fieldName, $fields)) { | |
511 | echo "Table does not contain $fieldName\n"; | |
512 | print_r($fields); | |
513 | exit(); | |
514 | } | |
515 | } | |
516 | ||
5e434adf ARW |
517 | $table['primaryKey'] = &$primaryKey; |
518 | } | |
519 | ||
2558c2b0 EM |
520 | /** |
521 | * @param $indexXML | |
522 | * @param $fields | |
523 | * @param $indices | |
524 | */ | |
00be9182 | 525 | public function getIndex(&$indexXML, &$fields, &$indices) { |
5e434adf ARW |
526 | //echo "\n\n*******************************************************\n"; |
527 | //echo "entering getIndex\n"; | |
528 | ||
529 | $index = array(); | |
530 | // empty index name is fine | |
353ffa53 TO |
531 | $indexName = trim((string) $indexXML->name); |
532 | $index['name'] = $indexName; | |
5e434adf ARW |
533 | $index['field'] = array(); |
534 | ||
535 | // populate fields | |
536 | foreach ($indexXML->fieldName as $v) { | |
2aa397bc TO |
537 | $fieldName = (string) ($v); |
538 | $length = (string) ($v['length']); | |
5e434adf ARW |
539 | if (strlen($length) > 0) { |
540 | $fieldName = "$fieldName($length)"; | |
541 | } | |
542 | $index['field'][] = $fieldName; | |
543 | } | |
544 | ||
545 | $index['localizable'] = FALSE; | |
546 | foreach ($index['field'] as $fieldName) { | |
547 | if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) { | |
548 | $index['localizable'] = TRUE; | |
549 | break; | |
550 | } | |
551 | } | |
552 | ||
553 | // check for unique index | |
554 | if ($this->value('unique', $indexXML)) { | |
555 | $index['unique'] = TRUE; | |
556 | } | |
557 | ||
5e434adf ARW |
558 | // field array cannot be empty |
559 | if (empty($index['field'])) { | |
560 | echo "No fields defined for index $indexName\n"; | |
561 | return; | |
562 | } | |
563 | ||
564 | // all fieldnames have to be defined and should exist in schema. | |
565 | foreach ($index['field'] as $fieldName) { | |
566 | if (!$fieldName) { | |
567 | echo "Invalid field defination for index $indexName\n"; | |
568 | return; | |
569 | } | |
570 | $parenOffset = strpos($fieldName, '('); | |
571 | if ($parenOffset > 0) { | |
572 | $fieldName = substr($fieldName, 0, $parenOffset); | |
573 | } | |
574 | if (!array_key_exists($fieldName, $fields)) { | |
575 | echo "Table does not contain $fieldName\n"; | |
576 | print_r($fields); | |
577 | exit(); | |
578 | } | |
579 | } | |
580 | $indices[$indexName] = &$index; | |
581 | } | |
582 | ||
2558c2b0 EM |
583 | /** |
584 | * @param $foreignXML | |
585 | * @param $fields | |
586 | * @param $foreignKeys | |
100fef9d | 587 | * @param string $currentTableName |
2558c2b0 | 588 | */ |
00be9182 | 589 | public function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) { |
5e434adf ARW |
590 | $name = trim((string ) $foreignXML->name); |
591 | ||
592 | /** need to make sure there is a field of type name */ | |
593 | if (!array_key_exists($name, $fields)) { | |
2aa397bc | 594 | echo "foreign $name in $currentTableName does not have a field definition, ignoring\n"; |
5e434adf ARW |
595 | return; |
596 | } | |
597 | ||
598 | /** need to check for existence of table and key **/ | |
599 | $table = trim($this->value('table', $foreignXML)); | |
600 | $foreignKey = array( | |
601 | 'name' => $name, | |
602 | 'table' => $table, | |
603 | 'uniqName' => "FK_{$currentTableName}_{$name}", | |
604 | 'key' => trim($this->value('key', $foreignXML)), | |
605 | 'import' => $this->value('import', $foreignXML, FALSE), | |
606 | 'export' => $this->value('import', $foreignXML, FALSE), | |
b44e3f84 | 607 | // we do this matching in a separate phase (resolveForeignKeys) |
5e434adf ARW |
608 | 'className' => NULL, |
609 | 'onDelete' => $this->value('onDelete', $foreignXML, FALSE), | |
610 | ); | |
611 | $foreignKeys[$name] = &$foreignKey; | |
612 | } | |
613 | ||
2558c2b0 EM |
614 | /** |
615 | * @param $foreignXML | |
616 | * @param $dynamicForeignKeys | |
617 | */ | |
00be9182 | 618 | public function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) { |
5e434adf ARW |
619 | $foreignKey = array( |
620 | 'idColumn' => trim($foreignXML->idColumn), | |
621 | 'typeColumn' => trim($foreignXML->typeColumn), | |
622 | 'key' => trim($this->value('key', $foreignXML)), | |
623 | ); | |
624 | $dynamicForeignKeys[] = $foreignKey; | |
625 | } | |
626 | ||
2558c2b0 EM |
627 | /** |
628 | * @param $key | |
629 | * @param $object | |
630 | * @param null $default | |
631 | * | |
632 | * @return null|string | |
633 | */ | |
5e434adf ARW |
634 | protected function value($key, &$object, $default = NULL) { |
635 | if (isset($object->$key)) { | |
636 | return (string ) $object->$key; | |
637 | } | |
638 | return $default; | |
639 | } | |
640 | ||
2558c2b0 EM |
641 | /** |
642 | * @param $attributes | |
643 | * @param $object | |
100fef9d | 644 | * @param string $name |
2558c2b0 EM |
645 | * @param null $pre |
646 | * @param null $post | |
647 | */ | |
5e434adf ARW |
648 | protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) { |
649 | if (!isset($object->$name)) { | |
650 | return; | |
651 | } | |
652 | ||
653 | $value = $pre . trim($object->$name) . $post; | |
654 | $this->append($attributes, ' ', trim($value)); | |
655 | } | |
656 | ||
2558c2b0 EM |
657 | /** |
658 | * @param $str | |
659 | * @param $delim | |
660 | * @param $name | |
661 | */ | |
5e434adf ARW |
662 | protected function append(&$str, $delim, $name) { |
663 | if (empty($name)) { | |
664 | return; | |
665 | } | |
666 | ||
667 | if (is_array($name)) { | |
668 | foreach ($name as $n) { | |
669 | if (empty($n)) { | |
670 | continue; | |
671 | } | |
672 | if (empty($str)) { | |
673 | $str = $n; | |
674 | } | |
675 | else { | |
676 | $str .= $delim . $n; | |
677 | } | |
678 | } | |
679 | } | |
680 | else { | |
681 | if (empty($str)) { | |
682 | $str = $name; | |
683 | } | |
684 | else { | |
685 | $str .= $delim . $name; | |
686 | } | |
687 | } | |
688 | } | |
689 | ||
690 | /** | |
fe482240 | 691 | * Sets the size property of a textfield. |
ea3ddccf | 692 | * |
693 | * @param string $fieldXML | |
694 | * | |
695 | * @return null|string | |
5e434adf ARW |
696 | */ |
697 | protected function getSize($fieldXML) { | |
698 | // Extract from <size> tag if supplied | |
5e545f38 | 699 | if (!empty($fieldXML->html) && $this->value('size', $fieldXML->html)) { |
54f5e87f | 700 | return $this->value('size', $fieldXML->html); |
5e434adf ARW |
701 | } |
702 | // Infer from <length> tag if <size> was not explicitly set or was invalid | |
f53213b2 TM |
703 | // This map is slightly different from CRM_Core_Form_Renderer::$_sizeMapper |
704 | // Because we usually want fields to render as smaller than their maxlength | |
705 | $sizes = array( | |
706 | 2 => 'TWO', | |
707 | 4 => 'FOUR', | |
708 | 6 => 'SIX', | |
709 | 8 => 'EIGHT', | |
710 | 16 => 'TWELVE', | |
711 | 32 => 'MEDIUM', | |
712 | 64 => 'BIG', | |
713 | ); | |
714 | foreach ($sizes as $length => $name) { | |
715 | if ($fieldXML->length <= $length) { | |
716 | return "CRM_Utils_Type::$name"; | |
717 | } | |
718 | } | |
719 | return 'CRM_Utils_Type::HUGE'; | |
5e434adf | 720 | } |
96025800 | 721 | |
5e434adf | 722 | } |