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();
192 foreach($cur as $ts) {
193 var_dump($ts->on_table);
194 var_dump($ts->on_col);
200 function applyFiltersTree($q,$roo)
202 if (empty($q['node'])) {
203 $roo->jerr("invalid node");
207 case ($q['node'] == 'transtree'):
208 case ($q['node'] == 'langlist'):
209 // DB_DataObject::debugLevel(1);
210 $x = DB_Dataobject::Factory($this->tableName());
212 $x->selectAdd('distinct(lang) as lang');
214 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
215 $x->whereAdd("lang != ''");
217 foreach( $x->fetchAll() as $l) {
219 'text'=>$l->lang_name,
220 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
233 case preg_match('/^lang:/', $q['node']):
235 $lang = preg_replace('/^lang:/', '', $q['node']);
237 $x = DB_DataObject::factory('core_templatestr');
240 $x->selectAdd('distinct(view_name) as view_name');
242 $x->whereAdd('join_template_id_id.is_deleted = 0');
243 $x->orderBy('view_name DESC');
247 'text'=> $x->view_name,
248 'id' => 'view:'. $lang .':'. $x->view_name,
253 $ff = HTML_FlexyFramework::get()->Pman_Core;
255 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
256 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
259 'on_table' => $table,
260 'id' => 'table:'. $lang .':'. $table,
272 case preg_match('/^view:/', $q['node']):
275 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
277 $x = DB_DataObject::factory($this->tableName());
280 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
281 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
282 $x->whereAdd('join_template_id_id.is_deleted = 0');
284 $ids = $x->fetchAll('template_id');
287 //add the table type lists
289 $ff = HTML_FlexyFramework::get()->Pman_Core;
292 // $x->orderBy('template ASC');
293 // $x->whereAdd("lang != ''");
296 $xx = DB_Dataobject::Factory('core_template');
297 $xx->whereAddIn('id', $ids, 'int');
301 id, concat( template) as template_name
303 $xx->orderBy('template_name ASC');
305 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
325 * @param object $tmpl core_template data object
326 * @param array $words array of words
328 function syncTemplateWords($tmpl, $keyvalue = false)
331 $words = $tmpl->words;
332 // mapping for template :
333 //tablename => $n (templatename)
334 //tableid => $k (key value)
335 //colname => $n (templatename)
338 //print_r($words);exit;
340 $tt = DB_DataObject::factory($this->tableName());
343 $t->template_id = $tmpl->id;
344 $t->whereAdd("lang = ''");
347 // we have a situation where old md sums where created..
351 $cur = $t->fetchAll('mdsum', 'id');
359 // now loop through current..
360 $cwords = array();// not in used??
362 // echo "sync Template Words... \n";
365 foreach($words as $k=>$v) {
375 $md = $keyvalue ? $k : md5($v);
377 // check to see if there are more that one versions of that md5
380 if (!isset($cur[$md])) {
381 // create a record for it..
382 $t = DB_DataObject::factory($this->tableName());
385 'lang' => '',// by default should a english
386 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
387 'template_id'=>$tmpl->id,
392 $active[] = $t->insert();
395 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
397 $active[] = $cur[$md];
399 // we have it already? -
400 $t = DB_DataObject::factory($this->tableName());
401 $t->query("UPDATE {$this->tableName()}
414 if (count(array_values($cur))) {// de-active unused
416 $t = DB_DataObject::factory($this->tableName());
417 // echo "de-active current?? \n";
420 $deactive = array_values($cur);
421 $t->query("UPDATE core_templatestr
422 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
426 // delete all the items that are not relivant.
427 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
428 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
429 $t = DB_DataObject::factory($this->tableName());
431 // this will active the child data
432 if (empty($active)) {// set the active array to empty
435 $t->query("UPDATE core_templatestr
438 src_id IN (". implode(',' ,$active) . ")
440 template_id = {$tmpl->id}
442 //deactive the child data
443 if (empty($deactive)) {
444 $deactive = array(-1);
446 $t->query("UPDATE core_templatestr
449 src_id IN (". implode(',' ,$deactive) . ")
451 template_id = {$tmpl->id}
457 function checkDupes($tid, $lang, $id, $mdsum) {
459 $t = DB_DataObject::factory($this->tableName());
460 $t->template_id = $tid;
462 $t->whereAdd("lang = '{$lang}'");
463 if ($t->count() == 1) {
464 return $id; // only got one ... no issues..
467 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
469 //DB_DataObject::debugLevel(1);
470 // find out if any of them have got translations.
471 $ids = $t->fetchAll('id');
474 $t = DB_DataObject::factory($this->tableName());
475 $t->whereAddIn('src_id', $ids, 'int');
476 $t->whereAdd("txt != ''");
477 $t->orderBy('updated DESC');
480 // do any translations exist?
486 // delete all the others...
487 $t = DB_DataObject::factory($this->tableName());
488 $t->whereAddIn('src_id', $ids, 'int');
489 $t->whereAdd("src_id != $id");
493 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
496 $t = DB_DataObject::factory($this->tableName());
497 $t->whereAddIn('id', $ids, 'int');
498 $t->whereAdd("id != $id");
502 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
505 // this is done by calling code
506 //$t = DB_DataObject::factory($this->tableName());
507 //$t->query("update core_templatestr set active= 1 where src_id = $id");
518 function syncLang($lang)
520 // bugs with our old code...
522 $tn = $this->tableName();
523 $t = DB_DataObject::factory($tn);
524 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
526 // find all the id's from lang that have not been generated..
529 static $id_tmp = false;
531 if ($id_tmp == false) {
533 $t = DB_DataObject::factory($tn);
534 $t->whereAdd("lang = ''");
537 //old code, this did not support the on_table
538 // $id_tmp = $t->fetchAll('id','template_id');
539 // $ids = array_keys($id_tmp);
541 //new code for support the sync tables
542 foreach($t->fetchAll() as $ori){
543 $id_tmp[$ori->id] = $ori;
546 $ids = array_keys($id_tmp);
548 // matching by language:
549 $t = DB_DataObject::factory($tn);
550 $t->whereAddIn('src_id', $ids , 'int');
553 $got = $t->fetchAll('src_id');
554 $missing = array_diff($ids, $got);
556 if (empty($missing)) {
559 $t = DB_DataObject::factory($tn);
560 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
570 FROM core_templatestr
572 id IN (". implode(',', $missing) . ")
575 DB_DataObject::factory($tn)->query($q);
576 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
577 DB_DataObject::factory($tn)->query($q);
578 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
579 DB_DataObject::factory($tn)->query($q);
585 // called from flexy to translate a string.
588 function translateFlexyString($flexy, $string)
591 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
593 // using $flexy->currentTemplate -> find the template we are looking at..
594 // then find the string for $flexy->options['locale']
595 //DB_DataObject::debugLevel(1);
596 if ($debug) { var_dump($string); }
598 static $cache = array(); // cache of templates..
600 $ff = HTML_FlexyFramework::get();
601 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
602 if (empty($view_name)) {
603 $pg = HTML_FlexyFramework::get()->page;
604 if (isset($pg->templateViewName)) {
605 $view_name = $pg->templateViewName;
612 if ($debug) { var_dump(array('view_name'=> $view_name)); }
615 foreach($flexy->options['templateDir'] as $td) {
616 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
623 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
626 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
627 if ($debug) { echo "from cache no match - $string\n"; }
631 if (!isset($cache[$tmpname])) {
635 $tmpl = DB_DataObject::factory('core_template');
636 if ($view_name !== false) {
637 $tmpl->view_name = $view_name;
639 if(!$tmpl->get('template', $tmpname)){
640 // strip of site prefix if set...
642 $tmpl = DB_DataObject::factory('core_template');
643 if(!$tmpl->get('template', $tmpname)){
644 //var_dump("no template? {$tmpname} or {$relpath}" );
645 $cache[$tmpname] = false;
646 if ($debug) { echo "no template found - no match - $string\n"; }
650 $cache[$tmpname] = $tmpl;
652 $tmpl = $cache[$tmpname] ;
657 //get original template id
659 $orig = DB_DataObject::factory($this->tableName());
661 $orig->template_id = $tmpl->id;
664 $cache[$tmpname]->words =
666 if(!$orig->get( 'mdsum' , md5(trim($string)))){
667 //var_dump('no text? '. $string);
668 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
673 if (empty($cache[$tmpname]->translations)) {
675 //find out the text by language
676 //DB_DataObject::DebugLevel(1);
677 $x = DB_DataObject::factory($this->tableName());
678 $x->lang = $flexy->options['locale'];
679 $x->template_id = $tmpl->id;
681 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
682 if (empty($cache[$tmpname]->translations)) {
683 $cache[$tmpname]->translations = true;
685 //var_Dump($cache[$tmpname]->translations);
689 if ($cache[$tmpname]->translations === true) {
692 //var_dump("Checking: " . md5(trim($string)));
694 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
695 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
696 return $cache[$tmpname]->translations [md5(trim($string))];
703 function translateChanged($flexy)
706 $date = $this->lastUpdated($flexy);
707 if ($date === false) {
710 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
711 return strtotime($date) > $utime;
714 // determine if a complied template need recompling
716 function lastUpdated($flexy)
719 // var_dump('check changed?');
720 //DB_DataObject::debugLevel(1);
721 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
723 //var_dump($flexy->compiledTemplate);
724 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
727 static $cache = array(); // cache of templates..
729 $ff = HTML_FlexyFramework::get();
730 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
732 // find which of the template directories was actually used for the template.
735 foreach($flexy->options['templateDir'] as $td) {
736 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
743 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
745 // we do not have any record of this template..
746 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
750 if (!isset($cache[$tmpname])) {
754 $tmpl = DB_DataObject::factory('core_template');
755 if ($view_name !== false) {
756 $tmpl->view_name = $view_name;
758 if(!$tmpl->get('template', $tmpname)){
759 // strip of site prefix if set...
761 $tmpl = DB_DataObject::factory('core_template');
762 if(!$tmpl->get('template', $tmpname)){
763 //var_dump("no template? {$tmpname} or {$relpath}" );
764 $cache[$tmpname] = false;
768 $cache[$tmpname] = $tmpl;
770 $tmpl = $cache[$tmpname] ;
775 $x = DB_DataObject::factory($this->tableName());
776 $x->lang = $flexy->options['locale'];
778 $x->template_id = $tmpl->id;
779 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
780 if ($x->count() < 1) {
781 return false; // we don't have any record of it.
784 $x->selectAdd('max(updated) as max_updated');
786 return $x->max_updated;
791 function toRooArray($req) {
792 $ret = $this->toArray();
794 if (empty($req['csvCols'])) {
800 // translations for table columns
801 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
802 $ret['template_id_view_name'] = 'database';
803 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
809 function postListFilter($ar, $au, $req)
811 if (empty($req['csvCols'])) {
820 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
825 // translations for table columns
826 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
828 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
830 if(!empty($ret[$key])) {
837 $ret = array_values($ret);