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