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