ea7a62081bb1b6a7562e4095bc4b33d0668378b4
2 ini_set('include_path', '.' . PATH_SEPARATOR
. '..' . DIRECTORY_SEPARATOR
. 'packages' . PATH_SEPARATOR
. '..');
3 // make sure the memory_limit is at least 512 MB
4 $memLimitString = trim(ini_get('memory_limit'));
5 $memLimitUnit = strtolower(substr($memLimitString, -1));
6 $memLimit = (int) $memLimitString;
7 switch ($memLimitUnit) {
8 case 'g': $memLimit *= 1024;
9 case 'm': $memLimit *= 1024;
10 case 'k': $memLimit *= 1024;
13 if ($memLimit >= 0 and $memLimit < 536870912) {
14 ini_set('memory_limit', '512M');
16 date_default_timezone_set('UTC'); // avoid php warnings if timezone is not set - CRM-10844
18 define('CIVICRM_UF', 'Drupal');
20 require_once 'CRM/Core/ClassLoader.php';
21 CRM_Core_ClassLoader
::singleton()->register();
23 $genCode = new CRM_GenCode_Main('../CRM/Core/DAO/', '../sql/', '../', '../templates/');
27 empty($argv[1]) ?
'schema/Schema.xml' : $argv[1]
30 class CRM_GenCode_Util_File
{
31 static function createDir($dir, $perm = 0755) {
33 mkdir($dir, $perm, TRUE);
37 static function removeDir($dir) {
38 foreach (glob("$dir/*") as $tempFile) {
44 static function createTempDir($prefix) {
45 if (isset($_SERVER['TMPDIR'])) {
46 $tempDir = $_SERVER['TMPDIR'];
52 $newTempDir = $tempDir . '/' . $prefix . rand(1, 10000);
54 if (file_exists($newTempDir)) {
55 self
::removeDir($newTempDir);
57 self
::createDir($newTempDir);
63 class CRM_GenCode_Main
{
75 function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath) {
76 $this->CoreDAOCodePath
= $CoreDAOCodePath;
77 $this->sqlCodePath
= $sqlCodePath;
78 $this->phpCodePath
= $phpCodePath;
79 $this->tplCodePath
= $tplCodePath;
81 require_once 'Smarty/Smarty.class.php';
82 $this->smarty
= new Smarty();
83 $this->smarty
->template_dir
= './templates';
84 $this->smarty
->plugins_dir
= array('../packages/Smarty/plugins', '../CRM/Core/Smarty/plugins');
85 $this->compileDir
= CRM_GenCode_Util_File
::createTempDir('templates_c_');
86 $this->smarty
->compile_dir
= $this->compileDir
;
87 $this->smarty
->clear_all_cache();
89 // CRM-5308 / CRM-3507 - we need {localize} to work in the templates
90 require_once 'CRM/Core/Smarty/plugins/block.localize.php';
91 $this->smarty
->register_block('localize', 'smarty_block_localize');
93 require_once 'PHP/Beautifier.php';
95 $this->beautifier
= new PHP_Beautifier();
96 $this->beautifier
->addFilter('ArrayNested');
97 // add one or more filters
98 $this->beautifier
->addFilter('Pear');
99 // add one or more filters
100 $this->beautifier
->addFilter('NewLines', array('after' => 'class, public, require, comment'));
101 $this->beautifier
->setIndentChar(' ');
102 $this->beautifier
->setIndentNumber(2);
103 $this->beautifier
->setNewLine("\n");
105 CRM_GenCode_Util_File
::createDir($this->sqlCodePath
);
108 function __destruct() {
109 CRM_GenCode_Util_File
::removeDir($this->compileDir
);
113 * Automatically generate a variety of files
115 * @param $argVersion string, optional
116 * @param $argCms string, optional; "drupal" or "joomla"
117 * @param $file, the path to the XML schema file
119 function main($argVersion, $argCms, $file) {
120 $versionFile = "version.xml";
121 $versionXML = &$this->parseInput($versionFile);
122 $db_version = $versionXML->version_no
;
123 $this->buildVersion
= preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $db_version);
124 if (isset($argVersion)) {
125 // change the version to that explicitly passed, if any
126 $db_version = $argVersion;
128 echo "\ncivicrm_domain.version := $db_version\n\n";
129 if ($this->buildVersion
< 1.1) {
130 echo "The Database is not compatible for this version";
134 if (substr(phpversion(), 0, 1) != 5) {
135 echo phpversion() . ', ' . substr(phpversion(), 0, 1) . "\n";
137 CiviCRM requires a PHP Version >= 5
138 Please upgrade your php / webserver configuration
139 Alternatively you can get a version of CiviCRM that matches your PHP version
144 $this->generateTemplateVersion($argVersion);
146 $this->setupCms($argCms, $db_version);
148 echo "Parsing input file $file\n";
149 $dbXML = $this->parseInput($file);
150 // print_r( $dbXML );
152 echo "Extracting database information\n";
153 $database = &$this->getDatabase($dbXML);
154 // print_r( $database );
156 $this->classNames
= array();
158 echo "Extracting table information\n";
159 $tables = &$this->getTables($dbXML, $database);
161 $this->resolveForeignKeys($tables, $this->classNames
);
162 $tables = $this->orderTables($tables);
164 // add archive tables here
165 $archiveTables = array( );
166 foreach ($tables as $name => $table ) {
167 if ( $table['archive'] == 'true' ) {
168 $name = 'archive_' . $table['name'];
169 $table['name'] = $name;
170 $table['archive'] = 'false';
171 if ( isset($table['foreignKey']) ) {
172 foreach ($table['foreignKey'] as $fkName => $fkValue) {
173 if ($tables[$fkValue['table']]['archive'] == 'true') {
174 $table['foreignKey'][$fkName]['table'] = 'archive_' . $table['foreignKey'][$fkName]['table'];
175 $table['foreignKey'][$fkName]['uniqName'] =
176 str_replace( 'FK_', 'FK_archive_', $table['foreignKey'][$fkName]['uniqName'] );
179 $archiveTables[$name] = $table;
184 $this->generateListAll($tables);
185 $this->generateCiviTestTruncate($tables);
186 $this->generateCreateSql($database, $tables, 'civicrm.mysql');
187 $this->generateDropSql($tables, 'civicrm_drop.mysql');
189 // also create the archive tables
190 // $this->generateCreateSql($database, $archiveTables, 'civicrm_archive.mysql' );
191 // $this->generateDropSql($archiveTables, 'civicrm_archive_drop.mysql');
193 $this->generateNavigation();
194 $this->generateLocalDataSql($db_version, $this->findLocales());
195 $this->generateSample();
196 $this->generateInstallLangs();
197 $this->generateDAOs($tables);
198 $this->generateSchemaStructure($tables);
201 function generateListAll($tables) {
202 $this->smarty
->clear_all_assign();
203 $this->smarty
->assign('tables', $tables);
204 file_put_contents($this->CoreDAOCodePath
. "AllCoreTables.php", $this->smarty
->fetch('listAll.tpl'));
207 function generateCiviTestTruncate($tables) {
208 echo "Generating tests truncate file\n";
210 $truncate = '<?xml version="1.0" encoding="UTF-8" ?>
211 <!-- Truncate all tables that will be used in the tests -->
213 $tbls = array_keys($tables);
214 foreach ($tbls as $d => $t) {
215 $truncate = $truncate . "\n <$t />\n";
218 $truncate = $truncate . "</dataset>\n";
219 file_put_contents($this->sqlCodePath
. "../tests/phpunit/CiviTest/truncate.xml", $truncate);
223 function generateCreateSql($database, $tables, $fileName = 'civicrm.mysql') {
224 echo "Generating sql file\n";
225 $this->reset_smarty_assignments();
226 $this->smarty
->assign_by_ref('database', $database);
227 $this->smarty
->assign_by_ref('tables', $tables);
228 $dropOrder = array_reverse(array_keys($tables));
229 $this->smarty
->assign_by_ref('dropOrder', $dropOrder);
230 $this->smarty
->assign('mysql', 'modern');
231 file_put_contents($this->sqlCodePath
. $fileName, $this->smarty
->fetch('schema.tpl'));
234 function generateDropSql($tables, $fileName = 'civicrm_drop.mysql') {
235 echo "Generating sql drop tables file\n";
236 $dropOrder = array_reverse(array_keys($tables));
237 $this->smarty
->assign_by_ref('dropOrder', $dropOrder);
238 file_put_contents($this->sqlCodePath
. $fileName, $this->smarty
->fetch('drop.tpl'));
241 function generateNavigation() {
242 echo "Generating navigation file\n";
243 $this->reset_smarty_assignments();
244 file_put_contents($this->sqlCodePath
. "civicrm_navigation.mysql", $this->smarty
->fetch('civicrm_navigation.tpl'));
247 function generateLocalDataSql($db_version, $locales) {
248 $this->reset_smarty_assignments();
251 $oldTsLocale = $tsLocale;
252 foreach ($locales as $locale) {
253 echo "Generating data files for $locale\n";
255 $this->smarty
->assign('locale', $locale);
258 $data[] = $this->smarty
->fetch('civicrm_country.tpl');
259 $data[] = $this->smarty
->fetch('civicrm_state_province.tpl');
260 $data[] = $this->smarty
->fetch('civicrm_currency.tpl');
261 $data[] = $this->smarty
->fetch('civicrm_data.tpl');
262 $data[] = $this->smarty
->fetch('civicrm_navigation.tpl');
264 $data[] = " UPDATE civicrm_domain SET version = '$db_version';";
266 $data = implode("\n", $data);
268 $ext = ($locale != 'en_US' ?
".$locale" : '');
269 // write the initialize base-data sql script
270 file_put_contents($this->sqlCodePath
. "civicrm_data$ext.mysql", $data);
272 // write the acl sql script
273 file_put_contents($this->sqlCodePath
. "civicrm_acl$ext.mysql", $this->smarty
->fetch('civicrm_acl.tpl'));
275 $tsLocale = $oldTsLocale;
278 function generateSample() {
279 $this->reset_smarty_assignments();
280 $sample = $this->smarty
->fetch('civicrm_sample.tpl');
281 $sample .= $this->smarty
->fetch('civicrm_acl.tpl');
282 file_put_contents($this->sqlCodePath
. 'civicrm_sample.mysql', $sample);
285 function generateInstallLangs() {
286 // CRM-7161: generate install/langs.php from the languages template
287 // grep it for enabled languages and create a 'xx_YY' => 'Language name' $langs mapping
289 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);
291 for ($i = 0; $i < count($matches[0]); $i++
) {
292 $langs[$matches[1][$i]] = $matches[2][$i];
294 file_put_contents('../install/langs.php', "<?php \$langs = unserialize('" . serialize($langs) . "');");
297 function generateDAOs($tables) {
298 foreach (array_keys($tables) as $name) {
299 $this->smarty
->clear_all_cache();
300 echo "Generating $name as " . $tables[$name]['fileName'] . "\n";
301 $this->reset_smarty_assignments();
303 $this->smarty
->assign_by_ref('table', $tables[$name]);
304 $php = $this->smarty
->fetch('dao.tpl');
306 $this->beautifier
->setInputString($php);
308 if (empty($tables[$name]['base'])) {
309 echo "No base defined for $name, skipping output generation\n";
313 $directory = $this->phpCodePath
. $tables[$name]['base'];
314 CRM_GenCode_Util_File
::createDir($directory);
315 $this->beautifier
->setOutputFile($directory . $tables[$name]['fileName']);
317 $this->beautifier
->process();
319 $this->beautifier
->save();
323 function generateSchemaStructure($tables) {
324 echo "Generating CRM_Core_I18n_SchemaStructure...\n";
327 foreach ($tables as $table) {
328 if ($table['localizable']) {
329 $columns[$table['name']] = array();
334 foreach ($table['fields'] as $field) {
335 if ($field['localizable']) {
336 $columns[$table['name']][$field['name']] = $field['sqlType'];
339 if (isset($table['index'])) {
340 foreach ($table['index'] as $index) {
341 if ($index['localizable']) {
342 $indices[$table['name']][$index['name']] = $index;
348 $this->reset_smarty_assignments();
349 $this->smarty
->assign_by_ref('columns', $columns);
350 $this->smarty
->assign_by_ref('indices', $indices);
352 $this->beautifier
->setInputString($this->smarty
->fetch('schema_structure.tpl'));
353 $this->beautifier
->setOutputFile($this->phpCodePath
. "/CRM/Core/I18n/SchemaStructure.php");
354 $this->beautifier
->process();
355 $this->beautifier
->save();
358 function generateTemplateVersion($argVersion) {
359 // add the Subversion revision to templates
360 // use svnversion if the version was not specified explicitely on the commandline
361 if (isset($argVersion) and $argVersion != '') {
362 $svnversion = $argVersion;
365 $svnversion = `svnversion
.`
;
367 file_put_contents($this->tplCodePath
. "/CRM/common/version.tpl", $svnversion);
370 function findLocales() {
371 require_once 'CRM/Core/Config.php';
372 $config = CRM_Core_Config
::singleton(FALSE);
374 if (substr($config->gettextResourceDir
, 0, 1) === '/') {
375 $localeDir = $config->gettextResourceDir
;
378 $localeDir = '../' . $config->gettextResourceDir
;
380 if (file_exists($localeDir)) {
381 $config->gettextResourceDir
= $localeDir;
382 $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir));
385 $localesMask = getenv('CIVICRM_LOCALES');
386 if (!empty($localesMask)) {
387 $mask = explode(',', $localesMask);
388 $locales = array_intersect($locales, $mask);
391 if (!in_array('en_US', $locales)) {
392 array_unshift($locales, 'en_US');
398 function setupCms($argCms, $db_version) {
399 // default cms is 'drupal', if not specified
400 $cms = isset($argCms) ?
strtolower($argCms) : 'drupal';
401 if (!in_array($cms, array(
402 'drupal', 'joomla'))) {
403 echo "Config file for '{$cms}' not known.";
406 elseif ($cms !== 'joomla') {
407 echo "Generating civicrm.config.php\n";
408 copy("../{$cms}/civicrm.config.php.{$cms}", '../civicrm.config.php');
411 echo "Generating civicrm-version file\n";
412 $this->smarty
->assign('db_version', $db_version);
413 $this->smarty
->assign('cms', ucwords($cms));
414 file_put_contents($this->phpCodePath
. "civicrm-version.php", $this->smarty
->fetch('civicrm_version.tpl'));
417 // -----------------------------
418 // ---- Schema manipulation ----
419 // -----------------------------
420 function &parseInput($file) {
421 $dom = new DomDocument();
424 $dbXML = simplexml_import_dom($dom);
428 function &getDatabase(&$dbXML) {
429 $database = array('name' => trim((string ) $dbXML->name
));
432 $this->checkAndAppend($attributes, $dbXML, 'character_set', 'DEFAULT CHARACTER SET ', '');
433 $this->checkAndAppend($attributes, $dbXML, 'collate', 'COLLATE ', '');
434 $database['attributes'] = $attributes;
436 $tableAttributes_modern = $tableAttributes_simple = '';
437 $this->checkAndAppend($tableAttributes_modern, $dbXML, 'table_type', 'ENGINE=', '');
438 $this->checkAndAppend($tableAttributes_simple, $dbXML, 'table_type', 'TYPE=', '');
439 $database['tableAttributes_modern'] = trim($tableAttributes_modern . ' ' . $attributes);
440 $database['tableAttributes_simple'] = trim($tableAttributes_simple);
442 $database['comment'] = $this->value('comment', $dbXML, '');
447 function &getTables(&$dbXML, &$database) {
449 foreach ($dbXML->tables
as $tablesXML) {
450 foreach ($tablesXML->table
as $tableXML) {
451 if ($this->value('drop', $tableXML, 0) > 0 and $this->value('drop', $tableXML, 0) <= $this->buildVersion
) {
455 if ($this->value('add', $tableXML, 0) <= $this->buildVersion
) {
456 $this->getTable($tableXML, $database, $tables);
464 function resolveForeignKeys(&$tables, &$classNames) {
465 foreach (array_keys($tables) as $name) {
466 $this->resolveForeignKey($tables, $classNames, $name);
470 function resolveForeignKey(&$tables, &$classNames, $name) {
471 if (!array_key_exists('foreignKey', $tables[$name])) {
475 foreach (array_keys($tables[$name]['foreignKey']) as $fkey) {
476 $ftable = $tables[$name]['foreignKey'][$fkey]['table'];
477 if (!array_key_exists($ftable, $classNames)) {
478 echo "$ftable is not a valid foreign key table in $name\n";
481 $tables[$name]['foreignKey'][$fkey]['className'] = $classNames[$ftable];
482 $tables[$name]['foreignKey'][$fkey]['fileName'] = str_replace('_', '/', $classNames[$ftable]) . '.php';
483 $tables[$name]['fields'][$fkey]['FKClassName'] = $classNames[$ftable];
487 function orderTables(&$tables) {
490 while (!empty($tables)) {
491 foreach (array_keys($tables) as $name) {
492 if ($this->validTable($tables, $ordered, $name)) {
493 $ordered[$name] = $tables[$name];
494 unset($tables[$name]);
501 function validTable(&$tables, &$valid, $name) {
502 if (!array_key_exists('foreignKey', $tables[$name])) {
506 foreach (array_keys($tables[$name]['foreignKey']) as $fkey) {
507 $ftable = $tables[$name]['foreignKey'][$fkey]['table'];
508 if (!array_key_exists($ftable, $valid) && $ftable !== $name) {
515 function getTable($tableXML, &$database, &$tables) {
516 $name = trim((string ) $tableXML->name
);
517 $klass = trim((string ) $tableXML->class);
518 $base = $this->value('base', $tableXML);
519 $sourceFile = "xml/schema/{$base}/{$klass}.xml";
520 $daoPath = "{$base}/DAO/";
521 $pre = str_replace('/', '_', $daoPath);
522 $this->classNames
[$name] = $pre . $klass;
524 $localizable = FALSE;
525 foreach ($tableXML->field
as $fieldXML) {
526 if ($fieldXML->localizable
) {
535 'sourceFile' => $sourceFile,
536 'fileName' => $klass . '.php',
537 'objectName' => $klass,
538 'labelName' => substr($name, 8),
539 'className' => $this->classNames
[$name],
540 'attributes_simple' => trim($database['tableAttributes_simple']),
541 'attributes_modern' => trim($database['tableAttributes_modern']),
542 'comment' => $this->value('comment', $tableXML),
543 'localizable' => $localizable,
544 'log' => $this->value('log', $tableXML, 'false'),
545 'archive' => $this->value('archive', $tableXML, 'false'),
549 foreach ($tableXML->field
as $fieldXML) {
550 if ($this->value('drop', $fieldXML, 0) > 0 and $this->value('drop', $fieldXML, 0) <= $this->buildVersion
) {
554 if ($this->value('add', $fieldXML, 0) <= $this->buildVersion
) {
555 $this->getField($fieldXML, $fields);
559 $table['fields'] = &$fields;
560 $table['hasEnum'] = FALSE;
561 foreach ($table['fields'] as $field) {
562 if ($field['crmType'] == 'CRM_Utils_Type::T_ENUM') {
563 $table['hasEnum'] = TRUE;
568 if ($this->value('primaryKey', $tableXML)) {
569 $this->getPrimaryKey($tableXML->primaryKey
, $fields, $table);
572 // some kind of refresh?
573 CRM_Core_Config
::singleton(FALSE);
574 if ($this->value('index', $tableXML)) {
576 foreach ($tableXML->index
as $indexXML) {
577 if ($this->value('drop', $indexXML, 0) > 0 and $this->value('drop', $indexXML, 0) <= $this->buildVersion
) {
581 $this->getIndex($indexXML, $fields, $index);
583 $table['index'] = &$index;
586 if ($this->value('foreignKey', $tableXML)) {
588 foreach ($tableXML->foreignKey
as $foreignXML) {
589 // print_r($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->getForeignKey($foreignXML, $fields, $foreign, $name);
598 $table['foreignKey'] = &$foreign;
601 if ($this->value('dynamicForeignKey', $tableXML)) {
602 $dynamicForeign = array();
603 foreach ($tableXML->dynamicForeignKey
as $foreignXML) {
604 if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion
) {
607 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion
) {
608 $this->getDynamicForeignKey($foreignXML, $dynamicForeign, $name);
611 $table['dynamicForeignKey'] = $dynamicForeign;
614 $tables[$name] = &$table;
618 function getField(&$fieldXML, &$fields) {
619 $name = trim((string ) $fieldXML->name
);
620 $field = array('name' => $name, 'localizable' => $fieldXML->localizable
);
621 $type = (string ) $fieldXML->type
;
625 $field['length'] = (int) $fieldXML->length
;
626 $field['sqlType'] = "$type({$field['length']})";
627 $field['phpType'] = 'string';
628 $field['crmType'] = 'CRM_Utils_Type::T_STRING';
629 $field['size'] = $this->getSize($fieldXML);
633 $value = (string ) $fieldXML->values
;
634 $field['sqlType'] = 'enum(';
635 $field['values'] = array();
636 $field['enumValues'] = $value;
637 $values = explode(',', $value);
639 foreach ($values as $v) {
641 $field['values'][] = $v;
644 $field['sqlType'] .= ', ';
647 $field['sqlType'] .= "'$v'";
649 $field['sqlType'] .= ')';
650 $field['phpType'] = $field['sqlType'];
651 $field['crmType'] = 'CRM_Utils_Type::T_ENUM';
655 $field['sqlType'] = $field['phpType'] = $type;
656 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
657 $field['rows'] = $this->value('rows', $fieldXML);
658 $field['cols'] = $this->value('cols', $fieldXML);
662 $field['sqlType'] = $field['phpType'] = $type;
663 $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME';
667 // need this case since some versions of mysql do not have boolean as a valid column type and hence it
668 // is changed to tinyint. hopefully after 2 yrs this case can be removed.
669 $field['sqlType'] = 'tinyint';
670 $field['phpType'] = $type;
671 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
675 $length = $fieldXML->length ?
$fieldXML->length
: '20,2';
676 $field['sqlType'] = 'decimal(' . $length . ')';
677 $field['phpType'] = 'float';
678 $field['crmType'] = 'CRM_Utils_Type::T_MONEY';
682 $field['sqlType'] = 'double';
683 $field['phpType'] = 'float';
684 $field['crmType'] = 'CRM_Utils_Type::T_FLOAT';
688 $field['sqlType'] = $field['phpType'] = $type;
689 if ($type == 'int unsigned') {
690 $field['crmType'] = 'CRM_Utils_Type::T_INT';
693 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
698 $field['required'] = $this->value('required', $fieldXML);
699 $field['comment'] = $this->value('comment', $fieldXML);
700 $field['default'] = $this->value('default', $fieldXML);
701 $field['import'] = $this->value('import', $fieldXML);
702 if ($this->value('export', $fieldXML)) {
703 $field['export'] = $this->value('export', $fieldXML);
706 $field['export'] = $this->value('import', $fieldXML);
708 $field['rule'] = $this->value('rule', $fieldXML);
709 $field['title'] = $this->value('title', $fieldXML);
710 if (!$field['title']) {
711 $field['title'] = $this->composeTitle($name);
713 $field['headerPattern'] = $this->value('headerPattern', $fieldXML);
714 $field['dataPattern'] = $this->value('dataPattern', $fieldXML);
715 $field['uniqueName'] = $this->value('uniqueName', $fieldXML);
716 $field['pseudoconstant'] = $this->value('pseudoconstant', $fieldXML);
717 if(!empty($fieldXML->pseudoconstant
)){
718 //ok this is a bit long-winded but it gets there & is consistent with above approach
719 $field['pseudoconstant'] = array();
720 $validOptions = array('name', 'optionGroupName', 'table', 'keyColumn', 'labelColumn','class');
721 foreach ($validOptions as $pseudoOption){
722 if(!empty($fieldXML->pseudoconstant
->$pseudoOption)){
723 $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant
);
727 $fields[$name] = &$field;
730 function composeTitle($name) {
731 $names = explode('_', strtolower($name));
733 for ($i = 0; $i < count($names); $i++
) {
734 if ($names[$i] === 'id' ||
$names[$i] === 'is') {
735 // id's do not get titles
739 if ($names[$i] === 'im') {
743 $names[$i] = ucfirst(trim($names[$i]));
746 $title = $title . ' ' . $names[$i];
751 function getPrimaryKey(&$primaryXML, &$fields, &$table) {
752 $name = trim((string ) $primaryXML->name
);
754 /** need to make sure there is a field of type name */
755 if (!array_key_exists($name, $fields)) {
756 echo "primary key $name in $table->name does not have a field definition, ignoring\n";
760 // set the autoincrement property of the field
761 $auto = $this->value('autoincrement', $primaryXML);
762 $fields[$name]['autoincrement'] = $auto;
765 'autoincrement' => $auto,
767 $table['primaryKey'] = &$primaryKey;
770 function getIndex(&$indexXML, &$fields, &$indices) {
771 //echo "\n\n*******************************************************\n";
772 //echo "entering getIndex\n";
775 // empty index name is fine
776 $indexName = trim((string)$indexXML->name
);
777 $index['name'] = $indexName;
778 $index['field'] = array();
781 foreach ($indexXML->fieldName
as $v) {
782 $fieldName = (string)($v);
783 $length = (string)($v['length']);
784 if (strlen($length) > 0) {
785 $fieldName = "$fieldName($length)";
787 $index['field'][] = $fieldName;
790 $index['localizable'] = FALSE;
791 foreach ($index['field'] as $fieldName) {
792 if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) {
793 $index['localizable'] = TRUE;
798 // check for unique index
799 if ($this->value('unique', $indexXML)) {
800 $index['unique'] = TRUE;
803 //echo "\$index = \n";
806 // field array cannot be empty
807 if (empty($index['field'])) {
808 echo "No fields defined for index $indexName\n";
812 // all fieldnames have to be defined and should exist in schema.
813 foreach ($index['field'] as $fieldName) {
815 echo "Invalid field defination for index $indexName\n";
818 $parenOffset = strpos($fieldName, '(');
819 if ($parenOffset > 0) {
820 $fieldName = substr($fieldName, 0, $parenOffset);
822 if (!array_key_exists($fieldName, $fields)) {
823 echo "Table does not contain $fieldName\n";
825 CRM_GenCode_Util_File
::removeDir($this->compileDir
);
829 $indices[$indexName] = &$index;
832 function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) {
833 $name = trim((string ) $foreignXML->name
);
835 /** need to make sure there is a field of type name */
836 if (!array_key_exists($name, $fields)) {
837 echo "foreign $name in $currentTableName does not have a field definition, ignoring\n";
841 /** need to check for existence of table and key **/
842 $table = trim($this->value('table', $foreignXML));
846 'uniqName' => "FK_{$currentTableName}_{$name}",
847 'key' => trim($this->value('key', $foreignXML)),
848 'import' => $this->value('import', $foreignXML, FALSE),
849 'export' => $this->value('import', $foreignXML, FALSE),
850 // we do this matching in a seperate phase (resolveForeignKeys)
852 'onDelete' => $this->value('onDelete', $foreignXML, FALSE),
854 $foreignKeys[$name] = &$foreignKey;
857 function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) {
859 'idColumn' => trim($foreignXML->idColumn
),
860 'typeColumn' => trim($foreignXML->typeColumn
),
861 'key' => trim($this->value('key', $foreignXML)),
863 $dynamicForeignKeys[] = $foreignKey;
866 protected function value($key, &$object, $default = NULL) {
867 if (isset($object->$key)) {
868 return (string ) $object->$key;
873 protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) {
874 if (!isset($object->$name)) {
878 $value = $pre . trim($object->$name) . $post;
879 $this->append($attributes, ' ', trim($value));
882 protected function append(&$str, $delim, $name) {
887 if (is_array($name)) {
888 foreach ($name as $n) {
905 $str .= $delim . $name;
911 * Sets the size property of a textfield
912 * See constants defined in CRM_Utils_Type for possible values
914 protected function getSize($fieldXML) {
915 // Extract from <size> tag if supplied
916 if ($this->value('size', $fieldXML)) {
917 $const = 'CRM_Utils_Type::' . strtoupper($fieldXML->size
);
918 if (defined($const)) {
922 // Infer from <length> tag if <size> was not explicitly set or was invalid
924 // This map is slightly different from CRM_Core_Form_Renderer::$_sizeMapper
925 // Because we usually want fields to render as smaller than their maxlength
935 foreach ($sizes as $length => $name) {
936 if ($fieldXML->length
<= $length) {
937 return "CRM_Utils_Type::$name";
940 return 'CRM_Utils_Type::HUGE';
944 * Clear the smarty cache and assign default values
946 function reset_smarty_assignments() {
947 $this->smarty
->clear_all_assign();
948 $this->smarty
->clear_all_cache();
949 $this->smarty
->assign('generated', "DO NOT EDIT. Generated by " . basename(__FILE__
));