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