d8ed559af661f59ae9220cbae6d3dc34d1d39617
[civicrm-core.git] / xml / GenCode.php
1 <?php
2 ini_set('include_path', '.' . PATH_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'packages' . PATH_SEPARATOR . '..');
3 ini_set('memory_limit', '512M');
4 date_default_timezone_set('UTC'); // avoid php warnings if timezone is not set - CRM-10844
5
6 define('CIVICRM_UF', 'Drupal');
7
8 require_once 'CRM/Core/ClassLoader.php';
9 CRM_Core_ClassLoader::singleton()->register();
10
11 $genCode = new CRM_GenCode_Main('../CRM/Core/DAO/', '../sql/', '../', '../templates/');
12 $genCode->main(
13 @$argv[2],
14 @$argv[3],
15 empty($argv[1]) ? 'schema/Schema.xml' : $argv[1]
16 );
17
18 class CRM_GenCode_Util_File {
19 static function createDir($dir, $perm = 0755) {
20 if (!is_dir($dir)) {
21 mkdir($dir, $perm, TRUE);
22 }
23 }
24
25 static function removeDir($dir) {
26 foreach (glob("$dir/*") as $tempFile) {
27 unlink($tempFile);
28 }
29 rmdir($dir);
30 }
31
32 static function createTempDir($prefix) {
33 if (isset($_SERVER['TMPDIR'])) {
34 $tempDir = $_SERVER['TMPDIR'];
35 }
36 else {
37 $tempDir = '/tmp';
38 }
39
40 $newTempDir = $tempDir . '/' . $prefix . rand(1, 10000);
41
42 if (file_exists($newTempDir)) {
43 self::removeDir($newTempDir);
44 }
45 self::createDir($newTempDir);
46
47 return $newTempDir;
48 }
49 }
50
51 class CRM_GenCode_Main {
52 var $buildVersion;
53 var $compileDir;
54 var $classNames;
55
56 var $CoreDAOCodePath;
57 var $sqlCodePath;
58 var $phpCodePath;
59 var $tplCodePath;
60
61 var $smarty;
62
63 function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath) {
64 $this->CoreDAOCodePath = $CoreDAOCodePath;
65 $this->sqlCodePath = $sqlCodePath;
66 $this->phpCodePath = $phpCodePath;
67 $this->tplCodePath = $tplCodePath;
68
69 require_once 'Smarty/Smarty.class.php';
70 $this->smarty = new Smarty();
71 $this->smarty->template_dir = './templates';
72 $this->smarty->plugins_dir = array('../packages/Smarty/plugins', '../CRM/Core/Smarty/plugins');
73 $this->compileDir = CRM_GenCode_Util_File::createTempDir('templates_c_');
74 $this->smarty->compile_dir = $this->compileDir;
75 $this->smarty->clear_all_cache();
76
77 // CRM-5308 / CRM-3507 - we need {localize} to work in the templates
78 require_once 'CRM/Core/Smarty/plugins/block.localize.php';
79 $this->smarty->register_block('localize', 'smarty_block_localize');
80
81 require_once 'PHP/Beautifier.php';
82 // create a instance
83 $this->beautifier = new PHP_Beautifier();
84 $this->beautifier->addFilter('ArrayNested');
85 // add one or more filters
86 $this->beautifier->addFilter('Pear');
87 // add one or more filters
88 $this->beautifier->addFilter('NewLines', array('after' => 'class, public, require, comment'));
89 $this->beautifier->setIndentChar(' ');
90 $this->beautifier->setIndentNumber(2);
91 $this->beautifier->setNewLine("\n");
92
93 CRM_GenCode_Util_File::createDir($this->sqlCodePath);
94 }
95
96 function __destruct() {
97 CRM_GenCode_Util_File::removeDir($this->compileDir);
98 }
99
100 /**
101 * Automatically generate a variety of files
102 *
103 * @param $argVersion string, optional
104 * @param $argCms string, optional; "drupal" or "joomla"
105 * @param $file, the path to the XML schema file
106 */
107 function main($argVersion, $argCms, $file) {
108 $versionFile = "version.xml";
109 $versionXML = &$this->parseInput($versionFile);
110 $db_version = $versionXML->version_no;
111 $this->buildVersion = preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $db_version);
112 if (isset($argVersion)) {
113 // change the version to that explicitly passed, if any
114 $db_version = $argVersion;
115 }
116 echo "\ncivicrm_domain.version := $db_version\n\n";
117 if ($this->buildVersion < 1.1) {
118 echo "The Database is not compatible for this version";
119 exit();
120 }
121
122 if (substr(phpversion(), 0, 1) != 5) {
123 echo phpversion() . ', ' . substr(phpversion(), 0, 1) . "\n";
124 echo "
125 CiviCRM requires a PHP Version >= 5
126 Please upgrade your php / webserver configuration
127 Alternatively you can get a version of CiviCRM that matches your PHP version
128 ";
129 exit();
130 }
131
132 $this->generateTemplateVersion($argVersion);
133
134 $this->setupCms($argCms, $db_version);
135
136 echo "Parsing input file $file\n";
137 $dbXML = $this->parseInput($file);
138 // print_r( $dbXML );
139
140 echo "Extracting database information\n";
141 $database = &$this->getDatabase($dbXML);
142 // print_r( $database );
143
144 $this->classNames = array();
145
146 echo "Extracting table information\n";
147 $tables = &$this->getTables($dbXML, $database);
148
149 $this->resolveForeignKeys($tables, $this->classNames);
150 $tables = $this->orderTables($tables);
151
152 // add archive tables here
153 $archiveTables = array( );
154 foreach ($tables as $name => $table ) {
155 if ( $table['archive'] == 'true' ) {
156 $name = 'archive_' . $table['name'];
157 $table['name'] = $name;
158 $table['archive'] = 'false';
159 if ( isset($table['foreignKey']) ) {
160 foreach ($table['foreignKey'] as $fkName => $fkValue) {
161 if ($tables[$fkValue['table']]['archive'] == 'true') {
162 $table['foreignKey'][$fkName]['table'] = 'archive_' . $table['foreignKey'][$fkName]['table'];
163 $table['foreignKey'][$fkName]['uniqName'] =
164 str_replace( 'FK_', 'FK_archive_', $table['foreignKey'][$fkName]['uniqName'] );
165 }
166 }
167 $archiveTables[$name] = $table;
168 }
169 }
170 }
171
172 $this->generateListAll($tables);
173 $this->generateCiviTestTruncate($tables);
174 $this->generateCreateSql($database, $tables, 'civicrm.mysql');
175 $this->generateDropSql($tables, 'civicrm_drop.mysql');
176
177 // also create the archive tables
178 // $this->generateCreateSql($database, $archiveTables, 'civicrm_archive.mysql' );
179 // $this->generateDropSql($archiveTables, 'civicrm_archive_drop.mysql');
180
181 $this->generateNavigation();
182 $this->generateLocalDataSql($db_version, $this->findLocales());
183 $this->generateSample();
184 $this->generateInstallLangs();
185 $this->generateDAOs($tables);
186 $this->generateSchemaStructure($tables);
187 }
188
189 function generateListAll($tables) {
190 $this->smarty->clear_all_assign();
191 $this->smarty->assign('tables', $tables);
192 file_put_contents($this->CoreDAOCodePath . "../AllCoreTables.php", $this->smarty->fetch('listAll.tpl'));
193 }
194
195 function generateCiviTestTruncate($tables) {
196 echo "Generating tests truncate file\n";
197
198 $truncate = '<?xml version="1.0" encoding="UTF-8" ?>
199 <!-- Truncate all tables that will be used in the tests -->
200 <dataset>';
201 $tbls = array_keys($tables);
202 foreach ($tbls as $d => $t) {
203 $truncate = $truncate . "\n <$t />\n";
204 }
205
206 $truncate = $truncate . "</dataset>\n";
207 file_put_contents($this->sqlCodePath . "../tests/phpunit/CiviTest/truncate.xml", $truncate);
208 unset($truncate);
209 }
210
211 function generateCreateSql($database, $tables, $fileName = 'civicrm.mysql') {
212 echo "Generating sql file\n";
213 $this->smarty->clear_all_assign();
214 $this->smarty->assign_by_ref('database', $database);
215 $this->smarty->assign_by_ref('tables', $tables);
216 $dropOrder = array_reverse(array_keys($tables));
217 $this->smarty->assign_by_ref('dropOrder', $dropOrder);
218 $this->smarty->assign('mysql', 'modern');
219 file_put_contents($this->sqlCodePath . $fileName, $this->smarty->fetch('schema.tpl'));
220 }
221
222 function generateDropSql($tables, $fileName = 'civicrm_drop.mysql') {
223 echo "Generating sql drop tables file\n";
224 $dropOrder = array_reverse(array_keys($tables));
225 $this->smarty->assign_by_ref('dropOrder', $dropOrder);
226 file_put_contents($this->sqlCodePath . $fileName, $this->smarty->fetch('drop.tpl'));
227 }
228
229 function generateNavigation() {
230 echo "Generating navigation file\n";
231 $this->smarty->clear_all_assign();
232 file_put_contents($this->sqlCodePath . "civicrm_navigation.mysql", $this->smarty->fetch('civicrm_navigation.tpl'));
233 }
234
235 function generateLocalDataSql($db_version, $locales) {
236 $this->smarty->clear_all_assign();
237
238 global $tsLocale;
239 $oldTsLocale = $tsLocale;
240 foreach ($locales as $locale) {
241 echo "Generating data files for $locale\n";
242 $tsLocale = $locale;
243 $this->smarty->assign('locale', $locale);
244
245 $data = array();
246 $data[] = $this->smarty->fetch('civicrm_country.tpl');
247 $data[] = $this->smarty->fetch('civicrm_state_province.tpl');
248 $data[] = $this->smarty->fetch('civicrm_currency.tpl');
249 $data[] = $this->smarty->fetch('civicrm_data.tpl');
250 $data[] = $this->smarty->fetch('civicrm_navigation.tpl');
251
252 $data[] = " UPDATE civicrm_domain SET version = '$db_version';";
253
254 $data = implode("\n", $data);
255
256 $ext = ($locale != 'en_US' ? ".$locale" : '');
257 // write the initialize base-data sql script
258 file_put_contents($this->sqlCodePath . "civicrm_data$ext.mysql", $data);
259
260 // write the acl sql script
261 file_put_contents($this->sqlCodePath . "civicrm_acl$ext.mysql", $this->smarty->fetch('civicrm_acl.tpl'));
262 }
263 $tsLocale = $oldTsLocale;
264 }
265
266 function generateSample() {
267 $this->smarty->clear_all_assign();
268 $sample = $this->smarty->fetch('civicrm_sample.tpl');
269 $sample .= $this->smarty->fetch('civicrm_acl.tpl');
270 file_put_contents($this->sqlCodePath . 'civicrm_sample.mysql', $sample);
271 }
272
273 function generateInstallLangs() {
274 // CRM-7161: generate install/langs.php from the languages template
275 // grep it for enabled languages and create a 'xx_YY' => 'Language name' $langs mapping
276 $matches = array();
277 preg_match_all('/, 1, \'([a-z][a-z]_[A-Z][A-Z])\', \'..\', \{localize\}\'\{ts escape="sql"\}(.+)\{\/ts\}\'\{\/localize\}, /', file_get_contents('templates/languages.tpl'), $matches);
278 $langs = array();
279 for ($i = 0; $i < count($matches[0]); $i++) {
280 $langs[$matches[1][$i]] = $matches[2][$i];
281 }
282 file_put_contents('../install/langs.php', "<?php \$langs = unserialize('" . serialize($langs) . "');");
283 }
284
285 function generateDAOs($tables) {
286 $this->smarty->clear_all_assign();
287 foreach (array_keys($tables) as $name) {
288 $this->smarty->clear_all_cache();
289 echo "Generating $name as " . $tables[$name]['fileName'] . "\n";
290 $this->smarty->clear_all_assign();
291
292 $this->smarty->assign_by_ref('table', $tables[$name]);
293 $php = $this->smarty->fetch('dao.tpl');
294
295 $this->beautifier->setInputString($php);
296
297 if (empty($tables[$name]['base'])) {
298 echo "No base defined for $name, skipping output generation\n";
299 continue;
300 }
301
302 $directory = $this->phpCodePath . $tables[$name]['base'];
303 CRM_GenCode_Util_File::createDir($directory);
304 $this->beautifier->setOutputFile($directory . $tables[$name]['fileName']);
305 // required
306 $this->beautifier->process();
307
308 $this->beautifier->save();
309 }
310 }
311
312 function generateSchemaStructure($tables) {
313 echo "Generating CRM_Core_I18n_SchemaStructure...\n";
314 $columns = array();
315 $indices = array();
316 foreach ($tables as $table) {
317 if ($table['localizable']) {
318 $columns[$table['name']] = array();
319 }
320 else {
321 continue;
322 }
323 foreach ($table['fields'] as $field) {
324 if ($field['localizable']) {
325 $columns[$table['name']][$field['name']] = $field['sqlType'];
326 }
327 }
328 if (isset($table['index'])) {
329 foreach ($table['index'] as $index) {
330 if ($index['localizable']) {
331 $indices[$table['name']][$index['name']] = $index;
332 }
333 }
334 }
335 }
336
337 $this->smarty->clear_all_cache();
338 $this->smarty->clear_all_assign();
339 $this->smarty->assign_by_ref('columns', $columns);
340 $this->smarty->assign_by_ref('indices', $indices);
341
342 $this->beautifier->setInputString($this->smarty->fetch('schema_structure.tpl'));
343 $this->beautifier->setOutputFile($this->phpCodePath . "/CRM/Core/I18n/SchemaStructure.php");
344 $this->beautifier->process();
345 $this->beautifier->save();
346 }
347
348 function generateTemplateVersion($argVersion) {
349 // add the Subversion revision to templates
350 // use svnversion if the version was not specified explicitely on the commandline
351 if (isset($argVersion) and $argVersion != '') {
352 $svnversion = $argVersion;
353 }
354 else {
355 $svnversion = `svnversion .`;
356 }
357 file_put_contents($this->tplCodePath . "/CRM/common/version.tpl", $svnversion);
358 }
359
360 function findLocales() {
361 require_once 'CRM/Core/Config.php';
362 $config = CRM_Core_Config::singleton(FALSE);
363 $locales = array();
364 if (substr($config->gettextResourceDir, 0, 1) === '/') {
365 $localeDir = $config->gettextResourceDir;
366 }
367 else {
368 $localeDir = '../' . $config->gettextResourceDir;
369 }
370 if (file_exists($localeDir)) {
371 $config->gettextResourceDir = $localeDir;
372 $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir));
373 }
374
375 $localesMask = getenv('CIVICRM_LOCALES');
376 if (!empty($localesMask)) {
377 $mask = explode(',', $localesMask);
378 $locales = array_intersect($locales, $mask);
379 }
380
381 if (!in_array('en_US', $locales)) {
382 array_unshift($locales, 'en_US');
383 }
384
385 return $locales;
386 }
387
388 function setupCms($argCms, $db_version) {
389 // default cms is 'drupal', if not specified
390 $cms = isset($argCms) ? strtolower($argCms) : 'drupal';
391 if (!in_array($cms, array(
392 'drupal', 'joomla'))) {
393 echo "Config file for '{$cms}' not known.";
394 exit();
395 }
396 elseif ($cms !== 'joomla') {
397 echo "Generating civicrm.config.php\n";
398 copy("../{$cms}/civicrm.config.php.{$cms}", '../civicrm.config.php');
399 }
400
401 echo "Generating civicrm-version file\n";
402 $this->smarty->assign('db_version', $db_version);
403 $this->smarty->assign('cms', ucwords($cms));
404 file_put_contents($this->phpCodePath . "civicrm-version.php", $this->smarty->fetch('civicrm_version.tpl'));
405 }
406
407 // -----------------------------
408 // ---- Schema manipulation ----
409 // -----------------------------
410 function &parseInput($file) {
411 $dom = new DomDocument();
412 $dom->load($file);
413 $dom->xinclude();
414 $dbXML = simplexml_import_dom($dom);
415 return $dbXML;
416 }
417
418 function &getDatabase(&$dbXML) {
419 $database = array('name' => trim((string ) $dbXML->name));
420
421 $attributes = '';
422 $this->checkAndAppend($attributes, $dbXML, 'character_set', 'DEFAULT CHARACTER SET ', '');
423 $this->checkAndAppend($attributes, $dbXML, 'collate', 'COLLATE ', '');
424 $database['attributes'] = $attributes;
425
426 $tableAttributes_modern = $tableAttributes_simple = '';
427 $this->checkAndAppend($tableAttributes_modern, $dbXML, 'table_type', 'ENGINE=', '');
428 $this->checkAndAppend($tableAttributes_simple, $dbXML, 'table_type', 'TYPE=', '');
429 $database['tableAttributes_modern'] = trim($tableAttributes_modern . ' ' . $attributes);
430 $database['tableAttributes_simple'] = trim($tableAttributes_simple);
431
432 $database['comment'] = $this->value('comment', $dbXML, '');
433
434 return $database;
435 }
436
437 function &getTables(&$dbXML, &$database) {
438 $tables = array();
439 foreach ($dbXML->tables as $tablesXML) {
440 foreach ($tablesXML->table as $tableXML) {
441 if ($this->value('drop', $tableXML, 0) > 0 and $this->value('drop', $tableXML, 0) <= $this->buildVersion) {
442 continue;
443 }
444
445 if ($this->value('add', $tableXML, 0) <= $this->buildVersion) {
446 $this->getTable($tableXML, $database, $tables);
447 }
448 }
449 }
450
451 return $tables;
452 }
453
454 function resolveForeignKeys(&$tables, &$classNames) {
455 foreach (array_keys($tables) as $name) {
456 $this->resolveForeignKey($tables, $classNames, $name);
457 }
458 }
459
460 function resolveForeignKey(&$tables, &$classNames, $name) {
461 if (!array_key_exists('foreignKey', $tables[$name])) {
462 return;
463 }
464
465 foreach (array_keys($tables[$name]['foreignKey']) as $fkey) {
466 $ftable = $tables[$name]['foreignKey'][$fkey]['table'];
467 if (!array_key_exists($ftable, $classNames)) {
468 echo "$ftable is not a valid foreign key table in $name\n";
469 continue;
470 }
471 $tables[$name]['foreignKey'][$fkey]['className'] = $classNames[$ftable];
472 $tables[$name]['foreignKey'][$fkey]['fileName'] = str_replace('_', '/', $classNames[$ftable]) . '.php';
473 $tables[$name]['fields'][$fkey]['FKClassName'] = $classNames[$ftable];
474 }
475 }
476
477 function orderTables(&$tables) {
478 $ordered = array();
479
480 while (!empty($tables)) {
481 foreach (array_keys($tables) as $name) {
482 if ($this->validTable($tables, $ordered, $name)) {
483 $ordered[$name] = $tables[$name];
484 unset($tables[$name]);
485 }
486 }
487 }
488 return $ordered;
489 }
490
491 function validTable(&$tables, &$valid, $name) {
492 if (!array_key_exists('foreignKey', $tables[$name])) {
493 return TRUE;
494 }
495
496 foreach (array_keys($tables[$name]['foreignKey']) as $fkey) {
497 $ftable = $tables[$name]['foreignKey'][$fkey]['table'];
498 if (!array_key_exists($ftable, $valid) && $ftable !== $name) {
499 return FALSE;
500 }
501 }
502 return TRUE;
503 }
504
505 function getTable($tableXML, &$database, &$tables) {
506 $name = trim((string ) $tableXML->name);
507 $klass = trim((string ) $tableXML->class);
508 $base = $this->value('base', $tableXML) . '/DAO/';
509 $pre = str_replace('/', '_', $base);
510 $this->classNames[$name] = $pre . $klass;
511
512 $localizable = FALSE;
513 foreach ($tableXML->field as $fieldXML) {
514 if ($fieldXML->localizable) {
515 $localizable = TRUE;
516 break;
517 }
518 }
519
520 $table = array(
521 'name' => $name,
522 'base' => $base,
523 'fileName' => $klass . '.php',
524 'objectName' => $klass,
525 'labelName' => substr($name, 8),
526 'className' => $this->classNames[$name],
527 'attributes_simple' => trim($database['tableAttributes_simple']),
528 'attributes_modern' => trim($database['tableAttributes_modern']),
529 'comment' => $this->value('comment', $tableXML),
530 'localizable' => $localizable,
531 'log' => $this->value('log', $tableXML, 'false'),
532 'archive' => $this->value('archive', $tableXML, 'false'),
533 );
534
535 $fields = array();
536 foreach ($tableXML->field as $fieldXML) {
537 if ($this->value('drop', $fieldXML, 0) > 0 and $this->value('drop', $fieldXML, 0) <= $this->buildVersion) {
538 continue;
539 }
540
541 if ($this->value('add', $fieldXML, 0) <= $this->buildVersion) {
542 $this->getField($fieldXML, $fields);
543 }
544 }
545
546 $table['fields'] = &$fields;
547 $table['hasEnum'] = FALSE;
548 foreach ($table['fields'] as $field) {
549 if ($field['crmType'] == 'CRM_Utils_Type::T_ENUM') {
550 $table['hasEnum'] = TRUE;
551 break;
552 }
553 }
554
555 if ($this->value('primaryKey', $tableXML)) {
556 $this->getPrimaryKey($tableXML->primaryKey, $fields, $table);
557 }
558
559 // some kind of refresh?
560 CRM_Core_Config::singleton(FALSE);
561 if ($this->value('index', $tableXML)) {
562 $index = array();
563 foreach ($tableXML->index as $indexXML) {
564 if ($this->value('drop', $indexXML, 0) > 0 and $this->value('drop', $indexXML, 0) <= $this->buildVersion) {
565 continue;
566 }
567
568 $this->getIndex($indexXML, $fields, $index);
569 }
570 $table['index'] = &$index;
571 }
572
573 if ($this->value('foreignKey', $tableXML)) {
574 $foreign = array();
575 foreach ($tableXML->foreignKey as $foreignXML) {
576 // print_r($foreignXML);
577
578 if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion) {
579 continue;
580 }
581 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion) {
582 $this->getForeignKey($foreignXML, $fields, $foreign, $name);
583 }
584 }
585 $table['foreignKey'] = &$foreign;
586 }
587
588 if ($this->value('dynamicForeignKey', $tableXML)) {
589 $dynamicForeign = array();
590 foreach ($tableXML->dynamicForeignKey as $foreignXML) {
591 if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion) {
592 continue;
593 }
594 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion) {
595 $this->getDynamicForeignKey($foreignXML, $dynamicForeign, $name);
596 }
597 }
598 $table['dynamicForeignKey'] = $dynamicForeign;
599 }
600
601 $tables[$name] = &$table;
602 return;
603 }
604
605 function getField(&$fieldXML, &$fields) {
606 $name = trim((string ) $fieldXML->name);
607 $field = array('name' => $name, 'localizable' => $fieldXML->localizable);
608 $type = (string ) $fieldXML->type;
609 switch ($type) {
610 case 'varchar':
611 case 'char':
612 $field['length'] = (int) $fieldXML->length;
613 $field['sqlType'] = "$type({$field['length']})";
614 $field['phpType'] = 'string';
615 $field['crmType'] = 'CRM_Utils_Type::T_STRING';
616 $field['size'] = $this->getSize($fieldXML);
617 break;
618
619 case 'enum':
620 $value = (string ) $fieldXML->values;
621 $field['sqlType'] = 'enum(';
622 $field['values'] = array();
623 $field['enumValues'] = $value;
624 $values = explode(',', $value);
625 $first = TRUE;
626 foreach ($values as $v) {
627 $v = trim($v);
628 $field['values'][] = $v;
629
630 if (!$first) {
631 $field['sqlType'] .= ', ';
632 }
633 $first = FALSE;
634 $field['sqlType'] .= "'$v'";
635 }
636 $field['sqlType'] .= ')';
637 $field['phpType'] = $field['sqlType'];
638 $field['crmType'] = 'CRM_Utils_Type::T_ENUM';
639 break;
640
641 case 'text':
642 $field['sqlType'] = $field['phpType'] = $type;
643 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
644 $field['rows'] = $this->value('rows', $fieldXML);
645 $field['cols'] = $this->value('cols', $fieldXML);
646 break;
647
648 case 'datetime':
649 $field['sqlType'] = $field['phpType'] = $type;
650 $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME';
651 break;
652
653 case 'boolean':
654 // need this case since some versions of mysql do not have boolean as a valid column type and hence it
655 // is changed to tinyint. hopefully after 2 yrs this case can be removed.
656 $field['sqlType'] = 'tinyint';
657 $field['phpType'] = $type;
658 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
659 break;
660
661 case 'decimal':
662 $length = $fieldXML->length ? $fieldXML->length : '20,2';
663 $field['sqlType'] = 'decimal(' . $length . ')';
664 $field['phpType'] = 'float';
665 $field['crmType'] = 'CRM_Utils_Type::T_MONEY';
666 break;
667
668 case 'float':
669 $field['sqlType'] = 'double';
670 $field['phpType'] = 'float';
671 $field['crmType'] = 'CRM_Utils_Type::T_FLOAT';
672 break;
673
674 default:
675 $field['sqlType'] = $field['phpType'] = $type;
676 if ($type == 'int unsigned') {
677 $field['crmType'] = 'CRM_Utils_Type::T_INT';
678 }
679 else {
680 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
681 }
682 break;
683 }
684
685 $field['required'] = $this->value('required', $fieldXML);
686 $field['comment'] = $this->value('comment', $fieldXML);
687 $field['default'] = $this->value('default', $fieldXML);
688 $field['import'] = $this->value('import', $fieldXML);
689 if ($this->value('export', $fieldXML)) {
690 $field['export'] = $this->value('export', $fieldXML);
691 }
692 else {
693 $field['export'] = $this->value('import', $fieldXML);
694 }
695 $field['rule'] = $this->value('rule', $fieldXML);
696 $field['title'] = $this->value('title', $fieldXML);
697 if (!$field['title']) {
698 $field['title'] = $this->composeTitle($name);
699 }
700 $field['headerPattern'] = $this->value('headerPattern', $fieldXML);
701 $field['dataPattern'] = $this->value('dataPattern', $fieldXML);
702 $field['uniqueName'] = $this->value('uniqueName', $fieldXML);
703 $field['pseudoconstant'] = $this->value('pseudoconstant', $fieldXML);
704 if(!empty($fieldXML->pseudoconstant)){
705 //ok this is a bit long-winded but it gets there & is consistent with above approach
706 $field['pseudoconstant'] = array();
707 $validOptions = array('name', 'optionGroupName', 'table', 'keyColumn', 'labelColumn','class');
708 foreach ($validOptions as $pseudoOption){
709 if(!empty($fieldXML->pseudoconstant->$pseudoOption)){
710 $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant);
711 }
712 }
713 }
714 $fields[$name] = &$field;
715 }
716
717 function composeTitle($name) {
718 $names = explode('_', strtolower($name));
719 $title = '';
720 for ($i = 0; $i < count($names); $i++) {
721 if ($names[$i] === 'id' || $names[$i] === 'is') {
722 // id's do not get titles
723 return NULL;
724 }
725
726 if ($names[$i] === 'im') {
727 $names[$i] = 'IM';
728 }
729 else {
730 $names[$i] = ucfirst(trim($names[$i]));
731 }
732
733 $title = $title . ' ' . $names[$i];
734 }
735 return trim($title);
736 }
737
738 function getPrimaryKey(&$primaryXML, &$fields, &$table) {
739 $name = trim((string ) $primaryXML->name);
740
741 /** need to make sure there is a field of type name */
742 if (!array_key_exists($name, $fields)) {
743 echo "primary key $name in $table->name does not have a field definition, ignoring\n";
744 return;
745 }
746
747 // set the autoincrement property of the field
748 $auto = $this->value('autoincrement', $primaryXML);
749 $fields[$name]['autoincrement'] = $auto;
750 $primaryKey = array(
751 'name' => $name,
752 'autoincrement' => $auto,
753 );
754 $table['primaryKey'] = &$primaryKey;
755 }
756
757 function getIndex(&$indexXML, &$fields, &$indices) {
758 //echo "\n\n*******************************************************\n";
759 //echo "entering getIndex\n";
760
761 $index = array();
762 // empty index name is fine
763 $indexName = trim((string)$indexXML->name);
764 $index['name'] = $indexName;
765 $index['field'] = array();
766
767 // populate fields
768 foreach ($indexXML->fieldName as $v) {
769 $fieldName = (string)($v);
770 $length = (string)($v['length']);
771 if (strlen($length) > 0) {
772 $fieldName = "$fieldName($length)";
773 }
774 $index['field'][] = $fieldName;
775 }
776
777 $index['localizable'] = FALSE;
778 foreach ($index['field'] as $fieldName) {
779 if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) {
780 $index['localizable'] = TRUE;
781 break;
782 }
783 }
784
785 // check for unique index
786 if ($this->value('unique', $indexXML)) {
787 $index['unique'] = TRUE;
788 }
789
790 //echo "\$index = \n";
791 //print_r($index);
792
793 // field array cannot be empty
794 if (empty($index['field'])) {
795 echo "No fields defined for index $indexName\n";
796 return;
797 }
798
799 // all fieldnames have to be defined and should exist in schema.
800 foreach ($index['field'] as $fieldName) {
801 if (!$fieldName) {
802 echo "Invalid field defination for index $indexName\n";
803 return;
804 }
805 $parenOffset = strpos($fieldName, '(');
806 if ($parenOffset > 0) {
807 $fieldName = substr($fieldName, 0, $parenOffset);
808 }
809 if (!array_key_exists($fieldName, $fields)) {
810 echo "Table does not contain $fieldName\n";
811 print_r($fields);
812 CRM_GenCode_Util_File::removeDir($this->compileDir);
813 exit();
814 }
815 }
816 $indices[$indexName] = &$index;
817 }
818
819 function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) {
820 $name = trim((string ) $foreignXML->name);
821
822 /** need to make sure there is a field of type name */
823 if (!array_key_exists($name, $fields)) {
824 echo "foreign $name in $currentTableName does not have a field definition, ignoring\n";
825 return;
826 }
827
828 /** need to check for existence of table and key **/
829 $table = trim($this->value('table', $foreignXML));
830 $foreignKey = array(
831 'name' => $name,
832 'table' => $table,
833 'uniqName' => "FK_{$currentTableName}_{$name}",
834 'key' => trim($this->value('key', $foreignXML)),
835 'import' => $this->value('import', $foreignXML, FALSE),
836 'export' => $this->value('import', $foreignXML, FALSE),
837 // we do this matching in a seperate phase (resolveForeignKeys)
838 'className' => NULL,
839 'onDelete' => $this->value('onDelete', $foreignXML, FALSE),
840 );
841 $foreignKeys[$name] = &$foreignKey;
842 }
843
844 function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) {
845 $foreignKey = array(
846 'idColumn' => trim($foreignXML->idColumn),
847 'typeColumn' => trim($foreignXML->typeColumn),
848 'key' => trim($this->value('key', $foreignXML)),
849 );
850 $dynamicForeignKeys[] = $foreignKey;
851 }
852
853 protected function value($key, &$object, $default = NULL) {
854 if (isset($object->$key)) {
855 return (string ) $object->$key;
856 }
857 return $default;
858 }
859
860 protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) {
861 if (!isset($object->$name)) {
862 return;
863 }
864
865 $value = $pre . trim($object->$name) . $post;
866 $this->append($attributes, ' ', trim($value));
867 }
868
869 protected function append(&$str, $delim, $name) {
870 if (empty($name)) {
871 return;
872 }
873
874 if (is_array($name)) {
875 foreach ($name as $n) {
876 if (empty($n)) {
877 continue;
878 }
879 if (empty($str)) {
880 $str = $n;
881 }
882 else {
883 $str .= $delim . $n;
884 }
885 }
886 }
887 else {
888 if (empty($str)) {
889 $str = $name;
890 }
891 else {
892 $str .= $delim . $name;
893 }
894 }
895 }
896
897 /**
898 * Sets the size property of a textfield
899 * See constants defined in CRM_Utils_Type for possible values
900 */
901 protected function getSize($fieldXML) {
902 // Extract from <size> tag if supplied
903 if ($this->value('size', $fieldXML)) {
904 $const = 'CRM_Utils_Type::' . strtoupper($fieldXML->size);
905 if (defined($const)) {
906 return $const;
907 }
908 }
909 // Infer from <length> tag if <size> was not explicitly set or was invalid
910
911 // This map is slightly different from CRM_Core_Form_Renderer::$_sizeMapper
912 // Because we usually want fields to render as smaller than their maxlength
913 $sizes = array(
914 2 => 'TWO',
915 4 => 'FOUR',
916 6 => 'SIX',
917 8 => 'EIGHT',
918 16 => 'TWELVE',
919 32 => 'MEDIUM',
920 64 => 'BIG',
921 );
922 foreach ($sizes as $length => $name) {
923 if ($fieldXML->length <= $length) {
924 return "CRM_Utils_Type::$name";
925 }
926 }
927 return 'CRM_Utils_Type::HUGE';
928 }
929 }