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];
101 foreach($cols as $c) {
102 $x = $this->factory($this->tableName());
103 $x->on_id = $obj->pid();
106 $x->lang = ''; /// eg. base language..
107 $up = $x->find(true);
108 if ($up && $x->txt == $obj->$c) {
110 // deactivate empty words
111 if(empty($obj->$c)) {
112 $deactive[] = $x->id;
114 continue; // update an no change..
118 if(empty($obj->$c)) {
124 $x->mdsum = md5($obj->$c);
126 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
127 $up ? $x->update() : $x->insert();
130 if(count($deactive)) {
131 $t = DB_DataObject::factory($this->tableName());
132 // deactivate the parent data
133 $t->query("UPDATE core_templatestr
134 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
137 // deactivate the child data
138 $t->query("UPDATE core_templatestr
141 src_id IN (". implode(',' ,$deactive) . ")
151 function applyFiltersTree($q,$roo)
153 if (empty($q['node'])) {
154 $roo->jerr("invalid node");
158 case ($q['node'] == 'transtree'):
159 case ($q['node'] == 'langlist'):
160 // DB_DataObject::debugLevel(1);
161 $x = DB_Dataobject::Factory($this->tableName());
163 $x->selectAdd('distinct(lang) as lang');
165 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
166 $x->whereAdd("lang != ''");
168 foreach( $x->fetchAll() as $l) {
170 'text'=>$l->lang_name,
171 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
184 case preg_match('/^lang:/', $q['node']):
186 $lang = preg_replace('/^lang:/', '', $q['node']);
188 $x = DB_DataObject::factory('core_templatestr');
191 $x->selectAdd('distinct(view_name) as view_name');
193 $x->whereAdd('join_template_id_id.is_deleted = 0');
194 $x->orderBy('view_name DESC');
198 'text'=> $x->view_name,
199 'id' => 'view:'. $lang .':'. $x->view_name,
204 $ff = HTML_FlexyFramework::get()->Pman_Core;
206 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
207 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
210 'on_table' => $table,
211 'id' => 'table:'. $lang .':'. $table,
223 case preg_match('/^view:/', $q['node']):
226 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
228 $x = DB_DataObject::factory($this->tableName());
231 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
232 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
233 $x->whereAdd('join_template_id_id.is_deleted = 0');
235 $ids = $x->fetchAll('template_id');
238 //add the table type lists
240 $ff = HTML_FlexyFramework::get()->Pman_Core;
243 // $x->orderBy('template ASC');
244 // $x->whereAdd("lang != ''");
247 $xx = DB_Dataobject::Factory('core_template');
248 $xx->whereAddIn('id', $ids, 'int');
252 id, concat( template) as template_name
254 $xx->orderBy('template_name ASC');
256 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
276 * @param object $tmpl core_template data object
277 * @param array $words array of words
279 function syncTemplateWords($tmpl, $keyvalue = false)
282 $words = $tmpl->words;
283 // mapping for template :
284 //tablename => $n (templatename)
285 //tableid => $k (key value)
286 //colname => $n (templatename)
289 //print_r($words);exit;
291 $tt = DB_DataObject::factory($this->tableName());
294 $t->template_id = $tmpl->id;
295 $t->whereAdd("lang = ''");
298 // we have a situation where old md sums where created..
302 $cur = $t->fetchAll('mdsum', 'id');
310 // now loop through current..
311 $cwords = array();// not in used??
313 // echo "sync Template Words... \n";
316 foreach($words as $k=>$v) {
326 $md = $keyvalue ? $k : md5($v);
328 // check to see if there are more that one versions of that md5
331 if (!isset($cur[$md])) {
332 // create a record for it..
333 $t = DB_DataObject::factory($this->tableName());
336 'lang' => '',// by default should a english
337 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
338 'template_id'=>$tmpl->id,
343 $active[] = $t->insert();
346 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
348 $active[] = $cur[$md];
350 // we have it already? -
351 $t = DB_DataObject::factory($this->tableName());
352 $t->query("UPDATE {$this->tableName()}
365 if (count(array_values($cur))) {// de-active unused
367 $t = DB_DataObject::factory($this->tableName());
368 // echo "de-active current?? \n";
371 $deactive = array_values($cur);
372 $t->query("UPDATE core_templatestr
373 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
377 // delete all the items that are not relivant.
378 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
379 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
380 $t = DB_DataObject::factory($this->tableName());
382 // this will active the child data
383 if (empty($active)) {// set the active array to empty
386 $t->query("UPDATE core_templatestr
389 src_id IN (". implode(',' ,$active) . ")
391 template_id = {$tmpl->id}
393 //deactive the child data
394 if (empty($deactive)) {
395 $deactive = array(-1);
397 $t->query("UPDATE core_templatestr
400 src_id IN (". implode(',' ,$deactive) . ")
402 template_id = {$tmpl->id}
408 function checkDupes($tid, $lang, $id, $mdsum) {
410 $t = DB_DataObject::factory($this->tableName());
411 $t->template_id = $tid;
413 $t->whereAdd("lang = '{$lang}'");
414 if ($t->count() == 1) {
415 return $id; // only got one ... no issues..
418 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
420 //DB_DataObject::debugLevel(1);
421 // find out if any of them have got translations.
422 $ids = $t->fetchAll('id');
425 $t = DB_DataObject::factory($this->tableName());
426 $t->whereAddIn('src_id', $ids, 'int');
427 $t->whereAdd("txt != ''");
428 $t->orderBy('updated DESC');
431 // do any translations exist?
437 // delete all the others...
438 $t = DB_DataObject::factory($this->tableName());
439 $t->whereAddIn('src_id', $ids, 'int');
440 $t->whereAdd("src_id != $id");
444 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
447 $t = DB_DataObject::factory($this->tableName());
448 $t->whereAddIn('id', $ids, 'int');
449 $t->whereAdd("id != $id");
453 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
456 // this is done by calling code
457 //$t = DB_DataObject::factory($this->tableName());
458 //$t->query("update core_templatestr set active= 1 where src_id = $id");
469 function syncLang($lang)
471 // bugs with our old code...
473 $tn = $this->tableName();
474 $t = DB_DataObject::factory($tn);
475 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
477 // find all the id's from lang that have not been generated..
480 static $id_tmp = false;
482 if ($id_tmp == false) {
484 $t = DB_DataObject::factory($tn);
485 $t->whereAdd("lang = ''");
488 //old code, this did not support the on_table
489 // $id_tmp = $t->fetchAll('id','template_id');
490 // $ids = array_keys($id_tmp);
492 //new code for support the sync tables
493 foreach($t->fetchAll() as $ori){
494 $id_tmp[$ori->id] = $ori;
497 $ids = array_keys($id_tmp);
499 // matching by language:
500 $t = DB_DataObject::factory($tn);
501 $t->whereAddIn('src_id', $ids , 'int');
504 $got = $t->fetchAll('src_id');
505 $missing = array_diff($ids, $got);
507 if (empty($missing)) {
510 $t = DB_DataObject::factory($tn);
511 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
521 FROM core_templatestr
523 id IN (". implode(',', $missing) . ")
526 DB_DataObject::factory($tn)->query($q);
527 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
528 DB_DataObject::factory($tn)->query($q);
529 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
530 DB_DataObject::factory($tn)->query($q);
536 // called from flexy to translate a string.
539 function translateFlexyString($flexy, $string)
542 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
544 // using $flexy->currentTemplate -> find the template we are looking at..
545 // then find the string for $flexy->options['locale']
546 //DB_DataObject::debugLevel(1);
547 if ($debug) { var_dump($string); }
549 static $cache = array(); // cache of templates..
551 $ff = HTML_FlexyFramework::get();
552 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
553 if (empty($view_name)) {
554 $pg = HTML_FlexyFramework::get()->page;
555 if (isset($pg->templateViewName)) {
556 $view_name = $pg->templateViewName;
563 if ($debug) { var_dump(array('view_name'=> $view_name)); }
566 foreach($flexy->options['templateDir'] as $td) {
567 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
574 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
577 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
578 if ($debug) { echo "from cache no match - $string\n"; }
582 if (!isset($cache[$tmpname])) {
586 $tmpl = DB_DataObject::factory('core_template');
587 if ($view_name !== false) {
588 $tmpl->view_name = $view_name;
590 if(!$tmpl->get('template', $tmpname)){
591 // strip of site prefix if set...
593 $tmpl = DB_DataObject::factory('core_template');
594 if(!$tmpl->get('template', $tmpname)){
595 //var_dump("no template? {$tmpname} or {$relpath}" );
596 $cache[$tmpname] = false;
597 if ($debug) { echo "no template found - no match - $string\n"; }
601 $cache[$tmpname] = $tmpl;
603 $tmpl = $cache[$tmpname] ;
608 //get original template id
610 $orig = DB_DataObject::factory($this->tableName());
612 $orig->template_id = $tmpl->id;
615 $cache[$tmpname]->words =
617 if(!$orig->get( 'mdsum' , md5(trim($string)))){
618 //var_dump('no text? '. $string);
619 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
624 if (empty($cache[$tmpname]->translations)) {
626 //find out the text by language
627 //DB_DataObject::DebugLevel(1);
628 $x = DB_DataObject::factory($this->tableName());
629 $x->lang = $flexy->options['locale'];
630 $x->template_id = $tmpl->id;
632 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
633 if (empty($cache[$tmpname]->translations)) {
634 $cache[$tmpname]->translations = true;
636 //var_Dump($cache[$tmpname]->translations);
640 if ($cache[$tmpname]->translations === true) {
643 //var_dump("Checking: " . md5(trim($string)));
645 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
646 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
647 return $cache[$tmpname]->translations [md5(trim($string))];
654 function translateChanged($flexy)
657 $date = $this->lastUpdated($flexy);
658 if ($date === false) {
661 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
662 return strtotime($date) > $utime;
665 // determine if a complied template need recompling
667 function lastUpdated($flexy)
670 // var_dump('check changed?');
671 //DB_DataObject::debugLevel(1);
672 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
674 //var_dump($flexy->compiledTemplate);
675 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
678 static $cache = array(); // cache of templates..
680 $ff = HTML_FlexyFramework::get();
681 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
683 // find which of the template directories was actually used for the template.
686 foreach($flexy->options['templateDir'] as $td) {
687 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
694 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
696 // we do not have any record of this template..
697 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
701 if (!isset($cache[$tmpname])) {
705 $tmpl = DB_DataObject::factory('core_template');
706 if ($view_name !== false) {
707 $tmpl->view_name = $view_name;
709 if(!$tmpl->get('template', $tmpname)){
710 // strip of site prefix if set...
712 $tmpl = DB_DataObject::factory('core_template');
713 if(!$tmpl->get('template', $tmpname)){
714 //var_dump("no template? {$tmpname} or {$relpath}" );
715 $cache[$tmpname] = false;
719 $cache[$tmpname] = $tmpl;
721 $tmpl = $cache[$tmpname] ;
726 $x = DB_DataObject::factory($this->tableName());
727 $x->lang = $flexy->options['locale'];
729 $x->template_id = $tmpl->id;
730 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
731 if ($x->count() < 1) {
732 return false; // we don't have any record of it.
735 $x->selectAdd('max(updated) as max_updated');
737 return $x->max_updated;
742 function toRooArray($req) {
743 $ret = $this->toArray();
745 if (empty($req['csvCols'])) {
751 // translations for table columns
752 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
753 $ret['template_id_view_name'] = 'database';
754 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
760 function postListFilter($ar, $au, $req)
762 if (empty($req['csvCols'])) {
771 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
776 // translations for table columns
777 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
779 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
781 if(!empty($ret[$key])) {
788 $ret = array_values($ret);