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