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)
42 //DB_DataObject::debugLevel(1);
43 $x = DB_DataObject::factory('Companies');
44 $x->comptype= 'OWNER';
47 if (!empty($q['query']['company_project_id'])) {
49 if (!empty($q['query']['company_include_self'])) {
50 $add = ' OR Companies.id = ' . $x->id;
52 if (!empty($q['query']['company_not_self'])) {
53 $add = ' AND Companies.id != ' . $x->id;
56 $pid = $q['query']['company_project_id'];
57 if (strpos($pid, ',')) {
58 $bits = explode(',', $pid);
59 foreach($bits as $b) {
67 $pids = implode(',', $pids);
68 $this->whereAdd("Companies.id IN (
69 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 if (!empty($q['query']['province'])) {
82 $prov = $this->escape($q['query']['province']);
83 $this->whereAdd("province LIKE '$prov%'");
89 function toEventString() {
93 // ---------- AUTHENTICATION
96 $db = $this->getDatabaseConnection();
97 $sesPrefix = $db->dsn['database'];
99 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
101 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
102 $u = DB_DataObject::factory('Companies');
103 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
106 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
109 // not in session or not matched...
115 function getAuthUser()
117 if (!$this->isAuth()) {
120 $db = $this->getDatabaseConnection();
121 $sesPrefix = $db->dsn['database'];
122 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
123 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
125 $u = DB_DataObject::factory('Companies');
126 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
137 $this->isAuth(); // force session start..
138 $db = $this->getDatabaseConnection();
139 $sesPrefix = $db->dsn['database'];
140 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
145 $this->isAuth(); // force session start..
146 $db = $this->getDatabaseConnection();
147 $sesPrefix = $db->dsn['database'];
148 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
151 // ---------- AUTHENTICATION
152 function checkPassword($val)
154 //echo '<pre>'.$val . print_R($this,true);
155 if (substr($this->passwd,0,1) == '$') {
156 return crypt($val,$this->passwd) == $this->passwd ;
158 // old style md5 passwords...- cant be used with courier....
159 return md5($val) == $this->passwd;
161 function setPassword($value)
164 while(strlen($salt)<9) {
165 $salt.=chr(rand(64,126));
166 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
168 $this->passwd = crypt($value, '$1$'. $salt. '$');
171 function onUpload($controller)
173 $image = DB_DataObject::factory('Images');
174 return $image->onUploadWithTbl($this, 'logo_id');
177 function onUpdate($old, $req,$roo)
179 if (!empty($req['password1'])) {
180 $this->setPassword($req['password1']);
184 function onInsert($req, $roo)
186 if (!empty($this->logo_id)) { // update images table to sycn with this..
187 $img = DB_DataObject::factory('Images');
188 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
189 $img->onid = $this->id;
193 if (!empty($req['password1'])) {
194 $this->setPassword($req['password1']);
197 $img = DB_DataObject::factory('Images');
200 $img->ontable = 'Companies';
201 $img->imgtype = 'LOGO';
202 // should check uploader!!!
204 while($img->fetch()) {
206 $ii->onid = $this->id;
208 $this->logo_id = $ii->id;
218 function beforeDelete()
220 // should check for members....
222 $img = DB_DataObject::factory('Images');
223 $img->ontable = 'Companies';
224 $img->onid = $this->id;
226 while ($img->fetch()) {
227 $img->beforeDelete();
235 * check who is trying to access this. false == access denied..
237 function checkPerm($lvl, $au, $changes = false)
240 // do we have an empty system..
241 if ($au && $au->id == -1) {
247 if ($au->company()->comptype != 'OWNER') {
250 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
254 return $this->id == $au->company_id;
257 return $au->hasPerm("Core.".$this->tableName(), $lvl);