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];
99 foreach($cols as $c) {
101 // skip if empty string
102 if(empty($obj->$c)) {
106 if(strpos($c, ',') !== false) {
107 $arr = explode(',', $c);
111 $ar = explode('=', $cond);
114 // skip if condition not fulfilled
115 if($obj->{$ar[0]} != $ar[1]) {
120 $x = $this->factory($this->tableName());
121 $x->on_id = $obj->pid();
124 $x->lang = ''; /// eg. base language..
125 $up = $x->find(true);
127 // skip when no change
128 if($up && $x->txt == $obj->$c) {
135 $x->mdsum = md5($obj->$c);
137 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
138 $up ? $x->update() : $x->insert();
141 // if(count($unused)) {
142 // $t = DB_DataObject::factory($this->tableName());
143 // // deactivate the parent data
144 // $t->query("UPDATE core_templatestr
145 // SET active = 0 WHERE id in (" . implode(',' ,$unused) . ")
148 // // deactivate the child data
149 // $t->query("UPDATE core_templatestr
152 // src_id IN (". implode(',' , $unused) . ")
158 // if(count($used)) {
159 // $t = DB_DataObject::factory($this->tableName());
160 // // activate the aprent data
161 // $t->query("UPDATE core_templatestr
162 // SET active = 1 WHERE id in (" . implode(',' ,$active) . ")
164 // // deactivate the child data
165 // $t->query("UPDATE core_templatestr
168 // src_id IN (". implode(',' ,$active) . ")
177 function syncTableWords()
179 $ff = HTML_FlexyFramework::get()->Pman_Core;
181 if(empty($ff['DataObjects_Core_templatestr']['tables'])){
186 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table => $cols) {
190 $cts = DB_DataObject::factory($this->tableName());
191 $cts->whereAdd('on_table != ""');
193 $cur = $cts->fetchAll();
195 foreach($cur as $ts) {
196 var_dump($ts->on_table);
197 var_dump($ts->on_col);
203 function applyFiltersTree($q,$roo)
205 if (empty($q['node'])) {
206 $roo->jerr("invalid node");
210 case ($q['node'] == 'transtree'):
211 case ($q['node'] == 'langlist'):
212 // DB_DataObject::debugLevel(1);
213 $x = DB_Dataobject::Factory($this->tableName());
215 $x->selectAdd('distinct(lang) as lang');
217 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
218 $x->whereAdd("lang != ''");
220 foreach( $x->fetchAll() as $l) {
222 'text'=>$l->lang_name,
223 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
236 case preg_match('/^lang:/', $q['node']):
238 $lang = preg_replace('/^lang:/', '', $q['node']);
240 $x = DB_DataObject::factory('core_templatestr');
243 $x->selectAdd('distinct(view_name) as view_name');
245 $x->whereAdd('join_template_id_id.is_deleted = 0');
246 $x->orderBy('view_name DESC');
250 'text'=> $x->view_name,
251 'id' => 'view:'. $lang .':'. $x->view_name,
256 $ff = HTML_FlexyFramework::get()->Pman_Core;
258 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
259 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
262 'on_table' => $table,
263 'id' => 'table:'. $lang .':'. $table,
275 case preg_match('/^view:/', $q['node']):
278 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
280 $x = DB_DataObject::factory($this->tableName());
283 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
284 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
285 $x->whereAdd('join_template_id_id.is_deleted = 0');
287 $ids = $x->fetchAll('template_id');
290 //add the table type lists
292 $ff = HTML_FlexyFramework::get()->Pman_Core;
295 // $x->orderBy('template ASC');
296 // $x->whereAdd("lang != ''");
299 $xx = DB_Dataobject::Factory('core_template');
300 $xx->whereAddIn('id', $ids, 'int');
304 id, concat( template) as template_name
306 $xx->orderBy('template_name ASC');
308 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
328 * @param object $tmpl core_template data object
329 * @param array $words array of words
331 function syncTemplateWords($tmpl, $keyvalue = false)
334 $words = $tmpl->words;
335 // mapping for template :
336 //tablename => $n (templatename)
337 //tableid => $k (key value)
338 //colname => $n (templatename)
341 //print_r($words);exit;
343 $tt = DB_DataObject::factory($this->tableName());
346 $t->template_id = $tmpl->id;
347 $t->whereAdd("lang = ''");
350 // we have a situation where old md sums where created..
354 $cur = $t->fetchAll('mdsum', 'id');
362 // now loop through current..
363 $cwords = array();// not in used??
365 // echo "sync Template Words... \n";
368 foreach($words as $k=>$v) {
378 $md = $keyvalue ? $k : md5($v);
380 // check to see if there are more that one versions of that md5
383 if (!isset($cur[$md])) {
384 // create a record for it..
385 $t = DB_DataObject::factory($this->tableName());
388 'lang' => '',// by default should a english
389 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
390 'template_id'=>$tmpl->id,
395 $active[] = $t->insert();
398 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
400 $active[] = $cur[$md];
402 // we have it already? -
403 $t = DB_DataObject::factory($this->tableName());
404 $t->query("UPDATE {$this->tableName()}
417 if (count(array_values($cur))) {// de-active unused
419 $t = DB_DataObject::factory($this->tableName());
420 // echo "de-active current?? \n";
423 $deactive = array_values($cur);
424 $t->query("UPDATE core_templatestr
425 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
429 // delete all the items that are not relivant.
430 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
431 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
432 $t = DB_DataObject::factory($this->tableName());
434 // this will active the child data
435 if (empty($active)) {// set the active array to empty
438 $t->query("UPDATE core_templatestr
441 src_id IN (". implode(',' ,$active) . ")
443 template_id = {$tmpl->id}
445 //deactive the child data
446 if (empty($deactive)) {
447 $deactive = array(-1);
449 $t->query("UPDATE core_templatestr
452 src_id IN (". implode(',' ,$deactive) . ")
454 template_id = {$tmpl->id}
460 function checkDupes($tid, $lang, $id, $mdsum) {
462 $t = DB_DataObject::factory($this->tableName());
463 $t->template_id = $tid;
465 $t->whereAdd("lang = '{$lang}'");
466 if ($t->count() == 1) {
467 return $id; // only got one ... no issues..
470 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
472 //DB_DataObject::debugLevel(1);
473 // find out if any of them have got translations.
474 $ids = $t->fetchAll('id');
477 $t = DB_DataObject::factory($this->tableName());
478 $t->whereAddIn('src_id', $ids, 'int');
479 $t->whereAdd("txt != ''");
480 $t->orderBy('updated DESC');
483 // do any translations exist?
489 // delete all the others...
490 $t = DB_DataObject::factory($this->tableName());
491 $t->whereAddIn('src_id', $ids, 'int');
492 $t->whereAdd("src_id != $id");
496 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
499 $t = DB_DataObject::factory($this->tableName());
500 $t->whereAddIn('id', $ids, 'int');
501 $t->whereAdd("id != $id");
505 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
508 // this is done by calling code
509 //$t = DB_DataObject::factory($this->tableName());
510 //$t->query("update core_templatestr set active= 1 where src_id = $id");
521 function syncLang($lang)
523 // bugs with our old code...
525 $tn = $this->tableName();
526 $t = DB_DataObject::factory($tn);
527 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
529 // find all the id's from lang that have not been generated..
532 static $id_tmp = false;
534 if ($id_tmp == false) {
536 $t = DB_DataObject::factory($tn);
537 $t->whereAdd("lang = ''");
540 //old code, this did not support the on_table
541 // $id_tmp = $t->fetchAll('id','template_id');
542 // $ids = array_keys($id_tmp);
544 //new code for support the sync tables
545 foreach($t->fetchAll() as $ori){
546 $id_tmp[$ori->id] = $ori;
549 $ids = array_keys($id_tmp);
551 // matching by language:
552 $t = DB_DataObject::factory($tn);
553 $t->whereAddIn('src_id', $ids , 'int');
556 $got = $t->fetchAll('src_id');
557 $missing = array_diff($ids, $got);
559 if (empty($missing)) {
562 $t = DB_DataObject::factory($tn);
563 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
573 FROM core_templatestr
575 id IN (". implode(',', $missing) . ")
578 DB_DataObject::factory($tn)->query($q);
579 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
580 DB_DataObject::factory($tn)->query($q);
581 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
582 DB_DataObject::factory($tn)->query($q);
588 // called from flexy to translate a string.
591 function translateFlexyString($flexy, $string)
594 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
596 // using $flexy->currentTemplate -> find the template we are looking at..
597 // then find the string for $flexy->options['locale']
598 //DB_DataObject::debugLevel(1);
599 if ($debug) { var_dump($string); }
601 static $cache = array(); // cache of templates..
603 $ff = HTML_FlexyFramework::get();
604 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
605 if (empty($view_name)) {
606 $pg = HTML_FlexyFramework::get()->page;
607 if (isset($pg->templateViewName)) {
608 $view_name = $pg->templateViewName;
615 if ($debug) { var_dump(array('view_name'=> $view_name)); }
618 foreach($flexy->options['templateDir'] as $td) {
619 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
626 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
629 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
630 if ($debug) { echo "from cache no match - $string\n"; }
634 if (!isset($cache[$tmpname])) {
638 $tmpl = DB_DataObject::factory('core_template');
639 if ($view_name !== false) {
640 $tmpl->view_name = $view_name;
642 if(!$tmpl->get('template', $tmpname)){
643 // strip of site prefix if set...
645 $tmpl = DB_DataObject::factory('core_template');
646 if(!$tmpl->get('template', $tmpname)){
647 //var_dump("no template? {$tmpname} or {$relpath}" );
648 $cache[$tmpname] = false;
649 if ($debug) { echo "no template found - no match - $string\n"; }
653 $cache[$tmpname] = $tmpl;
655 $tmpl = $cache[$tmpname] ;
660 //get original template id
662 $orig = DB_DataObject::factory($this->tableName());
664 $orig->template_id = $tmpl->id;
667 $cache[$tmpname]->words =
669 if(!$orig->get( 'mdsum' , md5(trim($string)))){
670 //var_dump('no text? '. $string);
671 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
676 if (empty($cache[$tmpname]->translations)) {
678 //find out the text by language
679 //DB_DataObject::DebugLevel(1);
680 $x = DB_DataObject::factory($this->tableName());
681 $x->lang = $flexy->options['locale'];
682 $x->template_id = $tmpl->id;
684 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
685 if (empty($cache[$tmpname]->translations)) {
686 $cache[$tmpname]->translations = true;
688 //var_Dump($cache[$tmpname]->translations);
692 if ($cache[$tmpname]->translations === true) {
695 //var_dump("Checking: " . md5(trim($string)));
697 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
698 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
699 return $cache[$tmpname]->translations [md5(trim($string))];
706 function translateChanged($flexy)
709 $date = $this->lastUpdated($flexy);
710 if ($date === false) {
713 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
714 return strtotime($date) > $utime;
717 // determine if a complied template need recompling
719 function lastUpdated($flexy)
722 // var_dump('check changed?');
723 //DB_DataObject::debugLevel(1);
724 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
726 //var_dump($flexy->compiledTemplate);
727 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
730 static $cache = array(); // cache of templates..
732 $ff = HTML_FlexyFramework::get();
733 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
735 // find which of the template directories was actually used for the template.
738 foreach($flexy->options['templateDir'] as $td) {
739 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
746 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
748 // we do not have any record of this template..
749 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
753 if (!isset($cache[$tmpname])) {
757 $tmpl = DB_DataObject::factory('core_template');
758 if ($view_name !== false) {
759 $tmpl->view_name = $view_name;
761 if(!$tmpl->get('template', $tmpname)){
762 // strip of site prefix if set...
764 $tmpl = DB_DataObject::factory('core_template');
765 if(!$tmpl->get('template', $tmpname)){
766 //var_dump("no template? {$tmpname} or {$relpath}" );
767 $cache[$tmpname] = false;
771 $cache[$tmpname] = $tmpl;
773 $tmpl = $cache[$tmpname] ;
778 $x = DB_DataObject::factory($this->tableName());
779 $x->lang = $flexy->options['locale'];
781 $x->template_id = $tmpl->id;
782 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
783 if ($x->count() < 1) {
784 return false; // we don't have any record of it.
787 $x->selectAdd('max(updated) as max_updated');
789 return $x->max_updated;
794 function toRooArray($req) {
795 $ret = $this->toArray();
797 if (empty($req['csvCols'])) {
803 // translations for table columns
804 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
805 $ret['template_id_view_name'] = 'database';
806 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
812 function postListFilter($ar, $au, $req)
814 if (empty($req['csvCols'])) {
823 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
828 // translations for table columns
829 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
831 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
833 if(!empty($ret[$key])) {
840 $ret = array_values($ret);