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