db5e74b90f77159ed5b32712942a5241f034bf9a
[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         if(!empty($q['!name'])){
33             $names = explode(',', $q['!name']);
34             foreach($names as $name){
35                 $this->whereAdd("
36                     core_enum.name NOT IN ('$name')
37                 ");
38             }
39         }
40     }
41     
42     function postListFilter($data, $authUser, $q) {
43         
44         if(!empty($q['cmsTab'])){
45             $ret = array();
46             foreach($data as $k=>$v){
47                 if($v['name'] == 'page'){
48                     $data[$k]['display_name'] = $v['display_name'].' / Elements';
49                 }
50             }
51         }
52         
53         return $data;
54         
55     }
56     
57     function onUpdate($old, $req)
58     {
59         $x = $this->factory($this->tableName());
60         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')");
61         if ($old->etype != $this->etype) {
62             $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
63         }
64         
65     }
66     function beforeUpdate($old, $request,$roo)
67     {
68         $tn = $this->tableName();
69         $x = $this->factory($tn);
70         if(!($old->etype == $request['etype'] && $old->name == $request['name'])){
71             $x->whereAdd("etype = '{$request['etype']}' AND name = '{$request['name']}'");
72             $x->find(true);
73             if($x->count() > 0){
74                 $roo->jerr('is exsiting');
75             }
76         }
77     }
78     function beforeInsert($req, $roo)
79     {
80         $tn = $this->tableName();
81         $x = $this->factory($tn);
82         
83         if(empty($req['etype'])){
84             if($x->get('name', $req['name'])){
85                 $roo->jerr('name is exsiting');
86             }
87         }else{
88             $x->whereAdd("etype = '{$req['etype']}' AND name = '{$req['name']}'");
89             $x->find(true);
90             if($x->count() > 0){
91                 $roo->jerr('is exsiting');
92             }
93         }
94     }
95     function onInsert($req)
96     {
97         $x = $this->factory($this->tableName());
98         $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')");
99          
100     }
101     
102     function lookup($etype,$name) {
103         $ce = DB_DataObject::Factory('core_enum');
104         $ce->etype = $etype;
105         $ce->name = $name;
106         if ($ce->find(true)) {
107             return $ce->id;
108         }
109         return 0;
110         
111     }
112     
113     function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
114     {
115         $x = DB_DataObject::factory('core_enum');
116         $x->etype = $etype;
117         $x->active = 1;
118         return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
119     }
120     
121     function lookupObject($etype,$name, $create= false)
122     {
123         
124         static $cache = array();
125         $key = "$etype:$name";
126         if (isset($cache[$key]) ) {
127             return $cache[$key];
128         }
129         $ce = DB_DataObject::Factory('core_enum');
130         $ce->etype = $etype;
131         $ce->name = $name;
132         if ($ce->find(true)) {
133             $cache[$key] = $ce;
134             return $ce;
135         }
136         if ($create) {
137             $ce->active = 1;
138             $ce->insert();
139             $cache[$key] = $ce;
140             return $ce->id;
141             
142         }
143         
144         
145         return false;
146         
147     }
148     /**
149      * The base enums that the system always needs,
150      * If we need to add modular enums, then this can be extended, and the two
151      * base data applied.
152      *
153      * This might be moved to an external file later? - eg json...
154      *
155      */
156     function baseEnums()
157     {
158         //
159         return array(
160             array(
161                 'etype' => '',
162                 'name' => 'COMPTYPE',
163                 'display_name' =>  'Company Types',
164                 'cn' => array(
165                     array(
166                         'name' => 'OWNER',
167                         'display_name' => 'Owner',
168                         'seqid' => 999, // last...
169                     )
170                     
171                 )
172             )
173         );  
174         
175         
176         
177     }
178     
179     function initEnums($data, $base = array())
180     {
181         
182          
183         $seq_id = 0;
184         if (!empty($base['etype'])) {
185             $seq_id = 1;
186             $t = DB_DAtaObject::Factory('core_enum');
187             $t->etype = $base['etype'];
188             $t->selectAdD();
189             $t->selectAdD('max(seqid) as seqid');
190             if ($t->find(true)) {
191                 $seq_id = $t->seqid+1;
192             }
193         }
194         foreach($data as $row) {
195             $t = DB_DAtaObject::Factory('core_enum');
196             
197             $t->setFrom($row);
198             $t->setFrom($base);
199             unset($t->seqid); // these might have been changed
200             unset($t->display_name); // these might have been changed
201             
202             
203             if (!$t->find(true))
204             {
205                 $t->setFrom($row);
206                 $t->setFrom($base);
207             
208                 if (!empty($base['etype']) && empty($row['seqid'])) {
209                     $t->seqid = $seq_id;
210                     $seq_id++;
211                 }
212                 $t->insert();
213             }
214             if (!empty($row['cn'])) {
215                 $this->initEnums($row['cn'], array('etype' => $t->name));
216             }
217         }
218         
219     }
220     
221     
222 }