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 $cfg = HTML_FlexyFramework::get()->Pman_Cms;
132 $defaults = isset($cfg['languages']) ? $cfg['languages'] : array();
134 $x = DB_Dataobject::Factory($this->tableName());
136 $x->selectAdd('distinct(lang) as lang');
137 $x->whereAdd("lang != ''");
140 foreach( $x->fetchAll('lang') as $l) {
149 foreach($defaults as $l) {
150 if (in_array($l,$got)) {
170 $x = DB_DataObject::factory($this->tableName());
173 $x->selectAdd('distinct('.$this->tableName(). '.template_id) as template_id');
174 $x->whereAdd('join_template_id_id.is_deleted = 0');
175 $x->lang = $q['node'];
176 $ids = $x->fetchAll('template_id');
179 //add the table type lists
181 $ff = HTML_FlexyFramework::get()->Pman_Cms;
183 if(!empty($ff['DataObjects_Cms_templatestr']['tables'])){
184 foreach($ff['DataObjects_Cms_templatestr']['tables'] as $table=>$v){
187 'on_table' => $table,
194 // $x->orderBy('template ASC');
195 // $x->whereAdd("lang != ''");
198 $xx = DB_Dataobject::Factory('cms_template');
199 $xx->whereAddIn('id', $ids, 'int');
203 id, concat(view_name,':', template) as template_name
205 $xx->orderBy('template_name ASC');
207 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
216 $roo->jerr("not yet");
228 * @param object $tmpl cms_template data object
229 * @param array $words array of words
231 function syncTemplateWords($tmpl, $keyvalue = false)
234 $words = $tmpl->words;
235 // mapping for template :
236 //tablename => $n (templatename)
237 //tableid => $k (key value)
238 //colname => $n (templatename)
241 //print_r($words);exit;
243 $tt = DB_DataObject::factory($this->tableName());
246 $t = DB_DataObject::factory($this->tableName());
247 $t->template_id = $tmpl->id;
248 $t->whereAdd("lang = ''");
251 // we have a situation where old md sums where created..
255 $cur = $t->fetchAll('mdsum', 'id');
263 // now loop through current..
264 $cwords = array();// not in used??
266 // echo "sync Template Words... \n";
269 foreach($words as $k=>$v) {
274 $md = $keyvalue ? $k : md5($v);
276 // check to see if there are more that one versions of that md5
279 if (!isset($cur[$md])) {
280 // create a record for it..
281 $t = DB_DataObject::factory($this->tableName());
284 'lang' => '',// by default should a english
285 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
286 'template_id'=>$tmpl->id,
291 $active[] = $t->insert();
294 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
296 $active[] = $cur[$md];
298 // we have it already? -
299 $tt->query("UPDATE {$this->tableName()}
312 if (count(array_values($cur))) {// de-active unused
314 $t = DB_DataObject::factory($this->tableName());
315 // echo "de-active current?? \n";
318 $deactive = array_values($cur);
319 $t->query("UPDATE cms_templatestr
320 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
324 // delete all the items that are not relivant.
325 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
326 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
327 $t = DB_DataObject::factory($this->tableName());
329 // this will active the child data
330 if (empty($active)) {// set the active array to empty
333 $t->query("UPDATE cms_templatestr
336 src_id IN (". implode(',' ,$active) . ")
338 template_id = {$tmpl->id}
340 //deactive the child data
341 if (empty($deactive)) {
342 $deactive = array(-1);
344 $t->query("UPDATE cms_templatestr
347 src_id IN (". implode(',' ,$deactive) . ")
349 template_id = {$tmpl->id}
354 function checkDupes($tid, $lang, $id, $mdsum) {
356 $t = DB_DataObject::factory($this->tableName());
357 $t->template_id = $tid;
359 $t->whereAdd("lang = '{$lang}'");
360 if ($t->count() == 1) {
361 return $id; // only got one ... no issues..
364 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
366 //DB_DataObject::debugLevel(1);
367 // find out if any of them have got translations.
368 $ids = $t->fetchAll('id');
371 $t = DB_DataObject::factory($this->tableName());
372 $t->whereAddIn('src_id', $ids, 'int');
373 $t->whereAdd("txt != ''");
374 $t->orderBy('updated DESC');
377 // do any translations exist?
383 // delete all the others...
384 $t = DB_DataObject::factory($this->tableName());
385 $t->whereAddIn('src_id', $ids, 'int');
386 $t->whereAdd("src_id != $id");
390 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
393 $t = DB_DataObject::factory($this->tableName());
394 $t->whereAddIn('id', $ids, 'int');
395 $t->whereAdd("id != $id");
399 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
402 // this is done by calling code
403 //$t = DB_DataObject::factory($this->tableName());
404 //$t->query("update cms_templatestr set active= 1 where src_id = $id");
415 function syncLang($lang)
417 // bugs with our old code...
419 $tn = $this->tableName();
420 $t = DB_DataObject::factory($tn);
421 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
423 // find all the id's from lang that have not been generated..
426 $t = DB_DataObject::factory($tn);
427 $t->whereAdd("lang = ''");
430 //old code, this did not support the on_table
431 // $id_tmp = $t->fetchAll('id','template_id');
432 // $ids = array_keys($id_tmp);
434 //new code for support the sync tables
435 foreach($t->fetchAll() as $ori){
436 $id_tmp[$ori->id] = $ori;
438 $ids = array_keys($id_tmp);
440 // matching by language:
441 $t = DB_DataObject::factory($tn);
442 $t->whereAddIn('src_id', $ids , 'int');
445 $got = $t->fetchAll('src_id');
446 $missing = array_diff($ids, $got);
447 foreach($missing as $id) {
449 $t = DB_DataObject::factory($tn);
454 'updated' => date('Y-m-d H:i:s', strtotime("NOW")),
455 'template_id'=> $id_tmp[$id]->template_id,
456 'on_table' => $id_tmp[$id]->on_table,
457 'on_id' => $id_tmp[$id]->on_id,
458 'on_col' => $id_tmp[$id]->on_col,
468 function translateFlexyString($flexy, $string)
472 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
474 // using $flexy->currentTemplate -> find the template we are looking at..
475 // then find the string for $flexy->options['locale']
476 //DB_DataObject::debugLevel(1);
477 if ($debug) { var_dump($string); }
479 static $cache = array(); // cache of templates..
481 $ff = HTML_FlexyFramework::get();
482 $view_name = isset($ff->Pman_Cms['view_name']) ? $ff->Pman_Cms['view_name'] : false;
484 if ($debug) { var_dump(array('view_name'=> $view_name)); }
487 foreach($flexy->options['templateDir'] as $td) {
488 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
495 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
497 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
498 if ($debug) { echo "from cache no match - $string\n"; }
502 if (!isset($cache[$tmpname])) {
506 $tmpl = DB_DataObject::factory('cms_template');
507 if ($view_name !== false) {
508 $tmpl->view_name = $view_name;
510 if(!$tmpl->get('template', $tmpname)){
511 // strip of site prefix if set...
513 $tmpl = DB_DataObject::factory('cms_template');
514 if(!$tmpl->get('template', $tmpname)){
515 //var_dump("no template? {$tmpname} or {$relpath}" );
516 $cache[$tmpname] = false;
517 if ($debug) { echo "no template found - no match - $string\n"; }
521 $cache[$tmpname] = $tmpl;
523 $tmpl = $cache[$tmpname] ;
527 //get original template id
528 $orig = DB_DataObject::factory($this->tableName());
530 $orig->template_id = $tmpl->id;
532 if(!$orig->get( 'mdsum' , md5(trim($string)))){
533 //var_dump('no text? '. $string);
534 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
538 //find out the text by language
539 $x = DB_DataObject::factory($this->tableName());
540 $x->lang = $flexy->options['locale'];
541 $x->template_id = $tmpl->id;
542 if(!$x->get('src_id', $orig->id)){
543 //var_dump('no trans found' . $orig->id);
544 if ($debug) { echo "no translation found\n"; }
547 if ($debug) { echo "returning $x->txt\n"; }
549 return empty($x->txt) ? $string : $x->txt;
551 function translateChanged($flexy)
554 // var_dump('check changed?');
555 //DB_DataObject::debugLevel(1);
556 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
558 //var_dump($flexy->compiledTemplate);
559 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
562 static $cache = array(); // cache of templates..
564 $ff = HTML_FlexyFramework::get();
565 $view_name = isset($ff->Pman_Cms['view_name']) ? $ff->Pman_Cms['view_name'] : false;
568 foreach($flexy->options['templateDir'] as $td) {
569 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
576 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
578 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
582 if (!isset($cache[$tmpname])) {
586 $tmpl = DB_DataObject::factory('cms_template');
587 if ($view_name !== false) {
588 $tmpl->view_name = $view_name;
590 if(!$tmpl->get('template', $tmpname)){
591 // strip of site prefix if set...
593 $tmpl = DB_DataObject::factory('cms_template');
594 if(!$tmpl->get('template', $tmpname)){
595 //var_dump("no template? {$tmpname} or {$relpath}" );
596 $cache[$tmpname] = false;
600 $cache[$tmpname] = $tmpl;
602 $tmpl = $cache[$tmpname] ;
607 $x = DB_DataObject::factory($this->tableName());
608 $x->lang = $flexy->options['locale'];
610 $x->template_id = $tmpl->id;
611 $x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
613 return $x->count() ? true : false;