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)
87 $ff = HTML_FlexyFramework::get()->Pman_Core;
89 if(empty($ff['DataObjects_Core_templatestr']['tables'])){
92 $tn = $obj->tableName();
93 if(empty($ff['DataObjects_Core_templatestr']['tables'][$tn])){
96 $cols = $ff['DataObjects_Core_templatestr']['tables'][$tn];
99 foreach($cols as $c) {
101 if(strpos($c, ',') !== false) {
102 $arr = explode(',', $c);
106 $ar = explode('=', $cond);
109 // skip if condition not fulfilled
110 if($obj->{$ar[0]} != $ar[1]) {
115 // skip if empty string
116 if(empty($obj->$c)) {
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) {
136 $x->mdsum = md5($obj->$c);
138 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
139 $up ? $x->update() : $x->insert();
142 // make sure the used one are active
144 $t = DB_DataObject::factory($this->tableName());
145 // activate the aprent data
146 $t->query("UPDATE core_templatestr
147 SET active = 1 WHERE id in (" . implode(',' ,$used) . ")
149 // activate the child data
150 $t->query("UPDATE core_templatestr
153 src_id IN (". implode(',' ,$used) . ")
163 function applyFiltersTree($q,$roo)
165 if (empty($q['node'])) {
166 $roo->jerr("invalid node");
170 case ($q['node'] == 'transtree'):
171 case ($q['node'] == 'langlist'):
172 // DB_DataObject::debugLevel(1);
173 $x = DB_Dataobject::Factory($this->tableName());
175 $x->selectAdd('distinct(lang) as lang');
177 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
178 $x->whereAdd("lang != ''");
180 foreach( $x->fetchAll() as $l) {
182 'text'=>$l->lang_name,
183 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
196 case preg_match('/^lang:/', $q['node']):
198 $lang = preg_replace('/^lang:/', '', $q['node']);
200 $x = DB_DataObject::factory('core_templatestr');
203 $x->selectAdd('distinct(view_name) as view_name');
205 $x->whereAdd('join_template_id_id.is_deleted = 0');
206 $x->orderBy('view_name DESC');
210 'text'=> $x->view_name,
211 'id' => 'view:'. $lang .':'. $x->view_name,
216 $ff = HTML_FlexyFramework::get()->Pman_Core;
218 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
219 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
222 'on_table' => $table,
223 'id' => 'table:'. $lang .':'. $table,
235 case preg_match('/^view:/', $q['node']):
238 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
240 $x = DB_DataObject::factory($this->tableName());
243 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
244 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
245 $x->whereAdd('join_template_id_id.is_deleted = 0');
247 $ids = $x->fetchAll('template_id');
250 //add the table type lists
252 $ff = HTML_FlexyFramework::get()->Pman_Core;
255 // $x->orderBy('template ASC');
256 // $x->whereAdd("lang != ''");
259 $xx = DB_Dataobject::Factory('core_template');
260 $xx->whereAddIn('id', $ids, 'int');
264 id, concat( template) as template_name
266 $xx->orderBy('template_name ASC');
268 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
288 * @param object $tmpl core_template data object
289 * @param array $words array of words
291 function syncTemplateWords($tmpl, $keyvalue = false)
294 $words = $tmpl->words;
295 // mapping for template :
296 //tablename => $n (templatename)
297 //tableid => $k (key value)
298 //colname => $n (templatename)
301 //print_r($words);exit;
303 $tt = DB_DataObject::factory($this->tableName());
306 $t->template_id = $tmpl->id;
307 $t->whereAdd("lang = ''");
310 // we have a situation where old md sums where created..
314 $cur = $t->fetchAll('mdsum', 'id');
322 // now loop through current..
323 $cwords = array();// not in used??
325 // echo "sync Template Words... \n";
328 foreach($words as $k=>$v) {
338 $md = $keyvalue ? $k : md5($v);
340 // check to see if there are more that one versions of that md5
343 if (!isset($cur[$md])) {
344 // create a record for it..
345 $t = DB_DataObject::factory($this->tableName());
348 'lang' => '',// by default should a english
349 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
350 'template_id'=>$tmpl->id,
355 $active[] = $t->insert();
358 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
360 $active[] = $cur[$md];
362 // we have it already? -
363 $t = DB_DataObject::factory($this->tableName());
364 $t->query("UPDATE {$this->tableName()}
377 if (count(array_values($cur))) {// de-active unused
379 $t = DB_DataObject::factory($this->tableName());
380 // echo "de-active current?? \n";
383 $deactive = array_values($cur);
384 $t->query("UPDATE core_templatestr
385 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
389 // delete all the items that are not relivant.
390 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
391 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
392 $t = DB_DataObject::factory($this->tableName());
394 // this will active the child data
395 if (empty($active)) {// set the active array to empty
398 $t->query("UPDATE core_templatestr
401 src_id IN (". implode(',' ,$active) . ")
403 template_id = {$tmpl->id}
405 //deactive the child data
406 if (empty($deactive)) {
407 $deactive = array(-1);
409 $t->query("UPDATE core_templatestr
412 src_id IN (". implode(',' ,$deactive) . ")
414 template_id = {$tmpl->id}
420 function checkDupes($tid, $lang, $id, $mdsum) {
422 $t = DB_DataObject::factory($this->tableName());
423 $t->template_id = $tid;
425 $t->whereAdd("lang = '{$lang}'");
426 if ($t->count() == 1) {
427 return $id; // only got one ... no issues..
430 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
432 //DB_DataObject::debugLevel(1);
433 // find out if any of them have got translations.
434 $ids = $t->fetchAll('id');
437 $t = DB_DataObject::factory($this->tableName());
438 $t->whereAddIn('src_id', $ids, 'int');
439 $t->whereAdd("txt != ''");
440 $t->orderBy('updated DESC');
443 // do any translations exist?
449 // delete all the others...
450 $t = DB_DataObject::factory($this->tableName());
451 $t->whereAddIn('src_id', $ids, 'int');
452 $t->whereAdd("src_id != $id");
456 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
459 $t = DB_DataObject::factory($this->tableName());
460 $t->whereAddIn('id', $ids, 'int');
461 $t->whereAdd("id != $id");
465 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
468 // this is done by calling code
469 //$t = DB_DataObject::factory($this->tableName());
470 //$t->query("update core_templatestr set active= 1 where src_id = $id");
481 function syncLang($lang)
483 // bugs with our old code...
485 $tn = $this->tableName();
486 $t = DB_DataObject::factory($tn);
487 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
489 // find all the id's from lang that have not been generated..
492 static $id_tmp = false;
494 if ($id_tmp == false) {
496 $t = DB_DataObject::factory($tn);
497 $t->whereAdd("lang = ''");
500 //old code, this did not support the on_table
501 // $id_tmp = $t->fetchAll('id','template_id');
502 // $ids = array_keys($id_tmp);
504 //new code for support the sync tables
505 foreach($t->fetchAll() as $ori){
506 $id_tmp[$ori->id] = $ori;
509 $ids = array_keys($id_tmp);
511 // matching by language:
512 $t = DB_DataObject::factory($tn);
513 $t->whereAddIn('src_id', $ids , 'int');
516 $got = $t->fetchAll('src_id');
517 $missing = array_diff($ids, $got);
519 if (empty($missing)) {
522 $t = DB_DataObject::factory($tn);
523 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
533 FROM core_templatestr
535 id IN (". implode(',', $missing) . ")
538 DB_DataObject::factory($tn)->query($q);
539 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
540 DB_DataObject::factory($tn)->query($q);
541 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
542 DB_DataObject::factory($tn)->query($q);
548 // called from flexy to translate a string.
551 function translateFlexyString($flexy, $string)
554 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
556 // using $flexy->currentTemplate -> find the template we are looking at..
557 // then find the string for $flexy->options['locale']
558 //DB_DataObject::debugLevel(1);
559 if ($debug) { var_dump($string); }
561 static $cache = array(); // cache of templates..
563 $ff = HTML_FlexyFramework::get();
564 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
565 if (empty($view_name)) {
566 $pg = HTML_FlexyFramework::get()->page;
567 if (isset($pg->templateViewName)) {
568 $view_name = $pg->templateViewName;
575 if ($debug) { var_dump(array('view_name'=> $view_name)); }
578 foreach($flexy->options['templateDir'] as $td) {
579 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
586 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
589 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
590 if ($debug) { echo "from cache no match - $string\n"; }
594 if (!isset($cache[$tmpname])) {
598 $tmpl = DB_DataObject::factory('core_template');
599 if ($view_name !== false) {
600 $tmpl->view_name = $view_name;
602 if(!$tmpl->get('template', $tmpname)){
603 // strip of site prefix if set...
605 $tmpl = DB_DataObject::factory('core_template');
606 if(!$tmpl->get('template', $tmpname)){
607 //var_dump("no template? {$tmpname} or {$relpath}" );
608 $cache[$tmpname] = false;
609 if ($debug) { echo "no template found - no match - $string\n"; }
613 $cache[$tmpname] = $tmpl;
615 $tmpl = $cache[$tmpname] ;
620 //get original template id
622 $orig = DB_DataObject::factory($this->tableName());
624 $orig->template_id = $tmpl->id;
627 $cache[$tmpname]->words =
629 if(!$orig->get( 'mdsum' , md5(trim($string)))){
630 //var_dump('no text? '. $string);
631 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
636 if (empty($cache[$tmpname]->translations)) {
638 //find out the text by language
639 //DB_DataObject::DebugLevel(1);
640 $x = DB_DataObject::factory($this->tableName());
641 $x->lang = $flexy->options['locale'];
642 $x->template_id = $tmpl->id;
644 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
645 if (empty($cache[$tmpname]->translations)) {
646 $cache[$tmpname]->translations = true;
648 //var_Dump($cache[$tmpname]->translations);
652 if ($cache[$tmpname]->translations === true) {
655 //var_dump("Checking: " . md5(trim($string)));
657 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
658 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
659 return $cache[$tmpname]->translations [md5(trim($string))];
666 function translateChanged($flexy)
669 $date = $this->lastUpdated($flexy);
670 if ($date === false) {
673 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
674 return strtotime($date) > $utime;
677 // determine if a complied template need recompling
679 function lastUpdated($flexy)
682 // var_dump('check changed?');
683 //DB_DataObject::debugLevel(1);
684 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
686 //var_dump($flexy->compiledTemplate);
687 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
690 static $cache = array(); // cache of templates..
692 $ff = HTML_FlexyFramework::get();
693 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
695 // find which of the template directories was actually used for the template.
698 foreach($flexy->options['templateDir'] as $td) {
699 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
706 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
708 // we do not have any record of this template..
709 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
713 if (!isset($cache[$tmpname])) {
717 $tmpl = DB_DataObject::factory('core_template');
718 if ($view_name !== false) {
719 $tmpl->view_name = $view_name;
721 if(!$tmpl->get('template', $tmpname)){
722 // strip of site prefix if set...
724 $tmpl = DB_DataObject::factory('core_template');
725 if(!$tmpl->get('template', $tmpname)){
726 //var_dump("no template? {$tmpname} or {$relpath}" );
727 $cache[$tmpname] = false;
731 $cache[$tmpname] = $tmpl;
733 $tmpl = $cache[$tmpname] ;
738 $x = DB_DataObject::factory($this->tableName());
739 $x->lang = $flexy->options['locale'];
741 $x->template_id = $tmpl->id;
742 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
743 if ($x->count() < 1) {
744 return false; // we don't have any record of it.
747 $x->selectAdd('max(updated) as max_updated');
749 return $x->max_updated;
754 function toRooArray($req) {
755 $ret = $this->toArray();
757 if (empty($req['csvCols'])) {
763 // translations for table columns
764 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
765 $ret['template_id_view_name'] = 'database';
766 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
772 function postListFilter($ar, $au, $req)
774 if (empty($req['csvCols'])) {
783 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
788 // translations for table columns
789 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
791 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
793 if(!empty($ret[$key])) {
800 $ret = array_values($ret);