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