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