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