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