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'])){
114 $s = $this->escape($q['search']['name']);
116 {$tn}.name LIKE '%$s%'
119 if(!empty($q['search']['name_starts'])){
120 $s = $this->escape($q['search']['name_starts']);
122 {$tn}.name LIKE '$s%'
127 function toEventString() {
131 // ---------- AUTHENTICATION
132 // not sure where authetnication via company is used?? posibly media-outreach
136 $db = $this->getDatabaseConnection();
137 $sesPrefix = $db->dsn['database'];
139 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
141 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
142 $u = DB_DataObject::factory('core_company');
143 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
146 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
149 // not in session or not matched...
155 function getAuthUser()
157 if (!$this->isAuth()) {
160 $db = $this->getDatabaseConnection();
161 $sesPrefix = $db->dsn['database'];
162 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
163 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
165 $u = DB_DataObject::factory('core_company');
166 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
177 $this->isAuth(); // force session start..
178 $db = $this->getDatabaseConnection();
179 $sesPrefix = $db->dsn['database'];
180 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
185 $this->isAuth(); // force session start..
186 $db = $this->getDatabaseConnection();
187 $sesPrefix = $db->dsn['database'];
188 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
191 // ---------- AUTHENTICATION
192 function checkPassword($val)
194 //echo '<pre>'.$val . print_R($this,true);
195 if (substr($this->passwd,0,1) == '$') {
196 return crypt($val,$this->passwd) == $this->passwd ;
198 // old style md5 passwords...- cant be used with courier....
199 return md5($val) == $this->passwd;
201 function setPassword($value)
204 while(strlen($salt)<9) {
205 $salt.=chr(rand(64,126));
206 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
208 $this->passwd = crypt($value, '$1$'. $salt. '$');
211 function onUpload($controller)
213 $image = DB_DataObject::factory('Images');
214 return $image->onUploadWithTbl($this, 'logo_id');
217 function onUpdate($old, $req,$roo)
219 if (!empty($req['password1'])) {
220 $this->setPassword($req['password1']);
224 function onInsert($req, $roo)
226 if (!empty($this->logo_id)) { // update images table to sycn with this..
227 $img = DB_DataObject::factory('Images');
228 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
229 $img->onid = $this->id;
233 if (!empty($req['password1'])) {
234 $this->setPassword($req['password1']);
237 $img = DB_DataObject::factory('Images');
240 $img->ontable = $this->tableName();
241 $img->imgtype = 'LOGO';
242 // should check uploader!!!
244 while($img->fetch()) {
246 $ii->onid = $this->id;
248 $this->logo_id = $ii->id;
255 function beforeInsert($q, $roo)
257 if(!empty($q['_check_name'])){
258 if($this->checkName()){
266 function beforeUpdate($old, $q,$roo)
268 // we still use comptype in some old systems...
270 if (!empty($q['comptype_id'])) {
271 $en = DB_DataObject::Factory('core_enum');
272 $en->get($q['comptype_id']);
273 $this->comptype = $en->name;
277 if(!empty($q['_flag_delete'])){
278 $this->deleted_dt = $this->sqlValue("NOW()");
279 $this->deleted_by = $roo->getAuthUser()->id;
282 if(!empty($q['_flag_undelete'])){
283 $this->deleted_dt = "";
284 $this->deleted_by = 0;
286 if(!empty($q['_check_name'])){
287 if($this->checkName()){
294 if(!empty($q['_merge_id'])){
295 $this->merge($q['_merge_id'], $roo);
298 if(!empty($this->is_system) &&
299 ($old->code != $this->code ) // used to be not allowed to change name..
301 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
305 function beforeDelete($req, $roo)
308 // should check for members....
309 if(!empty($this->is_system) &&
310 ($old->code != $this->code || $old->name != $this->name)
312 $roo->jerr('This company is not allow to delete');
317 function onDelete($req, $roo)
319 $img = DB_DataObject::factory('Images');
320 $img->ontable = $this->tableName();
321 $img->onid = $this->id;
323 while ($img->fetch()) {
324 $img->beforeDelete();
332 * check who is trying to access this. false == access denied..
334 function checkPerm($lvl, $au, $changes = false)
337 // do we have an empty system..
338 if ($au && $au->id == -1) {
344 if ($au->company()->comptype != 'OWNER') {
347 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
351 return $this->id == $au->company_id;
354 return $au->hasPerm("Core.Companies", $lvl);
357 function logoImageToHTML($size)
359 $i = DB_DataObject::factory('Images');
360 if (!$this->logo_id || !$i->get($this->logo_id)) {
363 return $i->toHTML($size);
366 function firstImage($filter='image/%')
368 $i = DB_DataObject::factory('Images');
369 //DB_DataObject::debugLevel(1);
370 $im = $i->gather($this, $filter);
377 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
379 $fm = $this->firstImage($filter);
383 return $fm->toHTML($size, $base);
386 function toRooSingleArray($authUser, $request)
388 $ret = $this->toArray();
389 // DB_DataObject::debugLevel(1);
390 // get the comptype display
391 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
393 $ret['comptype_display'] = $ret['comptype'];
394 if ($e && !empty($e->name_display)) {
395 $ret['comptype_display'] = $e->name_display;
404 * create the suppliers...
410 function initCompaniesArray($roo, $data)
412 $tn = $this->tableName();
414 foreach($data as $d){
415 $com = DB_DataObject::factory($tn);
417 if(!$com->find(true)){
418 $com->created_dt = Date('Y-m-d H:i:s');
419 $com->updated_dt = Date('Y-m-d H:i:s');
420 $com->is_system = 1;// new column.. block the user changing the code and name..
429 function initCompanies($roo, $opts)
431 $companies = DB_DataObject::factory('core_company');
433 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
435 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
438 $roo->jerr("invalid company type '$ctype'");
440 if ($ctype =='OWNER') {
441 $companies = DB_DataObject::factory('core_company');
442 $companies->comptype_id = $enum;
443 if ($companies->count()) {
444 $roo->jerr("Owner company already exists");
447 $companies = DB_DataObject::factory('core_company');
450 $companies->setFrom(array(
451 'name' => $opts['add-company'],
452 'comptype' => $ctype,
453 'comptype_id' => $enum,
455 if ($companies->find(true)) {
456 $roo->jerr("company already exists");
458 $companies->setFrom(array(
459 'background_color' => '',
460 'created_dt' => $this->sqlValue('NOW()'),
461 'updated_dt' => $this->sqlValue('NOW()')
465 $companies->insert();
466 $companies->onInsert(array(), $roo);
468 static function lookupOwner()
470 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
471 $companies = DB_DataObject::factory('core_company');
472 $companies->comptype_id = $enum;
473 if ($companies->find(true)) {
479 function merge($merge_to, $roo)
483 $all_links = $this->databaseLinks();
485 foreach($all_links as $tbl => $links) {
486 foreach($links as $col => $totbl_col) {
487 $to = explode(':', $totbl_col);
488 if ($to[0] != $this->tableName()) {
492 $affects[$tbl .'.' . $col] = true;
496 foreach($affects as $k => $true) {
497 $ka = explode('.', $k);
499 $chk = DB_DataObject::factory($ka[0]);
501 if (!is_a($chk,'DB_DataObject')) {
502 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
505 $chk->{$ka[1]} = $this->id;
507 foreach ($chk->fetchAll() as $c){
509 $c->{$ka[1]} = $merge_to;
522 $company = DB_DataObject::factory('core_company');
523 $company->setFrom(array(
524 'name' => $this->name
527 if(!empty($this->id)){
528 $company->whereAdd("id != {$this->id}");
531 if(!$company->find(true)){