3 * Table Definition for cms_templatestr
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
10 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
12 class Pman_Cms_DataObjects_Cms_templatestr extends DB_DataObject
15 /* the code below is auto generated do not remove the above tag */
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;
30 /* the code above is auto generated do not remove the tag below */
33 function beforeInsert($q,$roo)
35 if(!empty($q['_rescan'])){
36 $this->syncLang($q['_rescan']);
42 function applyFilters($q, $au, $roo)
44 if (!empty($q['_tree'])) {
45 $this->applyFiltersTree($q,$roo);
48 if(!empty($q['on_table']) && !is_numeric($q['template_id'])){
49 $this->template_id = 0;
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}%'");
57 function translateTableCol($obj, $col, $lang)
59 $cts = DB_DataObject::factory('cms_templatestr');
61 $cts->on_table = $obj->tableName();
62 $cts->on_id = $obj->pid();
64 if(!$cts->find(true)){
77 * insert the origanal table text
84 function onTableChange($roo, $obj, $chg)
87 $ff = HTML_FlexyFramework::get()->Pman_Cms;
89 if(empty($ff['DataObjects_Cms_templatestr']['tables'])){
92 $tn = $obj->tableName();
93 if(empty($ff['DataObjects_Cms_templatestr']['tables'][$tn])){
96 $cols = $ff['DataObjects_Cms_templatestr']['tables'][$tn];
99 foreach($cols as $c) {
100 $x = $this->factory($this->tableName());
101 $x->on_id = $obj->pid();
104 $x->lang = ''; /// eg. base language..
105 $up = $x->find(true);
106 if ($up && $x->txt == $obj->$c) {
107 continue; // update an no change..
112 $x->mdsum = md5($obj->$c);
114 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
115 $up ? $x->update() : $x->insert();
122 function applyFiltersTree($q,$roo)
124 if (empty($q['node'])) {
125 $roo->jerr("invalid node");
130 // DB_DataObject::debugLevel(1);
131 $x = DB_Dataobject::Factory($this->tableName());
133 $x->selectAdd('distinct(lang) as lang');
134 $x->whereAdd("lang != ''");
136 foreach( $x->fetchAll('lang') as $l) {
154 $x = DB_DataObject::factory($this->tableName());
156 $x->selectAdd('distinct(template_id) as template_id');
157 $x->lang = $q['node'];
158 $ids = $x->fetchAll('template_id');
161 //add the table type lists
163 $ff = HTML_FlexyFramework::get()->Pman_Cms;
165 if(!empty($ff['DataObjects_Cms_templatestr']['tables'])){
166 foreach($ff['DataObjects_Cms_templatestr']['tables'] as $table=>$v){
169 'on_table' => $table,
176 // $x->orderBy('template ASC');
177 // $x->whereAdd("lang != ''");
180 $xx = DB_Dataobject::Factory('cms_template');
181 $xx->whereAddIn('id', $ids, 'int');
185 id, concat(view_name,':', template) as template_name
187 $xx->orderBy('template_name ASC');
189 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
198 $roo->jerr("not yet");
210 * @param object $tmpl cms_template data object
211 * @param array $words array of words
213 function syncTemplateWords($tmpl, $keyvalue = false)
216 $words = $tmpl->words;
217 // mapping for template :
218 //tablename => $n (templatename)
219 //tableid => $k (key value)
220 //colname => $n (templatename)
223 //print_r($words);exit;
225 $tt = DB_DataObject::factory($this->tableName());
228 $t = DB_DataObject::factory($this->tableName());
229 $t->template_id = $tmpl->id;
230 $t->whereAdd("lang = ''");
233 // we have a situation where old md sums where created..
237 $cur = $t->fetchAll('mdsum', 'id');
245 // now loop through current..
246 $cwords = array();// not in used??
248 // echo "sync Template Words... \n";
251 foreach($words as $k=>$v) {
256 $md = $keyvalue ? $k : md5($v);
258 // check to see if there are more that one versions of that md5
261 if (!isset($cur[$md])) {
262 // create a record for it..
263 $t = DB_DataObject::factory($this->tableName());
266 'lang' => '',// by default should a english
267 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
268 'template_id'=>$tmpl->id,
273 $active[] = $t->insert();
276 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
278 $active[] = $cur[$md];
280 // we have it already? -
281 $tt->query("UPDATE {$this->tableName()}
294 if (count(array_values($cur))) {// de-active unused
296 $t = DB_DataObject::factory($this->tableName());
297 // echo "de-active current?? \n";
300 $deactive = array_values($cur);
301 $t->query("UPDATE cms_templatestr
302 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
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());
311 // this will active the child data
312 if (empty($active)) {// set the active array to empty
315 $t->query("UPDATE cms_templatestr
318 src_id IN (". implode(',' ,$active) . ")
320 template_id = {$tmpl->id}
322 //deactive the child data
323 if (empty($deactive)) {
324 $deactive = array(-1);
326 $t->query("UPDATE cms_templatestr
329 src_id IN (". implode(',' ,$deactive) . ")
331 template_id = {$tmpl->id}
336 function checkDupes($tid, $lang, $id, $mdsum) {
338 $t = DB_DataObject::factory($this->tableName());
339 $t->template_id = $tid;
341 $t->whereAdd("lang = '{$lang}'");
342 if ($t->count() == 1) {
343 return $id; // only got one ... no issues..
346 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
348 //DB_DataObject::debugLevel(1);
349 // find out if any of them have got translations.
350 $ids = $t->fetchAll('id');
353 $t = DB_DataObject::factory($this->tableName());
354 $t->whereAddIn('src_id', $ids, 'int');
355 $t->whereAdd("txt != ''");
356 $t->orderBy('updated DESC');
359 // do any translations exist?
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");
372 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
375 $t = DB_DataObject::factory($this->tableName());
376 $t->whereAddIn('id', $ids, 'int');
377 $t->whereAdd("id != $id");
381 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
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");
397 function syncLang($lang)
399 // bugs with our old code...
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 = ''");
405 // find all the id's from lang that have not been generated..
408 $t = DB_DataObject::factory($tn);
409 $t->whereAdd("lang = ''");
412 //old code, this did not support the on_table
413 // $id_tmp = $t->fetchAll('id','template_id');
414 // $ids = array_keys($id_tmp);
416 //new code for support the sync tables
417 foreach($t->fetchAll() as $ori){
418 $id_tmp[$ori->id] = $ori;
420 $ids = array_keys($id_tmp);
422 // matching by language:
423 $t = DB_DataObject::factory($tn);
424 $t->whereAddIn('src_id', $ids , 'int');
427 $got = $t->fetchAll('src_id');
428 $missing = array_diff($ids, $got);
429 foreach($missing as $id) {
431 $t = DB_DataObject::factory($tn);
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,
450 function translateFlexyString($flexy, $string)
454 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
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); }
461 static $cache = array(); // cache of templates..
463 $ff = HTML_FlexyFramework::get();
464 $view_name = isset($ff->Pman_Cms['view_name']) ? $ff->Pman_Cms['view_name'] : false;
466 if ($debug) { var_dump(array('view_name'=> $view_name)); }
469 foreach($flexy->options['templateDir'] as $td) {
470 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
477 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
479 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
480 if ($debug) { echo "from cache no match - $string\n"; }
484 if (!isset($cache[$tmpname])) {
488 $tmpl = DB_DataObject::factory('cms_template');
489 if ($view_name !== false) {
490 $tmpl->view_name = $view_name;
492 if(!$tmpl->get('template', $tmpname)){
493 // strip of site prefix if set...
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"; }
503 $cache[$tmpname] = $tmpl;
505 $tmpl = $cache[$tmpname] ;
509 //get original template id
510 $orig = DB_DataObject::factory($this->tableName());
512 $orig->template_id = $tmpl->id;
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"; }
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"; }
529 if ($debug) { echo "returning $x->txt\n"; }
531 return empty($x->txt) ? $string : $x->txt;
533 function translateChanged($flexy)
536 // var_dump('check changed?');
537 //DB_DataObject::debugLevel(1);
538 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
540 //var_dump($flexy->compiledTemplate);
541 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
544 static $cache = array(); // cache of templates..
546 $ff = HTML_FlexyFramework::get();
547 $view_name = isset($ff->Pman_Cms['view_name']) ? $ff->Pman_Cms['view_name'] : false;
550 foreach($flexy->options['templateDir'] as $td) {
551 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
558 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
560 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
564 if (!isset($cache[$tmpname])) {
568 $tmpl = DB_DataObject::factory('cms_template');
569 if ($view_name !== false) {
570 $tmpl->view_name = $view_name;
572 if(!$tmpl->get('template', $tmpname)){
573 // strip of site prefix if set...
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;
582 $cache[$tmpname] = $tmpl;
584 $tmpl = $cache[$tmpname] ;
589 $x = DB_DataObject::factory($this->tableName());
590 $x->lang = $flexy->options['locale'];
592 $x->template_id = $tmpl->id;
593 $x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
595 return $x->count() ? true : false;