Merge pull request #22380 from braders/core-483-show-customised-preferences-on-validation
[civicrm-core.git] / CRM / Core / CodeGen / Schema.php
CommitLineData
5e434adf
ARW
1<?php
2
3/**
4 * Create SQL files to create and populate a new schema.
5 */
6class CRM_Core_CodeGen_Schema extends CRM_Core_CodeGen_BaseTask {
bf48aa29 7
2558c2b0 8 /**
bf48aa29 9 * CRM_Core_CodeGen_Schema constructor.
10 *
11 * @param \CRM_Core_CodeGen_Main $config
2558c2b0 12 */
bc02b97b
TO
13 public function __construct($config) {
14 parent::__construct($config);
5e434adf
ARW
15 $this->locales = $this->findLocales();
16 }
17
00be9182 18 public function run() {
5e434adf
ARW
19 CRM_Core_CodeGen_Util_File::createDir($this->config->sqlCodePath);
20
50b8ea3e
TO
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 };
5e434adf 29
50b8ea3e
TO
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 }
5e434adf
ARW
40
41 // also create the archive tables
42 // $this->generateCreateSql('civicrm_archive.mysql' );
43 // $this->generateDropSql('civicrm_archive_drop.mysql');
44
50b8ea3e
TO
45 echo "Generating navigation file\n";
46 $put($this->generateNavigation());
47
48 echo "Generating sample file\n";
49 $put($this->generateSample());
5e434adf
ARW
50 }
51
50b8ea3e 52 public function generateCreateSql() {
5e434adf
ARW
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
50b8ea3e 61 return ['civicrm.mysql' => $template->fetch('schema.tpl')];
5e434adf
ARW
62 }
63
50b8ea3e 64 public function generateDropSql() {
5e434adf
ARW
65 $dropOrder = array_reverse(array_keys($this->tables));
66 $template = new CRM_Core_CodeGen_Util_Template('sql');
67 $template->assign('dropOrder', $dropOrder);
61940bbf 68 $template->assign('isOutputLicense', TRUE);
50b8ea3e 69 return ['civicrm_drop.mysql' => $template->fetch('drop.tpl')];
5e434adf
ARW
70 }
71
00be9182 72 public function generateNavigation() {
5e434adf 73 $template = new CRM_Core_CodeGen_Util_Template('sql');
50b8ea3e 74 return ['civicrm_navigation.mysql' => $template->fetch('civicrm_navigation.tpl')];
5e434adf
ARW
75 }
76
50b8ea3e
TO
77 /**
78 * @param string $locale
79 * Ex: en_US, fr_FR
80 * @return array
81 */
82 public function generateLocaleDataSql($locale) {
5e434adf
ARW
83 $template = new CRM_Core_CodeGen_Util_Template('sql');
84
85 global $tsLocale;
86 $oldTsLocale = $tsLocale;
50b8ea3e
TO
87
88 try {
89
5e434adf
ARW
90 $tsLocale = $locale;
91 $template->assign('locale', $locale);
92 $template->assign('db_version', $this->config->db_version);
93
be2fb01f 94 $sections = [
5e434adf
ARW
95 'civicrm_country.tpl',
96 'civicrm_state_province.tpl',
97 'civicrm_currency.tpl',
98 'civicrm_data.tpl',
99 'civicrm_navigation.tpl',
100 'civicrm_version_sql.tpl',
be2fb01f 101 ];
5e434adf
ARW
102
103 $ext = ($locale != 'en_US' ? ".$locale" : '');
5e434adf 104
50b8ea3e
TO
105 return [
106 "civicrm_data$ext.mysql" => $template->fetchConcat($sections),
107 "civicrm_acl$ext.mysql" => $template->fetch('civicrm_acl.tpl'),
108 ];
109 }
110 finally {
111 $tsLocale = $oldTsLocale;
5e434adf 112 }
5e434adf
ARW
113 }
114
50b8ea3e
TO
115 /**
116 * @return array
117 * Array(string $fileName => string $fileContent).
118 * List of files
119 */
00be9182 120 public function generateSample() {
5e434adf 121 $template = new CRM_Core_CodeGen_Util_Template('sql');
be2fb01f 122 $sections = [
5e434adf
ARW
123 'civicrm_sample.tpl',
124 'civicrm_acl.tpl',
be2fb01f 125 ];
50b8ea3e
TO
126 return [
127 'civicrm_sample.mysql' => $template->fetchConcat($sections),
128 'case_sample.mysql' => $template->fetch('case_sample.tpl'),
129 ];
5e434adf
ARW
130 }
131
2558c2b0
EM
132 /**
133 * @return array
134 */
00be9182 135 public function findLocales() {
5e434adf
ARW
136 require_once 'CRM/Core/Config.php';
137 $config = CRM_Core_Config::singleton(FALSE);
be2fb01f 138 $locales = [];
4d8ce7f0 139 $localeDir = CRM_Core_I18n::getResourceDir();
5e434adf 140 if (file_exists($localeDir)) {
5e434adf
ARW
141 $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir));
142 }
143
144 $localesMask = getenv('CIVICRM_LOCALES');
145 if (!empty($localesMask)) {
146 $mask = explode(',', $localesMask);
147 $locales = array_intersect($locales, $mask);
148 }
149
150 if (!in_array('en_US', $locales)) {
151 array_unshift($locales, 'en_US');
152 }
153
154 return $locales;
155 }
96025800 156
5e434adf 157}