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