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