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];
103 foreach($cols as $c) {
104 $x = $this->factory($this->tableName());
105 $x->on_id = $obj->pid();
108 $x->lang = ''; /// eg. base language..
109 $up = $x->find(true);
112 if ($up && $x->txt == $obj->$c) {
114 // deactivate empty words
115 if(empty($obj->$c)) {
116 $deactive[] = $x->id;
121 if(empty($obj->$c)) {
127 $x->mdsum = md5($obj->$c);
129 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
130 $up ? $x->update() : $x->insert();
133 if(count($deactive)) {
134 $t = DB_DataObject::factory($this->tableName());
135 // deactivate the parent data
136 $t->query("UPDATE core_templatestr
137 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
140 // deactivate the child data
141 $t->query("UPDATE core_templatestr
144 src_id IN (". implode(',' ,$deactive) . ")
154 function applyFiltersTree($q,$roo)
156 if (empty($q['node'])) {
157 $roo->jerr("invalid node");
161 case ($q['node'] == 'transtree'):
162 case ($q['node'] == 'langlist'):
163 // DB_DataObject::debugLevel(1);
164 $x = DB_Dataobject::Factory($this->tableName());
166 $x->selectAdd('distinct(lang) as lang');
168 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
169 $x->whereAdd("lang != ''");
171 foreach( $x->fetchAll() as $l) {
173 'text'=>$l->lang_name,
174 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
187 case preg_match('/^lang:/', $q['node']):
189 $lang = preg_replace('/^lang:/', '', $q['node']);
191 $x = DB_DataObject::factory('core_templatestr');
194 $x->selectAdd('distinct(view_name) as view_name');
196 $x->whereAdd('join_template_id_id.is_deleted = 0');
197 $x->orderBy('view_name DESC');
201 'text'=> $x->view_name,
202 'id' => 'view:'. $lang .':'. $x->view_name,
207 $ff = HTML_FlexyFramework::get()->Pman_Core;
209 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
210 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
213 'on_table' => $table,
214 'id' => 'table:'. $lang .':'. $table,
226 case preg_match('/^view:/', $q['node']):
229 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
231 $x = DB_DataObject::factory($this->tableName());
234 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
235 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
236 $x->whereAdd('join_template_id_id.is_deleted = 0');
238 $ids = $x->fetchAll('template_id');
241 //add the table type lists
243 $ff = HTML_FlexyFramework::get()->Pman_Core;
246 // $x->orderBy('template ASC');
247 // $x->whereAdd("lang != ''");
250 $xx = DB_Dataobject::Factory('core_template');
251 $xx->whereAddIn('id', $ids, 'int');
255 id, concat( template) as template_name
257 $xx->orderBy('template_name ASC');
259 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
279 * @param object $tmpl core_template data object
280 * @param array $words array of words
282 function syncTemplateWords($tmpl, $keyvalue = false)
285 $words = $tmpl->words;
286 // mapping for template :
287 //tablename => $n (templatename)
288 //tableid => $k (key value)
289 //colname => $n (templatename)
292 //print_r($words);exit;
294 $tt = DB_DataObject::factory($this->tableName());
297 $t->template_id = $tmpl->id;
298 $t->whereAdd("lang = ''");
301 // we have a situation where old md sums where created..
305 $cur = $t->fetchAll('mdsum', 'id');
313 // now loop through current..
314 $cwords = array();// not in used??
316 // echo "sync Template Words... \n";
319 foreach($words as $k=>$v) {
329 $md = $keyvalue ? $k : md5($v);
331 // check to see if there are more that one versions of that md5
334 if (!isset($cur[$md])) {
335 // create a record for it..
336 $t = DB_DataObject::factory($this->tableName());
339 'lang' => '',// by default should a english
340 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
341 'template_id'=>$tmpl->id,
346 $active[] = $t->insert();
349 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
351 $active[] = $cur[$md];
353 // we have it already? -
354 $t = DB_DataObject::factory($this->tableName());
355 $t->query("UPDATE {$this->tableName()}
368 if (count(array_values($cur))) {// de-active unused
370 $t = DB_DataObject::factory($this->tableName());
371 // echo "de-active current?? \n";
374 $deactive = array_values($cur);
375 $t->query("UPDATE core_templatestr
376 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
380 // delete all the items that are not relivant.
381 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
382 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
383 $t = DB_DataObject::factory($this->tableName());
385 // this will active the child data
386 if (empty($active)) {// set the active array to empty
389 $t->query("UPDATE core_templatestr
392 src_id IN (". implode(',' ,$active) . ")
394 template_id = {$tmpl->id}
396 //deactive the child data
397 if (empty($deactive)) {
398 $deactive = array(-1);
400 $t->query("UPDATE core_templatestr
403 src_id IN (". implode(',' ,$deactive) . ")
405 template_id = {$tmpl->id}
411 function checkDupes($tid, $lang, $id, $mdsum) {
413 $t = DB_DataObject::factory($this->tableName());
414 $t->template_id = $tid;
416 $t->whereAdd("lang = '{$lang}'");
417 if ($t->count() == 1) {
418 return $id; // only got one ... no issues..
421 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
423 //DB_DataObject::debugLevel(1);
424 // find out if any of them have got translations.
425 $ids = $t->fetchAll('id');
428 $t = DB_DataObject::factory($this->tableName());
429 $t->whereAddIn('src_id', $ids, 'int');
430 $t->whereAdd("txt != ''");
431 $t->orderBy('updated DESC');
434 // do any translations exist?
440 // delete all the others...
441 $t = DB_DataObject::factory($this->tableName());
442 $t->whereAddIn('src_id', $ids, 'int');
443 $t->whereAdd("src_id != $id");
447 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
450 $t = DB_DataObject::factory($this->tableName());
451 $t->whereAddIn('id', $ids, 'int');
452 $t->whereAdd("id != $id");
456 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
459 // this is done by calling code
460 //$t = DB_DataObject::factory($this->tableName());
461 //$t->query("update core_templatestr set active= 1 where src_id = $id");
472 function syncLang($lang)
474 // bugs with our old code...
476 $tn = $this->tableName();
477 $t = DB_DataObject::factory($tn);
478 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
480 // find all the id's from lang that have not been generated..
483 static $id_tmp = false;
485 if ($id_tmp == false) {
487 $t = DB_DataObject::factory($tn);
488 $t->whereAdd("lang = ''");
491 //old code, this did not support the on_table
492 // $id_tmp = $t->fetchAll('id','template_id');
493 // $ids = array_keys($id_tmp);
495 //new code for support the sync tables
496 foreach($t->fetchAll() as $ori){
497 $id_tmp[$ori->id] = $ori;
500 $ids = array_keys($id_tmp);
502 // matching by language:
503 $t = DB_DataObject::factory($tn);
504 $t->whereAddIn('src_id', $ids , 'int');
507 $got = $t->fetchAll('src_id');
508 $missing = array_diff($ids, $got);
510 if (empty($missing)) {
513 $t = DB_DataObject::factory($tn);
514 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
524 FROM core_templatestr
526 id IN (". implode(',', $missing) . ")
529 DB_DataObject::factory($tn)->query($q);
530 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
531 DB_DataObject::factory($tn)->query($q);
532 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
533 DB_DataObject::factory($tn)->query($q);
539 // called from flexy to translate a string.
542 function translateFlexyString($flexy, $string)
545 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
547 // using $flexy->currentTemplate -> find the template we are looking at..
548 // then find the string for $flexy->options['locale']
549 //DB_DataObject::debugLevel(1);
550 if ($debug) { var_dump($string); }
552 static $cache = array(); // cache of templates..
554 $ff = HTML_FlexyFramework::get();
555 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
556 if (empty($view_name)) {
557 $pg = HTML_FlexyFramework::get()->page;
558 if (isset($pg->templateViewName)) {
559 $view_name = $pg->templateViewName;
566 if ($debug) { var_dump(array('view_name'=> $view_name)); }
569 foreach($flexy->options['templateDir'] as $td) {
570 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
577 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
580 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
581 if ($debug) { echo "from cache no match - $string\n"; }
585 if (!isset($cache[$tmpname])) {
589 $tmpl = DB_DataObject::factory('core_template');
590 if ($view_name !== false) {
591 $tmpl->view_name = $view_name;
593 if(!$tmpl->get('template', $tmpname)){
594 // strip of site prefix if set...
596 $tmpl = DB_DataObject::factory('core_template');
597 if(!$tmpl->get('template', $tmpname)){
598 //var_dump("no template? {$tmpname} or {$relpath}" );
599 $cache[$tmpname] = false;
600 if ($debug) { echo "no template found - no match - $string\n"; }
604 $cache[$tmpname] = $tmpl;
606 $tmpl = $cache[$tmpname] ;
611 //get original template id
613 $orig = DB_DataObject::factory($this->tableName());
615 $orig->template_id = $tmpl->id;
618 $cache[$tmpname]->words =
620 if(!$orig->get( 'mdsum' , md5(trim($string)))){
621 //var_dump('no text? '. $string);
622 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
627 if (empty($cache[$tmpname]->translations)) {
629 //find out the text by language
630 //DB_DataObject::DebugLevel(1);
631 $x = DB_DataObject::factory($this->tableName());
632 $x->lang = $flexy->options['locale'];
633 $x->template_id = $tmpl->id;
635 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
636 if (empty($cache[$tmpname]->translations)) {
637 $cache[$tmpname]->translations = true;
639 //var_Dump($cache[$tmpname]->translations);
643 if ($cache[$tmpname]->translations === true) {
646 //var_dump("Checking: " . md5(trim($string)));
648 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
649 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
650 return $cache[$tmpname]->translations [md5(trim($string))];
657 function translateChanged($flexy)
660 $date = $this->lastUpdated($flexy);
661 if ($date === false) {
664 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
665 return strtotime($date) > $utime;
668 // determine if a complied template need recompling
670 function lastUpdated($flexy)
673 // var_dump('check changed?');
674 //DB_DataObject::debugLevel(1);
675 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
677 //var_dump($flexy->compiledTemplate);
678 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
681 static $cache = array(); // cache of templates..
683 $ff = HTML_FlexyFramework::get();
684 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
686 // find which of the template directories was actually used for the template.
689 foreach($flexy->options['templateDir'] as $td) {
690 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
697 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
699 // we do not have any record of this template..
700 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
704 if (!isset($cache[$tmpname])) {
708 $tmpl = DB_DataObject::factory('core_template');
709 if ($view_name !== false) {
710 $tmpl->view_name = $view_name;
712 if(!$tmpl->get('template', $tmpname)){
713 // strip of site prefix if set...
715 $tmpl = DB_DataObject::factory('core_template');
716 if(!$tmpl->get('template', $tmpname)){
717 //var_dump("no template? {$tmpname} or {$relpath}" );
718 $cache[$tmpname] = false;
722 $cache[$tmpname] = $tmpl;
724 $tmpl = $cache[$tmpname] ;
729 $x = DB_DataObject::factory($this->tableName());
730 $x->lang = $flexy->options['locale'];
732 $x->template_id = $tmpl->id;
733 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
734 if ($x->count() < 1) {
735 return false; // we don't have any record of it.
738 $x->selectAdd('max(updated) as max_updated');
740 return $x->max_updated;
745 function toRooArray($req) {
746 $ret = $this->toArray();
748 if (empty($req['csvCols'])) {
754 // translations for table columns
755 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
756 $ret['template_id_view_name'] = 'database';
757 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
763 function postListFilter($ar, $au, $req)
765 if (empty($req['csvCols'])) {
774 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
779 // translations for table columns
780 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
782 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
784 if(!empty($ret[$key])) {
791 $ret = array_values($ret);