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) multiple_key
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 $ava_craft; // string(254)
27 public $url; // string(254) not_null
28 public $main_office_id; // int(11) not_null
29 public $created_by; // int(11) not_null
30 public $created_dt; // datetime(19) not_null binary
31 public $updated_by; // int(11) not_null
32 public $updated_dt; // datetime(19) not_null binary
33 public $passwd; // string(64) not_null
36 /* the code above is auto generated do not remove the tag below */
39 function applyFilters($q, $au)
41 $x = DB_DataObject::factory('Companies');
45 if (!empty($q['query']['company_project_id'])) {
47 if (!empty($q['query']['company_include_self'])) {
48 $add = ' OR Companies.id = ' . $x->id;
50 if (!empty($q['query']['company_not_self'])) {
51 $add = ' AND Companies.id != ' . $x->id;
54 $pid = $q['query']['company_project_id'];
55 if (strpos($pid, ',')) {
56 $bits = explode(',', $pid);
57 foreach($bits as $b) {
65 $pids = implode(',', $pids);
66 $this->whereAdd("Companies.id IN (
67 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
70 // DB_DataObject::debugLevel(1);
73 if (!empty($q['query']['comptype'])) {
75 $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
79 if (!empty($q['query']['province'])) {
80 $prov = $this->escape($q['query']['province']);
81 $this->whereAdd("province LIKE '$prov%'");
87 function toEventString() {
91 // ---------- AUTHENTICATION
94 $db = $this->getDatabaseConnection();
95 $sesPrefix = $db->dsn['database'];
97 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
99 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
100 $u = DB_DataObject::factory('Companies');
101 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
104 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
107 // not in session or not matched...
113 function getAuthUser()
115 if (!$this->isAuth()) {
118 $db = $this->getDatabaseConnection();
119 $sesPrefix = $db->dsn['database'];
120 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
121 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
123 $u = DB_DataObject::factory('Companies');
124 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
135 $this->isAuth(); // force session start..
136 $db = $this->getDatabaseConnection();
137 $sesPrefix = $db->dsn['database'];
138 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
143 $this->isAuth(); // force session start..
144 $db = $this->getDatabaseConnection();
145 $sesPrefix = $db->dsn['database'];
146 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
149 // ---------- AUTHENTICATION
150 function checkPassword($val)
152 //echo '<pre>'.$val . print_R($this,true);
153 if (substr($this->passwd,0,1) == '$') {
154 return crypt($val,$this->passwd) == $this->passwd ;
156 // old style md5 passwords...- cant be used with courier....
157 return md5($val) == $this->passwd;
159 function setPassword($value)
162 while(strlen($salt)<9) {
163 $salt.=chr(rand(64,126));
164 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
166 $this->passwd = crypt($value, '$1$'. $salt. '$');
169 function onUpload($controller)
171 $image = DB_DataObject::factory('Images');
172 return $image->onUploadWithTbl($this, 'logo_id');
175 function onUpdate($old, $req,$roo)
177 if (!empty($req['password1'])) {
178 $this->setPassword($req['password1']);
182 function onInsert($req, $roo)
184 if (!empty($this->logo_id)) { // update images table to sycn with this..
185 $img = DB_DataObject::factory('Images');
186 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
187 $img->onid = $this->id;
191 if (!empty($req['password1'])) {
192 $this->setPassword($req['password1']);
195 $img = DB_DataObject::factory('Images');
198 $img->ontable = 'Companies';
199 $img->imgtype = 'LOGO';
200 // should check uploader!!!
202 while($img->fetch()) {
204 $ii->onid = $this->id;
206 $this->logo_id = $ii->id;
216 function beforeDelete()
218 // should check for members....
220 $img = DB_DataObject::factory('Images');
221 $img->ontable = 'Companies';
222 $img->onid = $this->id;
224 while ($img->fetch()) {
225 $img->beforeDelete();
233 * check who is trying to access this. false == access denied..
235 function checkPerm($lvl, $au, $changes = false)
238 // do we have an empty system..
239 if ($au && $au->id == -1) {
245 if ($au->company()->comptype != 'OWNER') {
248 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
252 return $this->id == $au->company_id;
255 return $au->hasPerm("Core.".$this->tableName(), $lvl);