Merge pull request #18143 from mattwire/membertabbuttons
[civicrm-core.git] / CRM / Core / CodeGen / Schema.php
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 * CRM_Core_CodeGen_Schema constructor.
10 *
11 * @param \CRM_Core_CodeGen_Main $config
12 */
13 public function __construct($config) {
14 parent::__construct($config);
15 $this->locales = $this->findLocales();
16 }
17
18 public function run() {
19 CRM_Core_CodeGen_Util_File::createDir($this->config->sqlCodePath);
20
21 $put = function ($files) {
22 foreach ($files as $file => $content) {
23 if (substr($content, -1) !== "\n") {
24 $content .= "\n";
25 }
26 file_put_contents($this->config->sqlCodePath . $file, $content);
27 }
28 };
29
30 echo "Generating sql file\n";
31 $put($this->generateCreateSql());
32
33 echo "Generating sql drop tables file\n";
34 $put($this->generateDropSql());
35
36 foreach ($this->locales as $locale) {
37 echo "Generating data files for $locale\n";
38 $put($this->generateLocaleDataSql($locale));
39 }
40
41 // also create the archive tables
42 // $this->generateCreateSql('civicrm_archive.mysql' );
43 // $this->generateDropSql('civicrm_archive_drop.mysql');
44
45 echo "Generating navigation file\n";
46 $put($this->generateNavigation());
47
48 echo "Generating sample file\n";
49 $put($this->generateSample());
50 }
51
52 public function generateCreateSql() {
53 $template = new CRM_Core_CodeGen_Util_Template('sql');
54
55 $template->assign('database', $this->config->database);
56 $template->assign('tables', $this->tables);
57 $dropOrder = array_reverse(array_keys($this->tables));
58 $template->assign('dropOrder', $dropOrder);
59 $template->assign('mysql', 'modern');
60
61 return ['civicrm.mysql' => $template->fetch('schema.tpl')];
62 }
63
64 public function generateDropSql() {
65 $dropOrder = array_reverse(array_keys($this->tables));
66 $template = new CRM_Core_CodeGen_Util_Template('sql');
67 $template->assign('dropOrder', $dropOrder);
68 return ['civicrm_drop.mysql' => $template->fetch('drop.tpl')];
69 }
70
71 public function generateNavigation() {
72 $template = new CRM_Core_CodeGen_Util_Template('sql');
73 return ['civicrm_navigation.mysql' => $template->fetch('civicrm_navigation.tpl')];
74 }
75
76 /**
77 * @param string $locale
78 * Ex: en_US, fr_FR
79 * @return array
80 */
81 public function generateLocaleDataSql($locale) {
82 $template = new CRM_Core_CodeGen_Util_Template('sql');
83
84 global $tsLocale;
85 $oldTsLocale = $tsLocale;
86
87 try {
88
89 $tsLocale = $locale;
90 $template->assign('locale', $locale);
91 $template->assign('db_version', $this->config->db_version);
92
93 $sections = [
94 'civicrm_country.tpl',
95 'civicrm_state_province.tpl',
96 'civicrm_currency.tpl',
97 'civicrm_data.tpl',
98 'civicrm_navigation.tpl',
99 'civicrm_version_sql.tpl',
100 ];
101
102 $ext = ($locale != 'en_US' ? ".$locale" : '');
103
104 return [
105 "civicrm_data$ext.mysql" => $template->fetchConcat($sections),
106 "civicrm_acl$ext.mysql" => $template->fetch('civicrm_acl.tpl'),
107 ];
108 }
109 finally {
110 $tsLocale = $oldTsLocale;
111 }
112 }
113
114 /**
115 * @return array
116 * Array(string $fileName => string $fileContent).
117 * List of files
118 */
119 public function generateSample() {
120 $template = new CRM_Core_CodeGen_Util_Template('sql');
121 $sections = [
122 'civicrm_sample.tpl',
123 'civicrm_acl.tpl',
124 ];
125 return [
126 'civicrm_sample.mysql' => $template->fetchConcat($sections),
127 'case_sample.mysql' => $template->fetch('case_sample.tpl'),
128 ];
129 }
130
131 /**
132 * @return array
133 */
134 public function findLocales() {
135 require_once 'CRM/Core/Config.php';
136 $config = CRM_Core_Config::singleton(FALSE);
137 $locales = [];
138 $localeDir = CRM_Core_I18n::getResourceDir();
139 if (file_exists($localeDir)) {
140 $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir));
141 }
142
143 $localesMask = getenv('CIVICRM_LOCALES');
144 if (!empty($localesMask)) {
145 $mask = explode(',', $localesMask);
146 $locales = array_intersect($locales, $mask);
147 }
148
149 if (!in_array('en_US', $locales)) {
150 array_unshift($locales, 'en_US');
151 }
152
153 return $locales;
154 }
155
156 }