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         $tn = $this->tableName();
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         // depricated - should be moved to module specific (texon afair)
82         
83          if (!empty($q['query']['province'])) {
84              $prov = $this->escape($q['query']['province']);
85             $this->whereAdd("province LIKE '$prov%'");
86             
87             
88         }
89         // ADD comptype_display name.. = for combos..
90         $this->selectAdd("
91             (SELECT display_name
92                 FROM
93                     core_enum
94                 WHERE
95                     etype='comptype'
96                     AND
97                     name={$tn}.comptype
98                 LIMIT 1
99                 ) as comptype_display_name
100         ");
101         
102          
103     }
104     
105     function updated_dt($format)
106     {
107         return ($this->updated_dt) ? date($format, strtotime($this->updated_dt)) : null;
108     }
109     
110     function toEventString() {
111         return $this->name;
112     }
113     
114     // ---------- AUTHENTICATION
115      function isAuth()
116     {
117         $db = $this->getDatabaseConnection();
118         $sesPrefix = $db->dsn['database'];
119         @session_start();
120         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
121             // in session...
122             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
123             $u = DB_DataObject::factory('Companies');
124             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
125                 return true;
126             }
127             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
128             
129         }
130         // not in session or not matched...
131         
132         
133         return false;
134         
135     }
136     function getAuthUser()
137     {
138         if (!$this->isAuth()) {
139             return false;
140         }
141         $db = $this->getDatabaseConnection();
142         $sesPrefix = $db->dsn['database'];
143         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
144             $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
145             
146             $u = DB_DataObject::factory('Companies');
147             if ($u->get($a->id)) { /// && strlen($u->passwd)) {
148                 return clone($u);
149             }
150              
151         }
152         
153         
154         return false;
155     }     
156     function login()
157     {
158         $this->isAuth(); // force session start..
159          $db = $this->getDatabaseConnection();
160         $sesPrefix = $db->dsn['database'];
161         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
162         
163     }
164     function logout()
165     {
166         $this->isAuth(); // force session start..
167         $db = $this->getDatabaseConnection();
168         $sesPrefix = $db->dsn['database'];
169         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
170         
171     }    
172     // ---------- AUTHENTICATION
173     function checkPassword($val)
174     {
175         //echo '<pre>'.$val .  print_R($this,true);
176         if (substr($this->passwd,0,1) == '$') {
177             return crypt($val,$this->passwd) == $this->passwd ;
178         }
179         // old style md5 passwords...- cant be used with courier....
180         return md5($val) == $this->passwd;
181     }
182     function setPassword($value) 
183     {
184         $salt='';
185         while(strlen($salt)<9) {
186             $salt.=chr(rand(64,126));
187             //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
188         }
189         $this->passwd = crypt($value, '$1$'. $salt. '$');
190        
191     }      
192     function onUpload($controller)
193     {
194         $image = DB_DataObject::factory('Images');
195         return $image->onUploadWithTbl($this, 'logo_id');
196          
197     }
198     function  onUpdate($old, $req,$roo) 
199     {
200         if (!empty($req['password1'])) {
201             $this->setPassword($req['password1']);
202             $this->update();
203         }
204     }
205     function onInsert($req, $roo)
206     {
207         if (!empty($this->logo_id)) { // update images table to sycn with this..
208             $img = DB_DataObject::factory('Images');
209             if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
210                 $img->onid = $this->id;
211                 $img->update();
212             }
213         }
214         if (!empty($req['password1'])) {
215             $this->setPassword($req['password1']);
216             $this->update();
217         }
218         $img = DB_DataObject::factory('Images');
219         $img->onid= 0;
220         
221         $img->ontable = 'Companies';
222         $img->imgtype = 'LOGO';
223         // should check uploader!!!
224         if ($img->find()) {
225             while($img->fetch()) {
226                 $ii = clone($img);
227                 $ii->onid = $this->id;
228                 $ii->update();
229                 $this->logo_id = $ii->id;
230             }
231             $this->update();
232         }
233         
234         
235         
236         
237     }
238     
239     function beforeDelete()
240     {
241         // should check for members....
242         
243         $img = DB_DataObject::factory('Images');
244         $img->ontable = 'Companies';
245         $img->onid = $this->id;
246         $img->find();
247         while ($img->fetch()) {
248             $img->beforeDelete();
249             $img->delete();
250         }
251         return true;
252         
253          
254     }
255     /**
256      * check who is trying to access this. false == access denied..
257      */
258     function checkPerm($lvl, $au, $changes = false) 
259     {
260         
261         // do we have an empty system..
262         if ($au && $au->id == -1) {
263             return true;
264         }
265         
266         
267         
268         if ($au->company()->comptype != 'OWNER') {
269             
270             // hacking!
271             if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
272                 return false;
273             }
274             
275             return $this->id == $au->company_id;
276         }
277         
278         return $au->hasPerm("Core.Companies", $lvl);    
279     }
280     
281     function logoImageToHTML($size)
282     {
283         $i = DB_DataObject::factory('Images');
284         if (!$this->logo_id || !$i->get($this->logo_id)) {
285             return '';
286         }
287         return $i->toHTML($size);
288         
289     }
290      function firstImage($filter='image/%')
291     {
292         $i = DB_DataObject::factory('Images');
293         //DB_DataObject::debugLevel(1);
294         $im = $i->gather($this, $filter);
295         if (empty($im)) {
296             return false;
297         }
298         return $im[0];
299     }
300     
301     function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
302     {
303         $fm = $this->firstImage($filter);
304          if (empty($fm)) {
305             return '';
306         }
307         return $fm->toHTML($size, $base);
308     }
309     
310     function toRooSingleArray($authUser, $request)
311     {
312         $ret = $this->toArray();
313        // DB_DataObject::debugLevel(1);
314         // get the comptype display
315         $e = DB_DataObject::Factory('core_enum');
316         $e->etype = 'COMPTYPE';
317         $e->name = $this->comptype;
318         $ret['comptype_display'] = $ret['comptype'];
319         if ($e->find(true) && !empty($e->name_display)) {
320             $ret['comptype_display'] = $e->name_display;
321         }
322         
323         
324         return $ret;
325     }
326     
327     
328 }