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 = DB_DataObject::factory($this->tableName());
267 $t->template_id = $tmpl->id;
268 $t->whereAdd("lang = ''");
271 // we have a situation where old md sums where created..
275 $cur = $t->fetchAll('mdsum', 'id');
283 // now loop through current..
284 $cwords = array();// not in used??
286 // echo "sync Template Words... \n";
289 foreach($words as $k=>$v) {
294 $md = $keyvalue ? $k : md5($v);
296 // check to see if there are more that one versions of that md5
299 if (!isset($cur[$md])) {
300 // create a record for it..
301 $t = DB_DataObject::factory($this->tableName());
304 'lang' => '',// by default should a english
305 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
306 'template_id'=>$tmpl->id,
311 $active[] = $t->insert();
314 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
316 $active[] = $cur[$md];
318 // we have it already? -
319 $tt->query("UPDATE {$this->tableName()}
332 if (count(array_values($cur))) {// de-active unused
334 $t = DB_DataObject::factory($this->tableName());
335 // echo "de-active current?? \n";
338 $deactive = array_values($cur);
339 $t->query("UPDATE core_templatestr
340 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
344 // delete all the items that are not relivant.
345 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
346 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
347 $t = DB_DataObject::factory($this->tableName());
349 // this will active the child data
350 if (empty($active)) {// set the active array to empty
353 $t->query("UPDATE core_templatestr
356 src_id IN (". implode(',' ,$active) . ")
358 template_id = {$tmpl->id}
360 //deactive the child data
361 if (empty($deactive)) {
362 $deactive = array(-1);
364 $t->query("UPDATE core_templatestr
367 src_id IN (". implode(',' ,$deactive) . ")
369 template_id = {$tmpl->id}
375 function checkDupes($tid, $lang, $id, $mdsum) {
377 $t = DB_DataObject::factory($this->tableName());
378 $t->template_id = $tid;
380 $t->whereAdd("lang = '{$lang}'");
381 if ($t->count() == 1) {
382 return $id; // only got one ... no issues..
385 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
387 //DB_DataObject::debugLevel(1);
388 // find out if any of them have got translations.
389 $ids = $t->fetchAll('id');
392 $t = DB_DataObject::factory($this->tableName());
393 $t->whereAddIn('src_id', $ids, 'int');
394 $t->whereAdd("txt != ''");
395 $t->orderBy('updated DESC');
398 // do any translations exist?
404 // delete all the others...
405 $t = DB_DataObject::factory($this->tableName());
406 $t->whereAddIn('src_id', $ids, 'int');
407 $t->whereAdd("src_id != $id");
411 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
414 $t = DB_DataObject::factory($this->tableName());
415 $t->whereAddIn('id', $ids, 'int');
416 $t->whereAdd("id != $id");
420 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
423 // this is done by calling code
424 //$t = DB_DataObject::factory($this->tableName());
425 //$t->query("update core_templatestr set active= 1 where src_id = $id");
436 function syncLang($lang)
438 // bugs with our old code...
440 $tn = $this->tableName();
441 $t = DB_DataObject::factory($tn);
442 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
444 // find all the id's from lang that have not been generated..
447 $t = DB_DataObject::factory($tn);
448 $t->whereAdd("lang = ''");
451 //old code, this did not support the on_table
452 // $id_tmp = $t->fetchAll('id','template_id');
453 // $ids = array_keys($id_tmp);
455 //new code for support the sync tables
456 foreach($t->fetchAll() as $ori){
457 $id_tmp[$ori->id] = $ori;
459 $ids = array_keys($id_tmp);
461 // matching by language:
462 $t = DB_DataObject::factory($tn);
463 $t->whereAddIn('src_id', $ids , 'int');
466 $got = $t->fetchAll('src_id');
467 $missing = array_diff($ids, $got);
468 foreach($missing as $id) {
470 $t = DB_DataObject::factory($tn);
475 'updated' => date('Y-m-d H:i:s', strtotime("NOW")),
476 'template_id'=> $id_tmp[$id]->template_id,
477 'on_table' => $id_tmp[$id]->on_table,
478 'on_id' => $id_tmp[$id]->on_id,
479 'on_col' => $id_tmp[$id]->on_col,
490 // called from flexy to translate a string.
493 function translateFlexyString($flexy, $string)
496 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
498 // using $flexy->currentTemplate -> find the template we are looking at..
499 // then find the string for $flexy->options['locale']
500 //DB_DataObject::debugLevel(1);
501 if ($debug) { var_dump($string); }
503 static $cache = array(); // cache of templates..
505 $ff = HTML_FlexyFramework::get();
506 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
507 if (empty($view_name)) {
508 $pg = HTML_FlexyFramework::get()->page;
509 if (isset($pg->templateViewName)) {
510 $view_name = $pg->templateViewName;
517 if ($debug) { var_dump(array('view_name'=> $view_name)); }
520 foreach($flexy->options['templateDir'] as $td) {
521 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
528 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
531 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
532 if ($debug) { echo "from cache no match - $string\n"; }
536 if (!isset($cache[$tmpname])) {
540 $tmpl = DB_DataObject::factory('core_template');
541 if ($view_name !== false) {
542 $tmpl->view_name = $view_name;
544 if(!$tmpl->get('template', $tmpname)){
545 // strip of site prefix if set...
547 $tmpl = DB_DataObject::factory('core_template');
548 if(!$tmpl->get('template', $tmpname)){
549 //var_dump("no template? {$tmpname} or {$relpath}" );
550 $cache[$tmpname] = false;
551 if ($debug) { echo "no template found - no match - $string\n"; }
555 $cache[$tmpname] = $tmpl;
557 $tmpl = $cache[$tmpname] ;
562 //get original template id
564 $orig = DB_DataObject::factory($this->tableName());
566 $orig->template_id = $tmpl->id;
569 $cache[$tmpname]->words =
571 if(!$orig->get( 'mdsum' , md5(trim($string)))){
572 //var_dump('no text? '. $string);
573 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
578 if (empty($cache[$tmpname]->translations)) {
580 //find out the text by language
581 //DB_DataObject::DebugLevel(1);
582 $x = DB_DataObject::factory($this->tableName());
583 $x->lang = $flexy->options['locale'];
584 $x->template_id = $tmpl->id;
586 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
587 if (empty($cache[$tmpname]->translations)) {
588 $cache[$tmpname]->translations = true;
590 //var_Dump($cache[$tmpname]->translations);
594 if ($cache[$tmpname]->translations === true) {
597 //var_dump("Checking: " . md5(trim($string)));
599 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
600 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
601 return $cache[$tmpname]->translations [md5(trim($string))];
608 function translateChanged($flexy)
611 $date = $this->lastUpdated($flexy);
612 if ($date === false) {
615 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
616 return strtotime($date) > $utime;
619 // determine if a complied template need recompling
621 function lastUpdated($flexy)
624 // var_dump('check changed?');
625 //DB_DataObject::debugLevel(1);
626 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
628 //var_dump($flexy->compiledTemplate);
629 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
632 static $cache = array(); // cache of templates..
634 $ff = HTML_FlexyFramework::get();
635 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
637 // find which of the template directories was actually used for the template.
640 foreach($flexy->options['templateDir'] as $td) {
641 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
648 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
650 // we do not have any record of this template..
651 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
655 if (!isset($cache[$tmpname])) {
659 $tmpl = DB_DataObject::factory('core_template');
660 if ($view_name !== false) {
661 $tmpl->view_name = $view_name;
663 if(!$tmpl->get('template', $tmpname)){
664 // strip of site prefix if set...
666 $tmpl = DB_DataObject::factory('core_template');
667 if(!$tmpl->get('template', $tmpname)){
668 //var_dump("no template? {$tmpname} or {$relpath}" );
669 $cache[$tmpname] = false;
673 $cache[$tmpname] = $tmpl;
675 $tmpl = $cache[$tmpname] ;
680 $x = DB_DataObject::factory($this->tableName());
681 $x->lang = $flexy->options['locale'];
683 $x->template_id = $tmpl->id;
684 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
685 if ($x->count() < 1) {
686 return false; // we don't have any record of it.
689 $x->selectAdd('max(updated) as max_updated');
691 return $x->max_updated;