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);
113 if ($up && $x->txt == $obj->$c) {
115 // deactivate empty words
116 if(empty($obj->$c)) {
117 $deactive[] = $x->id;
119 continue; // update an no change..
123 if(empty($obj->$c)) {
129 $x->mdsum = md5($obj->$c);
131 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
132 $up ? $x->update() : $x->insert();
135 if(count($deactive)) {
136 $t = DB_DataObject::factory($this->tableName());
137 // deactivate the parent data
138 $t->query("UPDATE core_templatestr
139 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
142 // deactivate the child data
143 $t->query("UPDATE core_templatestr
146 src_id IN (". implode(',' ,$deactive) . ")
156 function applyFiltersTree($q,$roo)
158 if (empty($q['node'])) {
159 $roo->jerr("invalid node");
163 case ($q['node'] == 'transtree'):
164 case ($q['node'] == 'langlist'):
165 // DB_DataObject::debugLevel(1);
166 $x = DB_Dataobject::Factory($this->tableName());
168 $x->selectAdd('distinct(lang) as lang');
170 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
171 $x->whereAdd("lang != ''");
173 foreach( $x->fetchAll() as $l) {
175 'text'=>$l->lang_name,
176 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
189 case preg_match('/^lang:/', $q['node']):
191 $lang = preg_replace('/^lang:/', '', $q['node']);
193 $x = DB_DataObject::factory('core_templatestr');
196 $x->selectAdd('distinct(view_name) as view_name');
198 $x->whereAdd('join_template_id_id.is_deleted = 0');
199 $x->orderBy('view_name DESC');
203 'text'=> $x->view_name,
204 'id' => 'view:'. $lang .':'. $x->view_name,
209 $ff = HTML_FlexyFramework::get()->Pman_Core;
211 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
212 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
215 'on_table' => $table,
216 'id' => 'table:'. $lang .':'. $table,
228 case preg_match('/^view:/', $q['node']):
231 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
233 $x = DB_DataObject::factory($this->tableName());
236 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
237 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
238 $x->whereAdd('join_template_id_id.is_deleted = 0');
240 $ids = $x->fetchAll('template_id');
243 //add the table type lists
245 $ff = HTML_FlexyFramework::get()->Pman_Core;
248 // $x->orderBy('template ASC');
249 // $x->whereAdd("lang != ''");
252 $xx = DB_Dataobject::Factory('core_template');
253 $xx->whereAddIn('id', $ids, 'int');
257 id, concat( template) as template_name
259 $xx->orderBy('template_name ASC');
261 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
281 * @param object $tmpl core_template data object
282 * @param array $words array of words
284 function syncTemplateWords($tmpl, $keyvalue = false)
287 $words = $tmpl->words;
288 // mapping for template :
289 //tablename => $n (templatename)
290 //tableid => $k (key value)
291 //colname => $n (templatename)
294 //print_r($words);exit;
296 $tt = DB_DataObject::factory($this->tableName());
299 $t->template_id = $tmpl->id;
300 $t->whereAdd("lang = ''");
303 // we have a situation where old md sums where created..
307 $cur = $t->fetchAll('mdsum', 'id');
315 // now loop through current..
316 $cwords = array();// not in used??
318 // echo "sync Template Words... \n";
321 foreach($words as $k=>$v) {
331 $md = $keyvalue ? $k : md5($v);
333 // check to see if there are more that one versions of that md5
336 if (!isset($cur[$md])) {
337 // create a record for it..
338 $t = DB_DataObject::factory($this->tableName());
341 'lang' => '',// by default should a english
342 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
343 'template_id'=>$tmpl->id,
348 $active[] = $t->insert();
351 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
353 $active[] = $cur[$md];
355 // we have it already? -
356 $t = DB_DataObject::factory($this->tableName());
357 $t->query("UPDATE {$this->tableName()}
370 if (count(array_values($cur))) {// de-active unused
372 $t = DB_DataObject::factory($this->tableName());
373 // echo "de-active current?? \n";
376 $deactive = array_values($cur);
377 $t->query("UPDATE core_templatestr
378 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
382 // delete all the items that are not relivant.
383 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
384 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
385 $t = DB_DataObject::factory($this->tableName());
387 // this will active the child data
388 if (empty($active)) {// set the active array to empty
391 $t->query("UPDATE core_templatestr
394 src_id IN (". implode(',' ,$active) . ")
396 template_id = {$tmpl->id}
398 //deactive the child data
399 if (empty($deactive)) {
400 $deactive = array(-1);
402 $t->query("UPDATE core_templatestr
405 src_id IN (". implode(',' ,$deactive) . ")
407 template_id = {$tmpl->id}
413 function checkDupes($tid, $lang, $id, $mdsum) {
415 $t = DB_DataObject::factory($this->tableName());
416 $t->template_id = $tid;
418 $t->whereAdd("lang = '{$lang}'");
419 if ($t->count() == 1) {
420 return $id; // only got one ... no issues..
423 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
425 //DB_DataObject::debugLevel(1);
426 // find out if any of them have got translations.
427 $ids = $t->fetchAll('id');
430 $t = DB_DataObject::factory($this->tableName());
431 $t->whereAddIn('src_id', $ids, 'int');
432 $t->whereAdd("txt != ''");
433 $t->orderBy('updated DESC');
436 // do any translations exist?
442 // delete all the others...
443 $t = DB_DataObject::factory($this->tableName());
444 $t->whereAddIn('src_id', $ids, 'int');
445 $t->whereAdd("src_id != $id");
449 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
452 $t = DB_DataObject::factory($this->tableName());
453 $t->whereAddIn('id', $ids, 'int');
454 $t->whereAdd("id != $id");
458 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
461 // this is done by calling code
462 //$t = DB_DataObject::factory($this->tableName());
463 //$t->query("update core_templatestr set active= 1 where src_id = $id");
474 function syncLang($lang)
476 // bugs with our old code...
478 $tn = $this->tableName();
479 $t = DB_DataObject::factory($tn);
480 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
482 // find all the id's from lang that have not been generated..
485 static $id_tmp = false;
487 if ($id_tmp == false) {
489 $t = DB_DataObject::factory($tn);
490 $t->whereAdd("lang = ''");
493 //old code, this did not support the on_table
494 // $id_tmp = $t->fetchAll('id','template_id');
495 // $ids = array_keys($id_tmp);
497 //new code for support the sync tables
498 foreach($t->fetchAll() as $ori){
499 $id_tmp[$ori->id] = $ori;
502 $ids = array_keys($id_tmp);
504 // matching by language:
505 $t = DB_DataObject::factory($tn);
506 $t->whereAddIn('src_id', $ids , 'int');
509 $got = $t->fetchAll('src_id');
510 $missing = array_diff($ids, $got);
512 if (empty($missing)) {
515 $t = DB_DataObject::factory($tn);
516 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
526 FROM core_templatestr
528 id IN (". implode(',', $missing) . ")
531 DB_DataObject::factory($tn)->query($q);
532 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
533 DB_DataObject::factory($tn)->query($q);
534 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
535 DB_DataObject::factory($tn)->query($q);
541 // called from flexy to translate a string.
544 function translateFlexyString($flexy, $string)
547 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
549 // using $flexy->currentTemplate -> find the template we are looking at..
550 // then find the string for $flexy->options['locale']
551 //DB_DataObject::debugLevel(1);
552 if ($debug) { var_dump($string); }
554 static $cache = array(); // cache of templates..
556 $ff = HTML_FlexyFramework::get();
557 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
558 if (empty($view_name)) {
559 $pg = HTML_FlexyFramework::get()->page;
560 if (isset($pg->templateViewName)) {
561 $view_name = $pg->templateViewName;
568 if ($debug) { var_dump(array('view_name'=> $view_name)); }
571 foreach($flexy->options['templateDir'] as $td) {
572 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
579 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
582 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
583 if ($debug) { echo "from cache no match - $string\n"; }
587 if (!isset($cache[$tmpname])) {
591 $tmpl = DB_DataObject::factory('core_template');
592 if ($view_name !== false) {
593 $tmpl->view_name = $view_name;
595 if(!$tmpl->get('template', $tmpname)){
596 // strip of site prefix if set...
598 $tmpl = DB_DataObject::factory('core_template');
599 if(!$tmpl->get('template', $tmpname)){
600 //var_dump("no template? {$tmpname} or {$relpath}" );
601 $cache[$tmpname] = false;
602 if ($debug) { echo "no template found - no match - $string\n"; }
606 $cache[$tmpname] = $tmpl;
608 $tmpl = $cache[$tmpname] ;
613 //get original template id
615 $orig = DB_DataObject::factory($this->tableName());
617 $orig->template_id = $tmpl->id;
620 $cache[$tmpname]->words =
622 if(!$orig->get( 'mdsum' , md5(trim($string)))){
623 //var_dump('no text? '. $string);
624 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
629 if (empty($cache[$tmpname]->translations)) {
631 //find out the text by language
632 //DB_DataObject::DebugLevel(1);
633 $x = DB_DataObject::factory($this->tableName());
634 $x->lang = $flexy->options['locale'];
635 $x->template_id = $tmpl->id;
637 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
638 if (empty($cache[$tmpname]->translations)) {
639 $cache[$tmpname]->translations = true;
641 //var_Dump($cache[$tmpname]->translations);
645 if ($cache[$tmpname]->translations === true) {
648 //var_dump("Checking: " . md5(trim($string)));
650 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
651 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
652 return $cache[$tmpname]->translations [md5(trim($string))];
659 function translateChanged($flexy)
662 $date = $this->lastUpdated($flexy);
663 if ($date === false) {
666 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
667 return strtotime($date) > $utime;
670 // determine if a complied template need recompling
672 function lastUpdated($flexy)
675 // var_dump('check changed?');
676 //DB_DataObject::debugLevel(1);
677 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
679 //var_dump($flexy->compiledTemplate);
680 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
683 static $cache = array(); // cache of templates..
685 $ff = HTML_FlexyFramework::get();
686 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
688 // find which of the template directories was actually used for the template.
691 foreach($flexy->options['templateDir'] as $td) {
692 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
699 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
701 // we do not have any record of this template..
702 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
706 if (!isset($cache[$tmpname])) {
710 $tmpl = DB_DataObject::factory('core_template');
711 if ($view_name !== false) {
712 $tmpl->view_name = $view_name;
714 if(!$tmpl->get('template', $tmpname)){
715 // strip of site prefix if set...
717 $tmpl = DB_DataObject::factory('core_template');
718 if(!$tmpl->get('template', $tmpname)){
719 //var_dump("no template? {$tmpname} or {$relpath}" );
720 $cache[$tmpname] = false;
724 $cache[$tmpname] = $tmpl;
726 $tmpl = $cache[$tmpname] ;
731 $x = DB_DataObject::factory($this->tableName());
732 $x->lang = $flexy->options['locale'];
734 $x->template_id = $tmpl->id;
735 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
736 if ($x->count() < 1) {
737 return false; // we don't have any record of it.
740 $x->selectAdd('max(updated) as max_updated');
742 return $x->max_updated;
747 function toRooArray($req) {
748 $ret = $this->toArray();
750 if (empty($req['csvCols'])) {
756 // translations for table columns
757 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
758 $ret['template_id_view_name'] = 'database';
759 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
765 function postListFilter($ar, $au, $req)
767 if (empty($req['csvCols'])) {
776 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
781 // translations for table columns
782 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
784 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
786 if(!empty($ret[$key])) {
793 $ret = array_values($ret);