Fix #7591 - scanning templates for multiple projects using Content manager
[Pman.Cms] / DataObjects / Cms_template.php
1 <?php
2 /**
3  * Table Definition for cms_template 
4  *
5  *
6  * The idea here is that it contains all the strings in the templates with a language '' (empty)
7  * , it then generates a matching set of strings
8  * 
9  */
10 require_once 'Pman/Core/DataObjects/Core_template.php';
11  
12 class Pman_Cms_DataObjects_Cms_template  extends Pman_Core_DataObjects_Core_template 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
17     public $__table = 'cms_template';         // table name
18     public $id;                              // int(11)  not_null primary_key auto_increment
19     public $template;                           // string(64)  not_null
20
21     public $updated;                        // blob(65535)  blob
22     public $lang;    // text  NOT NULL;
23     public $view_name; // eg mobile or desktop
24     /* the code above is auto generated do not remove the tag below */
25     ###END_AUTOCODE
26     
27     
28     function applyFilters($q, $au, $roo)
29     {
30         //DB_DataObject::debugLEvel(1);
31 //        $x = DB_Dataobject::Factory($this->tableName());
32         
33         
34         // template scanning and syncing should be done by the UpdateDatabase Code.
35         //if (!$x->count() || !empty($q['_rescan'])) {
36             //DB_DataObject::debugLEvel(1);
37             //$tp = DB_DataObject::factory('cms_template');
38             //$opts = HTML_FlexyFramework::get()->Pman_Cms;
39             //$tp->syncTemplateDir(false, '', !empty($q['_rescan']));
40             //if (isset($q['lang'])) {
41             //    $this->syncLang($q['lang']);
42             //}
43         //} 
44 //        $this->whereAdd("
45 //                join_
46 //            ");
47         
48     }
49         
50     function toRooArray($req)
51     {
52         $ret = $this->toArray();
53         if (!empty($req['_clean_name']) ) {
54             $ret['template_clean'] = preg_replace('#\.html$#i', '', $this->template);
55             
56         }
57         return $ret;
58         
59     }
60      function factoryStr()
61     {
62         return DB_DataObject::factory('cms_templatestr');
63     }
64     /*
65      * ?? is this still used - code is in updateTemplateBJS?
66      * @param base (should be full path to template directory)
67      * @param subdir = empty for top or subpath.
68      */
69     function syncTemplateDir($base = false,  $subdir = '', $force = false)
70     {
71         echo "syncTemplateDir: $base , $subdir, $force \n";
72         //print_r(func_get_args());
73         if (!$base) {
74             $ff = HTML_FlexyFramework::get();
75             if (!isset($ff->Pman_Cms)) {
76                 echo "[ERROR] Not scanning template directory - no set in Pman_Cms[templateDir]\n";
77                 return;
78             }
79             $opts = $ff->Pman_Cms;
80             if (is_array($opts['templateDir'])) {
81                 foreach($opts['templateDir'] as $type=>$dir) {
82                     $this->syncTemplateDir($dir, '', $force);
83                 }
84                 return;
85             }
86             
87             $base = $opts['templateDir'];
88             
89             
90         }
91         if($force){
92             $tmpls = DB_DataObject::factory('cms_template');
93             $this->tmpls = $tmpls->fetchAll('template','id'); // dupes??
94         }
95         
96         $tmp_dir = $base . (empty($subdir) ? '' : '/') . $subdir;
97         
98         if(!is_dir($tmp_dir)){
99             return;
100         }
101         
102         $dh = opendir($tmp_dir);
103         if(!$dh){
104             return; // something went wrong!?
105         }
106         
107         while (($fn = readdir($dh)) !== false) {
108             // do we care that it will try and parse the template directory??? - not really..
109             // as we are only looking for php files..
110             if(empty($fn) || $fn[0] == '.'){
111                 continue;
112             }
113
114             $fullpath = $tmp_dir."/".$fn;
115 //            echo "filename: ".$fullpath." \n";    
116              
117             $relpath = $subdir . (empty($subdir) ? '' : '/') . $fn;
118             
119             if(is_dir($fullpath)){
120                 // then recursively call self...
121 //                var_Dump($base);
122 //                var_Dump($subdir . (empty($subdir) ? '' : '/') . $fn);
123                 $this->syncTemplateDir($base, $subdir . (empty($subdir) ? '' : '/') . $fn );
124                 
125                 continue;
126             }
127             if (!preg_match('/(\.html|\.txt)$/', $fn)) {
128                 continue;
129             }
130             
131              
132 //            var_dump($tmp);
133 //            var_dump($tmp_path);
134 //            $fn = basename($fn);
135             if (isset($this->tmpls[$relpath])) {
136                 unset($this->tmpls[$relpath]);
137             }
138             
139             
140             
141                
142             $template = $this->syncTemplate($relpath, true, false);
143 //            var_dump($template);
144             if (is_a($template, 'PEAR_Error')) {
145                 continue;
146             }
147         }
148         closedir($dh);
149 //        
150  
151         if($force){
152             foreach($this->tmpls as $id) {
153                 $x = DB_DataObject::factory($this->tableName());
154                 if ($x->get($id)) {
155                     $x->delete();
156                 }
157             }
158         }
159     }
160     
161     /**
162      * ?? is this used?
163      * ?? code is in UpdateBJS?
164      *
165      */ 
166      
167     function syncTemplatePage($pgdata)
168     {
169         $force = true;
170         echo "compiling:"; print_r($pgdata);
171         // read the template and extract the translatable strings.
172         ini_set('memory_limit', '512M');
173         
174         //var_dump($n);
175         $n= $pgdata['template'];  // remove trailing slash..
176         
177         $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
178         $opts = HTML_FlexyFramework::get()->Pman_Cms;
179         //print_R($opts);
180         //$dir = $opts['templateDir'] . '/' . $node;
181         $oo = array(
182             'disableTranslate' => false,
183             'templateDir' => $pgdata['template_dir'],
184             'compileDir' => $fopts['compileDir'] . '_translation_files',
185             'forceCompile' => true, //?? only for force above???
186         );
187          
188         // non-html templates - treat as such..
189         if (!preg_match('/\.html$/i', $pgdata['template'])) {
190             $oo['nonHTML'] = true;
191         }
192         
193         //print_r(array($oo, $n));
194         
195         
196         $flexy = new HTML_Template_Flexy( $oo );
197           
198         if (!$flexy->resolvePath ($pgdata['template'])) {
199             
200             echo "SKIP - could not resolve path?\n";
201             print_r($oo);
202             return false;
203         }
204           
205           
206         $r = $flexy->compile($pgdata['template']);
207        
208         //print_r( $flexy);
209       
210         //printf(" %0.3fs : $fname<BR>", $time);
211         if (is_a($r,'PEAR_Error')) {
212             
213             echo $r->toString(). "\n";
214             return $r;
215         }
216         //print_R(number_format(memory_get_usage(),0, '.', ','))  ;
217          
218         $tmpl = DB_DataObject::Factory($this->tableName());
219         $tmpl->words = file_exists($flexy->getTextStringsFile) ?
220                 unserialize(file_get_contents($flexy->getTextStringsFile)) :
221                 array();
222         
223         $tmpl->contentStrings   = $flexy->compiler->contentStrings;
224         //var_dump(file_exists($flexy->getTextStringsFile));
225         print_r($tmpl->words);
226         $tmpl->currentTemplate  = $flexy->currentTemplate;
227         
228         $tmpl->view_name = $pgdata['base'];
229         
230         
231         if (!$tmpl->get('template',  $pgdata['template'])) {
232             
233             $tmpl->template = $pgdata['template'];
234             $tmpl->lang = 'en'; /// ??? hard coded??
235             $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
236             $tmpl->insert();
237         } else {
238             $xx =clone($tmpl);
239             // has it been cahnged...
240             //if (!$force && filemtime($flexy->currentTemplate) == strtotime($tmpl->updated)) {
241                 // nothing changed..
242             //    return $tmpl;
243             //}
244             if (empty($tmpl->lang))  {
245                 //echo "FIX LANG?";exit;
246                 $tmpl->lang = 'en'; /// ??? hard coded??
247             }
248             
249             $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
250             $tmpl->update($xx);
251         }
252       
253         
254         $x = DB_DataObject::Factory('cms_templatestr');
255         $x->syncTemplateWords($tmpl);
256         
257         // if file_exists ( template/path/name.php << eg. a matching view..)
258         // then create a system page for this. 
259         
260         
261         
262         $x = DB_DataObject::Factory('cms_template_element');
263         $tmpl->elements = $x->syncTemplateElement($tmpl,
264                 file_get_contents($flexy->currentTemplate),
265                 $flexy->compiler->contentStrings,
266                 false);
267         
268         
269         
270         return clone($tmpl);
271     
272     }
273 }