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         $tn = $this->tableName();
128         $x = $this->factory($tn);
129         if(!($old->etype == $request['etype'] && $old->name == $request['name'])){
130             $x->whereAdd("etype = '{$this->escape($request['etype'])}' AND name = '{$this->escape($request['name'])}'");
131             $x->find(true);
132             if($x->count() > 0){
133                 $roo->jerr('is exsiting');
134             }
135         }
136     }
137     function beforeInsert($req, $roo)
138     {
139         $tn = $this->tableName();
140         $x = $this->factory($tn);
141         
142         if(empty($req['etype'])){
143             if($x->get('name', $req['name'])){
144                 $roo->jerr('name is exsiting');
145             }
146         }else{
147             $x->whereAdd("etype = '{$this->escape($req['etype'])}' AND name = '{$this->escape($req['name'])}'");
148             $x->find(true);
149             if($x->count() > 0){
150                 $roo->jerr('is exsiting');
151             }
152         }
153     }
154     
155     function onInsert($req)
156     {
157         $x = $this->factory($this->tableName());
158         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
159          
160     }
161     function onUpdate($old, $req)
162     {
163         $x = $this->factory($this->tableName());
164         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
165         if ($old->etype != $this->etype) {
166             $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
167         }
168         if($this->name != $old->name && empty($old->etype) && empty($this->etype)){
169             $x->query("UPDATE core_enum SET etype = '". $this->escape($this->name) 
170                 ."' WHERE etype = '". $this->escape($old->name)."'");
171         }
172     }
173     
174     /**
175      * lookup by etype/name and return id
176      */
177     function lookup($etype,$name) {
178         $ce = DB_DataObject::Factory('core_enum');
179         $ce->etype = $etype;
180         $ce->name = $name;
181         if ($ce->find(true)) {
182             return $ce->id;
183         }
184         return 0;
185     }
186     
187     function lookupCreate($etype,$name, $display_name=false) {
188         
189         // check
190         $ce = DB_DataObject::Factory('core_enum');
191         $ce->setFrom(array(
192             'etype' => '',
193             'name' => $etype
194         ));
195         if (!$ce->find(true)) {
196             $ce->display_name = $etype;
197             $ce->insert();
198         }
199         
200         $ce = DB_DataObject::Factory('core_enum');
201         $ce->etype = $etype;
202         $ce->name = $name;
203         if ($ce->find(true)) {
204             return $ce->id;
205         }
206         $ce->active = 1;
207         $ce->display_name = $display_name === false ? $ce->name : $display_name;
208         return  $ce->insert();
209         
210     }
211     
212     function lookupById($id) {
213         $ce = DB_DataObject::Factory('core_enum');
214         $ce->get($id);
215         return $ce;
216     }
217     
218     /**
219      * 
220      * 
221      * 
222      * @param string $etype
223      * @param array $name array of name
224      * @return array ID of core_enum 
225      */
226     
227     function lookupAllByName($etype,$names) {
228         $ce = DB_DataObject::Factory('core_enum');
229         $ce->etype = $etype;
230         $ce->whereAddIn('name', $names, 'string');
231         
232         if ($ce->count() > 0) {
233             return $ce->fetchAll('id');
234         }
235         return array();
236     }
237     
238     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
239     {
240         $x = DB_DataObject::factory('core_enum');
241         $x->etype = $etype;
242         $x->active = 1;
243         $x->orderBy('seqid ASC');
244         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
245     }
246     
247     function lookupObject($etype,$name, $create= false)
248     {
249         
250         static $cache = array();
251         $key = "$etype:$name";
252         if (isset($cache[$key]) ) {
253             return $cache[$key];
254         }
255         $ce = DB_DataObject::Factory('core_enum');
256         $ce->etype = $etype;
257         $ce->name = $name;
258         if ($ce->find(true)) {
259             $cache[$key] = $ce;
260             return $ce;
261         }
262         if ($create) {
263             $ce->active = 1;
264             $ce->insert();
265             $cache[$key] = $ce;
266             return $ce->id;
267             
268         }
269         
270         
271         return false;
272         
273     }
274      // fixme - all calls should be to initDatabase, we need to remove initEnums
275     function initDatabase($roo, $data)
276     {
277         $this->initEnums($data);
278     }
279     
280     
281     function initEnums($data, $base = array())
282     {
283         // base only contains etype...
284         //print_r($data);
285         $seq_id = 0;
286         if (!empty($base['etype'])) {
287             $seq_id = 1;
288             $t = DB_DAtaObject::Factory('core_enum');
289             $t->etype = $base['etype'];
290             $t->selectAdD();
291             $t->selectAdD('max(seqid) as seqid');
292             if ($t->find(true)) {
293                 $seq_id = $t->seqid+1;
294             }
295         }
296         foreach($data as $row) {
297             $t = DB_DAtaObject::Factory('core_enum');
298             
299             $t->etype = isset($row['etype']) ? $row['etype'] : '';
300             $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
301             
302             $t->name = isset($row['name']) ? $row['name'] : '';
303             
304             if (empty($t->name)) {
305                 print_R($data);
306                 die("ERROR: invalid name used for core_enum\n\n");
307             }
308             
309             if (!$t->count()) {
310                 // base already gave it the etype..
311                 $t->setFrom($row);
312                 
313                 
314                 //$t->is_system_enum = 1; // this should be on the caller..
315                 
316                 if (!empty($row['seqid'])) {
317                     $t->seqid = $seq_id;
318                     $seq_id++;
319                 }
320                 
321                 $t->insert();
322             }else{
323                 $t->find(true); // fetch it..
324                 if ( isset($row['is_system_enum'])) {
325                      $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
326                     
327                     $t->update();
328                 }
329             }
330             if (!empty($row['cn'])) {
331                 $this->initEnums($row['cn'], array('etype' => $t->name));
332             }
333         }
334         
335     }
336     
337     
338 }