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];
102 foreach($cols as $c) {
104 if(strpos($c, ',') !== false) {
105 $arr = explode(',', $c);
109 $ar = explode('=', $cond);
112 // skip if condition not fulfilled
113 if($obj->{$ar[0]} != $ar[1]) {
118 if(empty($obj->$c)) {
119 $deactive[] = $x->id;
123 $x = $this->factory($this->tableName());
124 $x->on_id = $obj->pid();
127 $x->lang = ''; /// eg. base language..
128 $up = $x->find(true);
131 // deactivate empty words
132 if(empty($obj->$c)) {
135 // activate non-empty words
140 if($x->txt == $obj->$c) {
141 continue; // skip when no change
146 if(empty($obj->$c)) {
154 $x->mdsum = md5($obj->$c);
156 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
157 $up ? $x->update() : $x->insert();
161 $t = DB_DataObject::factory($this->tableName());
162 // deactivate the parent data
163 $t->query("UPDATE core_templatestr
164 SET active = 0 WHERE id in (" . implode(',' ,$unused) . ")
167 // deactivate the child data
168 $t->query("UPDATE core_templatestr
171 src_id IN (". implode(',' , $unused) . ")
178 $t = DB_DataObject::factory($this->tableName());
179 // activate the aprent data
180 $t->query("UPDATE core_templatestr
181 SET active = 1 WHERE id in (" . implode(',' ,$active) . ")
183 // deactivate the child data
184 $t->query("UPDATE core_templatestr
187 src_id IN (". implode(',' ,$active) . ")
197 function applyFiltersTree($q,$roo)
199 if (empty($q['node'])) {
200 $roo->jerr("invalid node");
204 case ($q['node'] == 'transtree'):
205 case ($q['node'] == 'langlist'):
206 // DB_DataObject::debugLevel(1);
207 $x = DB_Dataobject::Factory($this->tableName());
209 $x->selectAdd('distinct(lang) as lang');
211 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
212 $x->whereAdd("lang != ''");
214 foreach( $x->fetchAll() as $l) {
216 'text'=>$l->lang_name,
217 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
230 case preg_match('/^lang:/', $q['node']):
232 $lang = preg_replace('/^lang:/', '', $q['node']);
234 $x = DB_DataObject::factory('core_templatestr');
237 $x->selectAdd('distinct(view_name) as view_name');
239 $x->whereAdd('join_template_id_id.is_deleted = 0');
240 $x->orderBy('view_name DESC');
244 'text'=> $x->view_name,
245 'id' => 'view:'. $lang .':'. $x->view_name,
250 $ff = HTML_FlexyFramework::get()->Pman_Core;
252 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
253 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
256 'on_table' => $table,
257 'id' => 'table:'. $lang .':'. $table,
269 case preg_match('/^view:/', $q['node']):
272 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
274 $x = DB_DataObject::factory($this->tableName());
277 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
278 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
279 $x->whereAdd('join_template_id_id.is_deleted = 0');
281 $ids = $x->fetchAll('template_id');
284 //add the table type lists
286 $ff = HTML_FlexyFramework::get()->Pman_Core;
289 // $x->orderBy('template ASC');
290 // $x->whereAdd("lang != ''");
293 $xx = DB_Dataobject::Factory('core_template');
294 $xx->whereAddIn('id', $ids, 'int');
298 id, concat( template) as template_name
300 $xx->orderBy('template_name ASC');
302 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
322 * @param object $tmpl core_template data object
323 * @param array $words array of words
325 function syncTemplateWords($tmpl, $keyvalue = false)
328 $words = $tmpl->words;
329 // mapping for template :
330 //tablename => $n (templatename)
331 //tableid => $k (key value)
332 //colname => $n (templatename)
335 //print_r($words);exit;
337 $tt = DB_DataObject::factory($this->tableName());
340 $t->template_id = $tmpl->id;
341 $t->whereAdd("lang = ''");
344 // we have a situation where old md sums where created..
348 $cur = $t->fetchAll('mdsum', 'id');
356 // now loop through current..
357 $cwords = array();// not in used??
359 // echo "sync Template Words... \n";
362 foreach($words as $k=>$v) {
372 $md = $keyvalue ? $k : md5($v);
374 // check to see if there are more that one versions of that md5
377 if (!isset($cur[$md])) {
378 // create a record for it..
379 $t = DB_DataObject::factory($this->tableName());
382 'lang' => '',// by default should a english
383 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
384 'template_id'=>$tmpl->id,
389 $active[] = $t->insert();
392 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
394 $active[] = $cur[$md];
396 // we have it already? -
397 $t = DB_DataObject::factory($this->tableName());
398 $t->query("UPDATE {$this->tableName()}
411 if (count(array_values($cur))) {// de-active unused
413 $t = DB_DataObject::factory($this->tableName());
414 // echo "de-active current?? \n";
417 $deactive = array_values($cur);
418 $t->query("UPDATE core_templatestr
419 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
423 // delete all the items that are not relivant.
424 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
425 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
426 $t = DB_DataObject::factory($this->tableName());
428 // this will active the child data
429 if (empty($active)) {// set the active array to empty
432 $t->query("UPDATE core_templatestr
435 src_id IN (". implode(',' ,$active) . ")
437 template_id = {$tmpl->id}
439 //deactive the child data
440 if (empty($deactive)) {
441 $deactive = array(-1);
443 $t->query("UPDATE core_templatestr
446 src_id IN (". implode(',' ,$deactive) . ")
448 template_id = {$tmpl->id}
454 function checkDupes($tid, $lang, $id, $mdsum) {
456 $t = DB_DataObject::factory($this->tableName());
457 $t->template_id = $tid;
459 $t->whereAdd("lang = '{$lang}'");
460 if ($t->count() == 1) {
461 return $id; // only got one ... no issues..
464 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
466 //DB_DataObject::debugLevel(1);
467 // find out if any of them have got translations.
468 $ids = $t->fetchAll('id');
471 $t = DB_DataObject::factory($this->tableName());
472 $t->whereAddIn('src_id', $ids, 'int');
473 $t->whereAdd("txt != ''");
474 $t->orderBy('updated DESC');
477 // do any translations exist?
483 // delete all the others...
484 $t = DB_DataObject::factory($this->tableName());
485 $t->whereAddIn('src_id', $ids, 'int');
486 $t->whereAdd("src_id != $id");
490 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
493 $t = DB_DataObject::factory($this->tableName());
494 $t->whereAddIn('id', $ids, 'int');
495 $t->whereAdd("id != $id");
499 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
502 // this is done by calling code
503 //$t = DB_DataObject::factory($this->tableName());
504 //$t->query("update core_templatestr set active= 1 where src_id = $id");
515 function syncLang($lang)
517 // bugs with our old code...
519 $tn = $this->tableName();
520 $t = DB_DataObject::factory($tn);
521 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
523 // find all the id's from lang that have not been generated..
526 static $id_tmp = false;
528 if ($id_tmp == false) {
530 $t = DB_DataObject::factory($tn);
531 $t->whereAdd("lang = ''");
534 //old code, this did not support the on_table
535 // $id_tmp = $t->fetchAll('id','template_id');
536 // $ids = array_keys($id_tmp);
538 //new code for support the sync tables
539 foreach($t->fetchAll() as $ori){
540 $id_tmp[$ori->id] = $ori;
543 $ids = array_keys($id_tmp);
545 // matching by language:
546 $t = DB_DataObject::factory($tn);
547 $t->whereAddIn('src_id', $ids , 'int');
550 $got = $t->fetchAll('src_id');
551 $missing = array_diff($ids, $got);
553 if (empty($missing)) {
556 $t = DB_DataObject::factory($tn);
557 $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
567 FROM core_templatestr
569 id IN (". implode(',', $missing) . ")
572 DB_DataObject::factory($tn)->query($q);
573 $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
574 DB_DataObject::factory($tn)->query($q);
575 $q = "DROP TEMPORARY TABLE core_templatestr_insert";
576 DB_DataObject::factory($tn)->query($q);
582 // called from flexy to translate a string.
585 function translateFlexyString($flexy, $string)
588 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
590 // using $flexy->currentTemplate -> find the template we are looking at..
591 // then find the string for $flexy->options['locale']
592 //DB_DataObject::debugLevel(1);
593 if ($debug) { var_dump($string); }
595 static $cache = array(); // cache of templates..
597 $ff = HTML_FlexyFramework::get();
598 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
599 if (empty($view_name)) {
600 $pg = HTML_FlexyFramework::get()->page;
601 if (isset($pg->templateViewName)) {
602 $view_name = $pg->templateViewName;
609 if ($debug) { var_dump(array('view_name'=> $view_name)); }
612 foreach($flexy->options['templateDir'] as $td) {
613 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
620 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
623 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
624 if ($debug) { echo "from cache no match - $string\n"; }
628 if (!isset($cache[$tmpname])) {
632 $tmpl = DB_DataObject::factory('core_template');
633 if ($view_name !== false) {
634 $tmpl->view_name = $view_name;
636 if(!$tmpl->get('template', $tmpname)){
637 // strip of site prefix if set...
639 $tmpl = DB_DataObject::factory('core_template');
640 if(!$tmpl->get('template', $tmpname)){
641 //var_dump("no template? {$tmpname} or {$relpath}" );
642 $cache[$tmpname] = false;
643 if ($debug) { echo "no template found - no match - $string\n"; }
647 $cache[$tmpname] = $tmpl;
649 $tmpl = $cache[$tmpname] ;
654 //get original template id
656 $orig = DB_DataObject::factory($this->tableName());
658 $orig->template_id = $tmpl->id;
661 $cache[$tmpname]->words =
663 if(!$orig->get( 'mdsum' , md5(trim($string)))){
664 //var_dump('no text? '. $string);
665 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
670 if (empty($cache[$tmpname]->translations)) {
672 //find out the text by language
673 //DB_DataObject::DebugLevel(1);
674 $x = DB_DataObject::factory($this->tableName());
675 $x->lang = $flexy->options['locale'];
676 $x->template_id = $tmpl->id;
678 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
679 if (empty($cache[$tmpname]->translations)) {
680 $cache[$tmpname]->translations = true;
682 //var_Dump($cache[$tmpname]->translations);
686 if ($cache[$tmpname]->translations === true) {
689 //var_dump("Checking: " . md5(trim($string)));
691 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
692 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
693 return $cache[$tmpname]->translations [md5(trim($string))];
700 function translateChanged($flexy)
703 $date = $this->lastUpdated($flexy);
704 if ($date === false) {
707 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
708 return strtotime($date) > $utime;
711 // determine if a complied template need recompling
713 function lastUpdated($flexy)
716 // var_dump('check changed?');
717 //DB_DataObject::debugLevel(1);
718 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
720 //var_dump($flexy->compiledTemplate);
721 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
724 static $cache = array(); // cache of templates..
726 $ff = HTML_FlexyFramework::get();
727 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
729 // find which of the template directories was actually used for the template.
732 foreach($flexy->options['templateDir'] as $td) {
733 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
740 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
742 // we do not have any record of this template..
743 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
747 if (!isset($cache[$tmpname])) {
751 $tmpl = DB_DataObject::factory('core_template');
752 if ($view_name !== false) {
753 $tmpl->view_name = $view_name;
755 if(!$tmpl->get('template', $tmpname)){
756 // strip of site prefix if set...
758 $tmpl = DB_DataObject::factory('core_template');
759 if(!$tmpl->get('template', $tmpname)){
760 //var_dump("no template? {$tmpname} or {$relpath}" );
761 $cache[$tmpname] = false;
765 $cache[$tmpname] = $tmpl;
767 $tmpl = $cache[$tmpname] ;
772 $x = DB_DataObject::factory($this->tableName());
773 $x->lang = $flexy->options['locale'];
775 $x->template_id = $tmpl->id;
776 //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
777 if ($x->count() < 1) {
778 return false; // we don't have any record of it.
781 $x->selectAdd('max(updated) as max_updated');
783 return $x->max_updated;
788 function toRooArray($req) {
789 $ret = $this->toArray();
791 if (empty($req['csvCols'])) {
797 // translations for table columns
798 if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
799 $ret['template_id_view_name'] = 'database';
800 $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
806 function postListFilter($ar, $au, $req)
808 if (empty($req['csvCols'])) {
817 if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
822 // translations for table columns
823 // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
825 $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
827 if(!empty($ret[$key])) {
834 $ret = array_values($ret);