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