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