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