![]() |
|---|
| [ Index ] |
Source Code Reference for V1.00 |
[Summary view] [Print] [Text view]
1 <?php /* $Id: departments.class.php 102 2008-03-18 19:52:59Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/modules/departments/departments.class.php $ */ 2 if (!defined('W2P_BASE_DIR')) { 3 die('You should not access this file directly.'); 4 } 5 6 ## 7 ## CDepartment Class 8 ## 9 10 class CDepartment extends CW2pObject { 11 var $dept_id = null; 12 var $dept_parent = null; 13 var $dept_company = null; 14 var $dept_name = null; 15 var $dept_phone = null; 16 var $dept_fax = null; 17 var $dept_address1 = null; 18 var $dept_address2 = null; 19 var $dept_city = null; 20 var $dept_state = null; 21 var $dept_zip = null; 22 var $dept_country = null; 23 var $dept_url = null; 24 var $dept_desc = null; 25 var $dept_owner = null; 26 var $dept_email = null; 27 var $dept_type = null; 28 29 function CDepartment() { 30 $this->CW2pObject('departments', 'dept_id'); 31 } 32 33 function load($oid) { 34 $q = new DBQuery; 35 $q->addTable('departments', 'dep'); 36 $q->addQuery('dep.*'); 37 $q->addWhere('dep.dept_id = ' . (int)$oid); 38 $result = $q->loadObject($this); 39 $q->clear(); 40 return $result; 41 } 42 43 function bind($hash) { 44 if (!is_array($hash)) { 45 return get_class($this) . "::bind failed"; 46 } else { 47 $q = new DBQuery; 48 $q->bindHashToObject($hash, $this); 49 $q->clear(); 50 return null; 51 } 52 } 53 54 function check() { 55 if ($this->dept_id === null) { 56 return 'department id is NULL'; 57 } 58 // TODO MORE 59 if ($this->dept_id && $this->dept_id == $this->dept_parent) { 60 return 'cannot make myself my own parent (' . $this->dept_id . '=' . $this->dept_parent . ')'; 61 } 62 return null; // object is ok 63 } 64 65 function store() { 66 $msg = $this->check(); 67 if ($msg) { 68 return get_class($this) . '::store-check failed - ' . $msg; 69 } 70 if ($this->dept_id) { 71 $q = new DBQuery; 72 $ret = $q->updateObject('departments', $this, 'dept_id', false); 73 $q->clear(); 74 } else { 75 $q = new DBQuery; 76 $ret = $q->insertObject('departments', $this, 'dept_id'); 77 $q->clear(); 78 } 79 if (!$ret) { 80 return get_class($this) . '::store failed ' . db_error(); 81 } else { 82 return null; 83 } 84 } 85 86 function delete() { 87 $q = new DBQuery; 88 $q->addTable('departments', 'dep'); 89 $q->addQuery('dep.dept_id'); 90 $q->addWhere('dep.dept_parent = ' . (int)$this->dept_id); 91 $rows = $q->loadList(); 92 $q->clear(); 93 94 if (count($rows)) { 95 return 'deptWithSub'; 96 } 97 98 $q->addTable('project_departments', 'pd'); 99 $q->addQuery('pd.project_id'); 100 $q->addWhere('pd.department_id = ' . (int)$this->dept_id); 101 $rows = $q->loadList(); 102 $q->clear(); 103 104 if (count($rows)) { 105 return 'deptWithProject'; 106 } 107 108 $q->addQuery('*'); 109 $q->setDelete('departments'); 110 $q->addWhere('dept_id = ' . (int)$this->dept_id); 111 if (!$q->exec()) { 112 $result = db_error(); 113 } else { 114 $result = null; 115 } 116 $q->clear(); 117 return $result; 118 } 119 /** 120 * Returns a list of records exposed to the user 121 * @param int User id number 122 * @param string Optional fields to be returned by the query, default is all 123 * @param string Optional sort order for the query 124 * @param string Optional name of field to index the returned array 125 * @param array Optional array of additional sql parameters (from and where supported) 126 * @return array 127 */ 128 // returns a list of records exposed to the user 129 function getAllowedRecords($uid, $fields = '*', $orderby = '', $index = null, $extra = null) { 130 $perms = &$GLOBALS['AppUI']->acl(); 131 $uid = intval($uid); 132 $uid || exit('FATAL ERROR<br />' . get_class($this) . '::getAllowedRecords failed'); 133 $deny = &$perms->getDeniedItems($this->_tbl, $uid); 134 $allow = &$perms->getAllowedItems($this->_tbl, $uid); 135 136 $this->_query->clear(); 137 $this->_query->addQuery($fields); 138 $this->_query->addTable($this->_tbl); 139 140 if ($extra['from']) { 141 $this->_query->addTable($extra['from']); 142 } 143 144 if ($extra['join'] && $extra['on']) { 145 $this->_query->addJoin($extra['join'], $extra['join'], $extra['on']); 146 } 147 148 if (count($allow)) { 149 if ((array_search('0', $allow)) === false) { 150 //If 0 (All Items of a module) are not permited then just add the allowed items only 151 $this->_query->addWhere('(' . $this->_tbl_key . ' IN (' . implode(',', $allow) . ') OR ' . $this->_tbl_key . ' IS NULL)'); 152 } else { 153 //If 0 (All Items of a module) are permited then don't add a where clause so the user is permitted to see all 154 } 155 //Denials are only required if we were able to see anything in the first place so now we handle the denials 156 if (count($deny)) { 157 if ((array_search('0', $deny)) === false) { 158 //If 0 (All Items of a module) are not on the denial array then just deny the denied items 159 $this->_query->addWhere('(' . $this->_tbl_key . ' NOT IN (' . implode(',', $deny) . ') OR ' . $this->_tbl_key . ' IS NULL)'); 160 } elseif ((array_search('0', $allow)) === false) { 161 //If 0 (All Items of a module) are denied and we have granted some then implicit denial to everything else is already in place 162 } else { 163 //If 0 (All Items of a module) are denied then add a false where clause 164 $this->_query->addWhere('(' . $this->_tbl_key . ' IS NULL)'); 165 } 166 } 167 } 168 169 if (isset($extra['where'])) { 170 $this->_query->addWhere($extra['where']); 171 } 172 173 if ($orderby) { 174 $this->_query->addOrder($orderby); 175 } 176 //print_r($this->_query->prepare()); 177 return $this->_query->loadHashList($index); 178 } 179 180 function getAllowedSQL($uid, $index = null) { 181 $perms = &$GLOBALS['AppUI']->acl(); 182 $uid = intval($uid); 183 $uid || exit('FATAL ERROR<br />' . get_class($this) . '::getAllowedSQL failed'); 184 $deny = &$perms->getDeniedItems($this->_tbl, $uid); 185 $allow = &$perms->getAllowedItems($this->_tbl, $uid); 186 187 if (!isset($index)) 188 $index = $this->_tbl_key; 189 $where = array(); 190 if (count($allow)) { 191 if ((array_search('0', $allow)) === false) { 192 //If 0 (All Items of a module) are not permited then just add the allowed items only 193 $where[] = '(' . $index . ' IN (' . implode(',', $allow) . ') OR ' . $index . ' IS NULL)'; 194 } else { 195 //If 0 (All Items of a module) are permited then don't add a where clause so the user is permitted to see all 196 } 197 //Denials are only required if we were able to see anything in the first place so now we handle the denials 198 if (count($deny)) { 199 if ((array_search('0', $deny)) === false) { 200 //If 0 (All Items of a module) are not on the denial array then just deny the denied items 201 $where[] = '(' . $index . ' NOT IN (' . implode(',', $deny) . ') OR ' . $index . ' IS NULL)'; 202 } elseif ((array_search('0', $allow)) === false) { 203 //If 0 (All Items of a module) are denied and we have granted some then implicit denial to everything else is already in place 204 } else { 205 //If 0 (All Items of a module) are denied then add a false where clause 206 $where[] = '(' . $index . ' IS NULL)'; 207 } 208 } 209 } else { 210 //if there are no allowances, only show NULL joins! 211 $where[] = '(' . $index . ' IS NULL)'; 212 } 213 return $where; 214 } 215 216 function setAllowedSQL($uid, &$query, $index = null, $key = null) { 217 $perms = &$GLOBALS['AppUI']->acl(); 218 $uid = intval($uid); 219 $uid || exit('FATAL ERROR<br />' . get_class($this) . '::getAllowedSQL failed'); 220 $deny = &$perms->getDeniedItems($this->_tbl, $uid); 221 $allow = &$perms->getAllowedItems($this->_tbl, $uid); 222 // Make sure that we add the table otherwise dependencies break 223 if (isset($index)) { 224 if (!$key) { 225 $key = substr($this->_tbl, 0, 3); 226 //$key = $this->_tbl; 227 } 228 $query->leftJoin($this->_tbl, $key, $key . '.' . $this->_tbl_key . '=' . $index); 229 } 230 231 if (count($allow)) { 232 if ((array_search('0', $allow)) === false) { 233 //If 0 (All Items of a module) is not permited then just add the allowed items only 234 $query->addWhere('(' . ((!$key) ? '' : $key . '.') . $this->_tbl_key . ' IN (' . implode(',', $allow) . ') OR ' . ((!$key) ? '' : $key . '.') . $this->_tbl_key . ' IS NULL)'); 235 } else { 236 //If 0 (All Items of a module) is permited then don't add a where clause so the user is permitted to see all 237 } 238 //Denials are only required if we were able to see anything in the first place so now we handle the denials 239 if (count($deny)) { 240 if ((array_search('0', $deny)) === false) { 241 //If 0 (All Items of a module) are not on the denial array then just deny the denied items 242 $query->addWhere('(' . ((!$key) ? '' : $key . '.') . $this->_tbl_key . ' NOT IN (' . implode(',', $deny) . ') OR ' . ((!$key) ? '' : $key . '.') . $this->_tbl_key . ' IS NULL)'); 243 } elseif ((array_search('0', $allow)) === false) { 244 //If 0 (All Items of a module) are denied and we have granted some then implicit denial to everything else is already in place 245 } else { 246 //If 0 (All Items of a module) are denied then add a false where clause 247 $query->addWhere('((0=1) OR ' . ((!$key) ? '' : $key . '.') . $this->_tbl_key . ' IS NULL)'); 248 } 249 250 } 251 } else { 252 //if there are no allowances, only show NULL joins! 253 $query->addWhere('((0=1) OR ' . ((!$key) ? '' : $key . '.') . $this->_tbl_key . ' IS NULL)'); 254 } 255 } 256 } 257 258 //writes out a single <option> element for display of departments 259 function showchilddept(&$a, $level = 1) { 260 global $buffer, $department; 261 $s = '<option value="' . $a['dept_id'] . '"' . (isset($department) && $department == $a['dept_id'] ? 'selected="selected"' : '') . '>'; 262 263 for ($y = 0; $y < $level; $y++) { 264 if ($y + 1 == $level) { 265 $s .= ''; 266 } else { 267 $s .= ' '; 268 } 269 } 270 271 $s .= ' ' . $a['dept_name'] . '</option>'; 272 $buffer .= $s; 273 274 // echo $s; 275 } 276 277 //recursive function to display children departments. 278 function findchilddept(&$tarr, $parent, $level = 1) { 279 $level = $level + 1; 280 $n = count($tarr); 281 for ($x = 0; $x < $n; $x++) { 282 if ($tarr[$x]['dept_parent'] == $parent && $tarr[$x]['dept_parent'] != $tarr[$x]['dept_id']) { 283 showchilddept($tarr[$x], $level); 284 findchilddept($tarr, $tarr[$x]['dept_id'], $level); 285 } 286 } 287 } 288 289 function addDeptId($dataset, $parent) { 290 global $dept_ids; 291 foreach ($dataset as $data) { 292 if ($data['dept_parent'] == $parent) { 293 $dept_ids[] = $data['dept_id']; 294 addDeptId($dataset, $data['dept_id']); 295 } 296 } 297 } 298 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 7 03:00:01 2009 | Cross-referenced by PHPXref 0.7 |