Pman.Dialog.CmsBlog.bjs
[Pman.Cms] / UpdateBjsTemplates.php
1 <?php
2
3 /**
4  *
5  
6  *
7  */
8
9 require_once 'Pman.php';
10 class Pman_Cms_UpdateBjsTemplates extends Pman
11 {
12     
13     static $cli_desc = "Update BJS Templates";
14  
15     static $cli_opts = array();
16     
17     var $cli = false;
18     
19     var $opts;
20     
21     function getAuth() {
22         
23         
24         $ff = HTML_FlexyFramework::get();
25         if (!empty($ff->cli)) {
26             $this->cli = true;
27             return true;
28         }
29         
30         return true;
31     }
32      
33     function get($tbl, $opts=array())
34     {
35         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
36         $this->opts = $opts;
37         $this->updateData();
38     }
39     
40     function updateData()
41     {   
42         $this->scanTemplates();
43         
44         $this->scanTables();
45         
46         $this->syncLanguage();
47         
48         $this->jok('OK');
49         
50     }
51     
52     function scanTemplates()
53     {
54         $ff = HTML_FlexyFramework::get();
55         
56         if (empty($ff->Pman_Cms)) {
57             $this->jerr("config[Pman_Cms] is not set");
58         }
59         
60         $base = $ff->Pman_Cms['project_name'];
61         
62         $dh = opendir($base);
63         
64         $ret = array();
65         
66         if(!$dh){
67             $this->jerr("could not open dir: config[Pman_Cms] = " . $base);
68             return $ret; // something went wrong!?
69         }
70          
71         while (($fn = readdir($dh)) !== false) {
72             
73             if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
74                 continue;
75             }
76             
77             if($this->cli){
78                 echo "Processing {$fn} \n";
79             }
80             
81             $template = DB_DataObject::factory('cms_template');
82             $template->setFrom(array(
83                 'template' => $fn,
84                 'lang' => 'en',
85                 'view_name' => $base
86             ));
87             
88             $o = false;
89             
90             if($template->find(true)){
91                 $o = clone ($template);
92             }
93             
94             $template->updated = $template->sqlValue("NOW()");
95             
96             (empty($o)) ? $template->insert() : $template->update($o);
97             
98             $data = json_decode(file_get_contents($base . '/' . $fn), true);
99             
100             $template->words = empty($data['strings']) ? array() : $data['strings'];
101             
102             $x = DB_DataObject::Factory('cms_templatestr');
103             $x->syncTemplateWords($template, false);
104         }
105         
106         $this->scanPmanTemplates();
107         
108     }
109     
110     function scanPmanTemplates()
111     {
112         foreach ($this->modules() as $m){
113             $view_name = "Pman.$m";
114             
115             $dh = opendir("Pman/$m");
116         
117             $ret = array();
118
119             if(!$dh){
120                 continue;
121             }
122             
123             while (($fn = readdir($dh)) !== false) {
124             
125                 if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
126                     continue;
127                 }
128
129                 if($this->cli){
130                     echo "Processing {$fn} \n";
131                 }
132
133                 $template = DB_DataObject::factory('cms_template');
134                 $template->setFrom(array(
135                     'template' => $fn,
136                     'lang' => 'en',
137                     'view_name' => $view_name
138                 ));
139
140                 $o = false;
141
142                 if($template->find(true)){
143                     $o = clone ($template);
144                 }
145
146                 $template->updated = $template->sqlValue("NOW()");
147
148                 (empty($o)) ? $template->insert() : $template->update($o);
149
150                 $data = json_decode(file_get_contents('Pman' . '/' . $m . '/' . $fn), true);
151
152                 $template->words = empty($data['strings']) ? array() : $data['strings'];
153
154                 $x = DB_DataObject::Factory('cms_templatestr');
155                 $x->syncTemplateWords($template, false);
156             }
157         }
158         
159     }
160     
161     function scanTables()
162     {
163         $ff = HTML_FlexyFramework::get();
164         
165         if (empty($ff->Pman_Cms)) {
166             $this->jerr("config[Pman_Cms] is not set");
167         }
168         
169         if(isset($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'])){
170             $cts = DB_DataObject::factory('cms_templatestr');
171             
172             if($this->cli){
173                 echo "Sync tables.....\n";
174             }
175             
176             foreach($ff->Pman_Cms['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
177                 $t = DB_DataObject::factory($table);
178                 foreach($t->fetchAll() as $d) {
179                     $cts->onTableChange($this, $d, 'update');
180                 }
181             }
182         }
183     }
184     
185     function syncLanguage()
186     {
187         $ff = HTML_FlexyFramework::get();
188         
189         if (empty($ff->Pman_Cms)) {
190             $this->jerr("config[Pman_Cms] is not set");
191         }
192         
193         $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
194         
195         if($this->cli){
196             echo "Sync the Languages template.....\n";
197         }
198         
199         foreach($opt_lang['l'] as $l) {
200             if($this->cli){
201                 echo "Sync $l Language.....\n";
202             }
203
204             $tps = DB_DataObject::factory('cms_templatestr');
205             $tps->syncLang($l); /// this should be configured somewhere..
206         }
207     }
208     
209     
210 }