3 * Table Definition for Companies
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_Company extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'core_company'; // table name
13 public $code; // string(32) not_null
14 public $name; // string(128)
15 public $remarks; // blob(65535) blobeforeDeleteb
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 public $is_system; // int(2)
45 /* the code above is auto generated do not remove the tag below */
48 function applyFilters($q, $au, $roo)
51 $tn = $this->tableName();
52 $this->selectAdd("i18n_translate('c' , {$tn}.country, 'en') as country_display_name ");
54 $tn = $this->tableName();
55 //DB_DataObject::debugLevel(1);
56 $x = DB_DataObject::factory('core_company');
57 $x->comptype= 'OWNER';
60 if (!empty($q['query']['company_project_id'])) {
62 if (!empty($q['query']['company_include_self'])) {
63 $add = " OR {$tn}.id = {$x->id}";
65 if (!empty($q['query']['company_not_self'])) {
66 $add = " AND {$tn}.id != {$x->id}";
70 $pid = $q['query']['company_project_id'];
71 if (strpos($pid, ',')) {
72 $bits = explode(',', $pid);
73 foreach($bits as $b) {
81 $pids = implode(',', $pids);
82 $this->whereAdd("{$tn}.id IN (
83 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
87 if (!empty($q['query']['comptype'])) {
89 $this->whereAddIn($tn.'.comptype', explode(',', $q['query']['comptype']), 'string');
93 // depricated - should be moved to module specific (texon afair)
95 if (!empty($q['query']['province'])) {
96 $prov = $this->escape($q['query']['province']);
97 $this->whereAdd("province LIKE '$prov%'");
101 // ADD comptype_display name.. = for combos..
111 ) as comptype_display_name
114 if(!empty($q['query']['name']) || !empty($q['search']['name'])){
116 $s = (!empty($q['query']['name'])) ? $this->escape($q['query']['name']) : $this->escape($q['search']['name']);
119 {$tn}.name LIKE '%$s%'
123 if(!empty($q['query']['name_starts']) || !empty($q['search']['name_starts'])){
125 $s = (!empty($q['query']['name_starts'])) ? $this->escape($q['query']['name_starts']) : $this->escape($q['search']['name_starts']);
128 {$tn}.name LIKE '$s%'
133 function toEventString() {
137 // ---------- AUTHENTICATION
138 // not sure where authetnication via company is used?? posibly media-outreach
142 $db = $this->getDatabaseConnection();
143 $sesPrefix = $db->dsn['database'];
144 if (empty($_SERVER['PHP_AUTH_USER'])) {
147 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
149 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
150 $u = DB_DataObject::factory('core_company');
151 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
154 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
157 // not in session or not matched...
163 function getAuthUser()
165 if (!$this->isAuth()) {
168 $db = $this->getDatabaseConnection();
169 $sesPrefix = $db->dsn['database'];
170 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
171 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
173 $u = DB_DataObject::factory('core_company');
174 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
185 $this->isAuth(); // force session start..
186 $db = $this->getDatabaseConnection();
187 $sesPrefix = $db->dsn['database'];
188 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
193 $this->isAuth(); // force session start..
194 $db = $this->getDatabaseConnection();
195 $sesPrefix = $db->dsn['database'];
196 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
199 // ---------- AUTHENTICATION
200 function checkPassword($val)
202 //echo '<pre>'.$val . print_R($this,true);
203 if (substr($this->passwd,0,1) == '$') {
204 return crypt($val,$this->passwd) == $this->passwd ;
206 // old style md5 passwords...- cant be used with courier....
207 return md5($val) == $this->passwd;
209 function setPassword($value)
212 while(strlen($salt)<9) {
213 $salt.=chr(rand(64,126));
214 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
216 $this->passwd = crypt($value, '$1$'. $salt. '$');
219 function onUpload($controller)
221 $image = DB_DataObject::factory('Images');
222 return $image->onUploadWithTbl($this, 'logo_id');
225 function onUpdate($old, $req,$roo)
227 if (!empty($req['password1'])) {
228 $this->setPassword($req['password1']);
232 function onInsert($req, $roo)
234 if (!empty($this->logo_id)) { // update images table to sycn with this..
235 $img = DB_DataObject::factory('Images');
236 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
237 $img->onid = $this->id;
241 if (!empty($req['password1'])) {
242 $this->setPassword($req['password1']);
245 $img = DB_DataObject::factory('Images');
248 $img->ontable = $this->tableName();
249 $img->imgtype = 'LOGO';
250 // should check uploader!!!
252 while($img->fetch()) {
254 $ii->onid = $this->id;
256 $this->logo_id = $ii->id;
263 function beforeInsert($q, $roo)
265 // we still use comptype in some old systems...
267 if(!empty($q['comptype']) && empty($q['comptype_id'])) {
268 $en = DB_DataObject::Factory('core_enum');
270 'etype' => 'COMPTYPE',
271 'name' => $q['comptype'],
274 if($en->find(true)) {
275 $this->comptype_id = $en->id;
279 if (!empty($q['comptype_id'])) {
280 $en = DB_DataObject::Factory('core_enum');
281 $en->get($q['comptype_id']);
282 $this->comptype = $en->name;
285 if(!empty($q['_check_name'])){
286 if($this->checkName()){
294 function beforeUpdate($old, $q,$roo)
296 // we still use comptype in some old systems...
298 if(!empty($q['comptype']) && empty($q['comptype_id'])) {
299 $en = DB_DataObject::Factory('core_enum');
301 'etype' => 'COMPTYPE',
302 'name' => $q['comptype'],
305 if($en->find(true)) {
306 $this->comptype_id = $en->id;
310 if (!empty($q['comptype_id'])) {
311 $en = DB_DataObject::Factory('core_enum');
312 $en->get($q['comptype_id']);
313 $this->comptype = $en->name;
316 if(!empty($q['_flag_delete'])){
317 $this->deleted_dt = $this->sqlValue("NOW()");
318 $this->deleted_by = $roo->getAuthUser()->id;
321 if(!empty($q['_flag_undelete'])){
322 $this->deleted_dt = "";
323 $this->deleted_by = 0;
325 if(!empty($q['_check_name'])){
326 if($this->checkName()){
333 if(!empty($q['_merge_id'])){
334 $this->merge($q['_merge_id'], $roo);
337 if(!empty($this->is_system) &&
338 ($old->code != $this->code ) // used to be not allowed to change name..
340 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
344 function beforeDelete($req, $roo)
347 // should check for members....
348 if(!empty($this->is_system) &&
349 ($old->code != $this->code || $old->name != $this->name)
351 $roo->jerr('This company is not allow to delete');
356 function onDelete($req, $roo)
358 $img = DB_DataObject::factory('Images');
359 $img->ontable = $this->tableName();
360 $img->onid = $this->id;
362 while ($img->fetch()) {
363 $img->beforeDelete(array(), $roo);
371 * check who is trying to access this. false == access denied..
373 function checkPerm($lvl, $au, $changes = false)
376 // do we have an empty system..
377 if ($au && $au->id == -1) {
383 if ($au->company()->comptype != 'OWNER') {
386 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
390 return $this->id == $au->company_id;
393 return $au->hasPerm("Core.Companies", $lvl);
396 function logoImageToHTML($size)
398 $i = DB_DataObject::factory('Images');
399 if (!$this->logo_id || !$i->get($this->logo_id)) {
402 return $i->toHTML($size);
405 function firstImage($filter='image/%')
407 $i = DB_DataObject::factory('Images');
408 //DB_DataObject::debugLevel(1);
409 $im = $i->gather($this, $filter);
416 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
418 $fm = $this->firstImage($filter);
422 return $fm->toHTML($size, $base);
425 function toRooSingleArray($authUser, $request)
427 $ret = $this->toArray();
428 // DB_DataObject::debugLevel(1);
429 // get the comptype display
430 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
432 $ret['comptype_display'] = $ret['comptype'];
433 if ($e && !empty($e->name_display)) {
434 $ret['comptype_display'] = $e->name_display;
443 * create the suppliers...
449 function initCompaniesArray($roo, $data)
451 $tn = $this->tableName();
453 foreach($data as $d){
454 $com = DB_DataObject::factory($tn);
456 if(!$com->find(true)){
457 $com->created_dt = Date('Y-m-d H:i:s');
458 $com->updated_dt = Date('Y-m-d H:i:s');
459 $com->is_system = 1;// new column.. block the user changing the code and name..
468 function initCompanies($roo, $opts)
470 $companies = DB_DataObject::factory('core_company');
472 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
474 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
477 $roo->jerr("invalid company type '$ctype'");
479 if ($ctype =='OWNER') {
480 $companies = DB_DataObject::factory('core_company');
481 $companies->comptype_id = $enum;
482 if ($companies->count()) {
483 $roo->jerr("Owner company already exists");
486 $companies = DB_DataObject::factory('core_company');
489 $companies->setFrom(array(
490 'name' => $opts['add-company'],
491 'comptype' => $ctype,
492 'comptype_id' => $enum,
494 if ($companies->find(true)) {
495 $roo->jerr("company already exists");
497 $companies->setFrom(array(
498 'background_color' => '',
499 'created_dt' => $this->sqlValue('NOW()'),
500 'updated_dt' => $this->sqlValue('NOW()')
504 $companies->insert();
505 $companies->onInsert(array(), $roo);
510 if (empty($this->owner_id)) {
513 static $cache = false;
514 if ($cache !== false && isset($cache[$this->owner_id])) {
515 return $cache[$this->owner_id];
517 $o = DB_DataObject::factory('core_company');
518 if (!$o->get($this->owner_id)) {
521 $cache[$this->owner_id] = $o;
526 /// look up the company which is the system owner...
527 static function lookupOwner()
529 static $cache = false;
530 if ($cache !== false) {
531 return clone($cache); // no updating this object..
534 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
535 $companies = DB_DataObject::factory('core_company');
536 $companies->comptype_id = $enum;
537 if ($companies->find(true)) {
538 $cache = clone($companies);
544 function merge($merge_to, $roo)
548 $all_links = $this->databaseLinks();
550 foreach($all_links as $tbl => $links) {
551 foreach($links as $col => $totbl_col) {
552 $to = explode(':', $totbl_col);
553 if ($to[0] != $this->tableName()) {
557 $affects[$tbl .'.' . $col] = true;
561 foreach($affects as $k => $true) {
562 $ka = explode('.', $k);
564 $chk = DB_DataObject::factory($ka[0]);
566 if (!is_a($chk,'DB_DataObject')) {
567 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
570 $chk->{$ka[1]} = $this->id;
572 foreach ($chk->fetchAll() as $c){
574 $c->{$ka[1]} = $merge_to;
587 $company = DB_DataObject::factory('core_company');
588 $company->setFrom(array(
589 'name' => $this->name
592 if(!empty($this->id)){
593 $company->whereAdd("id != {$this->id}");
596 if(!$company->find(true)){