3 * Table Definition for core_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_Core_DataObjects_Core_templatestr extends DB_DataObject
15 /* the code below is auto generated do not remove the above tag */
17 public $__table = 'core_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']) && (empty($q['template_id']) || !is_numeric($q['template_id']))){
49 $this->template_id = 0;
50 $this->whereAdd(" join_src_id_id.txt != ''");
52 if (!empty($q['_search_txt'])) {
53 $str = $this->escape($q['_search_txt']);
54 $this->whereAdd("core_templatestr.txt like '%{$str}%' OR join_src_id_id.txt like '%{$str}%'");
58 function translateTableCol($obj, $col, $lang)
60 $cts = DB_DataObject::factory('core_templatestr');
62 $cts->on_table = $obj->tableName();
63 $cts->on_id = $obj->pid();
65 if(!$cts->find(true)){
78 * insert the origanal table text
85 function onTableChange($roo, $obj, $chg)
88 $ff = HTML_FlexyFramework::get()->Pman_Core;
90 if(empty($ff['DataObjects_Core_templatestr']['tables'])){
93 $tn = $obj->tableName();
94 if(empty($ff['DataObjects_Core_templatestr']['tables'][$tn])){
97 $cols = $ff['DataObjects_Core_templatestr']['tables'][$tn];
100 foreach($cols as $c) {
101 $x = $this->factory($this->tableName());
102 $x->on_id = $obj->pid();
105 $x->lang = ''; /// eg. base language..
106 $up = $x->find(true);
107 if ($up && $x->txt == $obj->$c) {
108 continue; // update an no change..
113 $x->mdsum = md5($obj->$c);
115 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
116 $up ? $x->update() : $x->insert();
123 function applyFiltersTree($q,$roo)
125 if (empty($q['node'])) {
126 $roo->jerr("invalid node");
130 case ($q['node'] == 'transtree'):
131 case ($q['node'] == 'langlist'):
132 // DB_DataObject::debugLevel(1);
133 $x = DB_Dataobject::Factory($this->tableName());
135 $x->selectAdd('distinct(lang) as lang');
137 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
138 $x->whereAdd("lang != ''");
140 foreach( $x->fetchAll() as $l) {
142 'text'=>$l->lang_name,
143 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
156 case preg_match('/^lang:/', $q['node']):
158 $lang = preg_replace('/^lang:/', '', $q['node']);
160 $x = DB_DataObject::factory('core_templatestr');
163 $x->selectAdd('distinct(view_name) as view_name');
165 $x->whereAdd('join_template_id_id.is_deleted = 0');
166 $x->orderBy('view_name DESC');
170 'text'=> $x->view_name,
171 'id' => 'view:'. $lang .':'. $x->view_name,
176 $ff = HTML_FlexyFramework::get()->Pman_Core;
178 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
179 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
182 'on_table' => $table,
183 'id' => 'table:'. $lang .':'. $table,
195 case preg_match('/^view:/', $q['node']):
198 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
200 $x = DB_DataObject::factory($this->tableName());
203 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
204 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
205 $x->whereAdd('join_template_id_id.is_deleted = 0');
207 $ids = $x->fetchAll('template_id');
210 //add the table type lists
212 $ff = HTML_FlexyFramework::get()->Pman_Core;
215 // $x->orderBy('template ASC');
216 // $x->whereAdd("lang != ''");
219 $xx = DB_Dataobject::Factory('core_template');
220 $xx->whereAddIn('id', $ids, 'int');
224 id, concat( template) as template_name
226 $xx->orderBy('template_name ASC');
228 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
248 * @param object $tmpl core_template data object
249 * @param array $words array of words
251 function syncTemplateWords($tmpl, $keyvalue = false)
254 $words = $tmpl->words;
255 // mapping for template :
256 //tablename => $n (templatename)
257 //tableid => $k (key value)
258 //colname => $n (templatename)
261 //print_r($words);exit;
263 $tt = DB_DataObject::factory($this->tableName());
266 $t->template_id = $tmpl->id;
267 $t->whereAdd("lang = ''");
270 // we have a situation where old md sums where created..
274 $cur = $t->fetchAll('mdsum', 'id');
282 // now loop through current..
283 $cwords = array();// not in used??
285 // echo "sync Template Words... \n";
288 foreach($words as $k=>$v) {
293 $md = $keyvalue ? $k : md5($v);
295 // check to see if there are more that one versions of that md5
298 if (!isset($cur[$md])) {
299 // create a record for it..
300 $t = DB_DataObject::factory($this->tableName());
303 'lang' => '',// by default should a english
304 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
305 'template_id'=>$tmpl->id,
310 $active[] = $t->insert();
313 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
315 $active[] = $cur[$md];
317 // we have it already? -
318 $tt->query("UPDATE {$this->tableName()}
331 if (count(array_values($cur))) {// de-active unused
333 $t = DB_DataObject::factory($this->tableName());
334 // echo "de-active current?? \n";
337 $deactive = array_values($cur);
338 $t->query("UPDATE core_templatestr
339 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
343 // delete all the items that are not relivant.
344 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
345 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
346 $t = DB_DataObject::factory($this->tableName());
348 // this will active the child data
349 if (empty($active)) {// set the active array to empty
352 $t->query("UPDATE core_templatestr
355 src_id IN (". implode(',' ,$active) . ")
357 template_id = {$tmpl->id}
359 //deactive the child data
360 if (empty($deactive)) {
361 $deactive = array(-1);
363 $t->query("UPDATE core_templatestr
366 src_id IN (". implode(',' ,$deactive) . ")
368 template_id = {$tmpl->id}
374 function checkDupes($tid, $lang, $id, $mdsum) {
376 $t = DB_DataObject::factory($this->tableName());
377 $t->template_id = $tid;
379 $t->whereAdd("lang = '{$lang}'");
380 if ($t->count() == 1) {
381 return $id; // only got one ... no issues..
384 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
386 //DB_DataObject::debugLevel(1);
387 // find out if any of them have got translations.
388 $ids = $t->fetchAll('id');
391 $t = DB_DataObject::factory($this->tableName());
392 $t->whereAddIn('src_id', $ids, 'int');
393 $t->whereAdd("txt != ''");
394 $t->orderBy('updated DESC');
397 // do any translations exist?
403 // delete all the others...
404 $t = DB_DataObject::factory($this->tableName());
405 $t->whereAddIn('src_id', $ids, 'int');
406 $t->whereAdd("src_id != $id");
410 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
413 $t = DB_DataObject::factory($this->tableName());
414 $t->whereAddIn('id', $ids, 'int');
415 $t->whereAdd("id != $id");
419 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
422 // this is done by calling code
423 //$t = DB_DataObject::factory($this->tableName());
424 //$t->query("update core_templatestr set active= 1 where src_id = $id");
435 function syncLang($lang)
437 // bugs with our old code...
439 $tn = $this->tableName();
440 $t = DB_DataObject::factory($tn);
441 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
443 // find all the id's from lang that have not been generated..
446 $t = DB_DataObject::factory($tn);
447 $t->whereAdd("lang = ''");
450 //old code, this did not support the on_table
451 // $id_tmp = $t->fetchAll('id','template_id');
452 // $ids = array_keys($id_tmp);
454 //new code for support the sync tables
455 foreach($t->fetchAll() as $ori){
456 $id_tmp[$ori->id] = $ori;
458 $ids = array_keys($id_tmp);
460 // matching by language:
461 $t = DB_DataObject::factory($tn);
462 $t->whereAddIn('src_id', $ids , 'int');
465 $got = $t->fetchAll('src_id');
466 $missing = array_diff($ids, $got);
467 foreach($missing as $id) {
469 $t = DB_DataObject::factory($tn);
474 'updated' => date('Y-m-d H:i:s', strtotime("NOW")),
475 'template_id'=> $id_tmp[$id]->template_id,
476 'on_table' => $id_tmp[$id]->on_table,
477 'on_id' => $id_tmp[$id]->on_id,
478 'on_col' => $id_tmp[$id]->on_col,
489 // called from flexy to translate a string.
492 function translateFlexyString($flexy, $string)
495 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
497 // using $flexy->currentTemplate -> find the template we are looking at..
498 // then find the string for $flexy->options['locale']
499 //DB_DataObject::debugLevel(1);
500 if ($debug) { var_dump($string); }
502 static $cache = array(); // cache of templates..
504 $ff = HTML_FlexyFramework::get();
505 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
506 if (empty($view_name)) {
507 $pg = HTML_FlexyFramework::get()->page;
508 if (isset($pg->templateViewName)) {
509 $view_name = $pg->templateViewName;
516 if ($debug) { var_dump(array('view_name'=> $view_name)); }
519 foreach($flexy->options['templateDir'] as $td) {
520 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
527 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
530 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
531 if ($debug) { echo "from cache no match - $string\n"; }
535 if (!isset($cache[$tmpname])) {
539 $tmpl = DB_DataObject::factory('core_template');
540 if ($view_name !== false) {
541 $tmpl->view_name = $view_name;
543 if(!$tmpl->get('template', $tmpname)){
544 // strip of site prefix if set...
546 $tmpl = DB_DataObject::factory('core_template');
547 if(!$tmpl->get('template', $tmpname)){
548 //var_dump("no template? {$tmpname} or {$relpath}" );
549 $cache[$tmpname] = false;
550 if ($debug) { echo "no template found - no match - $string\n"; }
554 $cache[$tmpname] = $tmpl;
556 $tmpl = $cache[$tmpname] ;
561 //get original template id
563 $orig = DB_DataObject::factory($this->tableName());
565 $orig->template_id = $tmpl->id;
568 $cache[$tmpname]->words =
570 if(!$orig->get( 'mdsum' , md5(trim($string)))){
571 //var_dump('no text? '. $string);
572 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
577 if (empty($cache[$tmpname]->translations)) {
579 //find out the text by language
580 //DB_DataObject::DebugLevel(1);
581 $x = DB_DataObject::factory($this->tableName());
582 $x->lang = $flexy->options['locale'];
583 $x->template_id = $tmpl->id;
585 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
586 if (empty($cache[$tmpname]->translations)) {
587 $cache[$tmpname]->translations = true;
589 //var_Dump($cache[$tmpname]->translations);
593 if ($cache[$tmpname]->translations === true) {
596 //var_dump("Checking: " . md5(trim($string)));
598 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
599 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
600 return $cache[$tmpname]->translations [md5(trim($string))];
607 function translateChanged($flexy)
610 $date = $this->lastUpdated($flexy);
611 if ($date === false) {
614 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
615 return strtotime($date) > $utime;
618 // determine if a complied template need recompling
620 function lastUpdated($flexy)
623 // var_dump('check changed?');
624 //DB_DataObject::debugLevel(1);
625 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
627 //var_dump($flexy->compiledTemplate);
628 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
631 static $cache = array(); // cache of templates..
633 $ff = HTML_FlexyFramework::get();
634 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
636 // find which of the template directories was actually used for the template.
639 foreach($flexy->options['templateDir'] as $td) {
640 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
647 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
649 // we do not have any record of this template..
650 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
654 if (!isset($cache[$tmpname])) {
658 $tmpl = DB_DataObject::factory('core_template');
659 if ($view_name !== false) {
660 $tmpl->view_name = $view_name;
662 if(!$tmpl->get('template', $tmpname)){
663 // strip of site prefix if set...
665 $tmpl = DB_DataObject::factory('core_template');
666 if(!$tmpl->get('template', $tmpname)){
667 //var_dump("no template? {$tmpname} or {$relpath}" );
668 $cache[$tmpname] = false;
672 $cache[$tmpname] = $tmpl;
674 $tmpl = $cache[$tmpname] ;
679 $x = DB_DataObject::factory($this->tableName());
680 $x->lang = $flexy->options['locale'];
682 $x->template_id = $tmpl->id;
683 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
684 if ($x->count() < 1) {
685 return false; // we don't have any record of it.
688 $x->selectAdd('max(updated) as max_updated');
690 return $x->max_updated;