Merge pull request #18882 from eileenmcnaughton/pps
[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 $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
88 /**
89 * @param $dbXML
90 * @param $database
91 *
92 * @return array
93 */
94 public function getTables($dbXML, &$database) {
95 $tables = [];
96 foreach ($dbXML->tables as $tablesXML) {
97 foreach ($tablesXML->table as $tableXML) {
98 if ($this->value('drop', $tableXML, 0) > 0 && version_compare($this->value('drop', $tableXML, 0), $this->buildVersion, '<=')) {
99 continue;
100 }
101
102 if (version_compare($this->value('add', $tableXML, 0), $this->buildVersion, '<=')) {
103 $this->getTable($tableXML, $database, $tables);
104 }
105 }
106 }
107
108 return $tables;
109 }
110
111 /**
112 * @param $tables
113 * @param string $classNames
114 */
115 public function resolveForeignKeys(&$tables, &$classNames) {
116 foreach (array_keys($tables) as $name) {
117 $this->resolveForeignKey($tables, $classNames, $name);
118 }
119 }
120
121 /**
122 * @param $tables
123 * @param string $classNames
124 * @param string $name
125 */
126 public function resolveForeignKey(&$tables, &$classNames, $name) {
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
143 /**
144 * @param $tables
145 *
146 * @return array
147 */
148 public function orderTables(&$tables) {
149 $ordered = [];
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
162 /**
163 * @param $tables
164 * @param int $valid
165 * @param string $name
166 *
167 * @return bool
168 */
169 public function validTable(&$tables, &$valid, $name) {
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
183 /**
184 * @param $tableXML
185 * @param $database
186 * @param $tables
187 */
188 public function getTable($tableXML, &$database, &$tables) {
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/";
194 $baoPath = __DIR__ . '/../../../' . str_replace(' ', '', "{$base}/BAO/");
195 $useBao = $this->value('useBao', $tableXML, file_exists($baoPath . $klass . '.php'));
196 $pre = str_replace('/', '_', $daoPath);
197 $this->classNames[$name] = $pre . $klass;
198
199 $localizable = FALSE;
200 foreach ($tableXML->field as $fieldXML) {
201 if ($fieldXML->localizable) {
202 $localizable = TRUE;
203 break;
204 }
205 }
206
207 $titleFromClass = preg_replace('/([a-z])([A-Z])/', '$1 $2', $klass);
208 $table = [
209 'name' => $name,
210 'base' => $daoPath,
211 'sourceFile' => $sourceFile,
212 'fileName' => $klass . '.php',
213 'objectName' => $klass,
214 'title' => $tableXML->title ?? $titleFromClass,
215 'titlePlural' => $tableXML->titlePlural ?? CRM_Utils_String::pluralize($tableXML->title ?? $titleFromClass),
216 'icon' => $tableXML->icon ?? NULL,
217 'add' => $tableXML->add ?? NULL,
218 'paths' => (array) ($tableXML->paths ?? []),
219 'labelName' => substr($name, 8),
220 'className' => $this->classNames[$name],
221 'bao' => ($useBao ? str_replace('DAO', 'BAO', $this->classNames[$name]) : $this->classNames[$name]),
222 'entity' => $klass,
223 'attributes_simple' => trim($database['tableAttributes_simple']),
224 'attributes_modern' => trim($database['tableAttributes_modern']),
225 'comment' => $this->value('comment', $tableXML),
226 'localizable' => $localizable,
227 'log' => $this->value('log', $tableXML, 'false'),
228 'archive' => $this->value('archive', $tableXML, 'false'),
229 ];
230
231 $fields = [];
232 foreach ($tableXML->field as $fieldXML) {
233 if ($this->value('drop', $fieldXML, 0) > 0 && version_compare($this->value('drop', $fieldXML, 0), $this->buildVersion, '<=')) {
234 continue;
235 }
236
237 if (version_compare($this->value('add', $fieldXML, 0), $this->buildVersion, '<=')) {
238 $this->getField($fieldXML, $fields);
239 }
240 }
241
242 $table['fields'] = &$fields;
243
244 if ($this->value('primaryKey', $tableXML)) {
245 $this->getPrimaryKey($tableXML->primaryKey, $fields, $table);
246 }
247
248 if ($this->value('index', $tableXML)) {
249 $index = [];
250 foreach ($tableXML->index as $indexXML) {
251 if ($this->value('drop', $indexXML, 0) > 0 && version_compare($this->value('drop', $indexXML, 0), $this->buildVersion, '<=')) {
252 continue;
253 }
254
255 $this->getIndex($indexXML, $fields, $index);
256 }
257 CRM_Core_BAO_SchemaHandler::addIndexSignature($name, $index);
258 $table['index'] = &$index;
259 }
260
261 if ($this->value('foreignKey', $tableXML)) {
262 $foreign = [];
263 foreach ($tableXML->foreignKey as $foreignXML) {
264
265 if ($this->value('drop', $foreignXML, 0) > 0 && version_compare($this->value('drop', $foreignXML, 0), $this->buildVersion, '<=')) {
266 continue;
267 }
268 if (version_compare($this->value('add', $foreignXML, 0), $this->buildVersion, '<=')) {
269 $this->getForeignKey($foreignXML, $fields, $foreign, $name);
270 }
271 }
272 $table['foreignKey'] = &$foreign;
273 }
274
275 if ($this->value('dynamicForeignKey', $tableXML)) {
276 $dynamicForeign = [];
277 foreach ($tableXML->dynamicForeignKey as $foreignXML) {
278 if ($this->value('drop', $foreignXML, 0) > 0 && version_compare($this->value('drop', $foreignXML, 0), $this->buildVersion, '<=')) {
279 continue;
280 }
281 if (version_compare($this->value('add', $foreignXML, 0), $this->buildVersion, '<=')) {
282 $this->getDynamicForeignKey($foreignXML, $dynamicForeign, $name);
283 }
284 }
285 $table['dynamicForeignKey'] = $dynamicForeign;
286 }
287
288 $tables[$name] = &$table;
289 }
290
291 /**
292 * @param $fieldXML
293 * @param $fields
294 */
295 public function getField(&$fieldXML, &$fields) {
296 $name = trim((string ) $fieldXML->name);
297 $field = ['name' => $name, 'localizable' => ((bool) $fieldXML->localizable) ? 1 : 0];
298 $type = (string ) $fieldXML->type;
299 switch ($type) {
300 case 'varchar':
301 case 'char':
302 $field['length'] = (int) $fieldXML->length;
303 $field['sqlType'] = "$type({$field['length']})";
304 $field['phpType'] = 'string';
305 $field['crmType'] = 'CRM_Utils_Type::T_STRING';
306 $field['size'] = $this->getSize($fieldXML);
307 break;
308
309 case 'text':
310 $field['sqlType'] = $field['phpType'] = $type;
311 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
312 // CRM-13497 see fixme below
313 $field['rows'] = isset($fieldXML->html) ? $this->value('rows', $fieldXML->html) : NULL;
314 $field['cols'] = isset($fieldXML->html) ? $this->value('cols', $fieldXML->html) : NULL;
315 break;
316
317 break;
318
319 case 'datetime':
320 $field['sqlType'] = $field['phpType'] = $type;
321 $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME';
322 break;
323
324 case 'boolean':
325 // need this case since some versions of mysql do not have boolean as a valid column type and hence it
326 // is changed to tinyint. hopefully after 2 yrs this case can be removed.
327 $field['sqlType'] = 'tinyint';
328 $field['phpType'] = 'bool';
329 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
330 break;
331
332 case 'decimal':
333 $length = $fieldXML->length ? $fieldXML->length : '20,2';
334 $field['sqlType'] = 'decimal(' . $length . ')';
335 $field['phpType'] = 'float';
336 $field['crmType'] = 'CRM_Utils_Type::T_MONEY';
337 $field['precision'] = $length . ',';
338 break;
339
340 case 'float':
341 $field['sqlType'] = 'double';
342 $field['phpType'] = 'float';
343 $field['crmType'] = 'CRM_Utils_Type::T_FLOAT';
344 break;
345
346 default:
347 $field['phpType'] = $this->value('phpType', $fieldXML, $type);
348 $field['sqlType'] = $type;
349 if ($type == 'int unsigned' || $type == 'tinyint') {
350 $field['phpType'] = 'int';
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['required'] = $this->value('required', $fieldXML);
360 $field['collate'] = $this->value('collate', $fieldXML);
361 $field['comment'] = $this->value('comment', $fieldXML);
362 $field['default'] = $this->value('default', $fieldXML);
363 $field['import'] = $this->value('import', $fieldXML);
364 if ($this->value('export', $fieldXML)) {
365 $field['export'] = $this->value('export', $fieldXML);
366 }
367 else {
368 $field['export'] = $this->value('import', $fieldXML);
369 }
370 $field['rule'] = $this->value('rule', $fieldXML);
371 $field['title'] = $this->value('title', $fieldXML);
372 if (!$field['title']) {
373 $field['title'] = $this->composeTitle($name);
374 }
375 $field['headerPattern'] = $this->value('headerPattern', $fieldXML);
376 $field['dataPattern'] = $this->value('dataPattern', $fieldXML);
377 $field['uniqueName'] = $this->value('uniqueName', $fieldXML);
378 $field['uniqueTitle'] = $this->value('uniqueTitle', $fieldXML);
379 $field['serialize'] = $this->value('serialize', $fieldXML);
380 $field['html'] = $this->value('html', $fieldXML);
381 $field['contactType'] = $this->value('contactType', $fieldXML);
382 if (isset($fieldXML->permission)) {
383 $field['permission'] = trim($this->value('permission', $fieldXML));
384 $field['permission'] = $field['permission'] ? array_filter(array_map('trim', explode(',', $field['permission']))) : [];
385 if (isset($fieldXML->permission->or)) {
386 $field['permission'][] = array_filter(array_map('trim', explode(',', $fieldXML->permission->or)));
387 }
388 }
389 if (!empty($field['html'])) {
390 $validOptions = [
391 'type',
392 'formatType',
393 'label',
394 'controlField',
395 /* Fixme: prior to CRM-13497 these were in a flat structure
396 // CRM-13497 moved them to be nested within 'html' but there's no point
397 // making that change in the DAOs right now since we are in the process of
398 // moving to docrtine anyway.
399 // So translating from nested xml back to flat structure for now.
400 'rows',
401 'cols',
402 'size', */
403 ];
404 $field['html'] = [];
405 foreach ($validOptions as $htmlOption) {
406 if (!empty($fieldXML->html->$htmlOption)) {
407 $field['html'][$htmlOption] = $this->value($htmlOption, $fieldXML->html);
408 }
409 }
410 }
411
412 // in multilingual context popup, we need extra information to create appropriate widget
413 if ($fieldXML->localizable) {
414 if (isset($fieldXML->html)) {
415 $field['widget'] = (array) $fieldXML->html;
416 }
417 else {
418 // default
419 $field['widget'] = ['type' => 'Text'];
420 }
421 if (isset($fieldXML->required)) {
422 $field['widget']['required'] = $this->value('required', $fieldXML);
423 }
424 }
425 if (isset($fieldXML->localize_context)) {
426 $field['localize_context'] = $fieldXML->localize_context;
427 }
428 $field['add'] = $this->value('add', $fieldXML);
429 $field['pseudoconstant'] = $this->value('pseudoconstant', $fieldXML);
430 if (!empty($field['pseudoconstant'])) {
431 //ok this is a bit long-winded but it gets there & is consistent with above approach
432 $field['pseudoconstant'] = [];
433 $validOptions = [
434 // Fields can specify EITHER optionGroupName OR table, not both
435 // (since declaring optionGroupName means we are using the civicrm_option_value table)
436 'optionGroupName',
437 'table',
438 // If table is specified, keyColumn and labelColumn are also required
439 'keyColumn',
440 'labelColumn',
441 // Non-translated machine name for programmatic lookup. Defaults to 'name' if that column exists
442 'nameColumn',
443 // Column to fetch in "abbreviate" context
444 'abbrColumn',
445 // Where clause snippet (will be joined to the rest of the query with AND operator)
446 'condition',
447 // callback function incase of static arrays
448 'callback',
449 // Path to options edit form
450 'optionEditPath',
451 ];
452 foreach ($validOptions as $pseudoOption) {
453 if (!empty($fieldXML->pseudoconstant->$pseudoOption)) {
454 $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant);
455 }
456 }
457 if (!isset($field['pseudoconstant']['optionEditPath']) && !empty($field['pseudoconstant']['optionGroupName'])) {
458 $field['pseudoconstant']['optionEditPath'] = 'civicrm/admin/options/' . $field['pseudoconstant']['optionGroupName'];
459 }
460 // For now, fields that have option lists that are not in the db can simply
461 // declare an empty pseudoconstant tag and we'll add this placeholder.
462 // That field's BAO::buildOptions fn will need to be responsible for generating the option list
463 if (empty($field['pseudoconstant'])) {
464 $field['pseudoconstant'] = 'not in database';
465 }
466 }
467 $fields[$name] = &$field;
468 }
469
470 /**
471 * @param string $name
472 *
473 * @return string
474 */
475 public function composeTitle($name) {
476 $names = explode('_', strtolower($name));
477 $title = '';
478 for ($i = 0; $i < count($names); $i++) {
479 if ($names[$i] === 'id' || $names[$i] === 'is') {
480 // id's do not get titles
481 return NULL;
482 }
483
484 if ($names[$i] === 'im') {
485 $names[$i] = 'IM';
486 }
487 else {
488 $names[$i] = ucfirst(trim($names[$i]));
489 }
490
491 $title = $title . ' ' . $names[$i];
492 }
493 return trim($title);
494 }
495
496 /**
497 * @param object $primaryXML
498 * @param array $fields
499 * @param array $table
500 */
501 public function getPrimaryKey(&$primaryXML, &$fields, &$table) {
502 $name = trim((string ) $primaryXML->name);
503
504 // set the autoincrement property of the field
505 $auto = $this->value('autoincrement', $primaryXML);
506 if (isset($fields[$name])) {
507 $fields[$name]['autoincrement'] = $auto;
508 }
509 $fields[$name]['autoincrement'] = $auto;
510 $primaryKey = [
511 'name' => $name,
512 'autoincrement' => $auto,
513 ];
514
515 // populate fields
516 foreach ($primaryXML->fieldName as $v) {
517 $fieldName = (string) ($v);
518 $length = (string) ($v['length']);
519 if (strlen($length) > 0) {
520 $fieldName = "$fieldName($length)";
521 }
522 $primaryKey['field'][] = $fieldName;
523 }
524
525 // when field array is empty set it to the name of the primary key.
526 if (empty($primaryKey['field'])) {
527 $primaryKey['field'][] = $name;
528 }
529
530 // all fieldnames have to be defined and should exist in schema.
531 foreach ($primaryKey['field'] as $fieldName) {
532 if (!$fieldName) {
533 echo "Invalid field defination for index $name\n";
534 return;
535 }
536 $parenOffset = strpos($fieldName, '(');
537 if ($parenOffset > 0) {
538 $fieldName = substr($fieldName, 0, $parenOffset);
539 }
540 if (!array_key_exists($fieldName, $fields)) {
541 echo "Table does not contain $fieldName\n";
542 print_r($fields);
543 exit();
544 }
545 }
546
547 $table['primaryKey'] = &$primaryKey;
548 }
549
550 /**
551 * @param $indexXML
552 * @param $fields
553 * @param $indices
554 */
555 public function getIndex(&$indexXML, &$fields, &$indices) {
556 //echo "\n\n*******************************************************\n";
557 //echo "entering getIndex\n";
558
559 $index = [];
560 // empty index name is fine
561 $indexName = trim((string) $indexXML->name);
562 $index['name'] = $indexName;
563 $index['field'] = [];
564
565 // populate fields
566 foreach ($indexXML->fieldName as $v) {
567 $fieldName = (string) ($v);
568 $length = (string) ($v['length']);
569 if (strlen($length) > 0) {
570 $fieldName = "$fieldName($length)";
571 }
572 $index['field'][] = $fieldName;
573 }
574
575 $index['localizable'] = FALSE;
576 foreach ($index['field'] as $fieldName) {
577 if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) {
578 $index['localizable'] = TRUE;
579 break;
580 }
581 }
582
583 // check for unique index
584 if ($this->value('unique', $indexXML)) {
585 $index['unique'] = TRUE;
586 }
587
588 // field array cannot be empty
589 if (empty($index['field'])) {
590 echo "No fields defined for index $indexName\n";
591 return;
592 }
593
594 // all fieldnames have to be defined and should exist in schema.
595 foreach ($index['field'] as $fieldName) {
596 if (!$fieldName) {
597 echo "Invalid field defination for index $indexName\n";
598 return;
599 }
600 $parenOffset = strpos($fieldName, '(');
601 if ($parenOffset > 0) {
602 $fieldName = substr($fieldName, 0, $parenOffset);
603 }
604 if (!array_key_exists($fieldName, $fields)) {
605 echo "Table does not contain $fieldName\n";
606 print_r($fields);
607 exit();
608 }
609 }
610 $indices[$indexName] = &$index;
611 }
612
613 /**
614 * @param $foreignXML
615 * @param $fields
616 * @param $foreignKeys
617 * @param string $currentTableName
618 */
619 public function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) {
620 $name = trim((string ) $foreignXML->name);
621
622 /** need to make sure there is a field of type name */
623 if (!array_key_exists($name, $fields)) {
624 echo "foreign $name in $currentTableName does not have a field definition, ignoring\n";
625 return;
626 }
627
628 /** need to check for existence of table and key **/
629 $table = trim($this->value('table', $foreignXML));
630 $foreignKey = [
631 'name' => $name,
632 'table' => $table,
633 'uniqName' => "FK_{$currentTableName}_{$name}",
634 'key' => trim($this->value('key', $foreignXML)),
635 'import' => $this->value('import', $foreignXML, FALSE),
636 'export' => $this->value('import', $foreignXML, FALSE),
637 // we do this matching in a separate phase (resolveForeignKeys)
638 'className' => NULL,
639 'onDelete' => $this->value('onDelete', $foreignXML, FALSE),
640 ];
641 $foreignKeys[$name] = &$foreignKey;
642 }
643
644 /**
645 * @param $foreignXML
646 * @param $dynamicForeignKeys
647 */
648 public function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) {
649 $foreignKey = [
650 'idColumn' => trim($foreignXML->idColumn),
651 'typeColumn' => trim($foreignXML->typeColumn),
652 'key' => trim($this->value('key', $foreignXML)),
653 ];
654 $dynamicForeignKeys[] = $foreignKey;
655 }
656
657 /**
658 * @param $key
659 * @param $object
660 * @param null $default
661 *
662 * @return null|string
663 */
664 protected function value($key, &$object, $default = NULL) {
665 if (isset($object->$key)) {
666 return (string ) $object->$key;
667 }
668 return $default;
669 }
670
671 /**
672 * @param $attributes
673 * @param $object
674 * @param string $name
675 * @param null $pre
676 * @param null $post
677 */
678 protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) {
679 if (!isset($object->$name)) {
680 return;
681 }
682
683 $value = $pre . trim($object->$name) . $post;
684 $this->append($attributes, ' ', trim($value));
685 }
686
687 /**
688 * @param $str
689 * @param $delim
690 * @param $name
691 */
692 protected function append(&$str, $delim, $name) {
693 if (empty($name)) {
694 return;
695 }
696
697 if (is_array($name)) {
698 foreach ($name as $n) {
699 if (empty($n)) {
700 continue;
701 }
702 if (empty($str)) {
703 $str = $n;
704 }
705 else {
706 $str .= $delim . $n;
707 }
708 }
709 }
710 else {
711 if (empty($str)) {
712 $str = $name;
713 }
714 else {
715 $str .= $delim . $name;
716 }
717 }
718 }
719
720 /**
721 * Sets the size property of a textfield.
722 *
723 * @param string $fieldXML
724 *
725 * @return null|string
726 */
727 protected function getSize($fieldXML) {
728 // Extract from <size> tag if supplied
729 if (!empty($fieldXML->html) && $this->value('size', $fieldXML->html)) {
730 return $this->value('size', $fieldXML->html);
731 }
732 // Infer from <length> tag if <size> was not explicitly set or was invalid
733 // This map is slightly different from CRM_Core_Form_Renderer::$_sizeMapper
734 // Because we usually want fields to render as smaller than their maxlength
735 $sizes = [
736 2 => 'TWO',
737 4 => 'FOUR',
738 6 => 'SIX',
739 8 => 'EIGHT',
740 16 => 'TWELVE',
741 32 => 'MEDIUM',
742 64 => 'BIG',
743 ];
744 foreach ($sizes as $length => $name) {
745 if ($fieldXML->length <= $length) {
746 return "CRM_Utils_Type::$name";
747 }
748 }
749 return 'CRM_Utils_Type::HUGE';
750 }
751
752 }