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         if($this->name != $old->name && empty($old->etype) && empty($this->etype)){
174             $x->query("UPDATE core_enum SET etype = '". $this->escape($this->name) 
175                 ."' WHERE etype = '". $this->escape($old->name)."'");
176         }
177     }
178     
179     /**
180      * lookup by etype/name and return id
181      */
182     function lookup($etype,$name) {
183         $ce = DB_DataObject::Factory('core_enum');
184         $ce->etype = $etype;
185         $ce->name = $name;
186         if ($ce->find(true)) {
187             return $ce->id;
188         }
189         return 0;
190     }
191     
192     function lookupCreate($etype,$name, $display_name=false) {
193         
194         // check
195         $ce = DB_DataObject::Factory('core_enum');
196         $ce->setFrom(array(
197             'etype' => '',
198             'name' => $etype
199         ));
200         if (!$ce->find(true)) {
201             $ce->display_name = $etype;
202             $ce->insert();
203         }
204         
205         $ce = DB_DataObject::Factory('core_enum');
206         $ce->etype = $etype;
207         $ce->name = $name;
208         if ($ce->find(true)) {
209             return $ce->id;
210         }
211         $ce->active = 1;
212         $ce->display_name = $display_name === false ? $ce->name : $display_name;
213         return  $ce->insert();
214         
215     }
216     
217     function lookupById($id) {
218         $ce = DB_DataObject::Factory('core_enum');
219         $ce->get($id);
220         return $ce;
221     }
222     
223     /**
224      * 
225      * 
226      * 
227      * @param string $etype
228      * @param array $name array of name
229      * @return array ID of core_enum 
230      */
231     
232     function lookupAllByName($etype,$names) {
233         $ce = DB_DataObject::Factory('core_enum');
234         $ce->etype = $etype;
235         $ce->whereAddIn('name', $names, 'string');
236         
237         if ($ce->count() > 0) {
238             return $ce->fetchAll('id');
239         }
240         return array();
241     }
242     
243     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
244     {
245         $x = DB_DataObject::factory('core_enum');
246         $x->etype = $etype;
247         $x->active = 1;
248         $x->orderBy('seqid ASC');
249         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
250     }
251     
252     function lookupObject($etype,$name, $create= false)
253     {
254         
255         static $cache = array();
256         $key = "$etype:$name";
257         if (isset($cache[$key]) ) {
258             return $cache[$key];
259         }
260         $ce = DB_DataObject::Factory('core_enum');
261         $ce->etype = $etype;
262         $ce->name = $name;
263         if ($ce->find(true)) {
264             $cache[$key] = $ce;
265             return $ce;
266         }
267         if ($create) {
268             $ce->active = 1;
269             $ce->insert();
270             $cache[$key] = $ce;
271             return $ce->id;
272             
273         }
274         
275         
276         return false;
277         
278     }
279      // fixme - all calls should be to initDatabase, we need to remove initEnums
280     function initDatabase($roo, $data)
281     {
282         $this->initEnums($data);
283     }
284     
285     
286     function initEnums($data, $base = array())
287     {
288         // base only contains etype...
289         //print_r($data);
290         $seq_id = 0;
291         if (!empty($base['etype'])) {
292             $seq_id = 1;
293             $t = DB_DAtaObject::Factory('core_enum');
294             $t->etype = $base['etype'];
295             $t->selectAdD();
296             $t->selectAdD('max(seqid) as seqid');
297             if ($t->find(true)) {
298                 $seq_id = $t->seqid+1;
299             }
300         }
301         foreach($data as $row) {
302             $t = DB_DAtaObject::Factory('core_enum');
303             
304             $t->etype = isset($row['etype']) ? $row['etype'] : '';
305             $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
306             
307             $t->name = isset($row['name']) ? $row['name'] : '';
308             
309             if (empty($t->name)) {
310                 print_R($data);
311                 die("ERROR: invalid name used for core_enum\n\n");
312             }
313             
314             if (!$t->count()) {
315                 // base already gave it the etype..
316                 $t->setFrom($row);
317                 
318                 
319                 //$t->is_system_enum = 1; // this should be on the caller..
320                 
321                 if (!empty($row['seqid'])) {
322                     $t->seqid = $seq_id;
323                     $seq_id++;
324                 }
325                 
326                 $t->insert();
327             }else{
328                 $t->find(true); // fetch it..
329                 if ( isset($row['is_system_enum'])) {
330                      $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
331                     
332                     $t->update();
333                 }
334             }
335             if (!empty($row['cn'])) {
336                 $this->initEnums($row['cn'], array('etype' => $t->name));
337             }
338         }
339         
340     }
341     
342     function merge($merge_to, $roo)
343     {
344         $affects  = array();
345         
346         $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$this->_database];
347         
348         foreach($all_links as $tbl => $links) {
349             foreach($links as $col => $totbl_col) {
350                 $to = explode(':', $totbl_col);
351                 if ($to[0] != $this->tableName()) {
352                     continue;
353                 }
354                 
355                 $affects[$tbl .'.' . $col] = true;
356             }
357         }
358         
359         foreach($affects as $k => $true) {
360             $ka = explode('.', $k);
361
362             $chk = DB_DataObject::factory($ka[0]);
363             
364             if (!is_a($chk,'DB_DataObject')) {
365                 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
366             }
367             
368             $chk->{$ka[1]} = $this->id;
369
370             foreach ($chk->fetchAll() as $c){
371                 $cc = clone ($c);
372                 $c->{$ka[1]} = $merge_to;
373                 $c->update($cc);
374             }
375         }
376         
377         $this->delete();
378         
379         $roo->jok('Merged');
380         
381     }
382     
383     
384 }