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