init
[Pman.Core] / DataObjects / Companies.php
1 <?php
2 /**
3  * Table Definition for Companies
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Companies extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'Companies';                       // table name
13     public $code;                            // string(32)  not_null
14     public $name;                            // string(128)  multiple_key
15     public $remarks;                         // blob(65535)  blob
16     public $owner_id;                        // int(11)  not_null
17     public $address;                         // blob(65535)  blob
18     public $tel;                             // string(32)  
19     public $fax;                             // string(32)  
20     public $email;                           // string(128)  
21     public $id;                              // int(11)  not_null primary_key auto_increment
22     public $isOwner;                         // int(11)  
23     public $logo_id;                         // int(11)  not_null
24     public $background_color;                // string(8)  not_null
25     public $comptype;                        // string(8)  not_null
26     public $ava_craft;                       // string(254)  
27     public $url;                             // string(254)  not_null
28     public $main_office_id;                  // int(11)  not_null
29     public $created_by;                      // int(11)  not_null
30     public $created_dt;                      // datetime(19)  not_null binary
31     public $updated_by;                      // int(11)  not_null
32     public $updated_dt;                      // datetime(19)  not_null binary
33     public $passwd;                          // string(64)  not_null
34
35     
36     /* the code above is auto generated do not remove the tag below */
37     ###END_AUTOCODE
38     
39     function applyFilters($q, $au)
40     {
41         $x = DB_DataObject::factory('Companies');
42         $x->isOwner = 1;
43         $x->find(true);
44         
45         if (!empty($q['query']['company_project_id'])) {
46             $add = '';
47             if (!empty($q['query']['company_include_self'])) {
48                 $add = ' OR Companies.id = ' . $x->id;
49             }
50             if (!empty($q['query']['company_not_self'])) {
51                 $add = ' AND Companies.id != ' . $x->id;
52             }
53             $pids = array();
54             $pid = $q['query']['company_project_id'];
55             if (strpos($pid, ',')) {
56                 $bits = explode(',', $pid);
57                 foreach($bits as $b) {
58                     $pids[] = (int)$b;
59                 }
60             } else {
61                 $pids = array($pid);
62             }
63             
64             
65             $pids = implode(',', $pids);
66             $this->whereAdd("Companies.id IN (
67                 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
68             ) $add" );
69             
70            // DB_DataObject::debugLevel(1);
71             
72             
73         }
74         
75     }
76     function toEventString() {
77         return $this->name;
78     }
79     
80     // ---------- AUTHENTICATION
81      function isAuth()
82     {
83         $db = $this->getDatabaseConnection();
84         $sesPrefix = $db->dsn['database'];
85         @session_start();
86         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
87             // in session...
88             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
89             $u = DB_DataObject::factory('Companies');
90             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
91                 return true;
92             }
93             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
94             
95         }
96         // not in session or not matched...
97         
98         
99         return false;
100         
101     }
102     function getAuthUser()
103     {
104         if (!$this->isAuth()) {
105             return false;
106         }
107         $db = $this->getDatabaseConnection();
108         $sesPrefix = $db->dsn['database'];
109         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
110             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
111             
112             $u = DB_DataObject::factory('Companies');
113             if ($u->get($a->id)) { /// && strlen($u->passwd)) {
114                 return clone($u);
115             }
116              
117         }
118         
119         
120         return false;
121     }     
122     function login()
123     {
124         $this->isAuth(); // force session start..
125          $db = $this->getDatabaseConnection();
126         $sesPrefix = $db->dsn['database'];
127         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
128         
129     }
130     function logout()
131     {
132         $this->isAuth(); // force session start..
133         $db = $this->getDatabaseConnection();
134         $sesPrefix = $db->dsn['database'];
135         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
136         
137     }    
138     // ---------- AUTHENTICATION
139     function checkPassword($val)
140     {
141         //echo '<pre>'.$val .  print_R($this,true);
142         if (substr($this->passwd,0,1) == '$') {
143             return crypt($val,$this->passwd) == $this->passwd ;
144         }
145         // old style md5 passwords...- cant be used with courier....
146         return md5($val) == $this->passwd;
147     }
148     function setPassword($value) 
149     {
150         $salt='';
151         while(strlen($salt)<9) {
152             $salt.=chr(rand(64,126));
153             //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
154         }
155         $this->passwd = crypt($value, '$1$'. $salt. '$');
156        
157     }      
158     function onUpload($controller)
159     {
160         $image = DB_DataObject::factory('Images');
161         return $image->onUploadWithTbl($this, 'logo_id');
162          
163     }
164     function  onUpdate($old, $req,$roo) 
165     {
166         if (!empty($req['password1'])) {
167             $this->setPassword($req['password1']);
168             $this->update();
169         }
170     }
171     function onInsert($req, $roo)
172     {
173         if (!empty($this->logo_id)) { // update images table to sycn with this..
174             $img = DB_DataObject::factory('Images');
175             if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
176                 $img->onid = $this->id;
177                 $img->update();
178             }
179         }
180         if (!empty($req['password1'])) {
181             $this->setPassword($req['password1']);
182             $this->update();
183         }
184         $img = DB_DataObject::factory('Images');
185         $img->onid= 0;
186         
187         $img->ontable = 'Companies';
188         $img->imgtype = 'LOGO';
189         // should check uploader!!!
190         if ($img->find()) {
191             while($img->fetch()) {
192                 $ii = clone($img);
193                 $ii->onid = $this->id;
194                 $ii->update();
195                 $this->logo_id = $ii->id;
196             }
197             $this->update();
198         }
199         
200         
201         
202         
203     }
204     
205     function beforeDelete()
206     {
207         // should check for members....
208         
209         $img = DB_DataObject::factory('Images');
210         $img->ontable = 'Companies';
211         $img->onid = $this->id;
212         $img->find();
213         while ($img->fetch()) {
214             $img->beforeDelete();
215             $img->delete();
216         }
217         return true;
218         
219          
220     }
221     /**
222      * check who is trying to access this. false == access denied..
223      */
224     function checkPerm($lvl, $au, $changes = false) 
225     {
226         if ($au->company()->comptype != 'OWNER') {
227             
228             // hacking!
229             if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
230                 return false;
231             }
232             
233             return $this->id == $au->company_id;
234         }
235         
236         return $au->hasPerm("Core.".$this->tableName(), $lvl);    
237     } 
238     function whereAddIn($key, $list, $type= 'int') 
239     {
240         $ar = array();
241         foreach($list as $k) {
242             $ar[] = $type =='int' ? (int)$k : $this->escape($k);
243         }
244         if (!$ar) {
245             return;
246         }
247         return $this->whereAdd("$key IN (". implode(',', $ar). ')');
248     }
249     function fetchAll($k= false, $v = false) 
250     {
251         if ($k !== false) {
252             $this->selectAdd();
253             $this->selectAdd($k);
254             if ($v !== false) {
255                 $this->selectAdd($v);
256             }
257         }
258         
259         $this->find();
260         $ret = array();
261         while ($this->fetch()) {
262             if ($v !== false) {
263                 $ret[$this->$k] = $this->$v;
264                 continue;
265             }
266             $ret[] = $k === false ? clone($this) : $this->$k;
267         }
268         return $ret;
269          
270     }
271 }