DataObjects/Companies.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)  
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 $url;                             // string(254)  not_null
27     public $main_office_id;                  // int(11)  not_null
28     public $created_by;                      // int(11)  not_null
29     public $created_dt;                      // datetime(19)  not_null binary
30     public $updated_by;                      // int(11)  not_null
31     public $updated_dt;                      // datetime(19)  not_null binary
32     public $passwd;                          // string(64)  not_null
33     public $dispatch_port;                   // string(255)  not_null
34     public $province;                        // string(255)  not_null
35     public $country;                         // string(4)  not_null
36
37     
38     /* the code above is auto generated do not remove the tag below */
39     ###END_AUTOCODE
40     
41     function applyFilters($q, $au)
42     {
43         
44         //DB_DataObject::debugLevel(1);
45         $x = DB_DataObject::factory('Companies');
46         $x->comptype= 'OWNER';
47         $x->find(true);
48         
49         if (!empty($q['query']['company_project_id'])) {
50             $add = '';
51             if (!empty($q['query']['company_include_self'])) {
52                 $add = ' OR Companies.id = ' . $x->id;
53             }
54             if (!empty($q['query']['company_not_self'])) {
55                 $add = ' AND Companies.id != ' . $x->id;
56             }
57             $pids = array();
58             $pid = $q['query']['company_project_id'];
59             if (strpos($pid, ',')) {
60                 $bits = explode(',', $pid);
61                 foreach($bits as $b) {
62                     $pids[] = (int)$b;
63                 }
64             } else {
65                 $pids = array($pid);
66             }
67             
68             
69             $pids = implode(',', $pids);
70             $this->whereAdd("Companies.id IN (
71                 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
72             ) $add" );
73              
74         }
75         if (!empty($q['query']['comptype'])) {
76            
77             $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
78             
79         }
80         
81         // search company name and 
82         if (!empty($q['_search'])) {
83             $s = $this->escape($q['_search']);
84             $this->whereAdd("Companies.name like '%$s%' OR Companies.description like '%$s%' ");
85         }
86         
87         // depricated - should be moved to module specific (texon afair)
88         
89          if (!empty($q['query']['province'])) {
90              $prov = $this->escape($q['query']['province']);
91             $this->whereAdd("province LIKE '$prov%'");
92             
93             
94         }
95          
96     }
97     function toEventString() {
98         return $this->name;
99     }
100     
101     // ---------- AUTHENTICATION
102      function isAuth()
103     {
104         $db = $this->getDatabaseConnection();
105         $sesPrefix = $db->dsn['database'];
106         @session_start();
107         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
108             // in session...
109             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
110             $u = DB_DataObject::factory('Companies');
111             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
112                 return true;
113             }
114             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
115             
116         }
117         // not in session or not matched...
118         
119         
120         return false;
121         
122     }
123     function getAuthUser()
124     {
125         if (!$this->isAuth()) {
126             return false;
127         }
128         $db = $this->getDatabaseConnection();
129         $sesPrefix = $db->dsn['database'];
130         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
131             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
132             
133             $u = DB_DataObject::factory('Companies');
134             if ($u->get($a->id)) { /// && strlen($u->passwd)) {
135                 return clone($u);
136             }
137              
138         }
139         
140         
141         return false;
142     }     
143     function login()
144     {
145         $this->isAuth(); // force session start..
146          $db = $this->getDatabaseConnection();
147         $sesPrefix = $db->dsn['database'];
148         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
149         
150     }
151     function logout()
152     {
153         $this->isAuth(); // force session start..
154         $db = $this->getDatabaseConnection();
155         $sesPrefix = $db->dsn['database'];
156         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
157         
158     }    
159     // ---------- AUTHENTICATION
160     function checkPassword($val)
161     {
162         //echo '<pre>'.$val .  print_R($this,true);
163         if (substr($this->passwd,0,1) == '$') {
164             return crypt($val,$this->passwd) == $this->passwd ;
165         }
166         // old style md5 passwords...- cant be used with courier....
167         return md5($val) == $this->passwd;
168     }
169     function setPassword($value) 
170     {
171         $salt='';
172         while(strlen($salt)<9) {
173             $salt.=chr(rand(64,126));
174             //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
175         }
176         $this->passwd = crypt($value, '$1$'. $salt. '$');
177        
178     }      
179     function onUpload($controller)
180     {
181         $image = DB_DataObject::factory('Images');
182         return $image->onUploadWithTbl($this, 'logo_id');
183          
184     }
185     function  onUpdate($old, $req,$roo) 
186     {
187         if (!empty($req['password1'])) {
188             $this->setPassword($req['password1']);
189             $this->update();
190         }
191     }
192     function onInsert($req, $roo)
193     {
194         if (!empty($this->logo_id)) { // update images table to sycn with this..
195             $img = DB_DataObject::factory('Images');
196             if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
197                 $img->onid = $this->id;
198                 $img->update();
199             }
200         }
201         if (!empty($req['password1'])) {
202             $this->setPassword($req['password1']);
203             $this->update();
204         }
205         $img = DB_DataObject::factory('Images');
206         $img->onid= 0;
207         
208         $img->ontable = 'Companies';
209         $img->imgtype = 'LOGO';
210         // should check uploader!!!
211         if ($img->find()) {
212             while($img->fetch()) {
213                 $ii = clone($img);
214                 $ii->onid = $this->id;
215                 $ii->update();
216                 $this->logo_id = $ii->id;
217             }
218             $this->update();
219         }
220         
221         
222         
223         
224     }
225     
226     function beforeDelete()
227     {
228         // should check for members....
229         
230         $img = DB_DataObject::factory('Images');
231         $img->ontable = 'Companies';
232         $img->onid = $this->id;
233         $img->find();
234         while ($img->fetch()) {
235             $img->beforeDelete();
236             $img->delete();
237         }
238         return true;
239         
240          
241     }
242     /**
243      * check who is trying to access this. false == access denied..
244      */
245     function checkPerm($lvl, $au, $changes = false) 
246     {
247         
248         // do we have an empty system..
249         if ($au && $au->id == -1) {
250             return true;
251         }
252         
253         
254         
255         if ($au->company()->comptype != 'OWNER') {
256             
257             // hacking!
258             if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
259                 return false;
260             }
261             
262             return $this->id == $au->company_id;
263         }
264         
265         return $au->hasPerm("Core.Companies", $lvl);    
266     }
267     
268     function logoImageToHTML($size)
269     {
270         $i = DB_DataObject::factory('Images');
271         if (!$this->logo_id || !$i->get($this->logo_id)) {
272             return '';
273         }
274         return $i->toHTML($size);
275         
276     }
277      function firstImage($filter='image/%')
278     {
279         $i = DB_DataObject::factory('Images');
280         //DB_DataObject::debugLevel(1);
281         $im = $i->gather($this, $filter);
282         if (empty($im)) {
283             return false;
284         }
285         return $im[0];
286     }
287     
288     function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
289     {
290         $fm = $this->firstImage($filter);
291          if (empty($fm)) {
292             return '';
293         }
294         return $fm->toHTML($size, $base);
295     }
296     
297     
298     
299 }