DataObjects/ProjectDirectory.php
[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         
42         //DB_DataObject::debugLevel(1);
43         $x = DB_DataObject::factory('Companies');
44         $x->comptype= 'OWNER';
45         $x->find(true);
46         
47         if (!empty($q['query']['company_project_id'])) {
48             $add = '';
49             if (!empty($q['query']['company_include_self'])) {
50                 $add = ' OR Companies.id = ' . $x->id;
51             }
52             if (!empty($q['query']['company_not_self'])) {
53                 $add = ' AND Companies.id != ' . $x->id;
54             }
55             $pids = array();
56             $pid = $q['query']['company_project_id'];
57             if (strpos($pid, ',')) {
58                 $bits = explode(',', $pid);
59                 foreach($bits as $b) {
60                     $pids[] = (int)$b;
61                 }
62             } else {
63                 $pids = array($pid);
64             }
65             
66             
67             $pids = implode(',', $pids);
68             $this->whereAdd("Companies.id IN (
69                 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
70             ) $add" );
71             
72            
73             
74         }
75         if (!empty($q['query']['comptype'])) {
76            
77             $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
78             
79         }
80          
81          if (!empty($q['query']['province'])) {
82              $prov = $this->escape($q['query']['province']);
83             $this->whereAdd("province LIKE '$prov%'");
84             
85             
86         }
87          
88     }
89     function toEventString() {
90         return $this->name;
91     }
92     
93     // ---------- AUTHENTICATION
94      function isAuth()
95     {
96         $db = $this->getDatabaseConnection();
97         $sesPrefix = $db->dsn['database'];
98         @session_start();
99         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
100             // in session...
101             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
102             $u = DB_DataObject::factory('Companies');
103             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
104                 return true;
105             }
106             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
107             
108         }
109         // not in session or not matched...
110         
111         
112         return false;
113         
114     }
115     function getAuthUser()
116     {
117         if (!$this->isAuth()) {
118             return false;
119         }
120         $db = $this->getDatabaseConnection();
121         $sesPrefix = $db->dsn['database'];
122         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
123             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
124             
125             $u = DB_DataObject::factory('Companies');
126             if ($u->get($a->id)) { /// && strlen($u->passwd)) {
127                 return clone($u);
128             }
129              
130         }
131         
132         
133         return false;
134     }     
135     function login()
136     {
137         $this->isAuth(); // force session start..
138          $db = $this->getDatabaseConnection();
139         $sesPrefix = $db->dsn['database'];
140         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
141         
142     }
143     function logout()
144     {
145         $this->isAuth(); // force session start..
146         $db = $this->getDatabaseConnection();
147         $sesPrefix = $db->dsn['database'];
148         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
149         
150     }    
151     // ---------- AUTHENTICATION
152     function checkPassword($val)
153     {
154         //echo '<pre>'.$val .  print_R($this,true);
155         if (substr($this->passwd,0,1) == '$') {
156             return crypt($val,$this->passwd) == $this->passwd ;
157         }
158         // old style md5 passwords...- cant be used with courier....
159         return md5($val) == $this->passwd;
160     }
161     function setPassword($value) 
162     {
163         $salt='';
164         while(strlen($salt)<9) {
165             $salt.=chr(rand(64,126));
166             //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
167         }
168         $this->passwd = crypt($value, '$1$'. $salt. '$');
169        
170     }      
171     function onUpload($controller)
172     {
173         $image = DB_DataObject::factory('Images');
174         return $image->onUploadWithTbl($this, 'logo_id');
175          
176     }
177     function  onUpdate($old, $req,$roo) 
178     {
179         if (!empty($req['password1'])) {
180             $this->setPassword($req['password1']);
181             $this->update();
182         }
183     }
184     function onInsert($req, $roo)
185     {
186         if (!empty($this->logo_id)) { // update images table to sycn with this..
187             $img = DB_DataObject::factory('Images');
188             if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
189                 $img->onid = $this->id;
190                 $img->update();
191             }
192         }
193         if (!empty($req['password1'])) {
194             $this->setPassword($req['password1']);
195             $this->update();
196         }
197         $img = DB_DataObject::factory('Images');
198         $img->onid= 0;
199         
200         $img->ontable = 'Companies';
201         $img->imgtype = 'LOGO';
202         // should check uploader!!!
203         if ($img->find()) {
204             while($img->fetch()) {
205                 $ii = clone($img);
206                 $ii->onid = $this->id;
207                 $ii->update();
208                 $this->logo_id = $ii->id;
209             }
210             $this->update();
211         }
212         
213         
214         
215         
216     }
217     
218     function beforeDelete()
219     {
220         // should check for members....
221         
222         $img = DB_DataObject::factory('Images');
223         $img->ontable = 'Companies';
224         $img->onid = $this->id;
225         $img->find();
226         while ($img->fetch()) {
227             $img->beforeDelete();
228             $img->delete();
229         }
230         return true;
231         
232          
233     }
234     /**
235      * check who is trying to access this. false == access denied..
236      */
237     function checkPerm($lvl, $au, $changes = false) 
238     {
239         
240         // do we have an empty system..
241         if ($au && $au->id == -1) {
242             return true;
243         }
244         
245         
246         
247         if ($au->company()->comptype != 'OWNER') {
248             
249             // hacking!
250             if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
251                 return false;
252             }
253             
254             return $this->id == $au->company_id;
255         }
256         
257         return $au->hasPerm("Core.".$this->tableName(), $lvl);    
258     } 
259     
260 }