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