DataObjects/Core_enum.php
[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)
25     {
26         
27         //DB_DataObject::debugLevel(1);
28         if (!empty($q['query']['empty_etype'])) {
29             $this->whereAdd("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     }
52     
53     function autoJoinCmsTranslate($lang)
54     {
55         $this->join .= "
56             LEFT JOIN cms_templatestr ON 
57                 
58         ";
59         $l = $this->escape($lang);
60         $this->selectAdd("
61             CASE WHEN 
62                 '$lang' = 'en' THEN display_name 
63             ELSE
64                 cms_templatestr.txt
65             END as  display_name_tr 
66
67         ");
68         
69     }
70     
71     function postListFilter($data, $authUser, $q) {
72         
73         if(!empty($q['cmsTab'])){
74             $ret = array();
75             foreach($data as $k=>$v){
76                 if($v['name'] == 'element'){
77                     continue;
78                 }
79                 $ary = $v;
80                 if($ary['name'] == 'page'){
81                     $ary['display_name'] = $v['display_name'].' / Elements';
82                 }
83                 $ret[] = $ary;
84             }
85             $data = $ret;
86         }
87         
88         return $data;
89         
90     }
91     
92     
93     function beforeUpdate($old, $request,$roo)
94     {
95         $tn = $this->tableName();
96         $x = $this->factory($tn);
97         if(!($old->etype == $request['etype'] && $old->name == $request['name'])){
98             $x->whereAdd("etype = '{$request['etype']}' AND name = '{$request['name']}'");
99             $x->find(true);
100             if($x->count() > 0){
101                 $roo->jerr('is exsiting');
102             }
103         }
104     }
105     function beforeInsert($req, $roo)
106     {
107         $tn = $this->tableName();
108         $x = $this->factory($tn);
109         
110         if(empty($req['etype'])){
111             if($x->get('name', $req['name'])){
112                 $roo->jerr('name is exsiting');
113             }
114         }else{
115             $x->whereAdd("etype = '{$req['etype']}' AND name = '{$req['name']}'");
116             $x->find(true);
117             if($x->count() > 0){
118                 $roo->jerr('is exsiting');
119             }
120         }
121     }
122     
123     function onInsert($req)
124     {
125         $x = $this->factory($this->tableName());
126         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
127          
128     }
129     function onUpdate($old, $req)
130     {
131         $x = $this->factory($this->tableName());
132         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
133         if ($old->etype != $this->etype) {
134             $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
135         }
136     }
137     
138     /**
139      * lookup by etype/name and return id
140      */
141     function lookup($etype,$name) {
142         $ce = DB_DataObject::Factory('core_enum');
143         $ce->etype = $etype;
144         $ce->name = $name;
145         if ($ce->find(true)) {
146             return $ce->id;
147         }
148         return 0;
149     }
150     
151     
152     function lookupById($id) {
153         $ce = DB_DataObject::Factory('core_enum');
154         $ce->get($id);
155         return $ce;
156     }
157     
158     /**
159      * 
160      * 
161      * 
162      * @param string $etype
163      * @param array $name array of name
164      * @return array ID of core_enum 
165      */
166     
167     function lookupAllByName($etype,$name) {
168         $ce = DB_DataObject::Factory('core_enum');
169         $ce->etype = $etype;
170         $ce->whereAddIn('name', $name, 'string');
171         
172         if ($ce->count() > 0) {
173             return $ce->fetchAll('id');
174         }
175         return array();
176     }
177     
178     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
179     {
180         $x = DB_DataObject::factory('core_enum');
181         $x->etype = $etype;
182         $x->active = 1;
183         $x->orderBy('seqid ASC');
184         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
185     }
186     
187     function lookupObject($etype,$name, $create= false)
188     {
189         
190         static $cache = array();
191         $key = "$etype:$name";
192         if (isset($cache[$key]) ) {
193             return $cache[$key];
194         }
195         $ce = DB_DataObject::Factory('core_enum');
196         $ce->etype = $etype;
197         $ce->name = $name;
198         if ($ce->find(true)) {
199             $cache[$key] = $ce;
200             return $ce;
201         }
202         if ($create) {
203             $ce->active = 1;
204             $ce->insert();
205             $cache[$key] = $ce;
206             return $ce->id;
207             
208         }
209         
210         
211         return false;
212         
213     }
214      // fixme - all calls should be to initDatabase, we need to remove initEnums
215     function initDatabase($roo, $data)
216     {
217         $this->initEnums($data);
218     }
219     
220     
221     function initEnums($data, $base = array())
222     {
223         // base only contains etype...
224         //print_r($data);
225         $seq_id = 0;
226         if (!empty($base['etype'])) {
227             $seq_id = 1;
228             $t = DB_DAtaObject::Factory('core_enum');
229             $t->etype = $base['etype'];
230             $t->selectAdD();
231             $t->selectAdD('max(seqid) as seqid');
232             if ($t->find(true)) {
233                 $seq_id = $t->seqid+1;
234             }
235         }
236         foreach($data as $row) {
237             $t = DB_DAtaObject::Factory('core_enum');
238             
239             $t->etype = isset($row['etype']) ? $row['etype'] : '';
240             $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
241             
242             $t->name = isset($row['name']) ? $row['name'] : '';
243             
244             if (empty($t->name)) {
245                 print_R($data);
246                 die("ERROR: invalid name used for core_enum\n\n");
247             }
248             
249             if (!$t->count()) {
250                 // base already gave it the etype..
251                 $t->setFrom($row);
252                 
253                 
254                 //$t->is_system_enum = 1; // this should be on the caller..
255                 
256                 if (!empty($row['seqid'])) {
257                     $t->seqid = $seq_id;
258                     $seq_id++;
259                 }
260                 
261                 $t->insert();
262             }else{
263                 $t->find(true); // fetch it..
264                 if ( isset($row['is_system_enum'])) {
265                      $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
266                     
267                     $t->update();
268                 }
269             }
270             if (!empty($row['cn'])) {
271                 $this->initEnums($row['cn'], array('etype' => $t->name));
272             }
273         }
274         
275     }
276     
277     
278 }