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
127 $db = $this->getDatabaseConnection();
128 $sesPrefix = $db->dsn['database'];
130 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
132 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
133 $u = DB_DataObject::factory('core_company');
134 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
137 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
140 // not in session or not matched...
146 function getAuthUser()
148 if (!$this->isAuth()) {
151 $db = $this->getDatabaseConnection();
152 $sesPrefix = $db->dsn['database'];
153 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
154 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
156 $u = DB_DataObject::factory('core_company');
157 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
168 $this->isAuth(); // force session start..
169 $db = $this->getDatabaseConnection();
170 $sesPrefix = $db->dsn['database'];
171 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
176 $this->isAuth(); // force session start..
177 $db = $this->getDatabaseConnection();
178 $sesPrefix = $db->dsn['database'];
179 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
182 // ---------- AUTHENTICATION
183 function checkPassword($val)
185 //echo '<pre>'.$val . print_R($this,true);
186 if (substr($this->passwd,0,1) == '$') {
187 return crypt($val,$this->passwd) == $this->passwd ;
189 // old style md5 passwords...- cant be used with courier....
190 return md5($val) == $this->passwd;
192 function setPassword($value)
195 while(strlen($salt)<9) {
196 $salt.=chr(rand(64,126));
197 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
199 $this->passwd = crypt($value, '$1$'. $salt. '$');
202 function onUpload($controller)
204 $image = DB_DataObject::factory('Images');
205 return $image->onUploadWithTbl($this, 'logo_id');
208 function onUpdate($old, $req,$roo)
210 if (!empty($req['password1'])) {
211 $this->setPassword($req['password1']);
215 function onInsert($req, $roo)
217 if (!empty($this->logo_id)) { // update images table to sycn with this..
218 $img = DB_DataObject::factory('Images');
219 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
220 $img->onid = $this->id;
224 if (!empty($req['password1'])) {
225 $this->setPassword($req['password1']);
228 $img = DB_DataObject::factory('Images');
231 $img->ontable = $this->tableName();
232 $img->imgtype = 'LOGO';
233 // should check uploader!!!
235 while($img->fetch()) {
237 $ii->onid = $this->id;
239 $this->logo_id = $ii->id;
246 function beforeInsert($q, $roo)
248 if(!empty($q['_check_name'])){
249 if($this->checkName()){
257 function beforeUpdate($old, $q,$roo)
259 if(!empty($q['_check_name'])){
260 if($this->checkName()){
267 if(!empty($q['_merge_id'])){
268 $this->merge($q['_merge_id'], $roo);
271 if(!empty($this->is_system) &&
272 ($old->code != $this->code ) // used to be not allowed to change name..
274 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
278 function beforeDelete($req, $roo)
280 // should check for members....
281 if(!empty($this->is_system) &&
282 ($old->code != $this->code || $old->name != $this->name)
284 $roo->jerr('This company is not allow to delete');
286 $img = DB_DataObject::factory('Images');
287 $img->ontable = $this->tableName();
288 $img->onid = $this->id;
290 while ($img->fetch()) {
291 $img->beforeDelete();
299 * check who is trying to access this. false == access denied..
301 function checkPerm($lvl, $au, $changes = false)
304 // do we have an empty system..
305 if ($au && $au->id == -1) {
311 if ($au->company()->comptype != 'OWNER') {
314 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
318 return $this->id == $au->company_id;
321 return $au->hasPerm("Core.Companies", $lvl);
324 function logoImageToHTML($size)
326 $i = DB_DataObject::factory('Images');
327 if (!$this->logo_id || !$i->get($this->logo_id)) {
330 return $i->toHTML($size);
333 function firstImage($filter='image/%')
335 $i = DB_DataObject::factory('Images');
336 //DB_DataObject::debugLevel(1);
337 $im = $i->gather($this, $filter);
344 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
346 $fm = $this->firstImage($filter);
350 return $fm->toHTML($size, $base);
353 function toRooSingleArray($authUser, $request)
355 $ret = $this->toArray();
356 // DB_DataObject::debugLevel(1);
357 // get the comptype display
358 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
360 $ret['comptype_display'] = $ret['comptype'];
361 if ($e && !empty($e->name_display)) {
362 $ret['comptype_display'] = $e->name_display;
371 * create the suppliers...
377 function initCompaniesArray($roo, $data)
379 $tn = $this->tableName();
381 foreach($data as $d){
382 $com = DB_DataObject::factory($tn);
384 if(!$com->find(true)){
385 $com->created_dt = Date('Y-m-d H:i:s');
386 $com->updated_dt = Date('Y-m-d H:i:s');
387 $com->is_system = 1;// new column.. block the user changing the code and name..
396 function initCompanies($roo, $opts)
398 $companies = DB_DataObject::factory('core_company');
400 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
402 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
405 $roo->jerr("invalid company type '$ctype'");
407 if ($ctype =='OWNER') {
408 $companies = DB_DataObject::factory('core_company');
409 $companies->comptype_id = $enum;
410 if ($companies->count()) {
411 $roo->jerr("Owner company already exists");
414 $companies = DB_DataObject::factory('core_company');
417 $companies->setFrom(array(
418 'name' => $opts['add-company'],
419 'comptype' => $ctype,
420 'comptype_id' => $enum,
422 if ($companies->find(true)) {
423 $roo->jerr("company already exists");
425 $companies->setFrom(array(
426 'background_color' => '',
427 'created_dt' => $this->sqlValue('NOW()'),
428 'updated_dt' => $this->sqlValue('NOW()')
432 $companies->insert();
433 $companies->onInsert(array(), $roo);
435 static function lookupOwner()
437 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
438 $companies = DB_DataObject::factory('core_company');
439 $companies->comptype_id = $enum;
440 if ($companies->find(true)) {
446 function merge($merge_to, $roo)
450 $all_links = $x->databaseLinks();
452 foreach($all_links as $tbl => $links) {
453 foreach($links as $col => $totbl_col) {
454 $to = explode(':', $totbl_col);
455 if ($to[0] != $this->tableName()) {
459 $affects[$tbl .'.' . $col] = true;
463 foreach($affects as $k => $true) {
464 $ka = explode('.', $k);
466 $chk = DB_DataObject::factory($ka[0]);
468 if (!is_a($chk,'DB_DataObject')) {
469 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
472 $chk->{$ka[1]} = $this->id;
474 foreach ($chk->fetchAll() as $c){
476 $c->{$ka[1]} = $merge_to;
489 $company = DB_DataObject::factory('core_company');
490 $company->setFrom(array(
491 'name' => $this->name
494 if(!empty($this->id)){
495 $company->whereAdd("id != {$this->id}");
498 if(!$company->find(true)){