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']) && !is_numeric($q['template_id'])){
49 $this->template_id = 0;
51 if (!empty($q['_search_txt'])) {
52 $str = $this->escape($q['_search_txt']);
53 $this->whereAdd("core_templatestr.txt like '%{$str}%' OR join_src_id_id.txt like '%{$str}%'");
57 function translateTableCol($obj, $col, $lang)
59 $cts = DB_DataObject::factory('core_templatestr');
61 $cts->on_table = $obj->tableName();
62 $cts->on_id = $obj->pid();
64 if(!$cts->find(true)){
77 * insert the origanal table text
84 function onTableChange($roo, $obj, $chg)
87 $ff = HTML_FlexyFramework::get()->Pman_Core;
89 if(empty($ff['DataObjects_Core_templatestr']['tables'])){
92 $tn = $obj->tableName();
93 if(empty($ff['DataObjects_Core_templatestr']['tables'][$tn])){
96 $cols = $ff['DataObjects_Core_templatestr']['tables'][$tn];
99 foreach($cols as $c) {
100 $x = $this->factory($this->tableName());
101 $x->on_id = $obj->pid();
104 $x->lang = ''; /// eg. base language..
105 $up = $x->find(true);
106 if ($up && $x->txt == $obj->$c) {
107 continue; // update an no change..
112 $x->mdsum = md5($obj->$c);
114 $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
115 $up ? $x->update() : $x->insert();
122 function applyFiltersTree($q,$roo)
124 if (empty($q['node'])) {
125 $roo->jerr("invalid node");
129 case ($q['node'] == 'transtree'):
130 // DB_DataObject::debugLevel(1);
131 $x = DB_Dataobject::Factory($this->tableName());
133 $x->selectAdd('distinct(lang) as lang');
134 $x->whereAdd("lang != ''");
136 foreach( $x->fetchAll('lang') as $l) {
152 case preg_match('/^lang:/', $q['node']):
154 $lang = preg_replace('/^lang:/', '', $q['node']);
156 $x = DB_DataObject::factory('core_templatestr');
159 $x->selectAdd('distinct(view_name) as view_name');
161 $x->orderBy('view_name DESC');
165 'text'=> $x->view_name,
166 'id' => 'view:'. $lang .':'. $x->view_name,
177 case preg_match('/^view:/', $q['node']):
180 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
182 $x = DB_DataObject::factory($this->tableName());
185 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
186 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
188 $ids = $x->fetchAll('template_id');
191 //add the table type lists
193 $ff = HTML_FlexyFramework::get()->Pman_Core;
195 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
196 foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
199 'on_table' => $table,
206 // $x->orderBy('template ASC');
207 // $x->whereAdd("lang != ''");
210 $xx = DB_Dataobject::Factory('core_template');
211 $xx->whereAddIn('id', $ids, 'int');
215 id, concat( template) as template_name
217 $xx->orderBy('template_name ASC');
219 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
239 * @param object $tmpl core_template data object
240 * @param array $words array of words
242 function syncTemplateWords($tmpl, $keyvalue = false)
245 $words = $tmpl->words;
246 // mapping for template :
247 //tablename => $n (templatename)
248 //tableid => $k (key value)
249 //colname => $n (templatename)
252 //print_r($words);exit;
254 $tt = DB_DataObject::factory($this->tableName());
257 $t = DB_DataObject::factory($this->tableName());
258 $t->template_id = $tmpl->id;
259 $t->whereAdd("lang = ''");
262 // we have a situation where old md sums where created..
266 $cur = $t->fetchAll('mdsum', 'id');
274 // now loop through current..
275 $cwords = array();// not in used??
277 // echo "sync Template Words... \n";
280 foreach($words as $k=>$v) {
285 $md = $keyvalue ? $k : md5($v);
287 // check to see if there are more that one versions of that md5
290 if (!isset($cur[$md])) {
291 // create a record for it..
292 $t = DB_DataObject::factory($this->tableName());
295 'lang' => '',// by default should a english
296 'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
297 'template_id'=>$tmpl->id,
302 $active[] = $t->insert();
305 $cur[$md] = $this->checkDupes($tmpl->id, '', $cur[$md] , $md);
307 $active[] = $cur[$md];
309 // we have it already? -
310 $tt->query("UPDATE {$this->tableName()}
323 if (count(array_values($cur))) {// de-active unused
325 $t = DB_DataObject::factory($this->tableName());
326 // echo "de-active current?? \n";
329 $deactive = array_values($cur);
330 $t->query("UPDATE core_templatestr
331 SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
335 // delete all the items that are not relivant.
336 // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
337 // this does not help - as it just puts random strings in there.. - with no reference to the original text..
338 $t = DB_DataObject::factory($this->tableName());
340 // this will active the child data
341 if (empty($active)) {// set the active array to empty
344 $t->query("UPDATE core_templatestr
347 src_id IN (". implode(',' ,$active) . ")
349 template_id = {$tmpl->id}
351 //deactive the child data
352 if (empty($deactive)) {
353 $deactive = array(-1);
355 $t->query("UPDATE core_templatestr
358 src_id IN (". implode(',' ,$deactive) . ")
360 template_id = {$tmpl->id}
366 function checkDupes($tid, $lang, $id, $mdsum) {
368 $t = DB_DataObject::factory($this->tableName());
369 $t->template_id = $tid;
371 $t->whereAdd("lang = '{$lang}'");
372 if ($t->count() == 1) {
373 return $id; // only got one ... no issues..
376 //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
378 //DB_DataObject::debugLevel(1);
379 // find out if any of them have got translations.
380 $ids = $t->fetchAll('id');
383 $t = DB_DataObject::factory($this->tableName());
384 $t->whereAddIn('src_id', $ids, 'int');
385 $t->whereAdd("txt != ''");
386 $t->orderBy('updated DESC');
389 // do any translations exist?
395 // delete all the others...
396 $t = DB_DataObject::factory($this->tableName());
397 $t->whereAddIn('src_id', $ids, 'int');
398 $t->whereAdd("src_id != $id");
402 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
405 $t = DB_DataObject::factory($this->tableName());
406 $t->whereAddIn('id', $ids, 'int');
407 $t->whereAdd("id != $id");
411 $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
414 // this is done by calling code
415 //$t = DB_DataObject::factory($this->tableName());
416 //$t->query("update core_templatestr set active= 1 where src_id = $id");
427 function syncLang($lang)
429 // bugs with our old code...
431 $tn = $this->tableName();
432 $t = DB_DataObject::factory($tn);
433 $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table = ''");
435 // find all the id's from lang that have not been generated..
438 $t = DB_DataObject::factory($tn);
439 $t->whereAdd("lang = ''");
442 //old code, this did not support the on_table
443 // $id_tmp = $t->fetchAll('id','template_id');
444 // $ids = array_keys($id_tmp);
446 //new code for support the sync tables
447 foreach($t->fetchAll() as $ori){
448 $id_tmp[$ori->id] = $ori;
450 $ids = array_keys($id_tmp);
452 // matching by language:
453 $t = DB_DataObject::factory($tn);
454 $t->whereAddIn('src_id', $ids , 'int');
457 $got = $t->fetchAll('src_id');
458 $missing = array_diff($ids, $got);
459 foreach($missing as $id) {
461 $t = DB_DataObject::factory($tn);
466 'updated' => date('Y-m-d H:i:s', strtotime("NOW")),
467 'template_id'=> $id_tmp[$id]->template_id,
468 'on_table' => $id_tmp[$id]->on_table,
469 'on_id' => $id_tmp[$id]->on_id,
470 'on_col' => $id_tmp[$id]->on_col,
481 // called from flexy to translate a string.
484 function translateFlexyString($flexy, $string)
488 //if (!empty($_REQUEST['_debug'])) { $debug= true; }
490 // using $flexy->currentTemplate -> find the template we are looking at..
491 // then find the string for $flexy->options['locale']
492 //DB_DataObject::debugLevel(1);
493 if ($debug) { var_dump($string); }
495 static $cache = array(); // cache of templates..
497 $ff = HTML_FlexyFramework::get();
498 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
500 if ($debug) { var_dump(array('view_name'=> $view_name)); }
503 foreach($flexy->options['templateDir'] as $td) {
504 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
511 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
513 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
514 if ($debug) { echo "from cache no match - $string\n"; }
518 if (!isset($cache[$tmpname])) {
522 $tmpl = DB_DataObject::factory('core_template');
523 if ($view_name !== false) {
524 $tmpl->view_name = $view_name;
526 if(!$tmpl->get('template', $tmpname)){
527 // strip of site prefix if set...
529 $tmpl = DB_DataObject::factory('core_template');
530 if(!$tmpl->get('template', $tmpname)){
531 //var_dump("no template? {$tmpname} or {$relpath}" );
532 $cache[$tmpname] = false;
533 if ($debug) { echo "no template found - no match - $string\n"; }
537 $cache[$tmpname] = $tmpl;
539 $tmpl = $cache[$tmpname] ;
543 //get original template id
544 $orig = DB_DataObject::factory($this->tableName());
546 $orig->template_id = $tmpl->id;
548 if(!$orig->get( 'mdsum' , md5(trim($string)))){
549 //var_dump('no text? '. $string);
550 if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
554 //find out the text by language
555 $x = DB_DataObject::factory($this->tableName());
556 $x->lang = $flexy->options['locale'];
557 $x->template_id = $tmpl->id;
558 if(!$x->get('src_id', $orig->id)){
559 //var_dump('no trans found' . $orig->id);
560 if ($debug) { echo "no translation found\n"; }
563 if ($debug) { echo "returning $x->txt\n"; }
565 return empty($x->txt) ? $string : $x->txt;
568 // determine if a complied template need recompling
570 function translateChanged($flexy)
573 // var_dump('check changed?');
574 //DB_DataObject::debugLevel(1);
575 //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
577 //var_dump($flexy->compiledTemplate);
578 $utime = file_exists($flexy->compiledTemplate) ? filemtime( $flexy->compiledTemplate) : 0;
581 static $cache = array(); // cache of templates..
583 $ff = HTML_FlexyFramework::get();
584 $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
587 foreach($flexy->options['templateDir'] as $td) {
588 if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
595 $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
597 if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
601 if (!isset($cache[$tmpname])) {
605 $tmpl = DB_DataObject::factory('core_template');
606 if ($view_name !== false) {
607 $tmpl->view_name = $view_name;
609 if(!$tmpl->get('template', $tmpname)){
610 // strip of site prefix if set...
612 $tmpl = DB_DataObject::factory('core_template');
613 if(!$tmpl->get('template', $tmpname)){
614 //var_dump("no template? {$tmpname} or {$relpath}" );
615 $cache[$tmpname] = false;
619 $cache[$tmpname] = $tmpl;
621 $tmpl = $cache[$tmpname] ;
626 $x = DB_DataObject::factory($this->tableName());
627 $x->lang = $flexy->options['locale'];
629 $x->template_id = $tmpl->id;
630 $x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
632 return $x->count() ? true : false;