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 if(strpos($c, ',') !== false) {
105 $arr = explode(',', $c);
109 $ar = explode('=', $cond);
112 // skip if condition not fulfilled
113 if($obj->{$ar[0]} != $ar[1]) {
117 $x->on_id = $obj->pid();
120 $x->lang = ''; /// eg. base language..
121 $up = $x->find(true);
124 // deactivate empty words
125 if(empty($obj->$c)) {
126 $deactive[] = $x->id;
128 // activate non-empty words
133 if($x->txt == $obj->$c) {
134 continue; // skip when no change
139 if(empty($obj->$c)) {
147 $x->mdsum = md5($obj->$c);
149 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
150 $up ? $x->update() : $x->insert();
153 if(count($deactive)) {
154 $t = DB_DataObject::factory($this->tableName());
155 // deactivate the parent data
156 $t->query("UPDATE core_templatestr
157 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
160 // deactivate the child data
161 $t->query("UPDATE core_templatestr
164 src_id IN (". implode(',' ,$deactive) . ")
171 $t = DB_DataObject::factory($this->tableName());
172 // activate the aprent data
173 $t->query("UPDATE core_templatestr
174 SET active = 1 WHERE id in (" . implode(',' ,$active) . ")
176 // deactivate the child data
177 $t->query("UPDATE core_templatestr
180 src_id IN (". implode(',' ,$active) . ")
190 function applyFiltersTree($q,$roo)
192 if (empty($q['node'])) {
193 $roo->jerr("invalid node");
197 case ($q['node'] == 'transtree'):
198 case ($q['node'] == 'langlist'):
199 // DB_DataObject::debugLevel(1);
200 $x = DB_Dataobject::Factory($this->tableName());
202 $x->selectAdd('distinct(lang) as lang');
204 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
205 $x->whereAdd("lang != ''");
207 foreach( $x->fetchAll() as $l) {
209 'text'=>$l->lang_name,
210 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
223 case preg_match('/^lang:/', $q['node']):
225 $lang = preg_replace('/^lang:/', '', $q['node']);
227 $x = DB_DataObject::factory('core_templatestr');
230 $x->selectAdd('distinct(view_name) as view_name');
232 $x->whereAdd('join_template_id_id.is_deleted = 0');
233 $x->orderBy('view_name DESC');
237 'text'=> $x->view_name,
238 'id' => 'view:'. $lang .':'. $x->view_name,
243 $ff = HTML_FlexyFramework::get()->Pman_Core;
245 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
246 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
249 'on_table' => $table,
250 'id' => 'table:'. $lang .':'. $table,
262 case preg_match('/^view:/', $q['node']):
265 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
267 $x = DB_DataObject::factory($this->tableName());
270 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
271 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
272 $x->whereAdd('join_template_id_id.is_deleted = 0');
274 $ids = $x->fetchAll('template_id');
277 //add the table type lists
279 $ff = HTML_FlexyFramework::get()->Pman_Core;
282 // $x->orderBy('template ASC');
283 // $x->whereAdd("lang != ''");
286 $xx = DB_Dataobject::Factory('core_template');
287 $xx->whereAddIn('id', $ids, 'int');
291 id, concat( template) as template_name
293 $xx->orderBy('template_name ASC');
295 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
315 * @param object $tmpl core_template data object
316 * @param array $words array of words
318 function syncTemplateWords($tmpl, $keyvalue = false)
321 $words = $tmpl->words;
322 // mapping for template :
323 //tablename => $n (templatename)
324 //tableid => $k (key value)
325 //colname => $n (templatename)
328 //print_r($words);exit;
330 $tt = DB_DataObject::factory($this->tableName());
333 $t->template_id = $tmpl->id;
334 $t->whereAdd("lang = ''");
337 // we have a situation where old md sums where created..
341 $cur = $t->fetchAll('mdsum', 'id');
349 // now loop through current..
350 $cwords = array();// not in used??
352 // echo "sync Template Words... \n";
355 foreach($words as $k=>$v) {
365 $md = $keyvalue ? $k : md5($v);
367 // check to see if there are more that one versions of that md5
370 if (!isset($cur[$md])) {
371 // create a record for it..
372 $t = DB_DataObject::factory($this->tableName());
375 'lang' => '',// by default should a english
376 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
377 'template_id'=>$tmpl->id,
382 $active[] = $t->insert();
385 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
387 $active[] = $cur[$md];
389 // we have it already? -
390 $t = DB_DataObject::factory($this->tableName());
391 $t->query("UPDATE {$this->tableName()}
404 if (count(array_values($cur))) {// de-active unused
406 $t = DB_DataObject::factory($this->tableName());
407 // echo "de-active current?? \n";
410 $deactive = array_values($cur);
411 $t->query("UPDATE core_templatestr
412 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
416 // delete all the items that are not relivant.
417 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
418 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
419 $t = DB_DataObject::factory($this->tableName());
421 // this will active the child data
422 if (empty($active)) {// set the active array to empty
425 $t->query("UPDATE core_templatestr
428 src_id IN (". implode(',' ,$active) . ")
430 template_id = {$tmpl->id}
432 //deactive the child data
433 if (empty($deactive)) {
434 $deactive = array(-1);
436 $t->query("UPDATE core_templatestr
439 src_id IN (". implode(',' ,$deactive) . ")
441 template_id = {$tmpl->id}
447 function checkDupes($tid, $lang, $id, $mdsum) {
449 $t = DB_DataObject::factory($this->tableName());
450 $t->template_id = $tid;
452 $t->whereAdd("lang = '{$lang}'");
453 if ($t->count() == 1) {
454 return $id; // only got one ... no issues..
457 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
459 //DB_DataObject::debugLevel(1);
460 // find out if any of them have got translations.
461 $ids = $t->fetchAll('id');
464 $t = DB_DataObject::factory($this->tableName());
465 $t->whereAddIn('src_id', $ids, 'int');
466 $t->whereAdd("txt != ''");
467 $t->orderBy('updated DESC');
470 // do any translations exist?
476 // delete all the others...
477 $t = DB_DataObject::factory($this->tableName());
478 $t->whereAddIn('src_id', $ids, 'int');
479 $t->whereAdd("src_id != $id");
483 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
486 $t = DB_DataObject::factory($this->tableName());
487 $t->whereAddIn('id', $ids, 'int');
488 $t->whereAdd("id != $id");
492 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
495 // this is done by calling code
496 //$t = DB_DataObject::factory($this->tableName());
497 //$t->query("update core_templatestr set active= 1 where src_id = $id");
508 function syncLang($lang)
510 // bugs with our old code...
512 $tn = $this->tableName();
513 $t = DB_DataObject::factory($tn);
514 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
516 // find all the id's from lang that have not been generated..
519 static $id_tmp = false;
521 if ($id_tmp == false) {
523 $t = DB_DataObject::factory($tn);
524 $t->whereAdd("lang = ''");
527 //old code, this did not support the on_table
528 // $id_tmp = $t->fetchAll('id','template_id');
529 // $ids = array_keys($id_tmp);
531 //new code for support the sync tables
532 foreach($t->fetchAll() as $ori){
533 $id_tmp[$ori->id] = $ori;
536 $ids = array_keys($id_tmp);
538 // matching by language:
539 $t = DB_DataObject::factory($tn);
540 $t->whereAddIn('src_id', $ids , 'int');
543 $got = $t->fetchAll('src_id');
544 $missing = array_diff($ids, $got);
546 if (empty($missing)) {
549 $t = DB_DataObject::factory($tn);
550 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
560 FROM core_templatestr
562 id IN (". implode(',', $missing) . ")
565 DB_DataObject::factory($tn)->query($q);
566 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
567 DB_DataObject::factory($tn)->query($q);
568 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
569 DB_DataObject::factory($tn)->query($q);
575 // called from flexy to translate a string.
578 function translateFlexyString($flexy, $string)
581 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
583 // using $flexy->currentTemplate -> find the template we are looking at..
584 // then find the string for $flexy->options['locale']
585 //DB_DataObject::debugLevel(1);
586 if ($debug) { var_dump($string); }
588 static $cache = array(); // cache of templates..
590 $ff = HTML_FlexyFramework::get();
591 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
592 if (empty($view_name)) {
593 $pg = HTML_FlexyFramework::get()->page;
594 if (isset($pg->templateViewName)) {
595 $view_name = $pg->templateViewName;
602 if ($debug) { var_dump(array('view_name'=> $view_name)); }
605 foreach($flexy->options['templateDir'] as $td) {
606 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
613 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
616 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
617 if ($debug) { echo "from cache no match - $string\n"; }
621 if (!isset($cache[$tmpname])) {
625 $tmpl = DB_DataObject::factory('core_template');
626 if ($view_name !== false) {
627 $tmpl->view_name = $view_name;
629 if(!$tmpl->get('template', $tmpname)){
630 // strip of site prefix if set...
632 $tmpl = DB_DataObject::factory('core_template');
633 if(!$tmpl->get('template', $tmpname)){
634 //var_dump("no template? {$tmpname} or {$relpath}" );
635 $cache[$tmpname] = false;
636 if ($debug) { echo "no template found - no match - $string\n"; }
640 $cache[$tmpname] = $tmpl;
642 $tmpl = $cache[$tmpname] ;
647 //get original template id
649 $orig = DB_DataObject::factory($this->tableName());
651 $orig->template_id = $tmpl->id;
654 $cache[$tmpname]->words =
656 if(!$orig->get( 'mdsum' , md5(trim($string)))){
657 //var_dump('no text? '. $string);
658 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
663 if (empty($cache[$tmpname]->translations)) {
665 //find out the text by language
666 //DB_DataObject::DebugLevel(1);
667 $x = DB_DataObject::factory($this->tableName());
668 $x->lang = $flexy->options['locale'];
669 $x->template_id = $tmpl->id;
671 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
672 if (empty($cache[$tmpname]->translations)) {
673 $cache[$tmpname]->translations = true;
675 //var_Dump($cache[$tmpname]->translations);
679 if ($cache[$tmpname]->translations === true) {
682 //var_dump("Checking: " . md5(trim($string)));
684 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
685 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
686 return $cache[$tmpname]->translations [md5(trim($string))];
693 function translateChanged($flexy)
696 $date = $this->lastUpdated($flexy);
697 if ($date === false) {
700 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
701 return strtotime($date) > $utime;
704 // determine if a complied template need recompling
706 function lastUpdated($flexy)
709 // var_dump('check changed?');
710 //DB_DataObject::debugLevel(1);
711 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
713 //var_dump($flexy->compiledTemplate);
714 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
717 static $cache = array(); // cache of templates..
719 $ff = HTML_FlexyFramework::get();
720 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
722 // find which of the template directories was actually used for the template.
725 foreach($flexy->options['templateDir'] as $td) {
726 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
733 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
735 // we do not have any record of this template..
736 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
740 if (!isset($cache[$tmpname])) {
744 $tmpl = DB_DataObject::factory('core_template');
745 if ($view_name !== false) {
746 $tmpl->view_name = $view_name;
748 if(!$tmpl->get('template', $tmpname)){
749 // strip of site prefix if set...
751 $tmpl = DB_DataObject::factory('core_template');
752 if(!$tmpl->get('template', $tmpname)){
753 //var_dump("no template? {$tmpname} or {$relpath}" );
754 $cache[$tmpname] = false;
758 $cache[$tmpname] = $tmpl;
760 $tmpl = $cache[$tmpname] ;
765 $x = DB_DataObject::factory($this->tableName());
766 $x->lang = $flexy->options['locale'];
768 $x->template_id = $tmpl->id;
769 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
770 if ($x->count() < 1) {
771 return false; // we don't have any record of it.
774 $x->selectAdd('max(updated) as max_updated');
776 return $x->max_updated;
781 function toRooArray($req) {
782 $ret = $this->toArray();
784 if (empty($req['csvCols'])) {
790 // translations for table columns
791 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
792 $ret['template_id_view_name'] = 'database';
793 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
799 function postListFilter($ar, $au, $req)
801 if (empty($req['csvCols'])) {
810 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
815 // translations for table columns
816 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
818 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
820 if(!empty($ret[$key])) {
827 $ret = array_values($ret);