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