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