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