| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Create SQL files to create and populate a new schema. |
| 5 | */ |
| 6 | class CRM_Core_CodeGen_Schema extends CRM_Core_CodeGen_BaseTask { |
| 7 | /** |
| 8 | */ |
| 9 | public function __construct() { |
| 10 | parent::__construct(); |
| 11 | $this->locales = $this->findLocales(); |
| 12 | } |
| 13 | |
| 14 | public function run() { |
| 15 | CRM_Core_CodeGen_Util_File::createDir($this->config->sqlCodePath); |
| 16 | |
| 17 | $this->generateCreateSql(); |
| 18 | $this->generateDropSql(); |
| 19 | |
| 20 | $this->generateLocaleDataSql(); |
| 21 | |
| 22 | // also create the archive tables |
| 23 | // $this->generateCreateSql('civicrm_archive.mysql' ); |
| 24 | // $this->generateDropSql('civicrm_archive_drop.mysql'); |
| 25 | |
| 26 | $this->generateNavigation(); |
| 27 | $this->generateSample(); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param string $fileName |
| 32 | */ |
| 33 | public function generateCreateSql($fileName = 'civicrm.mysql') { |
| 34 | echo "Generating sql file\n"; |
| 35 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 36 | |
| 37 | $template->assign('database', $this->config->database); |
| 38 | $template->assign('tables', $this->tables); |
| 39 | $dropOrder = array_reverse(array_keys($this->tables)); |
| 40 | $template->assign('dropOrder', $dropOrder); |
| 41 | $template->assign('mysql', 'modern'); |
| 42 | |
| 43 | $template->run('schema.tpl', $this->config->sqlCodePath . $fileName); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param string $fileName |
| 48 | */ |
| 49 | public function generateDropSql($fileName = 'civicrm_drop.mysql') { |
| 50 | echo "Generating sql drop tables file\n"; |
| 51 | $dropOrder = array_reverse(array_keys($this->tables)); |
| 52 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 53 | $template->assign('dropOrder', $dropOrder); |
| 54 | $template->run('drop.tpl', $this->config->sqlCodePath . $fileName); |
| 55 | } |
| 56 | |
| 57 | public function generateNavigation() { |
| 58 | echo "Generating navigation file\n"; |
| 59 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 60 | $template->run('civicrm_navigation.tpl', $this->config->sqlCodePath . "civicrm_navigation.mysql"); |
| 61 | } |
| 62 | |
| 63 | public function generateLocaleDataSql() { |
| 64 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 65 | |
| 66 | global $tsLocale; |
| 67 | $oldTsLocale = $tsLocale; |
| 68 | foreach ($this->locales as $locale) { |
| 69 | echo "Generating data files for $locale\n"; |
| 70 | $tsLocale = $locale; |
| 71 | $template->assign('locale', $locale); |
| 72 | $template->assign('db_version', $this->config->db_version); |
| 73 | |
| 74 | $sections = array( |
| 75 | 'civicrm_country.tpl', |
| 76 | 'civicrm_state_province.tpl', |
| 77 | 'civicrm_currency.tpl', |
| 78 | 'civicrm_data.tpl', |
| 79 | 'civicrm_navigation.tpl', |
| 80 | 'civicrm_version_sql.tpl', |
| 81 | ); |
| 82 | |
| 83 | $ext = ($locale != 'en_US' ? ".$locale" : ''); |
| 84 | // write the initialize base-data sql script |
| 85 | $template->runConcat($sections, $this->config->sqlCodePath . "civicrm_data$ext.mysql"); |
| 86 | |
| 87 | // write the acl sql script |
| 88 | $template->run('civicrm_acl.tpl', $this->config->sqlCodePath . "civicrm_acl$ext.mysql"); |
| 89 | } |
| 90 | $tsLocale = $oldTsLocale; |
| 91 | } |
| 92 | |
| 93 | public function generateSample() { |
| 94 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 95 | $sections = array( |
| 96 | 'civicrm_sample.tpl', |
| 97 | 'civicrm_acl.tpl', |
| 98 | ); |
| 99 | $template->runConcat($sections, $this->config->sqlCodePath . 'civicrm_sample.mysql'); |
| 100 | |
| 101 | $template->run('case_sample.tpl', $this->config->sqlCodePath . 'case_sample.mysql'); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @return array |
| 106 | */ |
| 107 | public function findLocales() { |
| 108 | require_once 'CRM/Core/Config.php'; |
| 109 | $config = CRM_Core_Config::singleton(FALSE); |
| 110 | $locales = array(); |
| 111 | $localeDir = CRM_Core_I18n::getResourceDir(); |
| 112 | if (file_exists($localeDir)) { |
| 113 | $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir)); |
| 114 | } |
| 115 | |
| 116 | $localesMask = getenv('CIVICRM_LOCALES'); |
| 117 | if (!empty($localesMask)) { |
| 118 | $mask = explode(',', $localesMask); |
| 119 | $locales = array_intersect($locales, $mask); |
| 120 | } |
| 121 | |
| 122 | if (!in_array('en_US', $locales)) { |
| 123 | array_unshift($locales, 'en_US'); |
| 124 | } |
| 125 | |
| 126 | return $locales; |
| 127 | } |
| 128 | |
| 129 | } |