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