3 * Table Definition for Companies
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Companies extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
38 /* the code above is auto generated do not remove the tag below */
41 function applyFilters($q, $au)
43 $tn = $this->tableName();
44 //DB_DataObject::debugLevel(1);
45 $x = DB_DataObject::factory('Companies');
46 $x->comptype= 'OWNER';
49 if (!empty($q['query']['company_project_id'])) {
51 if (!empty($q['query']['company_include_self'])) {
52 $add = ' OR Companies.id = ' . $x->id;
54 if (!empty($q['query']['company_not_self'])) {
55 $add = ' AND Companies.id != ' . $x->id;
58 $pid = $q['query']['company_project_id'];
59 if (strpos($pid, ',')) {
60 $bits = explode(',', $pid);
61 foreach($bits as $b) {
69 $pids = implode(',', $pids);
70 $this->whereAdd("Companies.id IN (
71 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
75 if (!empty($q['query']['comptype'])) {
77 $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
81 // depricated - should be moved to module specific (texon afair)
83 if (!empty($q['query']['province'])) {
84 $prov = $this->escape($q['query']['province']);
85 $this->whereAdd("province LIKE '$prov%'");
89 // ADD comptype_display name.. = for combos..
99 ) as comptype_display_name
104 function toEventString() {
108 // ---------- AUTHENTICATION
111 $db = $this->getDatabaseConnection();
112 $sesPrefix = $db->dsn['database'];
114 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
116 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
117 $u = DB_DataObject::factory('Companies');
118 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
121 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
124 // not in session or not matched...
130 function getAuthUser()
132 if (!$this->isAuth()) {
135 $db = $this->getDatabaseConnection();
136 $sesPrefix = $db->dsn['database'];
137 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
138 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
140 $u = DB_DataObject::factory('Companies');
141 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
152 $this->isAuth(); // force session start..
153 $db = $this->getDatabaseConnection();
154 $sesPrefix = $db->dsn['database'];
155 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
160 $this->isAuth(); // force session start..
161 $db = $this->getDatabaseConnection();
162 $sesPrefix = $db->dsn['database'];
163 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
166 // ---------- AUTHENTICATION
167 function checkPassword($val)
169 //echo '<pre>'.$val . print_R($this,true);
170 if (substr($this->passwd,0,1) == '$') {
171 return crypt($val,$this->passwd) == $this->passwd ;
173 // old style md5 passwords...- cant be used with courier....
174 return md5($val) == $this->passwd;
176 function setPassword($value)
179 while(strlen($salt)<9) {
180 $salt.=chr(rand(64,126));
181 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
183 $this->passwd = crypt($value, '$1$'. $salt. '$');
186 function onUpload($controller)
188 $image = DB_DataObject::factory('Images');
189 return $image->onUploadWithTbl($this, 'logo_id');
192 function onUpdate($old, $req,$roo)
194 if (!empty($req['password1'])) {
195 $this->setPassword($req['password1']);
199 function onInsert($req, $roo)
201 if (!empty($this->logo_id)) { // update images table to sycn with this..
202 $img = DB_DataObject::factory('Images');
203 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
204 $img->onid = $this->id;
208 if (!empty($req['password1'])) {
209 $this->setPassword($req['password1']);
212 $img = DB_DataObject::factory('Images');
215 $img->ontable = 'Companies';
216 $img->imgtype = 'LOGO';
217 // should check uploader!!!
219 while($img->fetch()) {
221 $ii->onid = $this->id;
223 $this->logo_id = $ii->id;
233 function beforeDelete()
235 // should check for members....
237 $img = DB_DataObject::factory('Images');
238 $img->ontable = 'Companies';
239 $img->onid = $this->id;
241 while ($img->fetch()) {
242 $img->beforeDelete();
250 * check who is trying to access this. false == access denied..
252 function checkPerm($lvl, $au, $changes = false)
255 // do we have an empty system..
256 if ($au && $au->id == -1) {
262 if ($au->company()->comptype != 'OWNER') {
265 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
269 return $this->id == $au->company_id;
272 return $au->hasPerm("Core.Companies", $lvl);
275 function logoImageToHTML($size)
277 $i = DB_DataObject::factory('Images');
278 if (!$this->logo_id || !$i->get($this->logo_id)) {
281 return $i->toHTML($size);
284 function firstImage($filter='image/%')
286 $i = DB_DataObject::factory('Images');
287 //DB_DataObject::debugLevel(1);
288 $im = $i->gather($this, $filter);
295 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
297 $fm = $this->firstImage($filter);
301 return $fm->toHTML($size, $base);
304 function toRooSingleArray($authUser, $request)
306 $ret = $this->toArray();
307 // DB_DataObject::debugLevel(1);
308 // get the comptype display
309 $e = DB_DataObject::Factory('core_enum');
310 $e->etype = 'COMPTYPE';
311 $e->name = $this->comptype;
312 $ret['comptype_display'] = $ret['comptype'];
313 if ($e->find(true) && !empty($e->name_display)) {
314 $ret['comptype_display'] = $e->name_display;