· 8 years ago · Aug 20, 2018, 05:36 PM
1<?php
2/*******************************************************************************************
3
4Simplx Mirage Setup Snippet
5
6This Snippet sets up, most importantly, the mysql view needed to query template vars
7with efficiance.
8
9********************************************************************************************/
10
11$objectPropertiesViewName = isset($objectPropertiesViewName) ? $objectPropertiesViewName : 'view_mirage_object_properties';
12$tablePrefix = ('`'.$modx->getOption(xPDO::OPT_TABLE_PREFIX).'`');
13$modTemplateVarTemplateTable = $modx->getTableName('modTemplateVarTemplate');
14$modTemplateVarTable = $modx->getTableName('modTemplateVar');
15$modTemplateVarResourceTable = $modx->getTableName('modTemplateVarResource');
16
17$modx->log(modX::LOG_LEVEL_DEBUG, 'Snippet simplx.mirage.setup: $objectPropertiesViewName = '.$objectPropertiesViewName);
18$modx->log(modX::LOG_LEVEL_DEBUG, 'Snippet simplx.mirage.setup: $tablePrefix = '.$tablePrefix);
19$modx->log(modX::LOG_LEVEL_DEBUG, 'Snippet simplx.mirage.setup: $modTemplateVarTemplateTable = '.$modTemplateVarTemplateTable );
20$modx->log(modX::LOG_LEVEL_DEBUG, 'Snippet simplx.mirage.setup: $modTemplateVarTable = '.$modTemplateVarTable);
21$modx->log(modX::LOG_LEVEL_DEBUG, 'Snippet simplx.mirage.setup: $modTemplateVarResourceTable = '.$modTemplateVarResourceTable);$modx->log(modX::LOG_LEVEL_DEBUG, 'Snippet simplx_mirage_setup: $modTemplateVarResourceTable = '.$modTemplateVarResourceTable);
22
23$result = false;
24$handle;
25$contents = '';
26$success = false;
27$properties = array();
28
29
30/*******************************************************************************************
31
32* SYSTEM SETUP SECTION
33
34********************************************************************************************/
35
36
37/*
38* Create a new Namespace and get rid of the one that PackMan created.
39*/
40
41
42$simplx_mirage_ns = $modx->getObject('modNamespace',array(
43 'name' => 'simplxmirage'
44));
45
46if($simplx_mirage_ns){
47 $result = $simplx_mirage_ns->remove();
48
49 if($result){
50 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mirage.setup() Exception: Could not delete Namespace "simplxmirage". Please do this manually.');
51 }
52}
53
54unset($simplx_mirage_ns);
55
56$simplx_mirage_ns = $modx->newObject('modNamespace');
57
58if($simplx_mirage_ns){
59 $simplx_mirage_ns->set('name','simplx.mirage');
60 $simplx_mirage_ns->set('path',($modx->getOption('assets_path').'components/simplx/mirage/'));
61 $result = $simplx_mirage_ns->save();
62
63 if($result === false){
64 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mygit.setup() Exception: Could create Namespace "simplx.mirage". Please do this manually to keep things in good order.');
65 }
66}
67
68unset($simplx_mirage_cat);
69
70
71/*
72* Create a Settings
73*/
74
75
76$simplx_mirage_setting = $modx->getObject('modSystemSetting',array(
77'key' => 'simplx.mirage.setup.hasrun'
78));
79
80if($simplx_mirage_setting !== 1){
81 unset($simplx_mirage_setting);
82
83 $simplx_mirage_setting = $modx->newObject('modSystemSetting');
84 $simplx_mirage_setting->set('key','simplx.mirage.setup.hasrun');
85 $simplx_mirage_setting->set('value','0');
86 $simplx_mirage_setting->set('namespace','simplx.mirage');
87
88 $result = $simplx_mirage_setting->save();
89
90 if(!$result){
91 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mirage.setup() Exception: Could not create System Setting "simplx.mygit.username". Please do this manually.');
92 }
93
94 unset($simplx_mirage_setting);
95
96 $simplx_mirage_setting = $modx->newObject('modSystemSetting');
97 $simplx_mirage_setting->set('key','simplx.mirage.object.viewname');
98 $simplx_mirage_setting->set('value',$objectPropertiesViewName);
99 $simplx_mirage_setting->set('namespace','simplx.mirage');
100
101 $result = $simplx_mirage_setting->save();
102
103 if(!$result){
104 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mirage.setup() Exception: Could not create System Setting "simplx.mirage.object.viewname". This is a critical Setting. Please do this manually.');
105 }
106
107}else{
108 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mirage.setup() Exception: "simplx.mirage.setup.hasrun" exists so executing setup is not necessary. Aborting.');
109}
110
111unset($simplx_mygit_setting);
112
113
114/*
115*
116* Set up the simplx.mirage.class Property Set
117*
118*/
119
120
121$simplx_mirage_class_ps = $modx->getObject('modPropertySet',array(
122 'name' => 'simplx.mirage.class'
123));
124
125// If there is no 'simplx.mirage.class' yet, lets create it.
126if(!$simplx_mirage_class_ps){
127
128 $filename = $modx->getOption('core_path')."components/simplx/mirage/simplx_mirage_class_properties.js";
129
130 if (!file_exists($file_name)) {
131 $handle = fopen($filename, "rb");
132 $contents = fread($handle, filesize($filename));
133 fclose($handle);
134
135
136 // Create an array with all properties.
137 $properties = json_decode($contents,true);
138
139 if(is_array($properties)){
140 $simplx_mirage_class_ps = $modx->newObject('modPropertySet');
141 $simplx_mirage_class_ps->set('name','simplx.mirage.class');
142 $simplx_mirage_class_ps->set('description','Property Set which must be implemented by all Templates wanting to use with Mirage.');
143
144 $simplx_mirage_class_ps->setProperties($properties);
145
146 $success = $simplx_mirage_class_ps->save();
147
148 if(!$success){
149 // Could not set the properties.
150 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mirage.setup() Exception: Could not create Property Set "simplx.mirage.class". Please do this manually.');
151
152 }else{
153
154 }
155
156 }else{
157 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mirage.setup() Exception: Could not decode the "simplx.mirage.class" Property Set. Please import the Property Set manually.');
158 }
159
160
161 }else{
162 $modx->log(modX::LOG_LEVEL_ERROR, 'simplx.mirage.setup() Exception: Could not create Property Set "simplx.mirage.class". Please do this manually.');
163 }
164
165}else{
166 $modx->log(modX::LOG_LEVEL_DEBUG, 'simplx.mirage.setup(): The "simplx.mirage.class" Property Set is already installed.');
167}
168
169
170/*******************************************************************************************
171
172* SQL VIEW SETUP SECTION
173
174********************************************************************************************/
175
176
177/*
178 I suggest not removing the MERGE algorithm since not using it would ditch the underlying table indexes.
179 Remember however that MERGE does not work well with aggregate functions and DISTINC, ORDER BY etc
180*/
181
182$createViewQuery = '
183
184CREATE OR REPLACE ALGORITHM=MERGE VIEW `'.$objectPropertiesViewName.'` AS
185
186SELECT
187 `val`.`contentid` AS `modresource.id`,
188 `var`.`id` AS `modtemplatevar.id`,
189 `tpl`.`templateid` AS `modtemplate.id`,
190 `var`.`name` AS `modtemplatevar.name`,
191 `val`.`value` AS `modtemplatevarresource.value`
192FROM
193 '.$modTemplateVarTable.' AS `var`,
194 '.$modTemplateVarResourceTable.' AS `val`,
195 '.$modTemplateVarTemplateTable.' AS `tpl`
196WHERE
197 `val`.`tmplvarid` = `var`.`id`
198 AND
199 `tpl`.`tmplvarid` = `var`.`id`;
200';
201
202$modx->log(modX::LOG_LEVEL_DEBUG, 'Snippet simplx_mirage_setup: $createViewQuery = '.$createViewQuery);
203
204$result = $modx->exec($createViewQuery);
205
206if($result === false){
207 $modx->log(modX::LOG_LEVEL_ERROR, 'Snippet simplx_mirage_setup: CREATE VIEW returned false. Aborting.');
208 return false;
209}else{
210
211 // All's well that ends well. We can now set the "simplx.mirage.setup.hasrun" setting to 1.
212
213 $simplx_mirage_setting = $modx->getObject('modSystemSetting',array(
214 'key' => 'simplx.mirage.setup.hasrun'
215 ));
216
217 $simplx_mirage_setting->set('value','1');
218 $result = $simplx_mirage_setting->save();
219
220 // And return true.
221 return true;
222}
223
224
225
226/*******************************************************************************************
227*
228*
229********************************************************************************************/