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