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