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