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)
38 /* the code above is auto generated do not remove the tag below */
41 function applyFilters($q, $au)
44 $tn = $this->tableName();
45 $this->selectAdd("i18n_translate('c' , {$tn}.country, 'en') as country_display_name ");
47 $tn = $this->tableName();
48 //DB_DataObject::debugLevel(1);
49 $x = DB_DataObject::factory('core_company');
50 $x->comptype= 'OWNER';
53 if (!empty($q['query']['company_project_id'])) {
55 if (!empty($q['query']['company_include_self'])) {
56 $add = " OR {$tn}.id = {$x->id}";
58 if (!empty($q['query']['company_not_self'])) {
59 $add = " AND {$tn}.id != {$x->id}";
63 $pid = $q['query']['company_project_id'];
64 if (strpos($pid, ',')) {
65 $bits = explode(',', $pid);
66 foreach($bits as $b) {
74 $pids = implode(',', $pids);
75 $this->whereAdd("{$tn}.id IN (
76 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
80 if (!empty($q['query']['comptype'])) {
82 $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
86 // depricated - should be moved to module specific (texon afair)
88 if (!empty($q['query']['province'])) {
89 $prov = $this->escape($q['query']['province']);
90 $this->whereAdd("province LIKE '$prov%'");
94 // ADD comptype_display name.. = for combos..
104 ) as comptype_display_name
107 if(!empty($q['query']['name']) || !empty($q['search']['name'])){
109 $s = (!empty($q['query']['name'])) ? $this->escape($q['query']['name']) : $this->escape($q['search']['name']);
112 {$tn}.name LIKE '%$s%'
116 if(!empty($q['query']['name_starts']) || !empty($q['search']['name_starts'])){
118 $s = (!empty($q['query']['name_starts'])) ? $this->escape($q['query']['name_starts']) : $this->escape($q['search']['name_starts']);
121 {$tn}.name LIKE '$s%'
126 function toEventString() {
130 // ---------- AUTHENTICATION
131 // not sure where authetnication via company is used?? posibly media-outreach
135 $db = $this->getDatabaseConnection();
136 $sesPrefix = $db->dsn['database'];
138 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
140 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
141 $u = DB_DataObject::factory('core_company');
142 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
145 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
148 // not in session or not matched...
154 function getAuthUser()
156 if (!$this->isAuth()) {
159 $db = $this->getDatabaseConnection();
160 $sesPrefix = $db->dsn['database'];
161 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
162 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
164 $u = DB_DataObject::factory('core_company');
165 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
176 $this->isAuth(); // force session start..
177 $db = $this->getDatabaseConnection();
178 $sesPrefix = $db->dsn['database'];
179 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
184 $this->isAuth(); // force session start..
185 $db = $this->getDatabaseConnection();
186 $sesPrefix = $db->dsn['database'];
187 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
190 // ---------- AUTHENTICATION
191 function checkPassword($val)
193 //echo '<pre>'.$val . print_R($this,true);
194 if (substr($this->passwd,0,1) == '$') {
195 return crypt($val,$this->passwd) == $this->passwd ;
197 // old style md5 passwords...- cant be used with courier....
198 return md5($val) == $this->passwd;
200 function setPassword($value)
203 while(strlen($salt)<9) {
204 $salt.=chr(rand(64,126));
205 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
207 $this->passwd = crypt($value, '$1$'. $salt. '$');
210 function onUpload($controller)
212 $image = DB_DataObject::factory('Images');
213 return $image->onUploadWithTbl($this, 'logo_id');
216 function onUpdate($old, $req,$roo)
218 if (!empty($req['password1'])) {
219 $this->setPassword($req['password1']);
223 function onInsert($req, $roo)
225 if (!empty($this->logo_id)) { // update images table to sycn with this..
226 $img = DB_DataObject::factory('Images');
227 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
228 $img->onid = $this->id;
232 if (!empty($req['password1'])) {
233 $this->setPassword($req['password1']);
236 $img = DB_DataObject::factory('Images');
239 $img->ontable = $this->tableName();
240 $img->imgtype = 'LOGO';
241 // should check uploader!!!
243 while($img->fetch()) {
245 $ii->onid = $this->id;
247 $this->logo_id = $ii->id;
254 function beforeInsert($q, $roo)
256 // we still use comptype in some old systems...
258 if(!empty($q['comptype']) && empty($q['comptype_id'])) {
259 $en = DB_DataObject::Factory('core_enum');
261 'etype' => 'COMPTYPE',
262 'name' => $q['comptype'],
265 if($en->find(true)) {
266 $this->comptype_id = $en->id;
270 if (!empty($q['comptype_id'])) {
271 $en = DB_DataObject::Factory('core_enum');
272 $en->get($q['comptype_id']);
273 $this->comptype = $en->name;
276 if(!empty($q['_check_name'])){
277 if($this->checkName()){
285 function beforeUpdate($old, $q,$roo)
287 // we still use comptype in some old systems...
289 if(!empty($q['comptype']) && empty($q['comptype_id'])) {
290 $en = DB_DataObject::Factory('core_enum');
292 'etype' => 'COMPTYPE',
293 'name' => $q['comptype'],
296 if($en->find(true)) {
297 $this->comptype_id = $en->id;
301 if (!empty($q['comptype_id'])) {
302 $en = DB_DataObject::Factory('core_enum');
303 $en->get($q['comptype_id']);
304 $this->comptype = $en->name;
307 if(!empty($q['_flag_delete'])){
308 $this->deleted_dt = $this->sqlValue("NOW()");
309 $this->deleted_by = $roo->getAuthUser()->id;
312 if(!empty($q['_flag_undelete'])){
313 $this->deleted_dt = "";
314 $this->deleted_by = 0;
316 if(!empty($q['_check_name'])){
317 if($this->checkName()){
324 if(!empty($q['_merge_id'])){
325 $this->merge($q['_merge_id'], $roo);
328 if(!empty($this->is_system) &&
329 ($old->code != $this->code ) // used to be not allowed to change name..
331 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
335 function beforeDelete($req, $roo)
338 // should check for members....
339 if(!empty($this->is_system) &&
340 ($old->code != $this->code || $old->name != $this->name)
342 $roo->jerr('This company is not allow to delete');
347 function onDelete($req, $roo)
349 $img = DB_DataObject::factory('Images');
350 $img->ontable = $this->tableName();
351 $img->onid = $this->id;
353 while ($img->fetch()) {
354 $img->beforeDelete(array(), $roo);
362 * check who is trying to access this. false == access denied..
364 function checkPerm($lvl, $au, $changes = false)
367 // do we have an empty system..
368 if ($au && $au->id == -1) {
374 if ($au->company()->comptype != 'OWNER') {
377 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
381 return $this->id == $au->company_id;
384 return $au->hasPerm("Core.Companies", $lvl);
387 function logoImageToHTML($size)
389 $i = DB_DataObject::factory('Images');
390 if (!$this->logo_id || !$i->get($this->logo_id)) {
393 return $i->toHTML($size);
396 function firstImage($filter='image/%')
398 $i = DB_DataObject::factory('Images');
399 //DB_DataObject::debugLevel(1);
400 $im = $i->gather($this, $filter);
407 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
409 $fm = $this->firstImage($filter);
413 return $fm->toHTML($size, $base);
416 function toRooSingleArray($authUser, $request)
418 $ret = $this->toArray();
419 // DB_DataObject::debugLevel(1);
420 // get the comptype display
421 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
423 $ret['comptype_display'] = $ret['comptype'];
424 if ($e && !empty($e->name_display)) {
425 $ret['comptype_display'] = $e->name_display;
434 * create the suppliers...
440 function initCompaniesArray($roo, $data)
442 $tn = $this->tableName();
444 foreach($data as $d){
445 $com = DB_DataObject::factory($tn);
447 if(!$com->find(true)){
448 $com->created_dt = Date('Y-m-d H:i:s');
449 $com->updated_dt = Date('Y-m-d H:i:s');
450 $com->is_system = 1;// new column.. block the user changing the code and name..
459 function initCompanies($roo, $opts)
461 $companies = DB_DataObject::factory('core_company');
463 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
465 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
468 $roo->jerr("invalid company type '$ctype'");
470 if ($ctype =='OWNER') {
471 $companies = DB_DataObject::factory('core_company');
472 $companies->comptype_id = $enum;
473 if ($companies->count()) {
474 $roo->jerr("Owner company already exists");
477 $companies = DB_DataObject::factory('core_company');
480 $companies->setFrom(array(
481 'name' => $opts['add-company'],
482 'comptype' => $ctype,
483 'comptype_id' => $enum,
485 if ($companies->find(true)) {
486 $roo->jerr("company already exists");
488 $companies->setFrom(array(
489 'background_color' => '',
490 'created_dt' => $this->sqlValue('NOW()'),
491 'updated_dt' => $this->sqlValue('NOW()')
495 $companies->insert();
496 $companies->onInsert(array(), $roo);
498 static function lookupOwner()
500 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
501 $companies = DB_DataObject::factory('core_company');
502 $companies->comptype_id = $enum;
503 if ($companies->find(true)) {
509 function merge($merge_to, $roo)
513 $all_links = $this->databaseLinks();
515 foreach($all_links as $tbl => $links) {
516 foreach($links as $col => $totbl_col) {
517 $to = explode(':', $totbl_col);
518 if ($to[0] != $this->tableName()) {
522 $affects[$tbl .'.' . $col] = true;
526 foreach($affects as $k => $true) {
527 $ka = explode('.', $k);
529 $chk = DB_DataObject::factory($ka[0]);
531 if (!is_a($chk,'DB_DataObject')) {
532 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
535 $chk->{$ka[1]} = $this->id;
537 foreach ($chk->fetchAll() as $c){
539 $c->{$ka[1]} = $merge_to;
552 $company = DB_DataObject::factory('core_company');
553 $company->setFrom(array(
554 'name' => $this->name
557 if(!empty($this->id)){
558 $company->whereAdd("id != {$this->id}");
561 if(!$company->find(true)){