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