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