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