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