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