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'])){
185 $tables = $ff['DataObjects_Core_templatestr']['tables'];
187 $cts = DB_DataObject::factory($this->tableName());
188 $cts->whereAdd('on_table != ""');
190 $cur = $cts->fetchAll();
193 foreach($cur as $ts) {
195 if(empty($tables[$ts->on_table])) {
199 var_dump($ts->on_table);
200 var_dump($ts->on_col);
206 function applyFiltersTree($q,$roo)
208 if (empty($q['node'])) {
209 $roo->jerr("invalid node");
213 case ($q['node'] == 'transtree'):
214 case ($q['node'] == 'langlist'):
215 // DB_DataObject::debugLevel(1);
216 $x = DB_Dataobject::Factory($this->tableName());
218 $x->selectAdd('distinct(lang) as lang');
220 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
221 $x->whereAdd("lang != ''");
223 foreach( $x->fetchAll() as $l) {
225 'text'=>$l->lang_name,
226 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
239 case preg_match('/^lang:/', $q['node']):
241 $lang = preg_replace('/^lang:/', '', $q['node']);
243 $x = DB_DataObject::factory('core_templatestr');
246 $x->selectAdd('distinct(view_name) as view_name');
248 $x->whereAdd('join_template_id_id.is_deleted = 0');
249 $x->orderBy('view_name DESC');
253 'text'=> $x->view_name,
254 'id' => 'view:'. $lang .':'. $x->view_name,
259 $ff = HTML_FlexyFramework::get()->Pman_Core;
261 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
262 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
265 'on_table' => $table,
266 'id' => 'table:'. $lang .':'. $table,
278 case preg_match('/^view:/', $q['node']):
281 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
283 $x = DB_DataObject::factory($this->tableName());
286 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
287 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
288 $x->whereAdd('join_template_id_id.is_deleted = 0');
290 $ids = $x->fetchAll('template_id');
293 //add the table type lists
295 $ff = HTML_FlexyFramework::get()->Pman_Core;
298 // $x->orderBy('template ASC');
299 // $x->whereAdd("lang != ''");
302 $xx = DB_Dataobject::Factory('core_template');
303 $xx->whereAddIn('id', $ids, 'int');
307 id, concat( template) as template_name
309 $xx->orderBy('template_name ASC');
311 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
331 * @param object $tmpl core_template data object
332 * @param array $words array of words
334 function syncTemplateWords($tmpl, $keyvalue = false)
337 $words = $tmpl->words;
338 // mapping for template :
339 //tablename => $n (templatename)
340 //tableid => $k (key value)
341 //colname => $n (templatename)
344 //print_r($words);exit;
346 $tt = DB_DataObject::factory($this->tableName());
349 $t->template_id = $tmpl->id;
350 $t->whereAdd("lang = ''");
353 // we have a situation where old md sums where created..
357 $cur = $t->fetchAll('mdsum', 'id');
365 // now loop through current..
366 $cwords = array();// not in used??
368 // echo "sync Template Words... \n";
371 foreach($words as $k=>$v) {
381 $md = $keyvalue ? $k : md5($v);
383 // check to see if there are more that one versions of that md5
386 if (!isset($cur[$md])) {
387 // create a record for it..
388 $t = DB_DataObject::factory($this->tableName());
391 'lang' => '',// by default should a english
392 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
393 'template_id'=>$tmpl->id,
398 $active[] = $t->insert();
401 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
403 $active[] = $cur[$md];
405 // we have it already? -
406 $t = DB_DataObject::factory($this->tableName());
407 $t->query("UPDATE {$this->tableName()}
420 if (count(array_values($cur))) {// de-active unused
422 $t = DB_DataObject::factory($this->tableName());
423 // echo "de-active current?? \n";
426 $deactive = array_values($cur);
427 $t->query("UPDATE core_templatestr
428 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
432 // delete all the items that are not relivant.
433 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
434 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
435 $t = DB_DataObject::factory($this->tableName());
437 // this will active the child data
438 if (empty($active)) {// set the active array to empty
441 $t->query("UPDATE core_templatestr
444 src_id IN (". implode(',' ,$active) . ")
446 template_id = {$tmpl->id}
448 //deactive the child data
449 if (empty($deactive)) {
450 $deactive = array(-1);
452 $t->query("UPDATE core_templatestr
455 src_id IN (". implode(',' ,$deactive) . ")
457 template_id = {$tmpl->id}
463 function checkDupes($tid, $lang, $id, $mdsum) {
465 $t = DB_DataObject::factory($this->tableName());
466 $t->template_id = $tid;
468 $t->whereAdd("lang = '{$lang}'");
469 if ($t->count() == 1) {
470 return $id; // only got one ... no issues..
473 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
475 //DB_DataObject::debugLevel(1);
476 // find out if any of them have got translations.
477 $ids = $t->fetchAll('id');
480 $t = DB_DataObject::factory($this->tableName());
481 $t->whereAddIn('src_id', $ids, 'int');
482 $t->whereAdd("txt != ''");
483 $t->orderBy('updated DESC');
486 // do any translations exist?
492 // delete all the others...
493 $t = DB_DataObject::factory($this->tableName());
494 $t->whereAddIn('src_id', $ids, 'int');
495 $t->whereAdd("src_id != $id");
499 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
502 $t = DB_DataObject::factory($this->tableName());
503 $t->whereAddIn('id', $ids, 'int');
504 $t->whereAdd("id != $id");
508 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
511 // this is done by calling code
512 //$t = DB_DataObject::factory($this->tableName());
513 //$t->query("update core_templatestr set active= 1 where src_id = $id");
524 function syncLang($lang)
526 // bugs with our old code...
528 $tn = $this->tableName();
529 $t = DB_DataObject::factory($tn);
530 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
532 // find all the id's from lang that have not been generated..
535 static $id_tmp = false;
537 if ($id_tmp == false) {
539 $t = DB_DataObject::factory($tn);
540 $t->whereAdd("lang = ''");
543 //old code, this did not support the on_table
544 // $id_tmp = $t->fetchAll('id','template_id');
545 // $ids = array_keys($id_tmp);
547 //new code for support the sync tables
548 foreach($t->fetchAll() as $ori){
549 $id_tmp[$ori->id] = $ori;
552 $ids = array_keys($id_tmp);
554 // matching by language:
555 $t = DB_DataObject::factory($tn);
556 $t->whereAddIn('src_id', $ids , 'int');
559 $got = $t->fetchAll('src_id');
560 $missing = array_diff($ids, $got);
562 if (empty($missing)) {
565 $t = DB_DataObject::factory($tn);
566 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
576 FROM core_templatestr
578 id IN (". implode(',', $missing) . ")
581 DB_DataObject::factory($tn)->query($q);
582 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
583 DB_DataObject::factory($tn)->query($q);
584 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
585 DB_DataObject::factory($tn)->query($q);
591 // called from flexy to translate a string.
594 function translateFlexyString($flexy, $string)
597 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
599 // using $flexy->currentTemplate -> find the template we are looking at..
600 // then find the string for $flexy->options['locale']
601 //DB_DataObject::debugLevel(1);
602 if ($debug) { var_dump($string); }
604 static $cache = array(); // cache of templates..
606 $ff = HTML_FlexyFramework::get();
607 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
608 if (empty($view_name)) {
609 $pg = HTML_FlexyFramework::get()->page;
610 if (isset($pg->templateViewName)) {
611 $view_name = $pg->templateViewName;
618 if ($debug) { var_dump(array('view_name'=> $view_name)); }
621 foreach($flexy->options['templateDir'] as $td) {
622 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
629 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
632 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
633 if ($debug) { echo "from cache no match - $string\n"; }
637 if (!isset($cache[$tmpname])) {
641 $tmpl = DB_DataObject::factory('core_template');
642 if ($view_name !== false) {
643 $tmpl->view_name = $view_name;
645 if(!$tmpl->get('template', $tmpname)){
646 // strip of site prefix if set...
648 $tmpl = DB_DataObject::factory('core_template');
649 if(!$tmpl->get('template', $tmpname)){
650 //var_dump("no template? {$tmpname} or {$relpath}" );
651 $cache[$tmpname] = false;
652 if ($debug) { echo "no template found - no match - $string\n"; }
656 $cache[$tmpname] = $tmpl;
658 $tmpl = $cache[$tmpname] ;
663 //get original template id
665 $orig = DB_DataObject::factory($this->tableName());
667 $orig->template_id = $tmpl->id;
670 $cache[$tmpname]->words =
672 if(!$orig->get( 'mdsum' , md5(trim($string)))){
673 //var_dump('no text? '. $string);
674 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
679 if (empty($cache[$tmpname]->translations)) {
681 //find out the text by language
682 //DB_DataObject::DebugLevel(1);
683 $x = DB_DataObject::factory($this->tableName());
684 $x->lang = $flexy->options['locale'];
685 $x->template_id = $tmpl->id;
687 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
688 if (empty($cache[$tmpname]->translations)) {
689 $cache[$tmpname]->translations = true;
691 //var_Dump($cache[$tmpname]->translations);
695 if ($cache[$tmpname]->translations === true) {
698 //var_dump("Checking: " . md5(trim($string)));
700 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
701 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
702 return $cache[$tmpname]->translations [md5(trim($string))];
709 function translateChanged($flexy)
712 $date = $this->lastUpdated($flexy);
713 if ($date === false) {
716 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
717 return strtotime($date) > $utime;
720 // determine if a complied template need recompling
722 function lastUpdated($flexy)
725 // var_dump('check changed?');
726 //DB_DataObject::debugLevel(1);
727 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
729 //var_dump($flexy->compiledTemplate);
730 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
733 static $cache = array(); // cache of templates..
735 $ff = HTML_FlexyFramework::get();
736 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
738 // find which of the template directories was actually used for the template.
741 foreach($flexy->options['templateDir'] as $td) {
742 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
749 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
751 // we do not have any record of this template..
752 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
756 if (!isset($cache[$tmpname])) {
760 $tmpl = DB_DataObject::factory('core_template');
761 if ($view_name !== false) {
762 $tmpl->view_name = $view_name;
764 if(!$tmpl->get('template', $tmpname)){
765 // strip of site prefix if set...
767 $tmpl = DB_DataObject::factory('core_template');
768 if(!$tmpl->get('template', $tmpname)){
769 //var_dump("no template? {$tmpname} or {$relpath}" );
770 $cache[$tmpname] = false;
774 $cache[$tmpname] = $tmpl;
776 $tmpl = $cache[$tmpname] ;
781 $x = DB_DataObject::factory($this->tableName());
782 $x->lang = $flexy->options['locale'];
784 $x->template_id = $tmpl->id;
785 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
786 if ($x->count() < 1) {
787 return false; // we don't have any record of it.
790 $x->selectAdd('max(updated) as max_updated');
792 return $x->max_updated;
797 function toRooArray($req) {
798 $ret = $this->toArray();
800 if (empty($req['csvCols'])) {
806 // translations for table columns
807 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
808 $ret['template_id_view_name'] = 'database';
809 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
815 function postListFilter($ar, $au, $req)
817 if (empty($req['csvCols'])) {
826 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
831 // translations for table columns
832 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
834 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
836 if(!empty($ret[$key])) {
843 $ret = array_values($ret);