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) 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 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'])){
108 $s = $this->escape($q['query']['name']);
110 {$tn}.name LIKE '%$s%'
113 if(!empty($q['search']['name_starts'])){
114 $s = $this->escape($q['search']['name_starts']);
116 {$tn}.name LIKE '$s%'
121 function toEventString() {
125 // ---------- AUTHENTICATION
126 // not sure where authetnication via company is used?? posibly media-outreach
130 $db = $this->getDatabaseConnection();
131 $sesPrefix = $db->dsn['database'];
133 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
135 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
136 $u = DB_DataObject::factory('core_company');
137 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
140 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
143 // not in session or not matched...
149 function getAuthUser()
151 if (!$this->isAuth()) {
154 $db = $this->getDatabaseConnection();
155 $sesPrefix = $db->dsn['database'];
156 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
157 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
159 $u = DB_DataObject::factory('core_company');
160 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
171 $this->isAuth(); // force session start..
172 $db = $this->getDatabaseConnection();
173 $sesPrefix = $db->dsn['database'];
174 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
179 $this->isAuth(); // force session start..
180 $db = $this->getDatabaseConnection();
181 $sesPrefix = $db->dsn['database'];
182 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
185 // ---------- AUTHENTICATION
186 function checkPassword($val)
188 //echo '<pre>'.$val . print_R($this,true);
189 if (substr($this->passwd,0,1) == '$') {
190 return crypt($val,$this->passwd) == $this->passwd ;
192 // old style md5 passwords...- cant be used with courier....
193 return md5($val) == $this->passwd;
195 function setPassword($value)
198 while(strlen($salt)<9) {
199 $salt.=chr(rand(64,126));
200 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
202 $this->passwd = crypt($value, '$1$'. $salt. '$');
205 function onUpload($controller)
207 $image = DB_DataObject::factory('Images');
208 return $image->onUploadWithTbl($this, 'logo_id');
211 function onUpdate($old, $req,$roo)
213 if (!empty($req['password1'])) {
214 $this->setPassword($req['password1']);
218 function onInsert($req, $roo)
220 if (!empty($this->logo_id)) { // update images table to sycn with this..
221 $img = DB_DataObject::factory('Images');
222 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
223 $img->onid = $this->id;
227 if (!empty($req['password1'])) {
228 $this->setPassword($req['password1']);
231 $img = DB_DataObject::factory('Images');
234 $img->ontable = $this->tableName();
235 $img->imgtype = 'LOGO';
236 // should check uploader!!!
238 while($img->fetch()) {
240 $ii->onid = $this->id;
242 $this->logo_id = $ii->id;
249 function beforeInsert($q, $roo)
251 if(!empty($q['_check_name'])){
252 if($this->checkName()){
260 function beforeUpdate($old, $q,$roo)
263 if(!empty($q['_flag_delete']) && $q['_flag_delete'] * 1 == 1){
264 //$delete_dt = date('Y-m-d H:i:s');
265 $this->deleted_dt = $this->sqlValue("NOW()");
266 $this->deleted_by = $roo->getAuthUser()->name;
267 $this->id = $q['_ids'];
270 if(!empty($q['_check_name'])){
271 if($this->checkName()){
278 if(!empty($q['_merge_id'])){
279 $this->merge($q['_merge_id'], $roo);
282 if(!empty($this->is_system) &&
283 ($old->code != $this->code ) // used to be not allowed to change name..
285 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
289 function beforeDelete($req, $roo)
292 // should check for members....
293 if(!empty($this->is_system) &&
294 ($old->code != $this->code || $old->name != $this->name)
296 $roo->jerr('This company is not allow to delete');
301 function onDelete($req, $roo)
303 $img = DB_DataObject::factory('Images');
304 $img->ontable = $this->tableName();
305 $img->onid = $this->id;
307 while ($img->fetch()) {
308 $img->beforeDelete();
316 * check who is trying to access this. false == access denied..
318 function checkPerm($lvl, $au, $changes = false)
321 // do we have an empty system..
322 if ($au && $au->id == -1) {
328 if ($au->company()->comptype != 'OWNER') {
331 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
335 return $this->id == $au->company_id;
338 return $au->hasPerm("Core.Companies", $lvl);
341 function logoImageToHTML($size)
343 $i = DB_DataObject::factory('Images');
344 if (!$this->logo_id || !$i->get($this->logo_id)) {
347 return $i->toHTML($size);
350 function firstImage($filter='image/%')
352 $i = DB_DataObject::factory('Images');
353 //DB_DataObject::debugLevel(1);
354 $im = $i->gather($this, $filter);
361 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
363 $fm = $this->firstImage($filter);
367 return $fm->toHTML($size, $base);
370 function toRooSingleArray($authUser, $request)
372 $ret = $this->toArray();
373 // DB_DataObject::debugLevel(1);
374 // get the comptype display
375 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
377 $ret['comptype_display'] = $ret['comptype'];
378 if ($e && !empty($e->name_display)) {
379 $ret['comptype_display'] = $e->name_display;
388 * create the suppliers...
394 function initCompaniesArray($roo, $data)
396 $tn = $this->tableName();
398 foreach($data as $d){
399 $com = DB_DataObject::factory($tn);
401 if(!$com->find(true)){
402 $com->created_dt = Date('Y-m-d H:i:s');
403 $com->updated_dt = Date('Y-m-d H:i:s');
404 $com->is_system = 1;// new column.. block the user changing the code and name..
413 function initCompanies($roo, $opts)
415 $companies = DB_DataObject::factory('core_company');
417 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
419 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
422 $roo->jerr("invalid company type '$ctype'");
424 if ($ctype =='OWNER') {
425 $companies = DB_DataObject::factory('core_company');
426 $companies->comptype_id = $enum;
427 if ($companies->count()) {
428 $roo->jerr("Owner company already exists");
431 $companies = DB_DataObject::factory('core_company');
434 $companies->setFrom(array(
435 'name' => $opts['add-company'],
436 'comptype' => $ctype,
437 'comptype_id' => $enum,
439 if ($companies->find(true)) {
440 $roo->jerr("company already exists");
442 $companies->setFrom(array(
443 'background_color' => '',
444 'created_dt' => $this->sqlValue('NOW()'),
445 'updated_dt' => $this->sqlValue('NOW()')
449 $companies->insert();
450 $companies->onInsert(array(), $roo);
452 static function lookupOwner()
454 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
455 $companies = DB_DataObject::factory('core_company');
456 $companies->comptype_id = $enum;
457 if ($companies->find(true)) {
463 function merge($merge_to, $roo)
467 $all_links = $this->databaseLinks();
469 foreach($all_links as $tbl => $links) {
470 foreach($links as $col => $totbl_col) {
471 $to = explode(':', $totbl_col);
472 if ($to[0] != $this->tableName()) {
476 $affects[$tbl .'.' . $col] = true;
480 foreach($affects as $k => $true) {
481 $ka = explode('.', $k);
483 $chk = DB_DataObject::factory($ka[0]);
485 if (!is_a($chk,'DB_DataObject')) {
486 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
489 $chk->{$ka[1]} = $this->id;
491 foreach ($chk->fetchAll() as $c){
493 $c->{$ka[1]} = $merge_to;
506 $company = DB_DataObject::factory('core_company');
507 $company->setFrom(array(
508 'name' => $this->name
511 if(!empty($this->id)){
512 $company->whereAdd("id != {$this->id}");
515 if(!$company->find(true)){