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