fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_enum.php
1 <?php
2 /**
3  * Table Definition for core enum - it's used in pulldowns or simple option lists.
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_enum extends DB_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11     public $__table = 'core_enum';                       // table name
12     public $id;                              // int(11)  not_null primary_key auto_increment
13     public $etype;                           // string(32)  not_null
14     public $name;                            // string(255)  not_null
15     public $active;                          // int(2)  not_null
16     public $seqid;                           // int(11)  not_null multiple_key
17     public $seqmax;                           // int(11)  not_null multiple_key
18     public $display_name;
19
20     /* the code above is auto generated do not remove the tag below */
21     ###END_AUTOCODE
22
23     function applyFilters($q, $au, $roo)
24     {
25
26 //        DB_DataObject::debugLevel(1);
27         if (!empty($q['query']['empty_etype'])) {
28             $this->whereAdd("core_enum.etype = ''");
29         }
30         
31
32         // this should be handled by roo... using '!name[0]' ....
33         if(!empty($q['!name'])){
34             $names = is_array($q['!name']) ? $q['!name'] : explode(',', $q['!name']);
35             foreach($names as $name){
36                 $name  = $this->escape($name);
37                 $this->whereAdd("
38                     core_enum.name NOT IN ('$name')
39                 ");
40             }
41         }
42         if(!empty($q['search']['display_name'])) {
43             $name = $this->escape($q['search']['display_name']);
44             // ilike on postgres?!?
45             $this->whereAdd("
46                 core_enum.display_name LIKE '{$name}%'
47             ");
48
49         }
50
51         if(!empty($q['query']['search'])) {
52             $name = $this->escape($q['query']['search']);
53             // ilike on postgres?!?
54             $this->whereAdd("
55                     core_enum.name LIKE '%{$name}%'
56                 OR
57                     core_enum.display_name LIKE '%{$name}%'
58             ");
59         }
60         if(!empty($q['query']['search_begins'])) {
61             $name = $this->escape($q['query']['search_begins']);
62             // ilike on postgres?!?
63             $this->whereAdd("
64                     core_enum.name LIKE '{$name}%'
65                 OR
66                     core_enum.display_name LIKE '{$name}%'
67             ");
68         }
69
70         if (isset($q['_etypes'])) {
71             $this->whereAddIn('core_enum.etype', explode(',', $q['_etypes']), 'string');
72         }
73
74     }
75
76     function checkPerm($lvl, $au, $req=null)
77     {
78         if (!$au) {
79             return false;
80         }
81         return true;
82     }
83
84
85     function autoJoinCmsTranslate($lang)
86     {
87         $l = $this->escape($lang);
88
89         $this->_join .= "
90             LEFT JOIN
91                 cms_templatestr
92             ON
93                 cms_templatestr.lang = '$l'
94             AND
95                 cms_templatestr.on_table = 'core_enum'
96             AND
97                 cms_templatestr.on_id = core_enum.id
98             AND
99                 cms_templatestr.on_col = 'display_name'
100         ";
101
102         $this->selectAdd("
103             CASE WHEN
104                 '$l' = 'en' THEN display_name
105             ELSE
106                 CASE WHEN cms_templatestr.txt IS NOT NULL AND cms_templatestr.txt != '' THEN
107                     cms_templatestr.txt
108                 ELSE
109                     display_name
110                 END
111             END as  display_name_tr
112         ");
113
114     }
115
116     function postListFilter($data, $authUser, $q) {
117
118        /* if(!empty($q['cmsTab'])){
119             $ret = array();
120             foreach($data as $k=>$v){
121                 if($v['name'] == 'element'){
122                     continue;
123                 }
124                 $ary = $v;
125                 if($ary['name'] == 'page'){
126                     $ary['display_name'] = $v['display_name'].' / Elements';
127                 }
128                 $ret[] = $ary;
129             }
130             $data = $ret;
131         }
132         */
133         return $data;
134
135     }
136
137
138     function beforeUpdate($old, $request,$roo)
139     {
140
141         /* multiple id merge */
142         if(!empty($request['_merge_id'])){
143             if(!empty($request['_ids'])){
144                 //DB_DataObject::DebugLevel(1);
145                 $ce = DB_DataObject::factory('core_enum');
146                 $ce->whereAddIn("id", explode(",", $request['_ids']), "int");
147
148                 foreach($ce->fetchAll() as $mergeItem){
149
150                     $mergeItem->merge($request['_merge_id'], $roo);
151                 }
152             } else {
153                 $this->merge($request['_merge_id'], $roo);
154             }
155             $roo->jok('Merged'); 
156         }
157
158         $tn = $this->tableName();
159         $x = $this->factory($tn);
160         // check if record exists?
161         if(isset($request['etype']) &&   !($old->etype == $request['etype'] && $old->name == $request['name'])){
162             $x->whereAdd("etype = '{$this->escape($request['etype'])}' AND name = '{$this->escape($request['name'])}'");
163             $x->whereAdd("id != ".((int) $this->id));
164             $x->find(true);
165             if($x->count() > 0){
166                 $roo->jerr('a duplicate record already exists');
167             }
168         }
169     }
170     function beforeInsert($req, $roo)
171     {
172         $tn = $this->tableName();
173         $x = $this->factory($tn);
174
175         if(empty($req['etype']) || !strlen(trim($req['etype'])) ){
176
177             if (empty($req['name']) || !strlen(trim($req['name']))) {
178                 $roo->jerr('name or etype missing');
179             }
180
181             if($x->get('name', $req['name'])){
182                 $roo->jerr("name already exists - '{$req['name']}'"  );
183             }
184         } else if (!empty($req['_bulk_names'])) {
185
186             $lines = explode("\n", $req['_bulk_names']);
187             foreach($lines as $l) {
188                 $l = trim($l);
189                 if (!strlen($l)) {
190                     continue;
191                 }
192                 $bits = explode(',', $l);
193                 $rr = array(
194                     'etype' => $req['etype'],
195                     'name' => array_shift($bits)
196                 );
197
198                 $rr['display_name'] = empty($bits) ? $rr['name'] : $bits[0];
199
200                 $x = $this->factory($tn);
201                 $x->beforeInsert($rr, $roo);
202                 $x->setFrom($rr);
203                 $x->insert();
204
205             }
206             $roo->jok("inserted");
207
208         } else {
209             if (empty($req['name']) || !strlen(trim($req['name']))) {
210                 $roo->jerr('name missing');
211             }
212
213             $x->whereAdd("etype = '{$this->escape($req['etype'])}' AND name = '{$this->escape($req['name'])}'");
214             $x->find(true);
215             if($x->count() > 0){
216                 $roo->jerr("name already exists - '{$req['name']}'"  );
217             }
218         }
219     }
220
221     function onInsert($req, $roo)
222     {
223         $x = $this->factory($this->tableName());
224         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
225
226     }
227
228     function onUpdate($old, $req, $roo)
229     {
230         $x = $this->factory($this->tableName());
231         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
232         if ($old->etype != $this->etype) {
233             $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
234         }
235
236         if($this->name != $old->name && !empty($old->name) && empty($old->etype) && empty($this->etype)){
237             $x->query("UPDATE core_enum SET etype = '". $this->escape($this->name)
238                 ."' WHERE etype = '". $this->escape($old->name)."'");
239         }
240     }
241
242     /**
243      * lookup by etype/name and return id
244      */
245     function lookup($etype,$name) {
246         $ce = DB_DataObject::Factory('core_enum');
247         $ce->etype = $etype;
248         $ce->name = $name;
249         if ($ce->find(true)) {
250             return $ce->id;
251         }
252         return 0;
253     }
254
255     function lookupCreate($etype,$name, $display_name=false) {
256
257         // check
258         $ce = DB_DataObject::Factory('core_enum');
259         $ce->setFrom(array(
260             'etype' => '',
261             'name' => $etype
262         ));
263         if (!$ce->find(true)) {
264             $ce->display_name = $etype;
265             $ce->insert();
266         }
267
268         $ce = DB_DataObject::Factory('core_enum');
269         $ce->etype = $etype;
270         $ce->name = $name;
271         if ($ce->find(true)) {
272             return $ce->id;
273         }
274         $ce->active = 1;
275         $ce->display_name = $display_name === false ? $ce->name : $display_name;
276         return  $ce->insert();
277
278     }
279
280     function lookupById($id) {
281         $ce = DB_DataObject::Factory('core_enum');
282         $ce->get($id);
283         return $ce;
284     }
285
286     /**
287      *
288      *
289      *
290      * @param string $etype
291      * @param array $name array of name
292      * @return array ID of core_enum
293      */
294
295     function lookupAllByName($etype,$names) {
296         $ce = DB_DataObject::Factory('core_enum');
297         $ce->etype = $etype;
298         $ce->whereAddIn('name', $names, 'string');
299
300         if ($ce->count() > 0) {
301             return $ce->fetchAll('id');
302         }
303         return array();
304     }
305
306     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
307     {
308         $x = DB_DataObject::factory('core_enum');
309         $x->etype = $etype;
310         $x->active = 1;
311         $x->orderBy('seqid ASC');
312         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
313     }
314
315     function fetchAllByTypeOrderDisplay($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
316     {
317         $x = DB_DataObject::factory('core_enum');
318         $x->etype = $etype;
319         $x->active = 1;
320         $x->orderBy('display_name ASC');
321         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
322     }
323     
324     function lookupObject($etype,$name, $create= false)
325     {
326
327         static $cache = array();
328         $key = "$etype:$name";
329         if (isset($cache[$key]) ) {
330             return $cache[$key];
331         }
332         $ce = DB_DataObject::Factory('core_enum');
333         $ce->etype = $etype;
334         $ce->name = $name;
335         if ($ce->find(true)) {
336             $cache[$key] = $ce;
337             return $ce;
338         }
339         if ($create) {
340             $ce->active = 1;
341             $ce->insert();
342             $cache[$key] = $ce;
343             return $ce->id;
344
345         }
346         return false;
347
348     }
349      // fixme - all calls should be to initDatabase, we need to remove initEnums
350     function initDatabase($roo, $data)
351     {
352         $this->initEnums($data);
353     }
354
355
356     function initEnums($data, $base = array())
357     {
358         // base only contains etype...
359         //print_r($data);
360         $seq_id = 0;
361         if (!empty($base['etype'])) {
362             $seq_id = 1;
363             $t = DB_DAtaObject::Factory('core_enum');
364             $t->etype = $base['etype'];
365             $t->selectAdD();
366             $t->selectAdD('max(seqid) as seqid');
367             if ($t->find(true)) {
368                 $seq_id = $t->seqid+1;
369             }
370         }
371         
372         foreach($data as $row) {
373             $t = DB_DAtaObject::Factory('core_enum');
374
375             $t->etype = isset($row['etype']) ? $row['etype'] : '';
376             $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
377
378             $t->name = isset($row['name']) ? $row['name'] : '';
379
380             if (empty($t->name) && $t->name != 0) {
381                 print_R($data);
382                 die("ERROR: invalid name used for core_enum\n\n");
383             }
384
385             if (!$t->count()) {
386                 // base already gave it the etype..
387                 $t->setFrom($row);
388
389
390                 //$t->is_system_enum = 1; // this should be on the caller..
391
392                 if (!empty($row['seqid']) && !is_numeric($row['seqid'])) {
393                     $t->seqid = $seq_id;
394                     $seq_id++;
395                 }
396
397                 $t->insert();
398             }else{
399                 $t->find(true); // fetch it..
400                 $o = clone($t);
401
402                 if ( isset($row['is_system_enum'])) {
403                      $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
404                 }
405
406                 $t->display_name = isset($row['display_name']) ? $row['display_name'] : $t->display_name;
407
408                 $t->seqid = isset($row['seqid']) ? $row['seqid'] : $t->seqid;
409
410                 $t->update($o);
411
412             }
413             if (!empty($row['cn'])) {
414                 $this->initEnums($row['cn'], array('etype' => $t->name));
415             }
416         }
417
418     }
419
420     function merge($merge_to, $roo)
421     {
422         $affects  = array();
423         $tn = $this->tableName();
424         $x = $this->factory($tn);
425         $all_links = $x->databaseLinks();
426
427         foreach($all_links as $tbl => $links) {
428             foreach($links as $col => $totbl_col) {
429                 $to = explode(':', $totbl_col);
430                 if ($to[0] != $this->tableName()) {
431                     continue;
432                 }
433
434                 $affects[$tbl .'.' . $col] = true;
435             }
436         }
437
438         foreach($affects as $k => $true) {
439             $ka = explode('.', $k);
440
441             $chk = DB_DataObject::factory($ka[0]);
442
443             if (!is_a($chk,'DB_DataObject')) {
444                 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
445             }
446
447             $chk->{$ka[1]} = $this->id;
448
449             foreach ($chk->fetchAll() as $c){
450                 $cc = clone ($c);
451                 $c->{$ka[1]} = $merge_to;
452                 $c->update($cc);
453             }
454         }
455
456         $this->delete();
457
458         
459
460     }
461
462
463 }