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