merge fix
[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         $ff = HTML_FlexyFramework::get();
47       
48         if (empty($ff->Pman_Cms)) {
49             $this->jerr("config[Pman_Cms] is not set");
50         }
51         $this->scanPowerpoint();
52         $this->scanFiles();
53         $this->scanTables();
54         $this->syncLanguage();
55         
56         
57         $this->jok('OK');
58         
59     }
60     
61     function scanFiles()
62     {
63         $ff = HTML_FlexyFramework::get();
64         
65         
66         
67        
68         foreach(explode(',', $ff->Pman_Cms['project_name']) as $base) {
69             $this->scanFilesBase($base);
70         }
71     }
72     
73     /*
74      * this does all the scan work.
75      * it's pretty simple (does not do subdirectories...)
76      *
77      * only supports BJS / PHP / JS currently.
78      * 
79      */
80     
81     
82     function scanFilesBase($base)
83     {
84         
85         $ff = HTML_FlexyFramework::get();
86
87         $dir = $ff->Pman_Cms['site_dir'][$base];
88         //var_dump($dir);
89         $dh = opendir($dir);
90         
91         $tp = DB_DAtaObject::Factory('cms_template');
92         
93         $ret = array();
94         
95         if(!$dh){
96             $this->jerr("could not open dir: config[Pman_Cms] = " . $dir);
97             return $ret; // something went wrong!?
98         }
99          
100         while (($fn = readdir($dh)) !== false) {
101             
102             if(empty($fn) || $fn[0] == '.') {
103                 continue;
104             }
105             $fp = $dir . '/'. $fn;
106             //var_dump($fp);
107             if (is_dir($fp)) {
108                 continue;
109             }
110             if (is_link($fp)) {
111                 continue;
112             }
113             if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
114                 $temp = $tp->syncJsWords(array(
115                     'base' => $base, 
116                     'template_dir' =>  $dir,
117                     'template' => $fn
118                 ));
119                 if ($temp) {
120                     $ids[] = $temp->id;
121                 }
122                 continue;
123             }
124             if (preg_match('/\.php$/', $fn)) {
125                 $temp = $tp->syncPhpGetText(array(
126                     'base' =>$base, 
127                     'template_dir' =>  $dir,
128                     'template' => $fn
129                 ));
130                 if ($temp) {
131                     $ids[] = $temp->id;
132                 }
133                 continue;
134             }
135             
136             if (!preg_match('/\.bjs$/', $fn)){
137                 continue;
138             }
139             // var_dump($fn);exit;
140             if($this->cli){
141                 echo "Processing {$fn} \n";
142             }
143             
144             
145             
146             
147             $template = DB_DataObject::factory('cms_template');
148             $template->setFrom(array(
149                 'template' => $fn,
150                 'lang' => 'en',
151                 'view_name' => $base
152             ));
153             
154             $o = false;
155             
156             if($template->find(true)){
157                 $o = clone ($template);
158             }
159             $template->filetype = 'bjs';
160             
161             $template->updated = $template->sqlValue("NOW()");
162             
163             if (empty($o)) {
164                 $ids[] = $template->insert() ;
165             } else {
166                 $template->update($o);
167                 $ids[] = $o->id;
168             }
169             
170             
171             
172             $data = json_decode(file_get_contents($base . '/' . $fn), true);
173             
174             $template->words = empty($data['strings']) ? array() : $data['strings'];
175             
176             $x = DB_DataObject::Factory('cms_templatestr');
177             $x->syncTemplateWords($template, false);
178         }
179         
180         
181         $del = DB_DataObject::factory('cms_template');
182         $del->whereAddIn('!id', $ids, 'int');
183         $del->view_name = $base;
184         $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
185         $delids = $del->fetchAll('id');
186         if ($delids) {
187             DB_DataObject::factory('core_template')->query(
188                 'update cms_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
189             );
190         }
191         
192     }
193
194     function scanPowerpoint()
195     {
196         $ff = HTML_FlexyFramework::get();
197         
198          
199         
200         if(!isset($ff->Pman_Cms['powerpoint'])) {
201             return;
202         }
203         foreach($ff->Pman_Cms['powerpoint'] as $base => $dir) {
204             $strings = $this->scanPowerpointText($dir);
205
206             foreach($strings as $slide => $words) {
207                 $ct = DB_DataObject::factory('cms_template');
208                 $temp = $ct->syncPowerpointXMLText(array(
209                     'template' => $slide . '.xml',
210                     'template_dir' => $dir . '/ppt/slides',
211                     'base' => $base,
212                     'words' => $words
213                 ));
214
215                 if($temp) {
216                     $ids[] = $temp->id;
217                 }
218             }
219
220             $del = DB_DataObject::factory('cms_template');
221             $del->whereAddIn('!id', $ids, 'int');
222             $del->view_name = $base;
223             $del->filetype = 'xml';
224             $delids = $del->fetchAll('id');
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         
313         if(!isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
314             return;
315         }
316         $cts = DB_DataObject::factory('cms_templatestr');
317         
318         if($this->cli){
319             echo "Sync tables.....\n";
320         }
321         
322         foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
323             $t = DB_DataObject::factory($table);
324             foreach($t->fetchAll() as $d) {
325                 $cts->onTableChange($this, $d, 'update');
326             }
327         }
328         
329     }
330     
331     function syncLanguage()
332     {
333         $ff = HTML_FlexyFramework::get();
334         
335         if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
336             $this->jerr("config[Pman_Cms][languages] is not set");
337         }
338         
339         
340         if($this->cli){
341             echo "Sync the Languages template.....\n";
342         }
343         
344         foreach($ff->Pman_Cms['languages'] as $l) {
345             if($this->cli){
346                 echo "Sync $l Language.....\n";
347             }
348
349             $tps = DB_DataObject::factory('cms_templatestr');
350             $tps->syncLang($l); /// this should be configured somewhere..
351         }
352     }
353     
354     
355 }