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];
102 foreach($cols as $c) {
103 $x = $this->factory($this->tableName());
104 $x->on_id = $obj->pid();
107 $x->lang = ''; /// eg. base language..
108 $up = $x->find(true);
111 // deactivate empty words
112 if(empty($obj->$c)) {
113 $deactive[] = $x->id;
115 // activate non-empty words
120 if($x->txt == $obj->$c) {
121 continue; // skip when no change
126 if(empty($obj->$c)) {
134 $x->mdsum = md5($obj->$c);
136 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
137 $up ? $x->update() : $x->insert();
140 if(count($deactive)) {
141 $t = DB_DataObject::factory($this->tableName());
142 // deactivate the parent data
143 $t->query("UPDATE core_templatestr
144 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
147 // deactivate the child data
148 $t->query("UPDATE core_templatestr
151 src_id IN (". implode(',' ,$deactive) . ")
158 $t = DB_DataObject::factory($this->tableName());
159 // activate the aprent data
160 $t->query("UPDATE core_templatestr
161 SET active = 1 WHERE id in (" . implode(',' ,$active) . ")
163 // deactivate the child data
164 $t->query("UPDATE core_templatestr
167 src_id IN (". implode(',' ,$active) . ")
177 function applyFiltersTree($q,$roo)
179 if (empty($q['node'])) {
180 $roo->jerr("invalid node");
184 case ($q['node'] == 'transtree'):
185 case ($q['node'] == 'langlist'):
186 // DB_DataObject::debugLevel(1);
187 $x = DB_Dataobject::Factory($this->tableName());
189 $x->selectAdd('distinct(lang) as lang');
191 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
192 $x->whereAdd("lang != ''");
194 foreach( $x->fetchAll() as $l) {
196 'text'=>$l->lang_name,
197 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
210 case preg_match('/^lang:/', $q['node']):
212 $lang = preg_replace('/^lang:/', '', $q['node']);
214 $x = DB_DataObject::factory('core_templatestr');
217 $x->selectAdd('distinct(view_name) as view_name');
219 $x->whereAdd('join_template_id_id.is_deleted = 0');
220 $x->orderBy('view_name DESC');
224 'text'=> $x->view_name,
225 'id' => 'view:'. $lang .':'. $x->view_name,
230 $ff = HTML_FlexyFramework::get()->Pman_Core;
232 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
233 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
236 'on_table' => $table,
237 'id' => 'table:'. $lang .':'. $table,
249 case preg_match('/^view:/', $q['node']):
252 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
254 $x = DB_DataObject::factory($this->tableName());
257 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
258 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
259 $x->whereAdd('join_template_id_id.is_deleted = 0');
261 $ids = $x->fetchAll('template_id');
264 //add the table type lists
266 $ff = HTML_FlexyFramework::get()->Pman_Core;
269 // $x->orderBy('template ASC');
270 // $x->whereAdd("lang != ''");
273 $xx = DB_Dataobject::Factory('core_template');
274 $xx->whereAddIn('id', $ids, 'int');
278 id, concat( template) as template_name
280 $xx->orderBy('template_name ASC');
282 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
302 * @param object $tmpl core_template data object
303 * @param array $words array of words
305 function syncTemplateWords($tmpl, $keyvalue = false)
308 $words = $tmpl->words;
309 // mapping for template :
310 //tablename => $n (templatename)
311 //tableid => $k (key value)
312 //colname => $n (templatename)
315 //print_r($words);exit;
317 $tt = DB_DataObject::factory($this->tableName());
320 $t->template_id = $tmpl->id;
321 $t->whereAdd("lang = ''");
324 // we have a situation where old md sums where created..
328 $cur = $t->fetchAll('mdsum', 'id');
336 // now loop through current..
337 $cwords = array();// not in used??
339 // echo "sync Template Words... \n";
342 foreach($words as $k=>$v) {
352 $md = $keyvalue ? $k : md5($v);
354 // check to see if there are more that one versions of that md5
357 if (!isset($cur[$md])) {
358 // create a record for it..
359 $t = DB_DataObject::factory($this->tableName());
362 'lang' => '',// by default should a english
363 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
364 'template_id'=>$tmpl->id,
369 $active[] = $t->insert();
372 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
374 $active[] = $cur[$md];
376 // we have it already? -
377 $t = DB_DataObject::factory($this->tableName());
378 $t->query("UPDATE {$this->tableName()}
391 if (count(array_values($cur))) {// de-active unused
393 $t = DB_DataObject::factory($this->tableName());
394 // echo "de-active current?? \n";
397 $deactive = array_values($cur);
398 $t->query("UPDATE core_templatestr
399 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
403 // delete all the items that are not relivant.
404 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
405 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
406 $t = DB_DataObject::factory($this->tableName());
408 // this will active the child data
409 if (empty($active)) {// set the active array to empty
412 $t->query("UPDATE core_templatestr
415 src_id IN (". implode(',' ,$active) . ")
417 template_id = {$tmpl->id}
419 //deactive the child data
420 if (empty($deactive)) {
421 $deactive = array(-1);
423 $t->query("UPDATE core_templatestr
426 src_id IN (". implode(',' ,$deactive) . ")
428 template_id = {$tmpl->id}
434 function checkDupes($tid, $lang, $id, $mdsum) {
436 $t = DB_DataObject::factory($this->tableName());
437 $t->template_id = $tid;
439 $t->whereAdd("lang = '{$lang}'");
440 if ($t->count() == 1) {
441 return $id; // only got one ... no issues..
444 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
446 //DB_DataObject::debugLevel(1);
447 // find out if any of them have got translations.
448 $ids = $t->fetchAll('id');
451 $t = DB_DataObject::factory($this->tableName());
452 $t->whereAddIn('src_id', $ids, 'int');
453 $t->whereAdd("txt != ''");
454 $t->orderBy('updated DESC');
457 // do any translations exist?
463 // delete all the others...
464 $t = DB_DataObject::factory($this->tableName());
465 $t->whereAddIn('src_id', $ids, 'int');
466 $t->whereAdd("src_id != $id");
470 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
473 $t = DB_DataObject::factory($this->tableName());
474 $t->whereAddIn('id', $ids, 'int');
475 $t->whereAdd("id != $id");
479 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
482 // this is done by calling code
483 //$t = DB_DataObject::factory($this->tableName());
484 //$t->query("update core_templatestr set active= 1 where src_id = $id");
495 function syncLang($lang)
497 // bugs with our old code...
499 $tn = $this->tableName();
500 $t = DB_DataObject::factory($tn);
501 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
503 // find all the id's from lang that have not been generated..
506 static $id_tmp = false;
508 if ($id_tmp == false) {
510 $t = DB_DataObject::factory($tn);
511 $t->whereAdd("lang = ''");
514 //old code, this did not support the on_table
515 // $id_tmp = $t->fetchAll('id','template_id');
516 // $ids = array_keys($id_tmp);
518 //new code for support the sync tables
519 foreach($t->fetchAll() as $ori){
520 $id_tmp[$ori->id] = $ori;
523 $ids = array_keys($id_tmp);
525 // matching by language:
526 $t = DB_DataObject::factory($tn);
527 $t->whereAddIn('src_id', $ids , 'int');
530 $got = $t->fetchAll('src_id');
531 $missing = array_diff($ids, $got);
533 if (empty($missing)) {
536 $t = DB_DataObject::factory($tn);
537 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
547 FROM core_templatestr
549 id IN (". implode(',', $missing) . ")
552 DB_DataObject::factory($tn)->query($q);
553 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
554 DB_DataObject::factory($tn)->query($q);
555 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
556 DB_DataObject::factory($tn)->query($q);
562 // called from flexy to translate a string.
565 function translateFlexyString($flexy, $string)
568 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
570 // using $flexy->currentTemplate -> find the template we are looking at..
571 // then find the string for $flexy->options['locale']
572 //DB_DataObject::debugLevel(1);
573 if ($debug) { var_dump($string); }
575 static $cache = array(); // cache of templates..
577 $ff = HTML_FlexyFramework::get();
578 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
579 if (empty($view_name)) {
580 $pg = HTML_FlexyFramework::get()->page;
581 if (isset($pg->templateViewName)) {
582 $view_name = $pg->templateViewName;
589 if ($debug) { var_dump(array('view_name'=> $view_name)); }
592 foreach($flexy->options['templateDir'] as $td) {
593 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
600 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
603 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
604 if ($debug) { echo "from cache no match - $string\n"; }
608 if (!isset($cache[$tmpname])) {
612 $tmpl = DB_DataObject::factory('core_template');
613 if ($view_name !== false) {
614 $tmpl->view_name = $view_name;
616 if(!$tmpl->get('template', $tmpname)){
617 // strip of site prefix if set...
619 $tmpl = DB_DataObject::factory('core_template');
620 if(!$tmpl->get('template', $tmpname)){
621 //var_dump("no template? {$tmpname} or {$relpath}" );
622 $cache[$tmpname] = false;
623 if ($debug) { echo "no template found - no match - $string\n"; }
627 $cache[$tmpname] = $tmpl;
629 $tmpl = $cache[$tmpname] ;
634 //get original template id
636 $orig = DB_DataObject::factory($this->tableName());
638 $orig->template_id = $tmpl->id;
641 $cache[$tmpname]->words =
643 if(!$orig->get( 'mdsum' , md5(trim($string)))){
644 //var_dump('no text? '. $string);
645 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
650 if (empty($cache[$tmpname]->translations)) {
652 //find out the text by language
653 //DB_DataObject::DebugLevel(1);
654 $x = DB_DataObject::factory($this->tableName());
655 $x->lang = $flexy->options['locale'];
656 $x->template_id = $tmpl->id;
658 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
659 if (empty($cache[$tmpname]->translations)) {
660 $cache[$tmpname]->translations = true;
662 //var_Dump($cache[$tmpname]->translations);
666 if ($cache[$tmpname]->translations === true) {
669 //var_dump("Checking: " . md5(trim($string)));
671 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
672 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
673 return $cache[$tmpname]->translations [md5(trim($string))];
680 function translateChanged($flexy)
683 $date = $this->lastUpdated($flexy);
684 if ($date === false) {
687 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
688 return strtotime($date) > $utime;
691 // determine if a complied template need recompling
693 function lastUpdated($flexy)
696 // var_dump('check changed?');
697 //DB_DataObject::debugLevel(1);
698 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
700 //var_dump($flexy->compiledTemplate);
701 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
704 static $cache = array(); // cache of templates..
706 $ff = HTML_FlexyFramework::get();
707 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
709 // find which of the template directories was actually used for the template.
712 foreach($flexy->options['templateDir'] as $td) {
713 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
720 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
722 // we do not have any record of this template..
723 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
727 if (!isset($cache[$tmpname])) {
731 $tmpl = DB_DataObject::factory('core_template');
732 if ($view_name !== false) {
733 $tmpl->view_name = $view_name;
735 if(!$tmpl->get('template', $tmpname)){
736 // strip of site prefix if set...
738 $tmpl = DB_DataObject::factory('core_template');
739 if(!$tmpl->get('template', $tmpname)){
740 //var_dump("no template? {$tmpname} or {$relpath}" );
741 $cache[$tmpname] = false;
745 $cache[$tmpname] = $tmpl;
747 $tmpl = $cache[$tmpname] ;
752 $x = DB_DataObject::factory($this->tableName());
753 $x->lang = $flexy->options['locale'];
755 $x->template_id = $tmpl->id;
756 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
757 if ($x->count() < 1) {
758 return false; // we don't have any record of it.
761 $x->selectAdd('max(updated) as max_updated');
763 return $x->max_updated;
768 function toRooArray($req) {
769 $ret = $this->toArray();
771 if (empty($req['csvCols'])) {
777 // translations for table columns
778 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
779 $ret['template_id_view_name'] = 'database';
780 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
786 function postListFilter($ar, $au, $req)
788 if (empty($req['csvCols'])) {
797 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
802 // translations for table columns
803 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
805 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
807 if(!empty($ret[$key])) {
814 $ret = array_values($ret);