30235e5e0023a6d80164c13d6daa7dbe92723c21
[Pman.Cms] / DataObjects / Cms_templatestr.php
1 <?php
2 /**
3  * Table Definition for cms_templatestr
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 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
11
12 class Pman_Cms_DataObjects_Cms_templatestr extends DB_DataObject 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
17     public $__table = 'cms_templatestr';         // table name
18     public $id;                              // int(11)  not_null primary_key auto_increment
19     public $template_id;                           // string(64)  not_null
20     public $txt;                    // datetime(19)  multiple_key binary
21     public $updated;                        // blob(65535)  blob
22     public $src_id;                          // int(11)  not_null
23     public $lang;    // text  NOT NULL;
24     public $mdsum;    // text  NOT NULL;
25     public $active;
26     public $on_table;
27     public $on_id;
28     public $on_col;
29  
30     /* the code above is auto generated do not remove the tag below */
31     ###END_AUTOCODE
32     
33     function beforeInsert($q,$roo)
34     {
35         if(!empty($q['_rescan'])){
36             $this->syncLang($q['_rescan']);
37             $roo->jok('OK');
38         }
39     }
40     
41     
42     function applyFilters($q, $au, $roo)
43     {
44         if (!empty($q['_tree'])) {
45             $this->applyFiltersTree($q,$roo);
46         }
47         
48         if(!empty($q['on_table']) && !is_numeric($q['template_id'])){
49             $this->template_id = 0;
50         }
51         if (!empty($q['_search_txt'])) {
52             $str = $this->escape($q['_search_txt']);
53             $this->whereAdd("cms_templatestr.txt like '%{$str}%' OR join_src_id_id.txt like '%{$str}%'");
54             
55         }
56     }
57     function translateTableCol($obj, $col, $lang)
58     {
59         $cts = DB_DataObject::factory('cms_templatestr');
60         $cts->lang = $lang;
61         $cts->on_table = $obj->tableName();
62         $cts->on_id = $obj->pid();
63         $cc = clone($cts);
64         if(!$cts->find(true)){
65             return $obj->$col;
66         }
67         
68         
69         if(empty($cts->txt)){
70             return $obj->$col;
71         }
72             
73         return $cts->txt;
74     }
75     /**
76      *
77      * insert the origanal table text
78      * 
79      * @param type $roo
80      * @param type $obj
81      * @param type $chg
82      * @return type 
83      */
84     function onTableChange($roo, $obj, $chg)
85     {
86         
87         $ff = HTML_FlexyFramework::get()->Pman_Cms;
88             
89         if(empty($ff['DataObjects_Cms_templatestr']['tables'])){
90             return;
91         }
92         $tn = $obj->tableName();
93         if(empty($ff['DataObjects_Cms_templatestr']['tables'][$tn])){
94             return;
95         }
96         $cols = $ff['DataObjects_Cms_templatestr']['tables'][$tn];
97         
98         
99         foreach($cols as $c) {
100             $x = $this->factory($this->tableName());
101             $x->on_id = $obj->pid();
102             $x->on_table = $tn;
103             $x->on_col = $c;
104             $x->lang = ''; /// eg. base language..
105             $up = $x->find(true);
106             if ($up && $x->txt == $obj->$c) {
107                 continue; // update an no change..
108             }
109             $x->active = 1;
110             $x->src_id = 0;
111             $x->txt = $obj->$c;
112             $x->mdsum = md5($obj->$c);
113             $x->template_id = 0;
114             $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
115             $up ? $x->update() : $x->insert();
116         }
117         
118         
119     }
120     
121     
122     function applyFiltersTree($q,$roo)
123     {
124         if (empty($q['node'])) {
125             $roo->jerr("invalid node");
126         }
127         switch($q['node']) {
128             
129             case 'transtree':
130                // DB_DataObject::debugLevel(1);
131                 $x = DB_Dataobject::Factory($this->tableName());
132                 $x->selectAdd();
133                 $x->selectAdd('distinct(lang) as lang');
134                 $x->whereAdd("lang != ''");
135                 $ret= array();
136                 foreach( $x->fetchAll('lang') as $l) {
137                     $ret[] = array(
138                         'text'=>$l,
139                         'id' =>$l,
140                         'language' => true
141                     );
142                 }
143                 if (empty($ret)) {
144                     $ret[] = array(
145                         'text'=>'en',
146                         'id' => 'en',
147                         'language' => true
148                     );
149                 }
150                 $roo->jdata($ret);
151                 
152                 
153             default:
154                 $x = DB_DataObject::factory($this->tableName());
155                 $x->selectAdd();
156                 $x->selectAdd('distinct(template_id) as template_id');
157                 $x->lang = $q['node'];
158                 $ids = $x->fetchAll('template_id');
159                 
160                 $ret= array();
161                 //add the table type lists
162                 
163                 $ff = HTML_FlexyFramework::get()->Pman_Cms;
164                 
165                 if(!empty($ff['DataObjects_Cms_templatestr']['tables'])){
166                     foreach($ff['DataObjects_Cms_templatestr']['tables'] as $table=>$v){
167                         $ret[] = array(
168                             'text'=> $table,
169                             'on_table' => $table,
170                             'id' => 0,
171                             'leaf' => true
172                         );
173                     }
174                 }
175                 
176 //                $x->orderBy('template ASC');
177 //                $x->whereAdd("lang != ''");
178                 
179                 //below are old code
180                 $xx = DB_Dataobject::Factory('cms_template');
181                 $xx->whereAddIn('id', $ids, 'int');
182                 $xx->selectAdd();
183                 $xx->selectAdd("
184                                
185                     id, concat(view_name,':', template) as template_name
186                 ");
187                 $xx->orderBy('template_name ASC');
188                 
189                 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
190                     $ret[] = array(
191                         'text'=>$n,
192                         'id' => $l,
193                         'leaf' => true
194                     );
195                 }
196                 
197                 $roo->jdata($ret);
198                 $roo->jerr("not yet");
199                 break;
200         }
201                 
202         
203         
204     }
205     
206     
207     /**
208      *
209      * 
210      * @param object $tmpl cms_template data object
211      * @param array $words array of words 
212      */ 
213     function syncTemplateWords($tmpl, $keyvalue = false)
214     {
215         
216         $words = $tmpl->words;
217         // mapping for template : 
218         //tablename => $n (templatename) 
219         //tableid => $k (key value)
220         //colname => $n (templatename) 
221         // mdsum => md5(sum)
222         //
223         //print_r($words);exit;
224         // grab original
225         $tt = DB_DataObject::factory($this->tableName());
226
227
228         $t = DB_DataObject::factory($this->tableName());
229         $t->template_id = $tmpl->id;
230         $t->whereAdd("lang = ''");
231         
232         
233         // we have a situation where old md sums where created..
234         
235         
236         
237         $cur = $t->fetchAll('mdsum', 'id'); 
238         
239         
240         
241         
242         
243         
244         
245         // now loop through current..
246         $cwords = array();// not in used??
247         $active = array();
248 //        echo "sync Template Words... \n";
249 //        print_r($words);
250         
251         foreach($words as $k=>$v) {
252             
253             
254             $v = trim($v);
255             
256             $md = $keyvalue ? $k : md5($v);
257             
258             // check to see if there are more that one versions of that md5
259             
260             
261             if (!isset($cur[$md])) {
262                 // create a record for it..
263                 $t = DB_DataObject::factory($this->tableName());
264                 $t->setFrom(array(
265                     'txt' => $v,
266                     'lang' => '',// by default should a english 
267                     'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
268                     'template_id'=>$tmpl->id,
269                     'mdsum' => $md,
270                     'src_id' => 0,
271                     'active' => 1,
272                 ));
273                 $active[] =  $t->insert();
274                 continue;
275             }  
276             $cur[$md] = $this->checkDupes($tmpl->id, '',  $cur[$md] , $md);
277             
278             $active[] = $cur[$md];
279             
280             // we have it already? - 
281             $tt->query("UPDATE {$this->tableName()}
282                         SET active= 1
283                         WHERE
284                         id = ".$cur[$md]);
285             unset($cur[$md]);
286             
287         }
288         // delete unused.
289         
290         
291         
292
293         $deactive = array();
294         if (count(array_values($cur))) {// de-active unused
295
296             $t = DB_DataObject::factory($this->tableName());
297 //            echo "de-active current?? \n";
298 //            print_r($cur);
299 //            echo "\n";
300             $deactive = array_values($cur);
301             $t->query("UPDATE cms_templatestr
302                       SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
303                      ");
304         }
305         
306         // delete all the items that are not relivant.
307         // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
308         // this does not help - as it just puts random strings in there.. - with no reference to the original text..
309         $t = DB_DataObject::factory($this->tableName());
310     
311         // this will active the child data
312         if (empty($active)) {// set the active array to empty
313             $active = array(-1);
314         }
315         $t->query("UPDATE  cms_templatestr 
316                 SET active = 1
317                   WHERE
318                      src_id IN (". implode(',' ,$active) . ")
319                     AND
320                     template_id = {$tmpl->id}
321         ");
322         //deactive the child data
323         if (empty($deactive)) {
324             $deactive = array(-1);
325         }
326         $t->query("UPDATE  cms_templatestr 
327                 SET active = 0
328                   WHERE
329                     src_id IN (". implode(',' ,$deactive) . ")
330                     AND
331                     template_id = {$tmpl->id}
332                     AND
333                     lang != ''
334         ");
335     }
336     function checkDupes($tid, $lang, $id, $mdsum) {
337         
338         $t = DB_DataObject::factory($this->tableName());
339         $t->template_id = $tid;
340         $t->mdsum = $mdsum;
341         $t->whereAdd("lang = '{$lang}'");
342         if ($t->count() == 1) {
343             return $id; // only got one ... no issues..
344         }
345         
346         //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
347         
348         //DB_DataObject::debugLevel(1);
349         // find out if any of them have got translations.
350         $ids = $t->fetchAll('id');
351         
352         
353         $t = DB_DataObject::factory($this->tableName());
354         $t->whereAddIn('src_id', $ids, 'int');
355         $t->whereAdd("txt != ''");
356         $t->orderBy('updated DESC');
357         if ($t->count()) {
358             $t->limit(1);
359             // do any translations exist?
360             $t->find(true);
361             $id = $t->src_id;
362         }
363         
364         
365         // delete all the others...
366         $t = DB_DataObject::factory($this->tableName());
367         $t->whereAddIn('src_id', $ids, 'int');
368         $t->whereAdd("src_id != $id");
369         $t->find();
370         while($t->fetch()) {
371             $tt = clone($t);
372             $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
373             $tt->update($t);
374         }
375         $t = DB_DataObject::factory($this->tableName());
376         $t->whereAddIn('id', $ids, 'int');
377         $t->whereAdd("id != $id");
378         $t->find();
379         while($t->fetch()) {
380             $tt = clone($t);
381             $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
382             $tt->update($t);
383         }
384         // this is done by calling code 
385         //$t = DB_DataObject::factory($this->tableName());
386         //$t->query("update cms_templatestr set active= 1 where src_id = $id");
387         
388         
389         
390        //exit;
391         return $id;
392     
393                
394          
395     }
396     
397     function syncLang($lang)
398     {
399         // bugs with our old code...
400 //        die('in?');
401         $tn = $this->tableName();
402         $t = DB_DataObject::factory($tn);
403         $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table  = ''");
404         
405         // find all the id's from lang that have not been generated..
406         
407         //find the origanal 
408         $t = DB_DataObject::factory($tn);
409         $t->whereAdd("lang = ''");
410         $t->active = 1;
411         
412         //old code, this did not support the on_table
413 //        $id_tmp = $t->fetchAll('id','template_id');
414 //        $ids = array_keys($id_tmp);
415         $id_tmp = array();
416         //new code for support the sync tables 
417         foreach($t->fetchAll() as $ori){
418             $id_tmp[$ori->id] = $ori;
419         }
420         $ids = array_keys($id_tmp);
421         
422         // matching by language:
423         $t = DB_DataObject::factory($tn);
424         $t->whereAddIn('src_id', $ids , 'int');
425         $t->lang = $lang;
426         //$t->active = 1;
427         $got = $t->fetchAll('src_id');
428         $missing = array_diff($ids, $got);
429         foreach($missing as $id) {
430             
431             $t = DB_DataObject::factory($tn);
432             $t->setFrom(array(
433                 'src_id' => $id,
434                 'txt' =>  '',
435                 'lang' => $lang,
436                 'updated' => date('Y-m-d H:i:s', strtotime("NOW")),
437                 'template_id'=> $id_tmp[$id]->template_id,
438                 'on_table' => $id_tmp[$id]->on_table,
439                 'on_id' => $id_tmp[$id]->on_id,
440                 'on_col' => $id_tmp[$id]->on_col,
441                 'active' => 1,
442                 // no md5um
443             ));
444             $t->insert();
445         }
446         
447         
448         
449     }
450     function translateFlexyString($flexy, $string)
451     {
452         //var_dump($string);
453         $debug = false;;
454         //if (!empty($_REQUEST['_debug'])) { $debug= true; }
455         
456         // using $flexy->currentTemplate -> find the template we are looking at..
457         // then find the string for $flexy->options['locale']
458         //DB_DataObject::debugLevel(1);
459         if ($debug) { var_dump($string); }
460         
461         static $cache = array(); // cache of templates..
462         
463         $ff = HTML_FlexyFramework::get();
464         $view_name = isset($ff->Pman_Cms['view_name']) ? $ff->Pman_Cms['view_name'] : false;
465         
466         if ($debug) { var_dump(array('view_name'=> $view_name)); }
467         
468         $tempdir = '';
469         foreach($flexy->options['templateDir'] as $td) {
470             if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
471                 $tempdir = $td;
472                 break;
473             }
474         }
475         
476         
477         $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
478         
479         if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
480             if ($debug) { echo "from cache no match - $string\n"; }
481             return $string;
482         }
483         
484         if (!isset($cache[$tmpname])) {
485                 
486             
487             
488             $tmpl = DB_DataObject::factory('cms_template');
489             if ($view_name !== false) {
490                 $tmpl->view_name = $view_name;
491             }
492             if(!$tmpl->get('template', $tmpname)){
493                 // strip of site prefix if set...
494                  
495                 $tmpl = DB_DataObject::factory('cms_template');
496                 if(!$tmpl->get('template', $tmpname)){
497                     //var_dump("no template? {$tmpname} or {$relpath}" );
498                     $cache[$tmpname] = false;
499                     if ($debug) { echo "no template found - no match - $string\n"; }
500                     return $string;
501                 }
502             }
503             $cache[$tmpname] = $tmpl;
504         } else {
505             $tmpl = $cache[$tmpname] ;
506         }
507          
508     
509         //get original template id 
510         $orig = DB_DataObject::factory($this->tableName());
511         $orig->lang = '';
512         $orig->template_id = $tmpl->id;
513         $orig->active = 1;
514         if(!$orig->get( 'mdsum' , md5(trim($string)))){
515              //var_dump('no text? '. $string);
516             if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
517             return false;
518         }
519          
520         //find out the text by language
521         $x = DB_DataObject::factory($this->tableName());
522         $x->lang = $flexy->options['locale'];
523         $x->template_id = $tmpl->id;
524         if(!$x->get('src_id', $orig->id)){
525             //var_dump('no trans found' . $orig->id);
526             if ($debug) { echo "no translation found\n"; }
527             return false;
528         }
529         if ($debug) { echo "returning $x->txt\n"; }
530         //var_Dump($x->txt);
531         return empty($x->txt) ? $string : $x->txt;
532     }
533     function translateChanged($flexy)
534     {
535         //return true;
536         // var_dump('check changed?');
537         //DB_DataObject::debugLevel(1);
538         //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
539         
540         //var_dump($flexy->compiledTemplate);
541         $utime = file_exists($flexy->compiledTemplate) ?  filemtime( $flexy->compiledTemplate) : 0;
542         
543         
544         static $cache = array(); // cache of templates..
545         
546         $ff = HTML_FlexyFramework::get();
547         $view_name = isset($ff->Pman_Cms['view_name']) ? $ff->Pman_Cms['view_name'] : false;
548         
549         $tempdir = '';
550         foreach($flexy->options['templateDir'] as $td) {
551             if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
552                 $tempdir = $td;
553                 break;
554             }
555         }
556         
557         
558         $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
559         
560         if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
561             return false;
562         }
563         
564         if (!isset($cache[$tmpname])) {
565                 
566             
567             
568             $tmpl = DB_DataObject::factory('cms_template');
569             if ($view_name !== false) {
570                 $tmpl->view_name = $view_name;
571             }
572             if(!$tmpl->get('template', $tmpname)){
573                 // strip of site prefix if set...
574                  
575                 $tmpl = DB_DataObject::factory('cms_template');
576                 if(!$tmpl->get('template', $tmpname)){
577                     //var_dump("no template? {$tmpname} or {$relpath}" );
578                     $cache[$tmpname] = false;
579                     return false;
580                 }
581             }
582             $cache[$tmpname] = $tmpl;
583         } else {
584             $tmpl = $cache[$tmpname] ;
585         }
586           
587          
588         
589         $x = DB_DataObject::factory($this->tableName());
590         $x->lang = $flexy->options['locale'];
591         $x->active = 1;
592         $x->template_id = $tmpl->id;
593         $x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
594         
595         return $x->count() ? true : false;
596         
597         
598     }
599     
600 }