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("core_enum.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         if(!empty($q['query']['search'])) {
52             $name = $this->escape($q['query']['search']);
53             // ilike on postgres?!?
54             $this->whereAdd("
55                     core_enum.name LIKE '%{$name}%'
56                 OR
57                     core_enum.display_name LIKE '%{$name}%'
58             ");
59         }
60         
61     }
62     
63     function checkPerm($lvl, $au, $req=null)
64     {
65         if (!$au) {
66             return false;
67         }
68         return true;
69     }
70     
71     
72     function autoJoinCmsTranslate($lang)
73     {
74         $l = $this->escape($lang);
75         
76         $this->_join .= "
77             LEFT JOIN 
78                 cms_templatestr 
79             ON
80                 cms_templatestr.lang = '$l'
81             AND
82                 cms_templatestr.on_table = 'core_enum'
83             AND
84                 cms_templatestr.on_id = core_enum.id
85             AND
86                 cms_templatestr.on_col = 'display_name'
87         ";
88         
89         $this->selectAdd("
90             CASE WHEN 
91                 '$l' = 'en' THEN display_name 
92             ELSE
93                 CASE WHEN cms_templatestr.txt IS NOT NULL AND cms_templatestr.txt != '' THEN
94                     cms_templatestr.txt
95                 ELSE
96                     display_name
97                 END
98             END as  display_name_tr 
99         ");
100         
101     }
102     
103     function postListFilter($data, $authUser, $q) {
104         
105         if(!empty($q['cmsTab'])){
106             $ret = array();
107             foreach($data as $k=>$v){
108                 if($v['name'] == 'element'){
109                     continue;
110                 }
111                 $ary = $v;
112                 if($ary['name'] == 'page'){
113                     $ary['display_name'] = $v['display_name'].' / Elements';
114                 }
115                 $ret[] = $ary;
116             }
117             $data = $ret;
118         }
119         
120         return $data;
121         
122     }
123     
124     
125     function beforeUpdate($old, $request,$roo)
126     {   
127         if(!empty($request['_merge_id'])){
128             $this->merge($request['_merge_id'], $roo);
129         }
130         
131         $tn = $this->tableName();
132         $x = $this->factory($tn);
133         // check if record exists?
134         if(isset($request['etype']) &&   !($old->etype == $request['etype'] && $old->name == $request['name'])){
135             $x->whereAdd("etype = '{$this->escape($request['etype'])}' AND name = '{$this->escape($request['name'])}'");
136             $x->find(true);
137             if($x->count() > 0){
138                 $roo->jerr('record already exists');
139             }
140         }
141     }
142     function beforeInsert($req, $roo)
143     {
144         $tn = $this->tableName();
145         $x = $this->factory($tn);
146         
147         if(empty($req['etype'])){
148             if($x->get('name', $req['name'])){
149                 $roo->jerr('name is exsiting');
150             }
151         }else{
152             $x->whereAdd("etype = '{$this->escape($req['etype'])}' AND name = '{$this->escape($req['name'])}'");
153             $x->find(true);
154             if($x->count() > 0){
155                 $roo->jerr('is exsiting');
156             }
157         }
158     }
159     
160     function onInsert($req)
161     {
162         $x = $this->factory($this->tableName());
163         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
164          
165     }
166     function onUpdate($old, $req)
167     {
168         $x = $this->factory($this->tableName());
169         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
170         if ($old->etype != $this->etype) {
171             $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
172         }
173         
174         if($this->name != $old->name && !empty($old->name) && empty($old->etype) && empty($this->etype)){
175             $x->query("UPDATE core_enum SET etype = '". $this->escape($this->name) 
176                 ."' WHERE etype = '". $this->escape($old->name)."'");
177         }
178     }
179     
180     /**
181      * lookup by etype/name and return id
182      */
183     function lookup($etype,$name) {
184         $ce = DB_DataObject::Factory('core_enum');
185         $ce->etype = $etype;
186         $ce->name = $name;
187         if ($ce->find(true)) {
188             return $ce->id;
189         }
190         return 0;
191     }
192     
193     function lookupCreate($etype,$name, $display_name=false) {
194         
195         // check
196         $ce = DB_DataObject::Factory('core_enum');
197         $ce->setFrom(array(
198             'etype' => '',
199             'name' => $etype
200         ));
201         if (!$ce->find(true)) {
202             $ce->display_name = $etype;
203             $ce->insert();
204         }
205         
206         $ce = DB_DataObject::Factory('core_enum');
207         $ce->etype = $etype;
208         $ce->name = $name;
209         if ($ce->find(true)) {
210             return $ce->id;
211         }
212         $ce->active = 1;
213         $ce->display_name = $display_name === false ? $ce->name : $display_name;
214         return  $ce->insert();
215         
216     }
217     
218     function lookupById($id) {
219         $ce = DB_DataObject::Factory('core_enum');
220         $ce->get($id);
221         return $ce;
222     }
223     
224     /**
225      * 
226      * 
227      * 
228      * @param string $etype
229      * @param array $name array of name
230      * @return array ID of core_enum 
231      */
232     
233     function lookupAllByName($etype,$names) {
234         $ce = DB_DataObject::Factory('core_enum');
235         $ce->etype = $etype;
236         $ce->whereAddIn('name', $names, 'string');
237         
238         if ($ce->count() > 0) {
239             return $ce->fetchAll('id');
240         }
241         return array();
242     }
243     
244     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
245     {
246         $x = DB_DataObject::factory('core_enum');
247         $x->etype = $etype;
248         $x->active = 1;
249         $x->orderBy('seqid ASC');
250         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
251     }
252     
253     function lookupObject($etype,$name, $create= false)
254     {
255         
256         static $cache = array();
257         $key = "$etype:$name";
258         if (isset($cache[$key]) ) {
259             return $cache[$key];
260         }
261         $ce = DB_DataObject::Factory('core_enum');
262         $ce->etype = $etype;
263         $ce->name = $name;
264         if ($ce->find(true)) {
265             $cache[$key] = $ce;
266             return $ce;
267         }
268         if ($create) {
269             $ce->active = 1;
270             $ce->insert();
271             $cache[$key] = $ce;
272             return $ce->id;
273             
274         }
275         
276         
277         return false;
278         
279     }
280      // fixme - all calls should be to initDatabase, we need to remove initEnums
281     function initDatabase($roo, $data)
282     {
283         $this->initEnums($data);
284     }
285     
286     
287     function initEnums($data, $base = array())
288     {
289         // base only contains etype...
290         //print_r($data);
291         $seq_id = 0;
292         if (!empty($base['etype'])) {
293             $seq_id = 1;
294             $t = DB_DAtaObject::Factory('core_enum');
295             $t->etype = $base['etype'];
296             $t->selectAdD();
297             $t->selectAdD('max(seqid) as seqid');
298             if ($t->find(true)) {
299                 $seq_id = $t->seqid+1;
300             }
301         }
302         foreach($data as $row) {
303             $t = DB_DAtaObject::Factory('core_enum');
304             
305             $t->etype = isset($row['etype']) ? $row['etype'] : '';
306             $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
307             
308             $t->name = isset($row['name']) ? $row['name'] : '';
309             
310             if (empty($t->name)) {
311                 print_R($data);
312                 die("ERROR: invalid name used for core_enum\n\n");
313             }
314             
315             if (!$t->count()) {
316                 // base already gave it the etype..
317                 $t->setFrom($row);
318                 
319                 
320                 //$t->is_system_enum = 1; // this should be on the caller..
321                 
322                 if (!empty($row['seqid'])) {
323                     $t->seqid = $seq_id;
324                     $seq_id++;
325                 }
326                 
327                 $t->insert();
328             }else{
329                 $t->find(true); // fetch it..
330                 if ( isset($row['is_system_enum'])) {
331                      $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
332                     
333                     $t->update();
334                 }
335             }
336             if (!empty($row['cn'])) {
337                 $this->initEnums($row['cn'], array('etype' => $t->name));
338             }
339         }
340         
341     }
342     
343     function merge($merge_to, $roo)
344     {
345         $affects  = array();
346         
347         $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$this->_database];
348         
349         foreach($all_links as $tbl => $links) {
350             foreach($links as $col => $totbl_col) {
351                 $to = explode(':', $totbl_col);
352                 if ($to[0] != $this->tableName()) {
353                     continue;
354                 }
355                 
356                 $affects[$tbl .'.' . $col] = true;
357             }
358         }
359         
360         foreach($affects as $k => $true) {
361             $ka = explode('.', $k);
362
363             $chk = DB_DataObject::factory($ka[0]);
364             
365             if (!is_a($chk,'DB_DataObject')) {
366                 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
367             }
368             
369             $chk->{$ka[1]} = $this->id;
370
371             foreach ($chk->fetchAll() as $c){
372                 $cc = clone ($c);
373                 $c->{$ka[1]} = $merge_to;
374                 $c->update($cc);
375             }
376         }
377         
378         $this->delete();
379         
380         $roo->jok('Merged');
381         
382     }
383     
384     
385 }