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