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 //        print_r($this);
62         if(!empty($q['id'])){ // this should never be needed?!?!?!?!??
63             $this->id = $q['id'];
64         }
65         
66         if(!empty($q['_ids'])){
67             $this->whereAddIn('core_enum.id', explode(',',  $q['_ids']) , 'int');
68         }
69         
70     }
71     
72     function checkPerm($lvl, $au, $req=null)
73     {
74         if (!$au) {
75             return false;
76         }
77         return true;
78     }
79     
80     
81     function autoJoinCmsTranslate($lang)
82     {
83         $l = $this->escape($lang);
84         
85         $this->_join .= "
86             LEFT JOIN 
87                 cms_templatestr 
88             ON
89                 cms_templatestr.lang = '$l'
90             AND
91                 cms_templatestr.on_table = 'core_enum'
92             AND
93                 cms_templatestr.on_id = core_enum.id
94             AND
95                 cms_templatestr.on_col = 'display_name'
96         ";
97         
98         $this->selectAdd("
99             CASE WHEN 
100                 '$l' = 'en' THEN display_name 
101             ELSE
102                 CASE WHEN cms_templatestr.txt IS NOT NULL AND cms_templatestr.txt != '' THEN
103                     cms_templatestr.txt
104                 ELSE
105                     display_name
106                 END
107             END as  display_name_tr 
108         ");
109         
110     }
111     
112     function postListFilter($data, $authUser, $q) {
113         
114         if(!empty($q['cmsTab'])){
115             $ret = array();
116             foreach($data as $k=>$v){
117                 if($v['name'] == 'element'){
118                     continue;
119                 }
120                 $ary = $v;
121                 if($ary['name'] == 'page'){
122                     $ary['display_name'] = $v['display_name'].' / Elements';
123                 }
124                 $ret[] = $ary;
125             }
126             $data = $ret;
127         }
128         
129         return $data;
130         
131     }
132     
133     
134     function beforeUpdate($old, $request,$roo)
135     {   
136         $tn = $this->tableName();
137         $x = $this->factory($tn);
138         if(!($old->etype == $request['etype'] && $old->name == $request['name'])){
139             $x->whereAdd("etype = '{$this->escape($request['etype'])}' AND name = '{$this->escape($request['name'])}'");
140             $x->find(true);
141             if($x->count() > 0){
142                 $roo->jerr('is exsiting');
143             }
144         }
145     }
146     function beforeInsert($req, $roo)
147     {
148         $tn = $this->tableName();
149         $x = $this->factory($tn);
150         
151         if(empty($req['etype'])){
152             if($x->get('name', $req['name'])){
153                 $roo->jerr('name is exsiting');
154             }
155         }else{
156             $x->whereAdd("etype = '{$this->escape($req['etype'])}' AND name = '{$this->escape($req['name'])}'");
157             $x->find(true);
158             if($x->count() > 0){
159                 $roo->jerr('is exsiting');
160             }
161         }
162     }
163     
164     function onInsert($req)
165     {
166         $x = $this->factory($this->tableName());
167         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
168          
169     }
170     function onUpdate($old, $req)
171     {
172         $x = $this->factory($this->tableName());
173         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
174         if ($old->etype != $this->etype) {
175             $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
176         }
177         if($this->name != $old->name && empty($old->etype) && empty($this->etype)){
178             $x->query("UPDATE core_enum SET etype = '". $this->escape($this->name) 
179                 ."' WHERE etype = '". $this->escape($old->name)."'");
180         }
181     }
182     
183     /**
184      * lookup by etype/name and return id
185      */
186     function lookup($etype,$name) {
187         $ce = DB_DataObject::Factory('core_enum');
188         $ce->etype = $etype;
189         $ce->name = $name;
190         if ($ce->find(true)) {
191             return $ce->id;
192         }
193         return 0;
194     }
195     
196     function lookupCreate($etype,$name, $display_name=false) {
197         $ce = DB_DataObject::Factory('core_enum');
198         $ce->etype = $etype;
199         $ce->name = $name;
200         if ($ce->find(true)) {
201             return $ce->id;
202         }
203         $ce->display_name = $display_name === false ? $ce->name : $display_name;
204         return  $ce->insert();
205         
206     }
207     
208     function lookupById($id) {
209         $ce = DB_DataObject::Factory('core_enum');
210         $ce->get($id);
211         return $ce;
212     }
213     
214     /**
215      * 
216      * 
217      * 
218      * @param string $etype
219      * @param array $name array of name
220      * @return array ID of core_enum 
221      */
222     
223     function lookupAllByName($etype,$names) {
224         $ce = DB_DataObject::Factory('core_enum');
225         $ce->etype = $etype;
226         $ce->whereAddIn('name', $names, 'string');
227         
228         if ($ce->count() > 0) {
229             return $ce->fetchAll('id');
230         }
231         return array();
232     }
233     
234     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
235     {
236         $x = DB_DataObject::factory('core_enum');
237         $x->etype = $etype;
238         $x->active = 1;
239         $x->orderBy('seqid ASC');
240         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
241     }
242     
243     function lookupObject($etype,$name, $create= false)
244     {
245         
246         static $cache = array();
247         $key = "$etype:$name";
248         if (isset($cache[$key]) ) {
249             return $cache[$key];
250         }
251         $ce = DB_DataObject::Factory('core_enum');
252         $ce->etype = $etype;
253         $ce->name = $name;
254         if ($ce->find(true)) {
255             $cache[$key] = $ce;
256             return $ce;
257         }
258         if ($create) {
259             $ce->active = 1;
260             $ce->insert();
261             $cache[$key] = $ce;
262             return $ce->id;
263             
264         }
265         
266         
267         return false;
268         
269     }
270      // fixme - all calls should be to initDatabase, we need to remove initEnums
271     function initDatabase($roo, $data)
272     {
273         $this->initEnums($data);
274     }
275     
276     
277     function initEnums($data, $base = array())
278     {
279         // base only contains etype...
280         //print_r($data);
281         $seq_id = 0;
282         if (!empty($base['etype'])) {
283             $seq_id = 1;
284             $t = DB_DAtaObject::Factory('core_enum');
285             $t->etype = $base['etype'];
286             $t->selectAdD();
287             $t->selectAdD('max(seqid) as seqid');
288             if ($t->find(true)) {
289                 $seq_id = $t->seqid+1;
290             }
291         }
292         foreach($data as $row) {
293             $t = DB_DAtaObject::Factory('core_enum');
294             
295             $t->etype = isset($row['etype']) ? $row['etype'] : '';
296             $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
297             
298             $t->name = isset($row['name']) ? $row['name'] : '';
299             
300             if (empty($t->name)) {
301                 print_R($data);
302                 die("ERROR: invalid name used for core_enum\n\n");
303             }
304             
305             if (!$t->count()) {
306                 // base already gave it the etype..
307                 $t->setFrom($row);
308                 
309                 
310                 //$t->is_system_enum = 1; // this should be on the caller..
311                 
312                 if (!empty($row['seqid'])) {
313                     $t->seqid = $seq_id;
314                     $seq_id++;
315                 }
316                 
317                 $t->insert();
318             }else{
319                 $t->find(true); // fetch it..
320                 if ( isset($row['is_system_enum'])) {
321                      $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
322                     
323                     $t->update();
324                 }
325             }
326             if (!empty($row['cn'])) {
327                 $this->initEnums($row['cn'], array('etype' => $t->name));
328             }
329         }
330         
331     }
332     
333     
334 }