3 * Table Definition for Companies
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Companies extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'Companies'; // 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
38 /* the code above is auto generated do not remove the tag below */
41 function applyFilters($q, $au)
43 $tn = $this->tableName();
44 //DB_DataObject::debugLevel(1);
45 $x = DB_DataObject::factory('Companies');
46 $x->comptype= 'OWNER';
49 if (!empty($q['query']['company_project_id'])) {
51 if (!empty($q['query']['company_include_self'])) {
52 $add = ' OR Companies.id = ' . $x->id;
54 if (!empty($q['query']['company_not_self'])) {
55 $add = ' AND Companies.id != ' . $x->id;
58 $pid = $q['query']['company_project_id'];
59 if (strpos($pid, ',')) {
60 $bits = explode(',', $pid);
61 foreach($bits as $b) {
69 $pids = implode(',', $pids);
70 $this->whereAdd("Companies.id IN (
71 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
75 if (!empty($q['query']['comptype'])) {
77 $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
81 // depricated - should be moved to module specific (texon afair)
83 if (!empty($q['query']['province'])) {
84 $prov = $this->escape($q['query']['province']);
85 $this->whereAdd("province LIKE '$prov%'");
89 // ADD comptype_display name.. = for combos..
99 ) as comptype_display_name
105 function toEventString() {
109 // ---------- AUTHENTICATION
112 $db = $this->getDatabaseConnection();
113 $sesPrefix = $db->dsn['database'];
115 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
117 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
118 $u = DB_DataObject::factory('Companies');
119 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
122 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
125 // not in session or not matched...
131 function getAuthUser()
133 if (!$this->isAuth()) {
136 $db = $this->getDatabaseConnection();
137 $sesPrefix = $db->dsn['database'];
138 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
139 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
141 $u = DB_DataObject::factory('Companies');
142 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
153 $this->isAuth(); // force session start..
154 $db = $this->getDatabaseConnection();
155 $sesPrefix = $db->dsn['database'];
156 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
161 $this->isAuth(); // force session start..
162 $db = $this->getDatabaseConnection();
163 $sesPrefix = $db->dsn['database'];
164 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
167 // ---------- AUTHENTICATION
168 function checkPassword($val)
170 //echo '<pre>'.$val . print_R($this,true);
171 if (substr($this->passwd,0,1) == '$') {
172 return crypt($val,$this->passwd) == $this->passwd ;
174 // old style md5 passwords...- cant be used with courier....
175 return md5($val) == $this->passwd;
177 function setPassword($value)
180 while(strlen($salt)<9) {
181 $salt.=chr(rand(64,126));
182 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
184 $this->passwd = crypt($value, '$1$'. $salt. '$');
187 function onUpload($controller)
189 $image = DB_DataObject::factory('Images');
190 return $image->onUploadWithTbl($this, 'logo_id');
193 function onUpdate($old, $req,$roo)
195 if (!empty($req['password1'])) {
196 $this->setPassword($req['password1']);
200 function onInsert($req, $roo)
202 if (!empty($this->logo_id)) { // update images table to sycn with this..
203 $img = DB_DataObject::factory('Images');
204 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
205 $img->onid = $this->id;
209 if (!empty($req['password1'])) {
210 $this->setPassword($req['password1']);
213 $img = DB_DataObject::factory('Images');
216 $img->ontable = 'Companies';
217 $img->imgtype = 'LOGO';
218 // should check uploader!!!
220 while($img->fetch()) {
222 $ii->onid = $this->id;
224 $this->logo_id = $ii->id;
234 function beforeDelete()
236 // should check for members....
238 $img = DB_DataObject::factory('Images');
239 $img->ontable = 'Companies';
240 $img->onid = $this->id;
242 while ($img->fetch()) {
243 $img->beforeDelete();
251 * check who is trying to access this. false == access denied..
253 function checkPerm($lvl, $au, $changes = false)
256 // do we have an empty system..
257 if ($au && $au->id == -1) {
263 if ($au->company()->comptype != 'OWNER') {
266 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
270 return $this->id == $au->company_id;
273 return $au->hasPerm("Core.Companies", $lvl);
276 function logoImageToHTML($size)
278 $i = DB_DataObject::factory('Images');
279 if (!$this->logo_id || !$i->get($this->logo_id)) {
282 return $i->toHTML($size);
285 function firstImage($filter='image/%')
287 $i = DB_DataObject::factory('Images');
288 //DB_DataObject::debugLevel(1);
289 $im = $i->gather($this, $filter);
296 function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
298 $fm = $this->firstImage($filter);
302 return $fm->toHTML($size, $base);
305 function toRooSingleArray($authUser, $request)
307 $ret = $this->toArray();
308 // DB_DataObject::debugLevel(1);
309 // get the comptype display
310 $e = DB_DataObject::Factory('core_enum');
311 $e->etype = 'COMPTYPE';
312 $e->name = $this->comptype;
313 $ret['comptype_display'] = $ret['comptype'];
314 if ($e->find(true) && !empty($e->name_display)) {
315 $ret['comptype_display'] = $e->name_display;
322 function initCompanies($roo, $name, $type)
324 $companies = DB_DataObject::factory('companies');
325 $companies->setFrom(array(
328 'background_color' => '',
329 'created_dt' => $this->sqlValue('NOW()'),
330 'updated_dt' => $this->sqlValue('NOW()')
333 $companies->insert();
334 $companies->onInsert(array(), $roo);