DataObjects/Core_templatestr.php
[Pman.Core] / DataObjects / Core_templatestr.php
1 <?php
2 /**
3  * Table Definition for core_templatestr
4  *
5  *
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
8  * 
9  */
10 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
11
12 class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
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;
25     public $active;
26     public $on_table;
27     public $on_id;
28     public $on_col;
29  
30     /* the code above is auto generated do not remove the tag below */
31     ###END_AUTOCODE
32     
33     function beforeInsert($q,$roo)
34     {
35         if(!empty($q['_rescan'])){
36             $this->syncLang($q['_rescan']);
37             $roo->jok('OK');
38         }
39     }
40     
41     
42     function applyFilters($q, $au, $roo)
43     {
44         if (!empty($q['_tree'])) {
45             $this->applyFiltersTree($q,$roo);
46         }
47         
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 != ''");
51         }
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}%'");
55             
56         }
57     }
58     function translateTableCol($obj, $col, $lang)
59     {
60         $cts = DB_DataObject::factory('core_templatestr');
61         $cts->lang = $lang;
62         $cts->on_table = $obj->tableName();
63         $cts->on_id = $obj->pid();
64         $cc = clone($cts);
65         if(!$cts->find(true)){
66             return $obj->$col;
67         }
68         
69         
70         if(empty($cts->txt)){
71             return $obj->$col;
72         }
73             
74         return $cts->txt;
75     }
76     /**
77      *
78      * insert the origanal table text
79      * 
80      * @param type $roo
81      * @param type $obj
82      * @param type $chg
83      * @return type 
84      */
85     function onTableChange($roo, $obj, $chg)
86     {
87         
88         $ff = HTML_FlexyFramework::get()->Pman_Core;
89             
90         if(empty($ff['DataObjects_Core_templatestr']['tables'])){
91             return;
92         }
93         $tn = $obj->tableName();
94         if(empty($ff['DataObjects_Core_templatestr']['tables'][$tn])){
95             return;
96         }
97         $cols = $ff['DataObjects_Core_templatestr']['tables'][$tn];
98
99         foreach($cols as $c) {
100
101             // skip if empty string
102             if(empty($obj->$c)) {
103                 continue;
104             }
105             
106             if(strpos($c, ',') !== false) {
107                 $arr = explode(',', $c);
108                 $c = $arr[0];
109                 $cond = $arr[1];
110
111                 $ar = explode('=', $cond);
112                 $key = $ar[0];
113
114                 // skip if condition not fulfilled
115                 if($obj->{$ar[0]} != $ar[1]) {
116                     continue;
117                 }
118             }
119
120             $x = $this->factory($this->tableName());
121             $x->on_id = $obj->pid();
122             $x->on_table = $tn;
123             $x->on_col = $c;
124             $x->lang = ''; /// eg. base language..
125             $up = $x->find(true);
126
127             // skip when no change
128             if($up && $x->txt == $obj->$c) {
129                 continue;
130             }
131
132             $x->active = 1;
133             $x->src_id = 0;
134             $x->txt = $obj->$c;
135             $x->mdsum = md5($obj->$c);
136             $x->template_id = 0;
137             $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
138             $up ? $x->update() : $x->insert();
139         }
140
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) . ")
146         //              ");
147
148         //     // deactivate the child data
149         //     $t->query("UPDATE  core_templatestr 
150         //     SET active = 0
151         //     WHERE
152         //         src_id IN (". implode(',' , $unused) . ")
153         //         AND
154         //         lang != ''
155         //      ");
156         // }
157
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) . ")
163         //     ");
164         //     // deactivate the child data
165         //     $t->query("UPDATE  core_templatestr 
166         //     SET active = 1
167         //       WHERE
168         //          src_id IN (". implode(',' ,$active) . ")
169         //         AND
170         //         lang != ''
171         //     ");
172         // }
173         
174         
175     }
176
177     function syncTableWords()
178     {
179         $ff = HTML_FlexyFramework::get()->Pman_Core;
180             
181         if(empty($ff['DataObjects_Core_templatestr']['tables'])){
182             return;
183         }
184
185         $tables = $ff['DataObjects_Core_templatestr']['tables'];
186
187         $cts = DB_DataObject::factory($this->tableName());
188         $cts->whereAdd('on_table != ""');
189         $old = clone($cts);
190         $cur = $cts->fetchAll();
191
192         $unused = array();
193         foreach($cur as $ts) {
194             // old table
195             if(empty($tables[$ts->on_table])) {
196                 $unused[] = $ts->id;
197                 continue;
198             }
199             var_dump($ts->on_table);
200             var_dump($ts->on_col);
201         }
202         die('b');
203     }
204     
205     
206     function applyFiltersTree($q,$roo)
207     {
208         if (empty($q['node'])) {
209             $roo->jerr("invalid node");
210         }
211         switch(true) {
212             
213             case ($q['node'] == 'transtree'):
214             case ($q['node'] == 'langlist'):
215                // DB_DataObject::debugLevel(1);
216                 $x = DB_Dataobject::Factory($this->tableName());
217                 $x->selectAdd();
218                 $x->selectAdd('distinct(lang) as lang');
219                 
220                 $x->selectAdd("i18n_translate('l', lang, 'en') as lang_name");
221                 $x->whereAdd("lang != ''");
222                 $ret= array();
223                 foreach( $x->fetchAll() as $l) {
224                     $ret[] = array(
225                         'text'=>$l->lang_name,
226                         'id' => $q['node'] == 'langlist' ? $l->lang : 'lang:'.$l->lang ,
227                         'language' => true
228                     );
229                 }
230                 if (empty($ret)) {
231                     $ret[] = array(
232                         'text'=>'English',
233                         'id' => 'lang:en',
234                         'language' => true
235                     );
236                 }
237                 $roo->jdata($ret);
238             
239             case  preg_match('/^lang:/', $q['node']):
240                 
241                 $lang = preg_replace('/^lang:/', '', $q['node']);
242                 $ret= array();  
243                 $x = DB_DataObject::factory('core_templatestr');
244                 $x->autoJoin();
245                 $x->selectAdd();
246                 $x->selectAdd('distinct(view_name) as view_name');
247                 $x->lang = $lang;
248                 $x->whereAdd('join_template_id_id.is_deleted = 0');
249                 $x->orderBy('view_name DESC');
250                 $x->find();
251                 while($x->fetch()) {
252                     $ret[] = array( 
253                         'text'=> $x->view_name,
254                         'id' => 'view:'. $lang .':'.  $x->view_name,
255                         'leaf' => false
256                     );
257                 }
258
259                 $ff = HTML_FlexyFramework::get()->Pman_Core;
260
261                 if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
262                     foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
263                         $ret[] = array(
264                             'text'=> $table,
265                             'on_table' => $table,
266                             'id' => 'table:'. $lang .':'. $table,
267                             'leaf' => true
268                         );
269                     }
270                 }
271                 
272                  
273                 $roo->jdata($ret);
274                 
275                 break;
276                 
277                 
278             case  preg_match('/^view:/', $q['node']):
279                 
280                
281                 $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
282                 
283                 $x = DB_DataObject::factory($this->tableName());
284                 $x->autoJoin();
285                 $x->selectAdd();
286                 $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
287                 $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
288                 $x->whereAdd('join_template_id_id.is_deleted = 0');
289                 $x->lang = $bits[0];
290                 $ids = $x->fetchAll('template_id');
291                 
292                 $ret= array();
293                 //add the table type lists
294                 /*
295                 $ff = HTML_FlexyFramework::get()->Pman_Core;
296                 
297                 
298 //                $x->orderBy('template ASC');
299 //                $x->whereAdd("lang != ''");
300                 */
301                 //below are old code
302                 $xx = DB_Dataobject::Factory('core_template');
303                 $xx->whereAddIn('id', $ids, 'int');
304                 $xx->selectAdd();
305                 $xx->selectAdd("
306                                
307                     id, concat(  template) as template_name
308                 ");
309                 $xx->orderBy('template_name ASC');
310                 
311                 foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
312                     $ret[] = array(
313                         'text'=> $n,
314                         'id' => $l,
315                         'leaf' => true
316                     );
317                 }
318                 
319                 $roo->jdata($ret);
320                  break;
321         }
322                 
323         
324         
325     }
326     
327     
328     /**
329      *
330      * 
331      * @param object $tmpl core_template data object
332      * @param array $words array of words 
333      */ 
334     function syncTemplateWords($tmpl, $keyvalue = false)
335     {
336         
337         $words = $tmpl->words;
338         // mapping for template : 
339         //tablename => $n (templatename) 
340         //tableid => $k (key value)
341         //colname => $n (templatename) 
342         // mdsum => md5(sum)
343         //
344         //print_r($words);exit;
345         // grab original
346         $tt = DB_DataObject::factory($this->tableName());
347  
348         $t = clone($tt);
349         $t->template_id = $tmpl->id;
350         $t->whereAdd("lang = ''");
351         
352         
353         // we have a situation where old md sums where created..
354         
355         
356         
357         $cur = $t->fetchAll('mdsum', 'id'); 
358         
359         
360         
361         
362         
363         
364         
365         // now loop through current..
366         $cwords = array();// not in used??
367         $active = array();
368 //        echo "sync Template Words... \n";
369 //        print_r($words);
370         
371         foreach($words as $k=>$v) {
372             
373             
374             $v = trim($v);
375
376             // skip empty words
377             if(empty($v)) {
378                 continue;
379             }
380             
381             $md = $keyvalue ? $k : md5($v);
382             
383             // check to see if there are more that one versions of that md5
384             
385             
386             if (!isset($cur[$md])) {
387                 // create a record for it..
388                 $t = DB_DataObject::factory($this->tableName());
389                 $t->setFrom(array(
390                     'txt' => $v,
391                     'lang' => '',// by default should a english 
392                     'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
393                     'template_id'=>$tmpl->id,
394                     'mdsum' => $md,
395                     'src_id' => 0,
396                     'active' => 1,
397                 ));
398                 $active[] =  $t->insert();
399                 continue;
400             }  
401             $cur[$md] = $this->checkDupes($tmpl->id, '',  $cur[$md] , $md);
402             
403             $active[] = $cur[$md];
404             
405             // we have it already? - 
406             $t = DB_DataObject::factory($this->tableName());
407             $t->query("UPDATE {$this->tableName()}
408                         SET active= 1
409                         WHERE
410                         id = ".$cur[$md]);
411             unset($cur[$md]);
412             
413         }
414         // delete unused.
415         
416         
417         
418
419         $deactive = array();
420         if (count(array_values($cur))) {// de-active unused
421
422             $t = DB_DataObject::factory($this->tableName());
423 //            echo "de-active current?? \n";
424 //            print_r($cur);
425 //            echo "\n";
426             $deactive = array_values($cur);
427             $t->query("UPDATE core_templatestr
428                       SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
429                      ");
430         }
431         
432         // delete all the items that are not relivant.
433         // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
434         // this does not help - as it just puts random strings in there.. - with no reference to the original text..
435         $t = DB_DataObject::factory($this->tableName());
436     
437         // this will active the child data
438         if (empty($active)) {// set the active array to empty
439             $active = array(-1);
440         }
441         $t->query("UPDATE  core_templatestr 
442                 SET active = 1
443                   WHERE
444                      src_id IN (". implode(',' ,$active) . ")
445                     AND
446                     template_id = {$tmpl->id}
447         ");
448         //deactive the child data
449         if (empty($deactive)) {
450             $deactive = array(-1);
451         }
452         $t->query("UPDATE  core_templatestr 
453                 SET active = 0
454                   WHERE
455                     src_id IN (". implode(',' ,$deactive) . ")
456                     AND
457                     template_id = {$tmpl->id}
458                     AND
459                     lang != ''
460         ");
461     }
462     
463     function checkDupes($tid, $lang, $id, $mdsum) {
464         
465         $t = DB_DataObject::factory($this->tableName());
466         $t->template_id = $tid;
467         $t->mdsum = $mdsum;
468         $t->whereAdd("lang = '{$lang}'");
469         if ($t->count() == 1) {
470             return $id; // only got one ... no issues..
471         }
472         
473         //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
474         
475         //DB_DataObject::debugLevel(1);
476         // find out if any of them have got translations.
477         $ids = $t->fetchAll('id');
478         
479         
480         $t = DB_DataObject::factory($this->tableName());
481         $t->whereAddIn('src_id', $ids, 'int');
482         $t->whereAdd("txt != ''");
483         $t->orderBy('updated DESC');
484         if ($t->count()) {
485             $t->limit(1);
486             // do any translations exist?
487             $t->find(true);
488             $id = $t->src_id;
489         }
490         
491         
492         // delete all the others...
493         $t = DB_DataObject::factory($this->tableName());
494         $t->whereAddIn('src_id', $ids, 'int');
495         $t->whereAdd("src_id != $id");
496         $t->find();
497         while($t->fetch()) {
498             $tt = clone($t);
499             $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
500             $tt->update($t);
501         }
502         $t = DB_DataObject::factory($this->tableName());
503         $t->whereAddIn('id', $ids, 'int');
504         $t->whereAdd("id != $id");
505         $t->find();
506         while($t->fetch()) {
507             $tt = clone($t);
508             $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
509             $tt->update($t);
510         }
511         // this is done by calling code 
512         //$t = DB_DataObject::factory($this->tableName());
513         //$t->query("update core_templatestr set active= 1 where src_id = $id");
514         
515         
516         
517        //exit;
518         return $id;
519     
520                
521          
522     }
523     
524     function syncLang($lang)
525     {
526         // bugs with our old code...
527 //        die('in?');
528         $tn = $this->tableName();
529         $t = DB_DataObject::factory($tn);
530         $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table  = ''");
531         
532         // find all the id's from lang that have not been generated..
533         
534         
535         static $id_tmp = false;
536         
537         if ($id_tmp == false) {
538             //find the origanal 
539             $t = DB_DataObject::factory($tn);
540             $t->whereAdd("lang = ''");
541             $t->active = 1;
542             
543             //old code, this did not support the on_table
544     //        $id_tmp = $t->fetchAll('id','template_id');
545     //        $ids = array_keys($id_tmp);
546             $id_tmp = array();
547             //new code for support the sync tables 
548             foreach($t->fetchAll() as $ori){
549                 $id_tmp[$ori->id] = $ori;
550             }
551         }
552         $ids = array_keys($id_tmp);
553         
554         // matching by language:
555         $t = DB_DataObject::factory($tn);
556         $t->whereAddIn('src_id', $ids , 'int');
557         $t->lang = $lang;
558         //$t->active = 1;
559         $got = $t->fetchAll('src_id');
560         $missing = array_diff($ids, $got);
561         
562         if (empty($missing)) {
563             return;
564         }
565         $t = DB_DataObject::factory($tn);
566         $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
567             id as src_id,
568             '' as txt,
569             '$lang' as lang,
570             NOW() as updated,
571             template_id,
572             on_table,
573             on_id,
574             on_col,
575             1 as active
576             FROM core_templatestr
577             WHERE
578             id IN (". implode(',', $missing) . ")
579         ";
580         //echo $q; exit;
581         DB_DataObject::factory($tn)->query($q);
582         $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
583         DB_DataObject::factory($tn)->query($q);
584         $q = "DROP TEMPORARY TABLE core_templatestr_insert";
585         DB_DataObject::factory($tn)->query($q);
586             
587       
588         
589     }
590     
591     // called from flexy to translate a string.
592     
593     
594     function translateFlexyString($flexy, $string)
595     {
596          $debug = false;;
597         //if (!empty($_REQUEST['_debug'])) { $debug= true; }
598         
599         // using $flexy->currentTemplate -> find the template we are looking at..
600         // then find the string for $flexy->options['locale']
601         //DB_DataObject::debugLevel(1);
602         if ($debug) { var_dump($string); }
603         
604         static $cache = array(); // cache of templates..
605         
606         $ff = HTML_FlexyFramework::get();
607         $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
608         if (empty($view_name)) {
609             $pg = HTML_FlexyFramework::get()->page;
610             if (isset($pg->templateViewName)) {
611                 $view_name = $pg->templateViewName;
612             }
613             
614         }
615         
616         
617         
618         if ($debug) { var_dump(array('view_name'=> $view_name)); }
619         
620         $tempdir = '';
621         foreach($flexy->options['templateDir'] as $td) {
622             if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
623                 $tempdir = $td;
624                 break;
625             }
626         }
627         
628         
629         $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
630         
631          
632         if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
633             if ($debug) { echo "from cache no match - $string\n"; }
634             return $string;
635         }
636         
637         if (!isset($cache[$tmpname])) {
638                 
639             
640             
641             $tmpl = DB_DataObject::factory('core_template');
642             if ($view_name !== false) {
643                 $tmpl->view_name = $view_name;
644             }
645             if(!$tmpl->get('template', $tmpname)){
646                 // strip of site prefix if set...
647                  
648                 $tmpl = DB_DataObject::factory('core_template');
649                 if(!$tmpl->get('template', $tmpname)){
650                     //var_dump("no template? {$tmpname} or {$relpath}" );
651                     $cache[$tmpname] = false;
652                     if ($debug) { echo "no template found - no match - $string\n"; }
653                     return $string;
654                 }
655             }
656             $cache[$tmpname] = $tmpl;
657         } else {
658             $tmpl = $cache[$tmpname] ;
659         }
660         
661         
662     
663         //get original template id
664         /*
665         $orig = DB_DataObject::factory($this->tableName());
666         $orig->lang = '';
667         $orig->template_id = $tmpl->id;
668         $orig->active = 1;
669         
670         $cache[$tmpname]->words = 
671         
672         if(!$orig->get( 'mdsum' , md5(trim($string)))){
673              //var_dump('no text? '. $string);
674             if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
675             return false;
676         }
677         */
678         
679         if (empty($cache[$tmpname]->translations)) {
680         
681             //find out the text by language
682             //DB_DataObject::DebugLevel(1);
683             $x = DB_DataObject::factory($this->tableName());
684             $x->lang = $flexy->options['locale'];
685             $x->template_id = $tmpl->id;
686             $x->autoJoin();
687             $cache[$tmpname]->translations = $x->fetchAll('src_id_mdsum', 'txt');
688             if (empty($cache[$tmpname]->translations)) {
689                 $cache[$tmpname]->translations = true;
690             }
691             //var_Dump($cache[$tmpname]->translations);
692         }
693         
694         
695         if ($cache[$tmpname]->translations === true) {
696             return $string;
697         }
698         //var_dump("Checking: " . md5(trim($string)));
699         
700         if (!empty($cache[$tmpname]->translations [md5(trim($string))])){
701            // var_dump("RETURNING: ". $cache[$tmpname]->translations [md5(trim($string))]);
702             return $cache[$tmpname]->translations [md5(trim($string))];
703         }
704         return $string;
705         
706     }
707     
708     
709     function translateChanged($flexy)
710     {
711         
712         $date = $this->lastUpdated($flexy);
713         if ($date === false) {
714             return false;
715         }
716         $utime = file_exists($flexy->compiledTemplate) ?  filemtime( $flexy->compiledTemplate) : 0;
717         return strtotime($date) >  $utime;
718     }
719     
720     // determine if a complied template need recompling
721     
722     function lastUpdated($flexy)
723     {
724         //return true;
725         // var_dump('check changed?');
726         //DB_DataObject::debugLevel(1);
727         //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
728         
729         //var_dump($flexy->compiledTemplate);
730         $utime = file_exists($flexy->compiledTemplate) ?  filemtime( $flexy->compiledTemplate) : 0;
731         
732        
733         static $cache = array(); // cache of templates..
734         
735         $ff = HTML_FlexyFramework::get();
736         $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
737         
738         // find which of the template directories was actually used for the template.
739         
740         $tempdir = '';
741         foreach($flexy->options['templateDir'] as $td) {
742             if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
743                 $tempdir = $td;
744                 break;
745             }
746         }
747         
748         
749         $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
750         
751         // we do not have any record of this template..
752         if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
753             return false;
754         }
755         
756         if (!isset($cache[$tmpname])) {
757                 
758             
759             
760             $tmpl = DB_DataObject::factory('core_template');
761             if ($view_name !== false) {
762                 $tmpl->view_name = $view_name;
763             }
764             if(!$tmpl->get('template', $tmpname)){
765                 // strip of site prefix if set...
766                  
767                 $tmpl = DB_DataObject::factory('core_template');
768                 if(!$tmpl->get('template', $tmpname)){
769                     //var_dump("no template? {$tmpname} or {$relpath}" );
770                     $cache[$tmpname] = false;
771                     return false;
772                 }
773             }
774             $cache[$tmpname] = $tmpl;
775         } else {
776             $tmpl = $cache[$tmpname] ;
777         }
778           
779          
780         
781         $x = DB_DataObject::factory($this->tableName());
782         $x->lang = $flexy->options['locale'];
783         $x->active = 1;
784         $x->template_id = $tmpl->id;
785         //$x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
786         if ($x->count() < 1) {
787             return false; // we don't have any record of it.
788         }
789         $x->selectAdd();
790         $x->selectAdd('max(updated) as max_updated');
791         $x->find(true);
792         return $x->max_updated;
793         
794         
795     }
796     
797     function toRooArray($req) {
798         $ret = $this->toArray();
799
800         if (empty($req['csvCols'])) {
801             return $ret;
802         }
803
804         // for download
805
806         // translations for table columns
807         if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
808             $ret['template_id_view_name'] = 'database';
809             $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
810         }
811
812         return $ret;
813     }
814
815     function postListFilter($ar, $au, $req)
816     {
817         if (empty($req['csvCols'])) {
818             return $ar;
819         }
820
821         // for download
822
823         $ret = array();
824
825         foreach($ar as $v) {
826             if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
827                 $ret[] = $v;
828                 continue;
829             }
830
831             // translations for table columns
832             // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
833
834             $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
835
836             if(!empty($ret[$key])) {
837                 continue;
838             }
839
840             $ret[$key] = $v;
841         }
842
843         $ret = array_values($ret);
844         
845         return $ret;
846
847     }
848 }