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