3a8cd4ee78cdd08a57c4c0ae170cbdff1e7b88ff
[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->scanFiles();
47         
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     
192     function scanTables()
193     {
194         $ff = HTML_FlexyFramework::get();
195         
196         if (empty($ff->Pman_Cms)) {
197             $this->jerr("config[Pman_Cms] is not set");
198         }
199         
200         if(isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
201             $cts = DB_DataObject::factory('cms_templatestr');
202             
203             if($this->cli){
204                 echo "Sync tables.....\n";
205             }
206             
207             foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
208                 $t = DB_DataObject::factory($table);
209                 foreach($t->fetchAll() as $d) {
210                     $cts->onTableChange($this, $d, 'update');
211                 }
212             }
213         }
214     }
215     
216     function syncLanguage()
217     {
218         $ff = HTML_FlexyFramework::get();
219         
220         if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
221             $this->jerr("config[Pman_Cms][languages] is not set");
222         }
223         
224         
225         if($this->cli){
226             echo "Sync the Languages template.....\n";
227         }
228         
229         foreach($ff->Pman_Cms['languages'] as $l) {
230             if($this->cli){
231                 echo "Sync $l Language.....\n";
232             }
233
234             $tps = DB_DataObject::factory('cms_templatestr');
235             $tps->syncLang($l); /// this should be configured somewhere..
236         }
237     }
238     
239     
240 }