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