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