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 | } | |
245 | $table['index'] = &$index; | |
246 | } | |
247 | ||
248 | if ($this->value('foreignKey', $tableXML)) { | |
249 | $foreign = array(); | |
250 | foreach ($tableXML->foreignKey as $foreignXML) { | |
251 | // print_r($foreignXML); | |
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); |
5e434adf | 285 | $field = array('name' => $name, 'localizable' => $fieldXML->localizable); |
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); | |
5e545f38 CW |
364 | $field['html'] = $this->value('html', $fieldXML); |
365 | if (!empty($field['html'])) { | |
366 | $validOptions = array( | |
367 | 'type', | |
24317d89 | 368 | 'formatType', |
5e545f38 CW |
369 | /* Fixme: prior to CRM-13497 these were in a flat structure |
370 | // CRM-13497 moved them to be nested within 'html' but there's no point | |
371 | // making that change in the DAOs right now since we are in the process of | |
372 | // moving to docrtine anyway. | |
373 | // So translating from nested xml back to flat structure for now. | |
374 | 'rows', | |
375 | 'cols', | |
376 | 'size', */ | |
377 | ); | |
378 | $field['html'] = array(); | |
379 | foreach ($validOptions as $htmlOption) { | |
9b873358 | 380 | if (!empty($fieldXML->html->$htmlOption)) { |
5e545f38 CW |
381 | $field['html'][$htmlOption] = $this->value($htmlOption, $fieldXML->html); |
382 | } | |
383 | } | |
384 | } | |
313df5ee SV |
385 | |
386 | // in multilingual context popup, we need extra information to create appropriate widget | |
387 | if ($fieldXML->localizable) { | |
388 | if (isset($fieldXML->html)) { | |
389 | $field['widget'] = (array) $fieldXML->html; | |
390 | } | |
391 | else { | |
392 | // default | |
393 | $field['widget'] = array('type' => 'Text'); | |
394 | } | |
395 | if (isset($fieldXML->required)) { | |
396 | $field['widget']['required'] = $this->value('required', $fieldXML); | |
397 | } | |
398 | } | |
399 | ||
5e434adf | 400 | $field['pseudoconstant'] = $this->value('pseudoconstant', $fieldXML); |
9b873358 | 401 | if (!empty($field['pseudoconstant'])) { |
5e434adf ARW |
402 | //ok this is a bit long-winded but it gets there & is consistent with above approach |
403 | $field['pseudoconstant'] = array(); | |
404 | $validOptions = array( | |
405 | // Fields can specify EITHER optionGroupName OR table, not both | |
406 | // (since declaring optionGroupName means we are using the civicrm_option_value table) | |
407 | 'optionGroupName', | |
408 | 'table', | |
409 | // If table is specified, keyColumn and labelColumn are also required | |
410 | 'keyColumn', | |
411 | 'labelColumn', | |
412 | // Non-translated machine name for programmatic lookup. Defaults to 'name' if that column exists | |
413 | 'nameColumn', | |
76c1631a | 414 | // Where clause snippet (will be joined to the rest of the query with AND operator) |
5e434adf | 415 | 'condition', |
b44e3f84 | 416 | // callback function incase of static arrays |
cc6443c4 | 417 | 'callback', |
14f42e31 CW |
418 | // Path to options edit form |
419 | 'optionEditPath', | |
5e434adf ARW |
420 | ); |
421 | foreach ($validOptions as $pseudoOption) { | |
9b873358 | 422 | if (!empty($fieldXML->pseudoconstant->$pseudoOption)) { |
5e434adf ARW |
423 | $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant); |
424 | } | |
425 | } | |
14f42e31 CW |
426 | if (!isset($field['pseudoconstant']['optionEditPath']) && !empty($field['pseudoconstant']['optionGroupName'])) { |
427 | $field['pseudoconstant']['optionEditPath'] = 'civicrm/admin/options/' . $field['pseudoconstant']['optionGroupName']; | |
428 | } | |
5e434adf ARW |
429 | // For now, fields that have option lists that are not in the db can simply |
430 | // declare an empty pseudoconstant tag and we'll add this placeholder. | |
431 | // That field's BAO::buildOptions fn will need to be responsible for generating the option list | |
432 | if (empty($field['pseudoconstant'])) { | |
433 | $field['pseudoconstant'] = 'not in database'; | |
434 | } | |
435 | } | |
436 | $fields[$name] = &$field; | |
437 | } | |
438 | ||
2558c2b0 | 439 | /** |
100fef9d | 440 | * @param string $name |
2558c2b0 EM |
441 | * |
442 | * @return string | |
443 | */ | |
00be9182 | 444 | public function composeTitle($name) { |
5e434adf ARW |
445 | $names = explode('_', strtolower($name)); |
446 | $title = ''; | |
447 | for ($i = 0; $i < count($names); $i++) { | |
448 | if ($names[$i] === 'id' || $names[$i] === 'is') { | |
449 | // id's do not get titles | |
450 | return NULL; | |
451 | } | |
452 | ||
453 | if ($names[$i] === 'im') { | |
454 | $names[$i] = 'IM'; | |
455 | } | |
456 | else { | |
457 | $names[$i] = ucfirst(trim($names[$i])); | |
458 | } | |
459 | ||
460 | $title = $title . ' ' . $names[$i]; | |
461 | } | |
462 | return trim($title); | |
463 | } | |
464 | ||
2558c2b0 | 465 | /** |
21ca2cb6 | 466 | * @param object $primaryXML |
467 | * @param array $fields | |
468 | * @param array $table | |
2558c2b0 | 469 | */ |
00be9182 | 470 | public function getPrimaryKey(&$primaryXML, &$fields, &$table) { |
5e434adf ARW |
471 | $name = trim((string ) $primaryXML->name); |
472 | ||
473 | /** need to make sure there is a field of type name */ | |
474 | if (!array_key_exists($name, $fields)) { | |
21ca2cb6 | 475 | echo "primary key $name in {$table['name']} does not have a field definition, ignoring\n"; |
5e434adf ARW |
476 | return; |
477 | } | |
478 | ||
479 | // set the autoincrement property of the field | |
480 | $auto = $this->value('autoincrement', $primaryXML); | |
481 | $fields[$name]['autoincrement'] = $auto; | |
482 | $primaryKey = array( | |
483 | 'name' => $name, | |
484 | 'autoincrement' => $auto, | |
485 | ); | |
486 | $table['primaryKey'] = &$primaryKey; | |
487 | } | |
488 | ||
2558c2b0 EM |
489 | /** |
490 | * @param $indexXML | |
491 | * @param $fields | |
492 | * @param $indices | |
493 | */ | |
00be9182 | 494 | public function getIndex(&$indexXML, &$fields, &$indices) { |
5e434adf ARW |
495 | //echo "\n\n*******************************************************\n"; |
496 | //echo "entering getIndex\n"; | |
497 | ||
498 | $index = array(); | |
499 | // empty index name is fine | |
353ffa53 TO |
500 | $indexName = trim((string) $indexXML->name); |
501 | $index['name'] = $indexName; | |
5e434adf ARW |
502 | $index['field'] = array(); |
503 | ||
504 | // populate fields | |
505 | foreach ($indexXML->fieldName as $v) { | |
2aa397bc TO |
506 | $fieldName = (string) ($v); |
507 | $length = (string) ($v['length']); | |
5e434adf ARW |
508 | if (strlen($length) > 0) { |
509 | $fieldName = "$fieldName($length)"; | |
510 | } | |
511 | $index['field'][] = $fieldName; | |
512 | } | |
513 | ||
514 | $index['localizable'] = FALSE; | |
515 | foreach ($index['field'] as $fieldName) { | |
516 | if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) { | |
517 | $index['localizable'] = TRUE; | |
518 | break; | |
519 | } | |
520 | } | |
521 | ||
522 | // check for unique index | |
523 | if ($this->value('unique', $indexXML)) { | |
524 | $index['unique'] = TRUE; | |
525 | } | |
526 | ||
527 | //echo "\$index = \n"; | |
528 | //print_r($index); | |
529 | ||
530 | // field array cannot be empty | |
531 | if (empty($index['field'])) { | |
532 | echo "No fields defined for index $indexName\n"; | |
533 | return; | |
534 | } | |
535 | ||
536 | // all fieldnames have to be defined and should exist in schema. | |
537 | foreach ($index['field'] as $fieldName) { | |
538 | if (!$fieldName) { | |
539 | echo "Invalid field defination for index $indexName\n"; | |
540 | return; | |
541 | } | |
542 | $parenOffset = strpos($fieldName, '('); | |
543 | if ($parenOffset > 0) { | |
544 | $fieldName = substr($fieldName, 0, $parenOffset); | |
545 | } | |
546 | if (!array_key_exists($fieldName, $fields)) { | |
547 | echo "Table does not contain $fieldName\n"; | |
548 | print_r($fields); | |
549 | exit(); | |
550 | } | |
551 | } | |
552 | $indices[$indexName] = &$index; | |
553 | } | |
554 | ||
2558c2b0 EM |
555 | /** |
556 | * @param $foreignXML | |
557 | * @param $fields | |
558 | * @param $foreignKeys | |
100fef9d | 559 | * @param string $currentTableName |
2558c2b0 | 560 | */ |
00be9182 | 561 | public function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) { |
5e434adf ARW |
562 | $name = trim((string ) $foreignXML->name); |
563 | ||
564 | /** need to make sure there is a field of type name */ | |
565 | if (!array_key_exists($name, $fields)) { | |
2aa397bc | 566 | echo "foreign $name in $currentTableName does not have a field definition, ignoring\n"; |
5e434adf ARW |
567 | return; |
568 | } | |
569 | ||
570 | /** need to check for existence of table and key **/ | |
571 | $table = trim($this->value('table', $foreignXML)); | |
572 | $foreignKey = array( | |
573 | 'name' => $name, | |
574 | 'table' => $table, | |
575 | 'uniqName' => "FK_{$currentTableName}_{$name}", | |
576 | 'key' => trim($this->value('key', $foreignXML)), | |
577 | 'import' => $this->value('import', $foreignXML, FALSE), | |
578 | 'export' => $this->value('import', $foreignXML, FALSE), | |
b44e3f84 | 579 | // we do this matching in a separate phase (resolveForeignKeys) |
5e434adf ARW |
580 | 'className' => NULL, |
581 | 'onDelete' => $this->value('onDelete', $foreignXML, FALSE), | |
582 | ); | |
583 | $foreignKeys[$name] = &$foreignKey; | |
584 | } | |
585 | ||
2558c2b0 EM |
586 | /** |
587 | * @param $foreignXML | |
588 | * @param $dynamicForeignKeys | |
589 | */ | |
00be9182 | 590 | public function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) { |
5e434adf ARW |
591 | $foreignKey = array( |
592 | 'idColumn' => trim($foreignXML->idColumn), | |
593 | 'typeColumn' => trim($foreignXML->typeColumn), | |
594 | 'key' => trim($this->value('key', $foreignXML)), | |
595 | ); | |
596 | $dynamicForeignKeys[] = $foreignKey; | |
597 | } | |
598 | ||
2558c2b0 EM |
599 | /** |
600 | * @param $key | |
601 | * @param $object | |
602 | * @param null $default | |
603 | * | |
604 | * @return null|string | |
605 | */ | |
5e434adf ARW |
606 | protected function value($key, &$object, $default = NULL) { |
607 | if (isset($object->$key)) { | |
608 | return (string ) $object->$key; | |
609 | } | |
610 | return $default; | |
611 | } | |
612 | ||
2558c2b0 EM |
613 | /** |
614 | * @param $attributes | |
615 | * @param $object | |
100fef9d | 616 | * @param string $name |
2558c2b0 EM |
617 | * @param null $pre |
618 | * @param null $post | |
619 | */ | |
5e434adf ARW |
620 | protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) { |
621 | if (!isset($object->$name)) { | |
622 | return; | |
623 | } | |
624 | ||
625 | $value = $pre . trim($object->$name) . $post; | |
626 | $this->append($attributes, ' ', trim($value)); | |
627 | } | |
628 | ||
2558c2b0 EM |
629 | /** |
630 | * @param $str | |
631 | * @param $delim | |
632 | * @param $name | |
633 | */ | |
5e434adf ARW |
634 | protected function append(&$str, $delim, $name) { |
635 | if (empty($name)) { | |
636 | return; | |
637 | } | |
638 | ||
639 | if (is_array($name)) { | |
640 | foreach ($name as $n) { | |
641 | if (empty($n)) { | |
642 | continue; | |
643 | } | |
644 | if (empty($str)) { | |
645 | $str = $n; | |
646 | } | |
647 | else { | |
648 | $str .= $delim . $n; | |
649 | } | |
650 | } | |
651 | } | |
652 | else { | |
653 | if (empty($str)) { | |
654 | $str = $name; | |
655 | } | |
656 | else { | |
657 | $str .= $delim . $name; | |
658 | } | |
659 | } | |
660 | } | |
661 | ||
662 | /** | |
fe482240 | 663 | * Sets the size property of a textfield. |
ea3ddccf | 664 | * |
665 | * @param string $fieldXML | |
666 | * | |
667 | * @return null|string | |
5e434adf ARW |
668 | */ |
669 | protected function getSize($fieldXML) { | |
670 | // Extract from <size> tag if supplied | |
5e545f38 | 671 | if (!empty($fieldXML->html) && $this->value('size', $fieldXML->html)) { |
54f5e87f | 672 | return $this->value('size', $fieldXML->html); |
5e434adf ARW |
673 | } |
674 | // Infer from <length> tag if <size> was not explicitly set or was invalid | |
f53213b2 TM |
675 | // This map is slightly different from CRM_Core_Form_Renderer::$_sizeMapper |
676 | // Because we usually want fields to render as smaller than their maxlength | |
677 | $sizes = array( | |
678 | 2 => 'TWO', | |
679 | 4 => 'FOUR', | |
680 | 6 => 'SIX', | |
681 | 8 => 'EIGHT', | |
682 | 16 => 'TWELVE', | |
683 | 32 => 'MEDIUM', | |
684 | 64 => 'BIG', | |
685 | ); | |
686 | foreach ($sizes as $length => $name) { | |
687 | if ($fieldXML->length <= $length) { | |
688 | return "CRM_Utils_Type::$name"; | |
689 | } | |
690 | } | |
691 | return 'CRM_Utils_Type::HUGE'; | |
5e434adf | 692 | } |
96025800 | 693 | |
5e434adf | 694 | } |