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