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