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