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