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