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