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