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);
109 if ($up && $x->txt == $obj->$c) {
111 // deactivate empty words
112 if(empty($obj->$c)) {
113 $deactive[] = $x->id;
121 if(empty($obj->$c)) {
127 $x->mdsum = md5($obj->$c);
129 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
130 $up ? $x->update() : $x->insert();
133 if(count($deactive)) {
134 $t = DB_DataObject::factory($this->tableName());
135 // deactivate the parent data
136 $t->query("UPDATE core_templatestr
137 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
140 // deactivate the child data
141 $t->query("UPDATE core_templatestr
144 src_id IN (". implode(',' ,$deactive) . ")
151 $t = DB_DataObject::factory($this->tableName());
152 $t->query("UPDATE core_templatestr
153 SET active = 1 WHERE id in (" . implode(',' ,$active) . ")
156 $t->query("UPDATE core_templatestr
159 src_id IN (". implode(',' ,$active) . ")
161 template_id = {$tmpl->id}
169 function applyFiltersTree($q,$roo)
171 if (empty($q['node'])) {
172 $roo->jerr("invalid node");
176 case ($q['node'] == 'transtree'):
177 case ($q['node'] == 'langlist'):
178 // DB_DataObject::debugLevel(1);
179 $x = DB_Dataobject::Factory($this->tableName());
181 $x->selectAdd('distinct(lang) as lang');
183 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
184 $x->whereAdd("lang != ''");
186 foreach( $x->fetchAll() as $l) {
188 'text'=>$l->lang_name,
189 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
202 case preg_match('/^lang:/', $q['node']):
204 $lang = preg_replace('/^lang:/', '', $q['node']);
206 $x = DB_DataObject::factory('core_templatestr');
209 $x->selectAdd('distinct(view_name) as view_name');
211 $x->whereAdd('join_template_id_id.is_deleted = 0');
212 $x->orderBy('view_name DESC');
216 'text'=> $x->view_name,
217 'id' => 'view:'. $lang .':'. $x->view_name,
222 $ff = HTML_FlexyFramework::get()->Pman_Core;
224 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
225 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
228 'on_table' => $table,
229 'id' => 'table:'. $lang .':'. $table,
241 case preg_match('/^view:/', $q['node']):
244 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
246 $x = DB_DataObject::factory($this->tableName());
249 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
250 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
251 $x->whereAdd('join_template_id_id.is_deleted = 0');
253 $ids = $x->fetchAll('template_id');
256 //add the table type lists
258 $ff = HTML_FlexyFramework::get()->Pman_Core;
261 // $x->orderBy('template ASC');
262 // $x->whereAdd("lang != ''");
265 $xx = DB_Dataobject::Factory('core_template');
266 $xx->whereAddIn('id', $ids, 'int');
270 id, concat( template) as template_name
272 $xx->orderBy('template_name ASC');
274 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
294 * @param object $tmpl core_template data object
295 * @param array $words array of words
297 function syncTemplateWords($tmpl, $keyvalue = false)
300 $words = $tmpl->words;
301 // mapping for template :
302 //tablename => $n (templatename)
303 //tableid => $k (key value)
304 //colname => $n (templatename)
307 //print_r($words);exit;
309 $tt = DB_DataObject::factory($this->tableName());
312 $t->template_id = $tmpl->id;
313 $t->whereAdd("lang = ''");
316 // we have a situation where old md sums where created..
320 $cur = $t->fetchAll('mdsum', 'id');
328 // now loop through current..
329 $cwords = array();// not in used??
331 // echo "sync Template Words... \n";
334 foreach($words as $k=>$v) {
344 $md = $keyvalue ? $k : md5($v);
346 // check to see if there are more that one versions of that md5
349 if (!isset($cur[$md])) {
350 // create a record for it..
351 $t = DB_DataObject::factory($this->tableName());
354 'lang' => '',// by default should a english
355 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
356 'template_id'=>$tmpl->id,
361 $active[] = $t->insert();
364 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
366 $active[] = $cur[$md];
368 // we have it already? -
369 $t = DB_DataObject::factory($this->tableName());
370 $t->query("UPDATE {$this->tableName()}
383 if (count(array_values($cur))) {// de-active unused
385 $t = DB_DataObject::factory($this->tableName());
386 // echo "de-active current?? \n";
389 $deactive = array_values($cur);
390 $t->query("UPDATE core_templatestr
391 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
395 // delete all the items that are not relivant.
396 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
397 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
398 $t = DB_DataObject::factory($this->tableName());
400 // this will active the child data
401 if (empty($active)) {// set the active array to empty
404 $t->query("UPDATE core_templatestr
407 src_id IN (". implode(',' ,$active) . ")
409 template_id = {$tmpl->id}
411 //deactive the child data
412 if (empty($deactive)) {
413 $deactive = array(-1);
415 $t->query("UPDATE core_templatestr
418 src_id IN (". implode(',' ,$deactive) . ")
420 template_id = {$tmpl->id}
426 function checkDupes($tid, $lang, $id, $mdsum) {
428 $t = DB_DataObject::factory($this->tableName());
429 $t->template_id = $tid;
431 $t->whereAdd("lang = '{$lang}'");
432 if ($t->count() == 1) {
433 return $id; // only got one ... no issues..
436 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
438 //DB_DataObject::debugLevel(1);
439 // find out if any of them have got translations.
440 $ids = $t->fetchAll('id');
443 $t = DB_DataObject::factory($this->tableName());
444 $t->whereAddIn('src_id', $ids, 'int');
445 $t->whereAdd("txt != ''");
446 $t->orderBy('updated DESC');
449 // do any translations exist?
455 // delete all the others...
456 $t = DB_DataObject::factory($this->tableName());
457 $t->whereAddIn('src_id', $ids, 'int');
458 $t->whereAdd("src_id != $id");
462 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
465 $t = DB_DataObject::factory($this->tableName());
466 $t->whereAddIn('id', $ids, 'int');
467 $t->whereAdd("id != $id");
471 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
474 // this is done by calling code
475 //$t = DB_DataObject::factory($this->tableName());
476 //$t->query("update core_templatestr set active= 1 where src_id = $id");
487 function syncLang($lang)
489 // bugs with our old code...
491 $tn = $this->tableName();
492 $t = DB_DataObject::factory($tn);
493 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
495 // find all the id's from lang that have not been generated..
498 static $id_tmp = false;
500 if ($id_tmp == false) {
502 $t = DB_DataObject::factory($tn);
503 $t->whereAdd("lang = ''");
506 //old code, this did not support the on_table
507 // $id_tmp = $t->fetchAll('id','template_id');
508 // $ids = array_keys($id_tmp);
510 //new code for support the sync tables
511 foreach($t->fetchAll() as $ori){
512 $id_tmp[$ori->id] = $ori;
515 $ids = array_keys($id_tmp);
517 // matching by language:
518 $t = DB_DataObject::factory($tn);
519 $t->whereAddIn('src_id', $ids , 'int');
522 $got = $t->fetchAll('src_id');
523 $missing = array_diff($ids, $got);
525 if (empty($missing)) {
528 $t = DB_DataObject::factory($tn);
529 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
539 FROM core_templatestr
541 id IN (". implode(',', $missing) . ")
544 DB_DataObject::factory($tn)->query($q);
545 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
546 DB_DataObject::factory($tn)->query($q);
547 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
548 DB_DataObject::factory($tn)->query($q);
554 // called from flexy to translate a string.
557 function translateFlexyString($flexy, $string)
560 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
562 // using $flexy->currentTemplate -> find the template we are looking at..
563 // then find the string for $flexy->options['locale']
564 //DB_DataObject::debugLevel(1);
565 if ($debug) { var_dump($string); }
567 static $cache = array(); // cache of templates..
569 $ff = HTML_FlexyFramework::get();
570 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
571 if (empty($view_name)) {
572 $pg = HTML_FlexyFramework::get()->page;
573 if (isset($pg->templateViewName)) {
574 $view_name = $pg->templateViewName;
581 if ($debug) { var_dump(array('view_name'=> $view_name)); }
584 foreach($flexy->options['templateDir'] as $td) {
585 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
592 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
595 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
596 if ($debug) { echo "from cache no match - $string\n"; }
600 if (!isset($cache[$tmpname])) {
604 $tmpl = DB_DataObject::factory('core_template');
605 if ($view_name !== false) {
606 $tmpl->view_name = $view_name;
608 if(!$tmpl->get('template', $tmpname)){
609 // strip of site prefix if set...
611 $tmpl = DB_DataObject::factory('core_template');
612 if(!$tmpl->get('template', $tmpname)){
613 //var_dump("no template? {$tmpname} or {$relpath}" );
614 $cache[$tmpname] = false;
615 if ($debug) { echo "no template found - no match - $string\n"; }
619 $cache[$tmpname] = $tmpl;
621 $tmpl = $cache[$tmpname] ;
626 //get original template id
628 $orig = DB_DataObject::factory($this->tableName());
630 $orig->template_id = $tmpl->id;
633 $cache[$tmpname]->words =
635 if(!$orig->get( 'mdsum' , md5(trim($string)))){
636 //var_dump('no text? '. $string);
637 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
642 if (empty($cache[$tmpname]->translations)) {
644 //find out the text by language
645 //DB_DataObject::DebugLevel(1);
646 $x = DB_DataObject::factory($this->tableName());
647 $x->lang = $flexy->options['locale'];
648 $x->template_id = $tmpl->id;
650 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
651 if (empty($cache[$tmpname]->translations)) {
652 $cache[$tmpname]->translations = true;
654 //var_Dump($cache[$tmpname]->translations);
658 if ($cache[$tmpname]->translations === true) {
661 //var_dump("Checking: " . md5(trim($string)));
663 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
664 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
665 return $cache[$tmpname]->translations [md5(trim($string))];
672 function translateChanged($flexy)
675 $date = $this->lastUpdated($flexy);
676 if ($date === false) {
679 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
680 return strtotime($date) > $utime;
683 // determine if a complied template need recompling
685 function lastUpdated($flexy)
688 // var_dump('check changed?');
689 //DB_DataObject::debugLevel(1);
690 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
692 //var_dump($flexy->compiledTemplate);
693 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
696 static $cache = array(); // cache of templates..
698 $ff = HTML_FlexyFramework::get();
699 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
701 // find which of the template directories was actually used for the template.
704 foreach($flexy->options['templateDir'] as $td) {
705 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
712 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
714 // we do not have any record of this template..
715 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
719 if (!isset($cache[$tmpname])) {
723 $tmpl = DB_DataObject::factory('core_template');
724 if ($view_name !== false) {
725 $tmpl->view_name = $view_name;
727 if(!$tmpl->get('template', $tmpname)){
728 // strip of site prefix if set...
730 $tmpl = DB_DataObject::factory('core_template');
731 if(!$tmpl->get('template', $tmpname)){
732 //var_dump("no template? {$tmpname} or {$relpath}" );
733 $cache[$tmpname] = false;
737 $cache[$tmpname] = $tmpl;
739 $tmpl = $cache[$tmpname] ;
744 $x = DB_DataObject::factory($this->tableName());
745 $x->lang = $flexy->options['locale'];
747 $x->template_id = $tmpl->id;
748 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
749 if ($x->count() < 1) {
750 return false; // we don't have any record of it.
753 $x->selectAdd('max(updated) as max_updated');
755 return $x->max_updated;
760 function toRooArray($req) {
761 $ret = $this->toArray();
763 if (empty($req['csvCols'])) {
769 // translations for table columns
770 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
771 $ret['template_id_view_name'] = 'database';
772 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
778 function postListFilter($ar, $au, $req)
780 if (empty($req['csvCols'])) {
789 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
794 // translations for table columns
795 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
797 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
799 if(!empty($ret[$key])) {
806 $ret = array_values($ret);