UpdateBjsTemplates.php
[Pman.Cms] / UpdateBjsTemplates.php
1 <?php
2
3 /**
4  *
5  * simlar to Pman/Admin/UpdateBjsTemplates...
6  *
7  * scan BJS files
8  * scans JS (for  ._("...") )
9  * scan PHP for 
10  *
11  */
12
13 require_once 'Pman/Admin/UpdateBjsTemplates.php';
14 class Pman_Cms_UpdateBjsTemplates extends Pman_Admin_UpdateBjsTemplates
15 {
16     
17     static $cli_desc = "Update BJS Templates";
18  
19     static $cli_opts = array();
20     
21     var $cli = false;
22     
23     var $opts;
24     
25     function getAuth() {
26         
27         
28         $ff = HTML_FlexyFramework::get();
29         if (!empty($ff->cli)) {
30             $this->cli = true;
31             return true;
32         }
33         
34         return true;
35     }
36      
37     function get($tbl, $opts=array())
38     {
39         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
40         $this->opts = $opts;
41         $this->updateData();
42     }
43     
44     function updateData()
45     {   
46         $this->scanPowerpoint();
47         $this->scanFiles();
48         $this->scanTables();
49         $this->syncLanguage();
50         
51         
52         $this->jok('OK');
53         
54     }
55     
56     function scanFiles()
57     {
58         $ff = HTML_FlexyFramework::get();
59         
60         if (empty($ff->Pman_Cms)) {
61             $this->jerr("config[Pman_Cms] is not set");
62         }
63         
64        
65         foreach(explode(',', $ff->Pman_Cms['project_name']) as $base) {
66             $this->scanFilesBase($base);
67         }
68     }
69     
70     /*
71      * this does all the scan work.
72      * it's pretty simple (does not do subdirectories...)
73      *
74      * only supports BJS / PHP / JS currently.
75      * 
76      */
77     
78     
79     function scanFilesBase($base)
80     {
81         
82         $ff = HTML_FlexyFramework::get();
83
84         $dir = $ff->Pman_Cms['site_dir'][$base];
85         //var_dump($dir);
86         $dh = opendir($dir);
87         
88         $tp = DB_DAtaObject::Factory('cms_template');
89         
90         $ret = array();
91         
92         if(!$dh){
93             $this->jerr("could not open dir: config[Pman_Cms] = " . $dir);
94             return $ret; // something went wrong!?
95         }
96          
97         while (($fn = readdir($dh)) !== false) {
98             
99             if(empty($fn) || $fn[0] == '.') {
100                 continue;
101             }
102             $fp = $dir . '/'. $fn;
103             //var_dump($fp);
104             if (is_dir($fp)) {
105                 continue;
106             }
107             if (is_link($fp)) {
108                 continue;
109             }
110             if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
111                 $temp = $tp->syncJsWords(array(
112                     'base' => $base, 
113                     'template_dir' =>  $dir,
114                     'template' => $fn
115                 ));
116                 if ($temp) {
117                     $ids[] = $temp->id;
118                 }
119                 continue;
120             }
121             if (preg_match('/\.php$/', $fn)) {
122                 $temp = $tp->syncPhpGetText(array(
123                     'base' =>$base, 
124                     'template_dir' =>  $dir,
125                     'template' => $fn
126                 ));
127                 if ($temp) {
128                     $ids[] = $temp->id;
129                 }
130                 continue;
131             }
132             
133             if (!preg_match('/\.bjs$/', $fn)){
134                 continue;
135             }
136             // var_dump($fn);exit;
137             if($this->cli){
138                 echo "Processing {$fn} \n";
139             }
140             
141             
142             
143             
144             $template = DB_DataObject::factory('cms_template');
145             $template->setFrom(array(
146                 'template' => $fn,
147                 'lang' => 'en',
148                 'view_name' => $base
149             ));
150             
151             $o = false;
152             
153             if($template->find(true)){
154                 $o = clone ($template);
155             }
156             $template->filetype = 'bjs';
157             
158             $template->updated = $template->sqlValue("NOW()");
159             
160             if (empty($o)) {
161                 $ids[] = $template->insert() ;
162             } else {
163                 $template->update($o);
164                 $ids[] = $o->id;
165             }
166             
167             
168             
169             $data = json_decode(file_get_contents($base . '/' . $fn), true);
170             
171             $template->words = empty($data['strings']) ? array() : $data['strings'];
172             
173             $x = DB_DataObject::Factory('cms_templatestr');
174             $x->syncTemplateWords($template, false);
175         }
176         
177         
178         $del = DB_DataObject::factory('cms_template');
179         $del->whereAddIn('!id', $ids, 'int');
180         $del->view_name = $base;
181         $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
182         $delids = $del->fetchAll('id');
183         if ($delids) {
184             DB_DataObject::factory('core_template')->query(
185                 'update cms_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
186             );
187         }
188         
189     }
190
191     function scanPowerpoint()
192     {
193         $ff = HTML_FlexyFramework::get();
194         
195         if (empty($ff->Pman_Cms)) {
196             $this->jerr("config[Pman_Cms] is not set");
197         }
198         
199         if(isset($ff->Pman_Cms['powerpoint'])) {
200             foreach($ff->Pman_Cms['powerpoint'] as $base => $dir) {
201                 $strings = $this->scanPowerpointText($dir);
202
203                 foreach($strings as $slide => $words) {
204                     $ct = DB_DataObject::factory('cms_template');
205                     $temp = $ct->syncPowerpointXMLText(array(
206                         'template' => $slide . '.xml',
207                         'template_dir' => $dir . '/ppt/slides',
208                         'base' => $base,
209                         'words' => $words
210                     ));
211
212                     if($temp) {
213                         $ids[] = $temp->id;
214                     }
215                 }
216
217                 $del = DB_DataObject::factory('cms_template');
218                 $del->whereAddIn('!id', $ids, 'int');
219                 $del->view_name = $base;
220                 $del->filetype = 'xml';
221                 $delids = $del->fetchAll('id');
222
223                 var_dump($delids);
224                 die('b');
225                 if ($delids) {
226                     DB_DataObject::factory('core_template')->query(
227                         'update cms_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
228                     );
229                 }
230
231             }
232         }
233     }
234
235     /**
236      * scan translatable text items in a powerpoint folder
237      * translatable text items should have names starting with 'tr-'
238      * 
239      * @param String $path path of the powerpoint folder to be scanned
240      * @return Array $strings 2d array containing translatable strings grouped by slide name
241      * 
242      */
243     static function scanPowerpointText($path)
244     {
245
246         // get id of slides from presentation xml
247         $slideRids = array();
248
249         $xmlString = file_get_contents($path . '/ppt/presentation.xml');
250         $sxe = new SimpleXMLElement($xmlString);
251         $slides = $sxe->xpath('./p:sldIdLst/p:sldId');
252
253         foreach($slides as $slide) {
254             $slideRids[] = $slide->attributes('r', true)->id->__toString();
255
256         }
257
258         // get id and name of slides from presentation xml rels
259         $slideNames = array();
260
261          $xmlString = file_get_contents($path . '/ppt/_rels/presentation.xml.rels');
262         $sxe = new SimpleXMLElement($xmlString);
263         $relationships = $sxe->xpath('.')[0];
264
265         foreach($relationships->children() as $relationship) {
266             // a slide
267             if($relationship->attributes()->Type->__toString() == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide") {
268                 $target = $relationship->attributes()->Target->__toString();
269                 $slideName = substr(substr($target, 0 , strlen($target) - strlen('.xml')), strlen('slides/'));
270                 $slideNames[$relationship->attributes()->Id->__toString()] = $slideName;
271             }
272         }
273         
274         // get strings from slides
275         $strings = array();
276
277         foreach($slideRids as $slideRid) {
278             $slide = $slideNames[$slideRid];
279             $strings[$slide] = array();
280
281             $xmlString = file_get_contents($path . '/ppt/slides/' . $slide . '.xml');
282             $sxe = new SimpleXMLElement($xmlString);
283             $texts = $sxe->xpath('./p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:t');
284
285             foreach($texts as $text) {
286                 $property = $text->xpath('../../../../p:nvSpPr/p:cNvPr');
287
288                 // cannot find property (It won't happen in theory)
289                 if(empty($property)) { 
290                     continue;
291                 }
292
293                 $textName = $property[0]->attributes()->name->__toString();
294
295                 $textValue = $text->__toString();
296
297                 // a text is translatable if its name starts with 'tr-'
298                 if(strpos($textName, 'tr-') === 0 && !in_array($textValue, $strings[$slide])) {
299                     $strings[$slide][] = $textValue;
300                 }
301             }
302         }
303
304         return $strings;
305     }
306      
307     
308     function scanTables()
309     {
310         $ff = HTML_FlexyFramework::get();
311         
312         if (empty($ff->Pman_Cms)) {
313             $this->jerr("config[Pman_Cms] is not set");
314         }
315         
316         if(isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
317             $cts = DB_DataObject::factory('cms_templatestr');
318             
319             if($this->cli){
320                 echo "Sync tables.....\n";
321             }
322             
323             foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
324                 $t = DB_DataObject::factory($table);
325                 foreach($t->fetchAll() as $d) {
326                     $cts->onTableChange($this, $d, 'update');
327                 }
328             }
329         }
330     }
331     
332     function syncLanguage()
333     {
334         $ff = HTML_FlexyFramework::get();
335         
336         if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
337             $this->jerr("config[Pman_Cms][languages] is not set");
338         }
339         
340         
341         if($this->cli){
342             echo "Sync the Languages template.....\n";
343         }
344         
345         foreach($ff->Pman_Cms['languages'] as $l) {
346             if($this->cli){
347                 echo "Sync $l Language.....\n";
348             }
349
350             $tps = DB_DataObject::factory('cms_templatestr');
351             $tps->syncLang($l); /// this should be configured somewhere..
352         }
353     }
354     
355     
356 }