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