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');
85 /*if (!empty($q['query']['deleted_by'])) {
86 $deleted_by = $this->escape($q['query']['deleted_by']);
87 $this->whereAdd("deleted_by = '$deleted_by'");
89 // depricated - should be moved to module specific (texon afair)
91 if (!empty($q['query']['province'])) {
92 $prov = $this->escape($q['query']['province']);
93 $this->whereAdd("province LIKE '$prov%'");
97 // ADD comptype_display name.. = for combos..
107 ) as comptype_display_name
110 if(!empty($q['query']['name'])){
111 $s = $this->escape($q['query']['name']);
113 {$tn}.name LIKE '%$s%'
116 if(!empty($q['search']['name_starts'])){
117 $s = $this->escape($q['search']['name_starts']);
119 {$tn}.name LIKE '$s%'
124 function toEventString() {
128 // ---------- AUTHENTICATION
129 // not sure where authetnication via company is used?? posibly media-outreach
133 $db = $this->getDatabaseConnection();
134 $sesPrefix = $db->dsn['database'];
136 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
138 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
139 $u = DB_DataObject::factory('core_company');
140 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
143 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
146 // not in session or not matched...
152 function getAuthUser()
154 if (!$this->isAuth()) {
157 $db = $this->getDatabaseConnection();
158 $sesPrefix = $db->dsn['database'];
159 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
160 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
162 $u = DB_DataObject::factory('core_company');
163 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
174 $this->isAuth(); // force session start..
175 $db = $this->getDatabaseConnection();
176 $sesPrefix = $db->dsn['database'];
177 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
182 $this->isAuth(); // force session start..
183 $db = $this->getDatabaseConnection();
184 $sesPrefix = $db->dsn['database'];
185 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
188 // ---------- AUTHENTICATION
189 function checkPassword($val)
191 //echo '<pre>'.$val . print_R($this,true);
192 if (substr($this->passwd,0,1) == '$') {
193 return crypt($val,$this->passwd) == $this->passwd ;
195 // old style md5 passwords...- cant be used with courier....
196 return md5($val) == $this->passwd;
198 function setPassword($value)
201 while(strlen($salt)<9) {
202 $salt.=chr(rand(64,126));
203 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
205 $this->passwd = crypt($value, '$1$'. $salt. '$');
208 function onUpload($controller)
210 $image = DB_DataObject::factory('Images');
211 return $image->onUploadWithTbl($this, 'logo_id');
214 function onUpdate($old, $req,$roo)
216 if (!empty($req['password1'])) {
217 $this->setPassword($req['password1']);
221 function onInsert($req, $roo)
223 if (!empty($this->logo_id)) { // update images table to sycn with this..
224 $img = DB_DataObject::factory('Images');
225 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
226 $img->onid = $this->id;
230 if (!empty($req['password1'])) {
231 $this->setPassword($req['password1']);
234 $img = DB_DataObject::factory('Images');
237 $img->ontable = $this->tableName();
238 $img->imgtype = 'LOGO';
239 // should check uploader!!!
241 while($img->fetch()) {
243 $ii->onid = $this->id;
245 $this->logo_id = $ii->id;
252 function beforeInsert($q, $roo)
254 if(!empty($q['_check_name'])){
255 if($this->checkName()){
263 function beforeUpdate($old, $q,$roo)
265 if(!empty($q['_check_name'])){
266 if($this->checkName()){
273 if(!empty($q['_merge_id'])){
274 $this->merge($q['_merge_id'], $roo);
277 if(!empty($this->is_system) &&
278 ($old->code != $this->code ) // used to be not allowed to change name..
280 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
284 function beforeDelete($req, $roo)
287 // should check for members....
288 if(!empty($this->is_system) &&
289 ($old->code != $this->code || $old->name != $this->name)
291 $roo->jerr('This company is not allow to delete');
294 if(!empty($req['_flag_delete']) && $req['_flag_delete'] * 1 == 1){
295 $delete_dt = date('Y-m-d H:i:s');
296 $this->query("UPDATE {$x->tableName()} SET deleted_by = {$this->getAuthUser()} , deleted_dt = {$delete_dt} WHERE id = {$req['_delete']}");
297 //$this->addEvent("UPDATE", false, "update core_company record");
298 $this->jok("Updated");
301 function onDelete($req, $roo)
304 $img = DB_DataObject::factory('Images');
305 $img->ontable = $this->tableName();
306 $img->onid = $this->id;
308 while ($img->fetch()) {
309 $img->beforeDelete();
317 * check who is trying to access this. false == access denied..
319 function checkPerm($lvl, $au, $changes = false)
322 // do we have an empty system..
323 if ($au && $au->id == -1) {
329 if ($au->company()->comptype != 'OWNER') {
332 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
336 return $this->id == $au->company_id;
339 return $au->hasPerm("Core.Companies", $lvl);
342 function logoImageToHTML($size)
344 $i = DB_DataObject::factory('Images');
345 if (!$this->logo_id || !$i->get($this->logo_id)) {
348 return $i->toHTML($size);
351 function firstImage($filter='image/%')
353 $i = DB_DataObject::factory('Images');
354 //DB_DataObject::debugLevel(1);
355 $im = $i->gather($this, $filter);
362 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
364 $fm = $this->firstImage($filter);
368 return $fm->toHTML($size, $base);
371 function toRooSingleArray($authUser, $request)
373 $ret = $this->toArray();
374 // DB_DataObject::debugLevel(1);
375 // get the comptype display
376 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
378 $ret['comptype_display'] = $ret['comptype'];
379 if ($e && !empty($e->name_display)) {
380 $ret['comptype_display'] = $e->name_display;
389 * create the suppliers...
395 function initCompaniesArray($roo, $data)
397 $tn = $this->tableName();
399 foreach($data as $d){
400 $com = DB_DataObject::factory($tn);
402 if(!$com->find(true)){
403 $com->created_dt = Date('Y-m-d H:i:s');
404 $com->updated_dt = Date('Y-m-d H:i:s');
405 $com->is_system = 1;// new column.. block the user changing the code and name..
414 function initCompanies($roo, $opts)
416 $companies = DB_DataObject::factory('core_company');
418 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
420 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
423 $roo->jerr("invalid company type '$ctype'");
425 if ($ctype =='OWNER') {
426 $companies = DB_DataObject::factory('core_company');
427 $companies->comptype_id = $enum;
428 if ($companies->count()) {
429 $roo->jerr("Owner company already exists");
432 $companies = DB_DataObject::factory('core_company');
435 $companies->setFrom(array(
436 'name' => $opts['add-company'],
437 'comptype' => $ctype,
438 'comptype_id' => $enum,
440 if ($companies->find(true)) {
441 $roo->jerr("company already exists");
443 $companies->setFrom(array(
444 'background_color' => '',
445 'created_dt' => $this->sqlValue('NOW()'),
446 'updated_dt' => $this->sqlValue('NOW()')
450 $companies->insert();
451 $companies->onInsert(array(), $roo);
453 static function lookupOwner()
455 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
456 $companies = DB_DataObject::factory('core_company');
457 $companies->comptype_id = $enum;
458 if ($companies->find(true)) {
464 function merge($merge_to, $roo)
468 $all_links = $this->databaseLinks();
470 foreach($all_links as $tbl => $links) {
471 foreach($links as $col => $totbl_col) {
472 $to = explode(':', $totbl_col);
473 if ($to[0] != $this->tableName()) {
477 $affects[$tbl .'.' . $col] = true;
481 foreach($affects as $k => $true) {
482 $ka = explode('.', $k);
484 $chk = DB_DataObject::factory($ka[0]);
486 if (!is_a($chk,'DB_DataObject')) {
487 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
490 $chk->{$ka[1]} = $this->id;
492 foreach ($chk->fetchAll() as $c){
494 $c->{$ka[1]} = $merge_to;
507 $company = DB_DataObject::factory('core_company');
508 $company->setFrom(array(
509 'name' => $this->name
512 if(!empty($this->id)){
513 $company->whereAdd("id != {$this->id}");
516 if(!$company->find(true)){