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);
76 function toEventString() {
80 // ---------- AUTHENTICATION
83 $db = $this->getDatabaseConnection();
84 $sesPrefix = $db->dsn['database'];
86 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
88 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
89 $u = DB_DataObject::factory('Companies');
90 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
93 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
96 // not in session or not matched...
102 function getAuthUser()
104 if (!$this->isAuth()) {
107 $db = $this->getDatabaseConnection();
108 $sesPrefix = $db->dsn['database'];
109 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
110 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
112 $u = DB_DataObject::factory('Companies');
113 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
124 $this->isAuth(); // force session start..
125 $db = $this->getDatabaseConnection();
126 $sesPrefix = $db->dsn['database'];
127 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
132 $this->isAuth(); // force session start..
133 $db = $this->getDatabaseConnection();
134 $sesPrefix = $db->dsn['database'];
135 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
138 // ---------- AUTHENTICATION
139 function checkPassword($val)
141 //echo '<pre>'.$val . print_R($this,true);
142 if (substr($this->passwd,0,1) == '$') {
143 return crypt($val,$this->passwd) == $this->passwd ;
145 // old style md5 passwords...- cant be used with courier....
146 return md5($val) == $this->passwd;
148 function setPassword($value)
151 while(strlen($salt)<9) {
152 $salt.=chr(rand(64,126));
153 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
155 $this->passwd = crypt($value, '$1$'. $salt. '$');
158 function onUpload($controller)
160 $image = DB_DataObject::factory('Images');
161 return $image->onUploadWithTbl($this, 'logo_id');
164 function onUpdate($old, $req,$roo)
166 if (!empty($req['password1'])) {
167 $this->setPassword($req['password1']);
171 function onInsert($req, $roo)
173 if (!empty($this->logo_id)) { // update images table to sycn with this..
174 $img = DB_DataObject::factory('Images');
175 if ($img->get($this->logo_id) && ($img->onid != $this->id)) {
176 $img->onid = $this->id;
180 if (!empty($req['password1'])) {
181 $this->setPassword($req['password1']);
184 $img = DB_DataObject::factory('Images');
187 $img->ontable = 'Companies';
188 $img->imgtype = 'LOGO';
189 // should check uploader!!!
191 while($img->fetch()) {
193 $ii->onid = $this->id;
195 $this->logo_id = $ii->id;
205 function beforeDelete()
207 // should check for members....
209 $img = DB_DataObject::factory('Images');
210 $img->ontable = 'Companies';
211 $img->onid = $this->id;
213 while ($img->fetch()) {
214 $img->beforeDelete();
222 * check who is trying to access this. false == access denied..
224 function checkPerm($lvl, $au, $changes = false)
226 if ($au->company()->comptype != 'OWNER') {
229 if ($changes && isset($changes['comptype']) && $changes['comptype'] != $this->comptype) {
233 return $this->id == $au->company_id;
236 return $au->hasPerm("Core.".$this->tableName(), $lvl);
238 function whereAddIn($key, $list, $type= 'int')
241 foreach($list as $k) {
242 $ar[] = $type =='int' ? (int)$k : $this->escape($k);
247 return $this->whereAdd("$key IN (". implode(',', $ar). ')');
249 function fetchAll($k= false, $v = false)
253 $this->selectAdd($k);
255 $this->selectAdd($v);
261 while ($this->fetch()) {
263 $ret[$this->$k] = $this->$v;
266 $ret[] = $k === false ? clone($this) : $this->$k;