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