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];
100 foreach($cols as $c) {
101 $x = $this->factory($this->tableName());
102 $x->on_id = $obj->pid();
105 $x->lang = ''; /// eg. base language..
106 $up = $x->find(true);
107 if ($up && $x->txt == $obj->$c) {
108 continue; // update an no change..
113 $x->mdsum = md5($obj->$c);
115 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
116 $up ? $x->update() : $x->insert();
123 function applyFiltersTree($q,$roo)
125 if (empty($q['node'])) {
126 $roo->jerr("invalid node");
130 case ($q['node'] == 'transtree'):
131 case ($q['node'] == 'langlist'):
132 // DB_DataObject::debugLevel(1);
133 $x = DB_Dataobject::Factory($this->tableName());
135 $x->selectAdd('distinct(lang) as lang');
137 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
138 $x->whereAdd("lang != ''");
140 foreach( $x->fetchAll() as $l) {
142 'text'=>$l->lang_name,
143 'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
156 case preg_match('/^lang:/', $q['node']):
158 $lang = preg_replace('/^lang:/', '', $q['node']);
160 $x = DB_DataObject::factory('core_templatestr');
163 $x->selectAdd('distinct(view_name) as view_name');
165 $x->whereAdd('join_template_id_id.is_deleted = 0');
166 $x->orderBy('view_name DESC');
170 'text'=> $x->view_name,
171 'id' => 'view:'. $lang .':'. $x->view_name,
176 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
177 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
180 'on_table' => $table,
181 'id' => 'table:'. $lang .':'. $table,
193 case preg_match('/^view:/', $q['node']):
196 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
198 $x = DB_DataObject::factory($this->tableName());
201 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
202 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
203 $x->whereAdd('join_template_id_id.is_deleted = 0');
205 $ids = $x->fetchAll('template_id');
208 //add the table type lists
210 $ff = HTML_FlexyFramework::get()->Pman_Core;
213 // $x->orderBy('template ASC');
214 // $x->whereAdd("lang != ''");
217 $xx = DB_Dataobject::Factory('core_template');
218 $xx->whereAddIn('id', $ids, 'int');
222 id, concat( template) as template_name
224 $xx->orderBy('template_name ASC');
226 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
246 * @param object $tmpl core_template data object
247 * @param array $words array of words
249 function syncTemplateWords($tmpl, $keyvalue = false)
252 $words = $tmpl->words;
253 // mapping for template :
254 //tablename => $n (templatename)
255 //tableid => $k (key value)
256 //colname => $n (templatename)
259 //print_r($words);exit;
261 $tt = DB_DataObject::factory($this->tableName());
264 $t = DB_DataObject::factory($this->tableName());
265 $t->template_id = $tmpl->id;
266 $t->whereAdd("lang = ''");
269 // we have a situation where old md sums where created..
273 $cur = $t->fetchAll('mdsum', 'id');
281 // now loop through current..
282 $cwords = array();// not in used??
284 // echo "sync Template Words... \n";
287 foreach($words as $k=>$v) {
292 $md = $keyvalue ? $k : md5($v);
294 // check to see if there are more that one versions of that md5
297 if (!isset($cur[$md])) {
298 // create a record for it..
299 $t = DB_DataObject::factory($this->tableName());
302 'lang' => '',// by default should a english
303 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
304 'template_id'=>$tmpl->id,
309 $active[] = $t->insert();
312 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
314 $active[] = $cur[$md];
316 // we have it already? -
317 $tt->query("UPDATE {$this->tableName()}
330 if (count(array_values($cur))) {// de-active unused
332 $t = DB_DataObject::factory($this->tableName());
333 // echo "de-active current?? \n";
336 $deactive = array_values($cur);
337 $t->query("UPDATE core_templatestr
338 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
342 // delete all the items that are not relivant.
343 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
344 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
345 $t = DB_DataObject::factory($this->tableName());
347 // this will active the child data
348 if (empty($active)) {// set the active array to empty
351 $t->query("UPDATE core_templatestr
354 src_id IN (". implode(',' ,$active) . ")
356 template_id = {$tmpl->id}
358 //deactive the child data
359 if (empty($deactive)) {
360 $deactive = array(-1);
362 $t->query("UPDATE core_templatestr
365 src_id IN (". implode(',' ,$deactive) . ")
367 template_id = {$tmpl->id}
373 function checkDupes($tid, $lang, $id, $mdsum) {
375 $t = DB_DataObject::factory($this->tableName());
376 $t->template_id = $tid;
378 $t->whereAdd("lang = '{$lang}'");
379 if ($t->count() == 1) {
380 return $id; // only got one ... no issues..
383 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
385 //DB_DataObject::debugLevel(1);
386 // find out if any of them have got translations.
387 $ids = $t->fetchAll('id');
390 $t = DB_DataObject::factory($this->tableName());
391 $t->whereAddIn('src_id', $ids, 'int');
392 $t->whereAdd("txt != ''");
393 $t->orderBy('updated DESC');
396 // do any translations exist?
402 // delete all the others...
403 $t = DB_DataObject::factory($this->tableName());
404 $t->whereAddIn('src_id', $ids, 'int');
405 $t->whereAdd("src_id != $id");
409 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
412 $t = DB_DataObject::factory($this->tableName());
413 $t->whereAddIn('id', $ids, 'int');
414 $t->whereAdd("id != $id");
418 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
421 // this is done by calling code
422 //$t = DB_DataObject::factory($this->tableName());
423 //$t->query("update core_templatestr set active= 1 where src_id = $id");
434 function syncLang($lang)
436 // bugs with our old code...
438 $tn = $this->tableName();
439 $t = DB_DataObject::factory($tn);
440 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
442 // find all the id's from lang that have not been generated..
445 $t = DB_DataObject::factory($tn);
446 $t->whereAdd("lang = ''");
449 //old code, this did not support the on_table
450 // $id_tmp = $t->fetchAll('id','template_id');
451 // $ids = array_keys($id_tmp);
453 //new code for support the sync tables
454 foreach($t->fetchAll() as $ori){
455 $id_tmp[$ori->id] = $ori;
457 $ids = array_keys($id_tmp);
459 // matching by language:
460 $t = DB_DataObject::factory($tn);
461 $t->whereAddIn('src_id', $ids , 'int');
464 $got = $t->fetchAll('src_id');
465 $missing = array_diff($ids, $got);
466 foreach($missing as $id) {
468 $t = DB_DataObject::factory($tn);
473 'updated' => date('Y-m-d H:i:s', strtotime("NOW")),
474 'template_id'=> $id_tmp[$id]->template_id,
475 'on_table' => $id_tmp[$id]->on_table,
476 'on_id' => $id_tmp[$id]->on_id,
477 'on_col' => $id_tmp[$id]->on_col,
488 // called from flexy to translate a string.
491 function translateFlexyString($flexy, $string)
494 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
496 // using $flexy->currentTemplate -> find the template we are looking at..
497 // then find the string for $flexy->options['locale']
498 //DB_DataObject::debugLevel(1);
499 if ($debug) { var_dump($string); }
501 static $cache = array(); // cache of templates..
503 $ff = HTML_FlexyFramework::get();
504 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
505 if (empty($view_name)) {
506 $pg = HTML_FlexyFramework::get()->page;
507 if (isset($pg->templateViewName)) {
508 $view_name = $pg->templateViewName;
515 if ($debug) { var_dump(array('view_name'=> $view_name)); }
518 foreach($flexy->options['templateDir'] as $td) {
519 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
526 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
529 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
530 if ($debug) { echo "from cache no match - $string\n"; }
534 if (!isset($cache[$tmpname])) {
538 $tmpl = DB_DataObject::factory('core_template');
539 if ($view_name !== false) {
540 $tmpl->view_name = $view_name;
542 if(!$tmpl->get('template', $tmpname)){
543 // strip of site prefix if set...
545 $tmpl = DB_DataObject::factory('core_template');
546 if(!$tmpl->get('template', $tmpname)){
547 //var_dump("no template? {$tmpname} or {$relpath}" );
548 $cache[$tmpname] = false;
549 if ($debug) { echo "no template found - no match - $string\n"; }
553 $cache[$tmpname] = $tmpl;
555 $tmpl = $cache[$tmpname] ;
560 //get original template id
562 $orig = DB_DataObject::factory($this->tableName());
564 $orig->template_id = $tmpl->id;
567 $cache[$tmpname]->words =
569 if(!$orig->get( 'mdsum' , md5(trim($string)))){
570 //var_dump('no text? '. $string);
571 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
576 if (empty($cache[$tmpname]->translations)) {
578 //find out the text by language
579 //DB_DataObject::DebugLevel(1);
580 $x = DB_DataObject::factory($this->tableName());
581 $x->lang = $flexy->options['locale'];
582 $x->template_id = $tmpl->id;
584 $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
585 if (empty($cache[$tmpname]->translations)) {
586 $cache[$tmpname]->translations = true;
588 //var_Dump($cache[$tmpname]->translations);
592 if ($cache[$tmpname]->translations === true) {
595 //var_dump("Checking: " . md5(trim($string)));
597 if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
598 // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
599 return $cache[$tmpname]->translations [md5(trim($string))];
605 // determine if a complied template need recompling
607 function translateChanged($flexy)
610 // var_dump('check changed?');
611 //DB_DataObject::debugLevel(1);
612 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
614 //var_dump($flexy->compiledTemplate);
615 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
618 static $cache = array(); // cache of templates..
620 $ff = HTML_FlexyFramework::get();
621 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
624 foreach($flexy->options['templateDir'] as $td) {
625 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
632 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
634 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
638 if (!isset($cache[$tmpname])) {
642 $tmpl = DB_DataObject::factory('core_template');
643 if ($view_name !== false) {
644 $tmpl->view_name = $view_name;
646 if(!$tmpl->get('template', $tmpname)){
647 // strip of site prefix if set...
649 $tmpl = DB_DataObject::factory('core_template');
650 if(!$tmpl->get('template', $tmpname)){
651 //var_dump("no template? {$tmpname} or {$relpath}" );
652 $cache[$tmpname] = false;
656 $cache[$tmpname] = $tmpl;
658 $tmpl = $cache[$tmpname] ;
663 $x = DB_DataObject::factory($this->tableName());
664 $x->lang = $flexy->options['locale'];
666 $x->template_id = $tmpl->id;
667 $x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
669 return $x->count() ? true : false;