d8ed559af661f59ae9220cbae6d3dc34d1d39617
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
6 define('CIVICRM_UF', 'Drupal');
8 require_once 'CRM/Core/ClassLoader.php';
9 CRM_Core_ClassLoader
::singleton()->register();
11 $genCode = new CRM_GenCode_Main('../CRM/Core/DAO/', '../sql/', '../', '../templates/');
15 empty($argv[1]) ?
'schema/Schema.xml' : $argv[1]
18 class CRM_GenCode_Util_File
{
19 static function createDir($dir, $perm = 0755) {
21 mkdir($dir, $perm, TRUE);
25 static function removeDir($dir) {
26 foreach (glob("$dir/*") as $tempFile) {
32 static function createTempDir($prefix) {
33 if (isset($_SERVER['TMPDIR'])) {
34 $tempDir = $_SERVER['TMPDIR'];
40 $newTempDir = $tempDir . '/' . $prefix . rand(1, 10000);
42 if (file_exists($newTempDir)) {
43 self
::removeDir($newTempDir);
45 self
::createDir($newTempDir);
51 class CRM_GenCode_Main
{
63 function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath) {
64 $this->CoreDAOCodePath
= $CoreDAOCodePath;
65 $this->sqlCodePath
= $sqlCodePath;
66 $this->phpCodePath
= $phpCodePath;
67 $this->tplCodePath
= $tplCodePath;
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();
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');
81 require_once 'PHP/Beautifier.php';
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");
93 CRM_GenCode_Util_File
::createDir($this->sqlCodePath
);
96 function __destruct() {
97 CRM_GenCode_Util_File
::removeDir($this->compileDir
);
101 * Automatically generate a variety of files
103 * @param $argVersion string, optional
104 * @param $argCms string, optional; "drupal" or "joomla"
105 * @param $file, the path to the XML schema file
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;
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";
122 if (substr(phpversion(), 0, 1) != 5) {
123 echo phpversion() . ', ' . substr(phpversion(), 0, 1) . "\n";
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
132 $this->generateTemplateVersion($argVersion);
134 $this->setupCms($argCms, $db_version);
136 echo "Parsing input file $file\n";
137 $dbXML = $this->parseInput($file);
138 // print_r( $dbXML );
140 echo "Extracting database information\n";
141 $database = &$this->getDatabase($dbXML);
142 // print_r( $database );
144 $this->classNames
= array();
146 echo "Extracting table information\n";
147 $tables = &$this->getTables($dbXML, $database);
149 $this->resolveForeignKeys($tables, $this->classNames
);
150 $tables = $this->orderTables($tables);
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'] );
167 $archiveTables[$name] = $table;
172 $this->generateListAll($tables);
173 $this->generateCiviTestTruncate($tables);
174 $this->generateCreateSql($database, $tables, 'civicrm.mysql');
175 $this->generateDropSql($tables, 'civicrm_drop.mysql');
177 // also create the archive tables
178 // $this->generateCreateSql($database, $archiveTables, 'civicrm_archive.mysql' );
179 // $this->generateDropSql($archiveTables, 'civicrm_archive_drop.mysql');
181 $this->generateNavigation();
182 $this->generateLocalDataSql($db_version, $this->findLocales());
183 $this->generateSample();
184 $this->generateInstallLangs();
185 $this->generateDAOs($tables);
186 $this->generateSchemaStructure($tables);
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'));
195 function generateCiviTestTruncate($tables) {
196 echo "Generating tests truncate file\n";
198 $truncate = '<?xml version="1.0" encoding="UTF-8" ?>
199 <!-- Truncate all tables that will be used in the tests -->
201 $tbls = array_keys($tables);
202 foreach ($tbls as $d => $t) {
203 $truncate = $truncate . "\n <$t />\n";
206 $truncate = $truncate . "</dataset>\n";
207 file_put_contents($this->sqlCodePath
. "../tests/phpunit/CiviTest/truncate.xml", $truncate);
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'));
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'));
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'));
235 function generateLocalDataSql($db_version, $locales) {
236 $this->smarty
->clear_all_assign();
239 $oldTsLocale = $tsLocale;
240 foreach ($locales as $locale) {
241 echo "Generating data files for $locale\n";
243 $this->smarty
->assign('locale', $locale);
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');
252 $data[] = " UPDATE civicrm_domain SET version = '$db_version';";
254 $data = implode("\n", $data);
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);
260 // write the acl sql script
261 file_put_contents($this->sqlCodePath
. "civicrm_acl$ext.mysql", $this->smarty
->fetch('civicrm_acl.tpl'));
263 $tsLocale = $oldTsLocale;
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);
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
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);
279 for ($i = 0; $i < count($matches[0]); $i++
) {
280 $langs[$matches[1][$i]] = $matches[2][$i];
282 file_put_contents('../install/langs.php', "<?php \$langs = unserialize('" . serialize($langs) . "');");
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();
292 $this->smarty
->assign_by_ref('table', $tables[$name]);
293 $php = $this->smarty
->fetch('dao.tpl');
295 $this->beautifier
->setInputString($php);
297 if (empty($tables[$name]['base'])) {
298 echo "No base defined for $name, skipping output generation\n";
302 $directory = $this->phpCodePath
. $tables[$name]['base'];
303 CRM_GenCode_Util_File
::createDir($directory);
304 $this->beautifier
->setOutputFile($directory . $tables[$name]['fileName']);
306 $this->beautifier
->process();
308 $this->beautifier
->save();
312 function generateSchemaStructure($tables) {
313 echo "Generating CRM_Core_I18n_SchemaStructure...\n";
316 foreach ($tables as $table) {
317 if ($table['localizable']) {
318 $columns[$table['name']] = array();
323 foreach ($table['fields'] as $field) {
324 if ($field['localizable']) {
325 $columns[$table['name']][$field['name']] = $field['sqlType'];
328 if (isset($table['index'])) {
329 foreach ($table['index'] as $index) {
330 if ($index['localizable']) {
331 $indices[$table['name']][$index['name']] = $index;
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);
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();
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;
355 $svnversion = `svnversion
.`
;
357 file_put_contents($this->tplCodePath
. "/CRM/common/version.tpl", $svnversion);
360 function findLocales() {
361 require_once 'CRM/Core/Config.php';
362 $config = CRM_Core_Config
::singleton(FALSE);
364 if (substr($config->gettextResourceDir
, 0, 1) === '/') {
365 $localeDir = $config->gettextResourceDir
;
368 $localeDir = '../' . $config->gettextResourceDir
;
370 if (file_exists($localeDir)) {
371 $config->gettextResourceDir
= $localeDir;
372 $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir));
375 $localesMask = getenv('CIVICRM_LOCALES');
376 if (!empty($localesMask)) {
377 $mask = explode(',', $localesMask);
378 $locales = array_intersect($locales, $mask);
381 if (!in_array('en_US', $locales)) {
382 array_unshift($locales, 'en_US');
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.";
396 elseif ($cms !== 'joomla') {
397 echo "Generating civicrm.config.php\n";
398 copy("../{$cms}/civicrm.config.php.{$cms}", '../civicrm.config.php');
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'));
407 // -----------------------------
408 // ---- Schema manipulation ----
409 // -----------------------------
410 function &parseInput($file) {
411 $dom = new DomDocument();
414 $dbXML = simplexml_import_dom($dom);
418 function &getDatabase(&$dbXML) {
419 $database = array('name' => trim((string ) $dbXML->name
));
422 $this->checkAndAppend($attributes, $dbXML, 'character_set', 'DEFAULT CHARACTER SET ', '');
423 $this->checkAndAppend($attributes, $dbXML, 'collate', 'COLLATE ', '');
424 $database['attributes'] = $attributes;
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);
432 $database['comment'] = $this->value('comment', $dbXML, '');
437 function &getTables(&$dbXML, &$database) {
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
) {
445 if ($this->value('add', $tableXML, 0) <= $this->buildVersion
) {
446 $this->getTable($tableXML, $database, $tables);
454 function resolveForeignKeys(&$tables, &$classNames) {
455 foreach (array_keys($tables) as $name) {
456 $this->resolveForeignKey($tables, $classNames, $name);
460 function resolveForeignKey(&$tables, &$classNames, $name) {
461 if (!array_key_exists('foreignKey', $tables[$name])) {
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";
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];
477 function orderTables(&$tables) {
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]);
491 function validTable(&$tables, &$valid, $name) {
492 if (!array_key_exists('foreignKey', $tables[$name])) {
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) {
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;
512 $localizable = FALSE;
513 foreach ($tableXML->field
as $fieldXML) {
514 if ($fieldXML->localizable
) {
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'),
536 foreach ($tableXML->field
as $fieldXML) {
537 if ($this->value('drop', $fieldXML, 0) > 0 and $this->value('drop', $fieldXML, 0) <= $this->buildVersion
) {
541 if ($this->value('add', $fieldXML, 0) <= $this->buildVersion
) {
542 $this->getField($fieldXML, $fields);
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;
555 if ($this->value('primaryKey', $tableXML)) {
556 $this->getPrimaryKey($tableXML->primaryKey
, $fields, $table);
559 // some kind of refresh?
560 CRM_Core_Config
::singleton(FALSE);
561 if ($this->value('index', $tableXML)) {
563 foreach ($tableXML->index
as $indexXML) {
564 if ($this->value('drop', $indexXML, 0) > 0 and $this->value('drop', $indexXML, 0) <= $this->buildVersion
) {
568 $this->getIndex($indexXML, $fields, $index);
570 $table['index'] = &$index;
573 if ($this->value('foreignKey', $tableXML)) {
575 foreach ($tableXML->foreignKey
as $foreignXML) {
576 // print_r($foreignXML);
578 if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion
) {
581 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion
) {
582 $this->getForeignKey($foreignXML, $fields, $foreign, $name);
585 $table['foreignKey'] = &$foreign;
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
) {
594 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion
) {
595 $this->getDynamicForeignKey($foreignXML, $dynamicForeign, $name);
598 $table['dynamicForeignKey'] = $dynamicForeign;
601 $tables[$name] = &$table;
605 function getField(&$fieldXML, &$fields) {
606 $name = trim((string ) $fieldXML->name
);
607 $field = array('name' => $name, 'localizable' => $fieldXML->localizable
);
608 $type = (string ) $fieldXML->type
;
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);
620 $value = (string ) $fieldXML->values
;
621 $field['sqlType'] = 'enum(';
622 $field['values'] = array();
623 $field['enumValues'] = $value;
624 $values = explode(',', $value);
626 foreach ($values as $v) {
628 $field['values'][] = $v;
631 $field['sqlType'] .= ', ';
634 $field['sqlType'] .= "'$v'";
636 $field['sqlType'] .= ')';
637 $field['phpType'] = $field['sqlType'];
638 $field['crmType'] = 'CRM_Utils_Type::T_ENUM';
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);
649 $field['sqlType'] = $field['phpType'] = $type;
650 $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME';
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);
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';
669 $field['sqlType'] = 'double';
670 $field['phpType'] = 'float';
671 $field['crmType'] = 'CRM_Utils_Type::T_FLOAT';
675 $field['sqlType'] = $field['phpType'] = $type;
676 if ($type == 'int unsigned') {
677 $field['crmType'] = 'CRM_Utils_Type::T_INT';
680 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
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);
693 $field['export'] = $this->value('import', $fieldXML);
695 $field['rule'] = $this->value('rule', $fieldXML);
696 $field['title'] = $this->value('title', $fieldXML);
697 if (!$field['title']) {
698 $field['title'] = $this->composeTitle($name);
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
);
714 $fields[$name] = &$field;
717 function composeTitle($name) {
718 $names = explode('_', strtolower($name));
720 for ($i = 0; $i < count($names); $i++
) {
721 if ($names[$i] === 'id' ||
$names[$i] === 'is') {
722 // id's do not get titles
726 if ($names[$i] === 'im') {
730 $names[$i] = ucfirst(trim($names[$i]));
733 $title = $title . ' ' . $names[$i];
738 function getPrimaryKey(&$primaryXML, &$fields, &$table) {
739 $name = trim((string ) $primaryXML->name
);
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";
747 // set the autoincrement property of the field
748 $auto = $this->value('autoincrement', $primaryXML);
749 $fields[$name]['autoincrement'] = $auto;
752 'autoincrement' => $auto,
754 $table['primaryKey'] = &$primaryKey;
757 function getIndex(&$indexXML, &$fields, &$indices) {
758 //echo "\n\n*******************************************************\n";
759 //echo "entering getIndex\n";
762 // empty index name is fine
763 $indexName = trim((string)$indexXML->name
);
764 $index['name'] = $indexName;
765 $index['field'] = array();
768 foreach ($indexXML->fieldName
as $v) {
769 $fieldName = (string)($v);
770 $length = (string)($v['length']);
771 if (strlen($length) > 0) {
772 $fieldName = "$fieldName($length)";
774 $index['field'][] = $fieldName;
777 $index['localizable'] = FALSE;
778 foreach ($index['field'] as $fieldName) {
779 if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) {
780 $index['localizable'] = TRUE;
785 // check for unique index
786 if ($this->value('unique', $indexXML)) {
787 $index['unique'] = TRUE;
790 //echo "\$index = \n";
793 // field array cannot be empty
794 if (empty($index['field'])) {
795 echo "No fields defined for index $indexName\n";
799 // all fieldnames have to be defined and should exist in schema.
800 foreach ($index['field'] as $fieldName) {
802 echo "Invalid field defination for index $indexName\n";
805 $parenOffset = strpos($fieldName, '(');
806 if ($parenOffset > 0) {
807 $fieldName = substr($fieldName, 0, $parenOffset);
809 if (!array_key_exists($fieldName, $fields)) {
810 echo "Table does not contain $fieldName\n";
812 CRM_GenCode_Util_File
::removeDir($this->compileDir
);
816 $indices[$indexName] = &$index;
819 function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) {
820 $name = trim((string ) $foreignXML->name
);
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";
828 /** need to check for existence of table and key **/
829 $table = trim($this->value('table', $foreignXML));
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)
839 'onDelete' => $this->value('onDelete', $foreignXML, FALSE),
841 $foreignKeys[$name] = &$foreignKey;
844 function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) {
846 'idColumn' => trim($foreignXML->idColumn
),
847 'typeColumn' => trim($foreignXML->typeColumn
),
848 'key' => trim($this->value('key', $foreignXML)),
850 $dynamicForeignKeys[] = $foreignKey;
853 protected function value($key, &$object, $default = NULL) {
854 if (isset($object->$key)) {
855 return (string ) $object->$key;
860 protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) {
861 if (!isset($object->$name)) {
865 $value = $pre . trim($object->$name) . $post;
866 $this->append($attributes, ' ', trim($value));
869 protected function append(&$str, $delim, $name) {
874 if (is_array($name)) {
875 foreach ($name as $n) {
892 $str .= $delim . $name;
898 * Sets the size property of a textfield
899 * See constants defined in CRM_Utils_Type for possible values
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)) {
909 // Infer from <length> tag if <size> was not explicitly set or was invalid
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
922 foreach ($sizes as $length => $name) {
923 if ($fieldXML->length
<= $length) {
924 return "CRM_Utils_Type::$name";
927 return 'CRM_Utils_Type::HUGE';