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}";
62 $pid = $q['query']['company_project_id'];
63 if (strpos($pid, ',')) {
64 $bits = explode(',', $pid);
65 foreach($bits as $b) {
73 $pids = implode(',', $pids);
74 $this->whereAdd("{$tn}.id IN (
75 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
79 if (!empty($q['query']['comptype'])) {
81 $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
85 // depricated - should be moved to module specific (texon afair)
87 if (!empty($q['query']['province'])) {
88 $prov = $this->escape($q['query']['province']);
89 $this->whereAdd("province LIKE '$prov%'");
93 // ADD comptype_display name.. = for combos..
103 ) as comptype_display_name
106 if(!empty($q['search']['name'])){
107 $s = $this->escape($q['search']['name']);
109 {$tn}.name LIKE '%$s%'
112 if(!empty($q['search']['name_starts'])){
113 $s = $this->escape($q['search']['name_starts']);
115 {$tn}.name LIKE '$s%'
120 function toEventString() {
124 // ---------- AUTHENTICATION
125 // not sure where authetnication via company is used?? posibly media-outreach
129 $db = $this->getDatabaseConnection();
130 $sesPrefix = $db->dsn['database'];
132 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
134 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
135 $u = DB_DataObject::factory('core_company');
136 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
139 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
142 // not in session or not matched...
148 function getAuthUser()
150 if (!$this->isAuth()) {
153 $db = $this->getDatabaseConnection();
154 $sesPrefix = $db->dsn['database'];
155 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
156 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
158 $u = DB_DataObject::factory('core_company');
159 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
170 $this->isAuth(); // force session start..
171 $db = $this->getDatabaseConnection();
172 $sesPrefix = $db->dsn['database'];
173 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
178 $this->isAuth(); // force session start..
179 $db = $this->getDatabaseConnection();
180 $sesPrefix = $db->dsn['database'];
181 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
184 // ---------- AUTHENTICATION
185 function checkPassword($val)
187 //echo '<pre>'.$val . print_R($this,true);
188 if (substr($this->passwd,0,1) == '$') {
189 return crypt($val,$this->passwd) == $this->passwd ;
191 // old style md5 passwords...- cant be used with courier....
192 return md5($val) == $this->passwd;
194 function setPassword($value)
197 while(strlen($salt)<9) {
198 $salt.=chr(rand(64,126));
199 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
201 $this->passwd = crypt($value, '$1$'. $salt. '$');
204 function onUpload($controller)
206 $image = DB_DataObject::factory('Images');
207 return $image->onUploadWithTbl($this, 'logo_id');
210 function onUpdate($old, $req,$roo)
212 if (!empty($req['password1'])) {
213 $this->setPassword($req['password1']);
217 function onInsert($req, $roo)
219 if (!empty($this->logo_id)) { // update images table to sycn with this..
220 $img = DB_DataObject::factory('Images');
221 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
222 $img->onid = $this->id;
226 if (!empty($req['password1'])) {
227 $this->setPassword($req['password1']);
230 $img = DB_DataObject::factory('Images');
233 $img->ontable = $this->tableName();
234 $img->imgtype = 'LOGO';
235 // should check uploader!!!
237 while($img->fetch()) {
239 $ii->onid = $this->id;
241 $this->logo_id = $ii->id;
248 function beforeInsert($q, $roo)
250 if(!empty($q['_check_name'])){
251 if($this->checkName()){
259 function beforeUpdate($old, $q,$roo)
261 if(!empty($q['_check_name'])){
262 if($this->checkName()){
269 if(!empty($q['_merge_id'])){
270 $this->merge($q['_merge_id'], $roo);
273 if(!empty($this->is_system) &&
274 ($old->code != $this->code ) // used to be not allowed to change name..
276 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
280 function beforeDelete($req, $roo)
282 // should check for members....
283 if(!empty($this->is_system) &&
284 ($old->code != $this->code || $old->name != $this->name)
286 $roo->jerr('This company is not allow to delete');
289 function onDelete($req, $roo)
291 $img = DB_DataObject::factory('Images');
292 $img->ontable = $this->tableName();
293 $img->onid = $this->id;
295 while ($img->fetch()) {
296 $img->beforeDelete();
304 * check who is trying to access this. false == access denied..
306 function checkPerm($lvl, $au, $changes = false)
309 // do we have an empty system..
310 if ($au && $au->id == -1) {
316 if ($au->company()->comptype != 'OWNER') {
319 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
323 return $this->id == $au->company_id;
326 return $au->hasPerm("Core.Companies", $lvl);
329 function logoImageToHTML($size)
331 $i = DB_DataObject::factory('Images');
332 if (!$this->logo_id || !$i->get($this->logo_id)) {
335 return $i->toHTML($size);
338 function firstImage($filter='image/%')
340 $i = DB_DataObject::factory('Images');
341 //DB_DataObject::debugLevel(1);
342 $im = $i->gather($this, $filter);
349 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
351 $fm = $this->firstImage($filter);
355 return $fm->toHTML($size, $base);
358 function toRooSingleArray($authUser, $request)
360 $ret = $this->toArray();
361 // DB_DataObject::debugLevel(1);
362 // get the comptype display
363 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
365 $ret['comptype_display'] = $ret['comptype'];
366 if ($e && !empty($e->name_display)) {
367 $ret['comptype_display'] = $e->name_display;
376 * create the suppliers...
382 function initCompaniesArray($roo, $data)
384 $tn = $this->tableName();
386 foreach($data as $d){
387 $com = DB_DataObject::factory($tn);
389 if(!$com->find(true)){
390 $com->created_dt = Date('Y-m-d H:i:s');
391 $com->updated_dt = Date('Y-m-d H:i:s');
392 $com->is_system = 1;// new column.. block the user changing the code and name..
401 function initCompanies($roo, $opts)
403 $companies = DB_DataObject::factory('core_company');
405 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
407 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
410 $roo->jerr("invalid company type '$ctype'");
412 if ($ctype =='OWNER') {
413 $companies = DB_DataObject::factory('core_company');
414 $companies->comptype_id = $enum;
415 if ($companies->count()) {
416 $roo->jerr("Owner company already exists");
419 $companies = DB_DataObject::factory('core_company');
422 $companies->setFrom(array(
423 'name' => $opts['add-company'],
424 'comptype' => $ctype,
425 'comptype_id' => $enum,
427 if ($companies->find(true)) {
428 $roo->jerr("company already exists");
430 $companies->setFrom(array(
431 'background_color' => '',
432 'created_dt' => $this->sqlValue('NOW()'),
433 'updated_dt' => $this->sqlValue('NOW()')
437 $companies->insert();
438 $companies->onInsert(array(), $roo);
440 static function lookupOwner()
442 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
443 $companies = DB_DataObject::factory('core_company');
444 $companies->comptype_id = $enum;
445 if ($companies->find(true)) {
451 function merge($merge_to, $roo)
455 $all_links = $x->databaseLinks();
457 foreach($all_links as $tbl => $links) {
458 foreach($links as $col => $totbl_col) {
459 $to = explode(':', $totbl_col);
460 if ($to[0] != $this->tableName()) {
464 $affects[$tbl .'.' . $col] = true;
468 foreach($affects as $k => $true) {
469 $ka = explode('.', $k);
471 $chk = DB_DataObject::factory($ka[0]);
473 if (!is_a($chk,'DB_DataObject')) {
474 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
477 $chk->{$ka[1]} = $this->id;
479 foreach ($chk->fetchAll() as $c){
481 $c->{$ka[1]} = $merge_to;
494 $company = DB_DataObject::factory('core_company');
495 $company->setFrom(array(
496 'name' => $this->name
499 if(!empty($this->id)){
500 $company->whereAdd("id != {$this->id}");
503 if(!$company->find(true)){