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