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