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)
43 $tn = $this->tableName();
44 $this->selectAdd("i18n_translate('c' , {$tn}.country, 'en') as country_display_name ");
46 $tn = $this->tableName();
47 //DB_DataObject::debugLevel(1);
48 $x = DB_DataObject::factory('core_company');
49 $x->comptype= 'OWNER';
52 if (!empty($q['query']['company_project_id'])) {
54 if (!empty($q['query']['company_include_self'])) {
55 $add = " OR {$tn}.id = {$x->id}";
57 if (!empty($q['query']['company_not_self'])) {
58 $add = " AND {$tn}.id != {$x->id}";
61 $pid = $q['query']['company_project_id'];
62 if (strpos($pid, ',')) {
63 $bits = explode(',', $pid);
64 foreach($bits as $b) {
72 $pids = implode(',', $pids);
73 $this->whereAdd("{$tn}.id IN (
74 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
78 if (!empty($q['query']['comptype'])) {
80 $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
84 // depricated - should be moved to module specific (texon afair)
86 if (!empty($q['query']['province'])) {
87 $prov = $this->escape($q['query']['province']);
88 $this->whereAdd("province LIKE '$prov%'");
92 // ADD comptype_display name.. = for combos..
102 ) as comptype_display_name
105 if(!empty($q['search']['name'])){
106 $s = $this->escape($q['search']['name']);
108 {$tn}.name LIKE '%$s%'
111 if(!empty($q['search']['name_starts'])){
112 $s = $this->escape($q['search']['name_starts']);
114 {$tn}.name LIKE '$s%'
119 function toEventString() {
123 // ---------- AUTHENTICATION
126 $db = $this->getDatabaseConnection();
127 $sesPrefix = $db->dsn['database'];
129 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
131 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
132 $u = DB_DataObject::factory('core_company');
133 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
136 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
139 // not in session or not matched...
145 function getAuthUser()
147 if (!$this->isAuth()) {
150 $db = $this->getDatabaseConnection();
151 $sesPrefix = $db->dsn['database'];
152 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
153 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
155 $u = DB_DataObject::factory('core_company');
156 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
167 $this->isAuth(); // force session start..
168 $db = $this->getDatabaseConnection();
169 $sesPrefix = $db->dsn['database'];
170 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
175 $this->isAuth(); // force session start..
176 $db = $this->getDatabaseConnection();
177 $sesPrefix = $db->dsn['database'];
178 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
181 // ---------- AUTHENTICATION
182 function checkPassword($val)
184 //echo '<pre>'.$val . print_R($this,true);
185 if (substr($this->passwd,0,1) == '$') {
186 return crypt($val,$this->passwd) == $this->passwd ;
188 // old style md5 passwords...- cant be used with courier....
189 return md5($val) == $this->passwd;
191 function setPassword($value)
194 while(strlen($salt)<9) {
195 $salt.=chr(rand(64,126));
196 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
198 $this->passwd = crypt($value, '$1$'. $salt. '$');
201 function onUpload($controller)
203 $image = DB_DataObject::factory('Images');
204 return $image->onUploadWithTbl($this, 'logo_id');
207 function onUpdate($old, $req,$roo)
209 if (!empty($req['password1'])) {
210 $this->setPassword($req['password1']);
214 function onInsert($req, $roo)
216 if (!empty($this->logo_id)) { // update images table to sycn with this..
217 $img = DB_DataObject::factory('Images');
218 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
219 $img->onid = $this->id;
223 if (!empty($req['password1'])) {
224 $this->setPassword($req['password1']);
227 $img = DB_DataObject::factory('Images');
230 $img->ontable = $this->tableName();
231 $img->imgtype = 'LOGO';
232 // should check uploader!!!
234 while($img->fetch()) {
236 $ii->onid = $this->id;
238 $this->logo_id = $ii->id;
245 function beforeInsert($q, $roo)
247 if(!empty($q['_check_name'])){
248 if($this->checkName()){
256 function beforeUpdate($old, $q,$roo)
258 if(!empty($q['_check_name'])){
259 if($this->checkName()){
266 if(!empty($q['_merge_id'])){
267 $this->merge($q['_merge_id'], $roo);
270 if(!empty($this->is_system) &&
271 ($old->code != $this->code ) // used to be not allowed to change name..
273 $roo->jerr('This company is not allow to editing Ref. or Company Name...');
277 function beforeDelete($req, $roo)
279 // should check for members....
280 if(!empty($this->is_system) &&
281 ($old->code != $this->code || $old->name != $this->name)
283 $roo->jerr('This company is not allow to delete');
285 $img = DB_DataObject::factory('Images');
286 $img->ontable = $this->tableName();
287 $img->onid = $this->id;
289 while ($img->fetch()) {
290 $img->beforeDelete();
298 * check who is trying to access this. false == access denied..
300 function checkPerm($lvl, $au, $changes = false)
303 // do we have an empty system..
304 if ($au && $au->id == -1) {
310 if ($au->company()->comptype != 'OWNER') {
313 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
317 return $this->id == $au->company_id;
320 return $au->hasPerm("Core.Companies", $lvl);
323 function logoImageToHTML($size)
325 $i = DB_DataObject::factory('Images');
326 if (!$this->logo_id || !$i->get($this->logo_id)) {
329 return $i->toHTML($size);
332 function firstImage($filter='image/%')
334 $i = DB_DataObject::factory('Images');
335 //DB_DataObject::debugLevel(1);
336 $im = $i->gather($this, $filter);
343 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
345 $fm = $this->firstImage($filter);
349 return $fm->toHTML($size, $base);
352 function toRooSingleArray($authUser, $request)
354 $ret = $this->toArray();
355 // DB_DataObject::debugLevel(1);
356 // get the comptype display
357 $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
359 $ret['comptype_display'] = $ret['comptype'];
360 if ($e && !empty($e->name_display)) {
361 $ret['comptype_display'] = $e->name_display;
370 * create the suppliers...
376 function initCompaniesArray($roo, $data)
378 $tn = $this->tableName();
380 foreach($data as $d){
381 $com = DB_DataObject::factory($tn);
383 if(!$com->find(true)){
384 $com->created_dt = Date('Y-m-d H:i:s');
385 $com->updated_dt = Date('Y-m-d H:i:s');
386 $com->is_system = 1;// new column.. block the user changing the code and name..
395 function initCompanies($roo, $opts)
397 $companies = DB_DataObject::factory('core_company');
399 $ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
401 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype );
404 $roo->jerr("invalid company type '$ctype'");
406 if ($ctype =='OWNER') {
407 $companies = DB_DataObject::factory('core_company');
408 $companies->comptype_id = $enum;
409 if ($companies->count()) {
410 $roo->jerr("Owner company already exists");
413 $companies = DB_DataObject::factory('core_company');
416 $companies->setFrom(array(
417 'name' => $opts['add-company'],
418 'comptype' => $ctype,
419 'comptype_id' => $enum,
421 if ($companies->find(true)) {
422 $roo->jerr("company already exists");
424 $companies->setFrom(array(
425 'background_color' => '',
426 'created_dt' => $this->sqlValue('NOW()'),
427 'updated_dt' => $this->sqlValue('NOW()')
431 $companies->insert();
432 $companies->onInsert(array(), $roo);
434 static function lookupOwner()
436 $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
437 $companies = DB_DataObject::factory('core_company');
438 $companies->comptype_id = $enum;
439 if ($companies->find(true)) {
445 function merge($merge_to, $roo)
449 $all_links = $x->databaseLinks();
451 foreach($all_links as $tbl => $links) {
452 foreach($links as $col => $totbl_col) {
453 $to = explode(':', $totbl_col);
454 if ($to[0] != $this->tableName()) {
458 $affects[$tbl .'.' . $col] = true;
462 foreach($affects as $k => $true) {
463 $ka = explode('.', $k);
465 $chk = DB_DataObject::factory($ka[0]);
467 if (!is_a($chk,'DB_DataObject')) {
468 $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
471 $chk->{$ka[1]} = $this->id;
473 foreach ($chk->fetchAll() as $c){
475 $c->{$ka[1]} = $merge_to;
488 $company = DB_DataObject::factory('core_company');
489 $company->setFrom(array(
490 'name' => $this->name
493 if(!empty($this->id)){
494 $company->whereAdd("id != {$this->id}");
497 if(!$company->find(true)){