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 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     
188     function lookupById($id) {
189         $ce = DB_DataObject::Factory('core_enum');
190         $ce->get($id);
191         return $ce;
192     }
193     
194     /**
195      * 
196      * 
197      * 
198      * @param string $etype
199      * @param array $name array of name
200      * @return array ID of core_enum 
201      */
202     
203     function lookupAllByName($etype,$names) {
204         $ce = DB_DataObject::Factory('core_enum');
205         $ce->etype = $etype;
206         $ce->whereAddIn('name', $names, 'string');
207         
208         if ($ce->count() > 0) {
209             return $ce->fetchAll('id');
210         }
211         return array();
212     }
213     
214     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
215     {
216         $x = DB_DataObject::factory('core_enum');
217         $x->etype = $etype;
218         $x->active = 1;
219         $x->orderBy('seqid ASC');
220         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
221     }
222     
223     function lookupObject($etype,$name, $create= false)
224     {
225         
226         static $cache = array();
227         $key = "$etype:$name";
228         if (isset($cache[$key]) ) {
229             return $cache[$key];
230         }
231         $ce = DB_DataObject::Factory('core_enum');
232         $ce->etype = $etype;
233         $ce->name = $name;
234         if ($ce->find(true)) {
235             $cache[$key] = $ce;
236             return $ce;
237         }
238         if ($create) {
239             $ce->active = 1;
240             $ce->insert();
241             $cache[$key] = $ce;
242             return $ce->id;
243             
244         }
245         
246         
247         return false;
248         
249     }
250      // fixme - all calls should be to initDatabase, we need to remove initEnums
251     function initDatabase($roo, $data)
252     {
253         $this->initEnums($data);
254     }
255     
256     
257     function initEnums($data, $base = array())
258     {
259         // base only contains etype...
260         //print_r($data);
261         $seq_id = 0;
262         if (!empty($base['etype'])) {
263             $seq_id = 1;
264             $t = DB_DAtaObject::Factory('core_enum');
265             $t->etype = $base['etype'];
266             $t->selectAdD();
267             $t->selectAdD('max(seqid) as seqid');
268             if ($t->find(true)) {
269                 $seq_id = $t->seqid+1;
270             }
271         }
272         foreach($data as $row) {
273             $t = DB_DAtaObject::Factory('core_enum');
274             
275             $t->etype = isset($row['etype']) ? $row['etype'] : '';
276             $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
277             
278             $t->name = isset($row['name']) ? $row['name'] : '';
279             
280             if (empty($t->name)) {
281                 print_R($data);
282                 die("ERROR: invalid name used for core_enum\n\n");
283             }
284             
285             if (!$t->count()) {
286                 // base already gave it the etype..
287                 $t->setFrom($row);
288                 
289                 
290                 //$t->is_system_enum = 1; // this should be on the caller..
291                 
292                 if (!empty($row['seqid'])) {
293                     $t->seqid = $seq_id;
294                     $seq_id++;
295                 }
296                 
297                 $t->insert();
298             }else{
299                 $t->find(true); // fetch it..
300                 if ( isset($row['is_system_enum'])) {
301                      $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
302                     
303                     $t->update();
304                 }
305             }
306             if (!empty($row['cn'])) {
307                 $this->initEnums($row['cn'], array('etype' => $t->name));
308             }
309         }
310         
311     }
312     
313     
314 }