| [ Index ] |
|
Code source de dotProject 2.1 RC1 |
1 <?php /* CLASSES $Id: dp.class.php,v 1.25.6.6 2007/01/31 09:36:52 ajdonnison Exp $ */ 2 3 /** 4 * @package dotproject 5 * @subpackage modules 6 * @version $Revision: 1.25.6.6 $ 7 */ 8 9 if (!defined('DP_BASE_DIR')){ 10 die('You should not access this file directly'); 11 } 12 13 require_once $AppUI->getSystemClass('query'); 14 15 /** 16 * CDpObject Abstract Class. 17 * 18 * Parent class to all database table derived objects 19 * @author Andrew Eddie <eddieajau@users.sourceforge.net> 20 * @abstract 21 */ 22 class CDpObject { 23 /** 24 * @var string Name of the table in the db schema relating to child class 25 */ 26 var $_tbl = ''; 27 /** 28 * @var string Name of the primary key field in the table 29 */ 30 var $_tbl_key = ''; 31 /** 32 * @var string Error message 33 */ 34 var $_error = ''; 35 36 /** 37 * @var object Query Handler 38 */ 39 var $_query; 40 41 /** 42 * Object constructor to set table and key field 43 * 44 * Can be overloaded/supplemented by the child class 45 * @param string $table name of the table in the db schema relating to child class 46 * @param string $key name of the primary key field in the table 47 */ 48 function CDpObject( $table, $key ) { 49 global $dPconfig; 50 $this->_tbl = $table; 51 $this->_tbl_key = $key; 52 if (isset($dPconfig['dbprefix'])) { 53 $this->_prefix = $dPconfig['dbprefix']; 54 } 55 else { 56 $this->_prefix = ''; 57 } 58 $this->_query =& new DBQuery; 59 } 60 /** 61 * @return string Returns the error message 62 */ 63 function getError() { 64 return $this->_error; 65 } 66 /** 67 * Binds a named array/hash to this object 68 * 69 * can be overloaded/supplemented by the child class 70 * @param array $hash named array 71 * @return null|string null is operation was satisfactory, otherwise returns an error 72 */ 73 function bind( $hash ) { 74 if (!is_array( $hash )) { 75 $this->_error = get_class( $this )."::bind failed."; 76 return false; 77 } 78 else { 79 bindHashToObject( $hash, $this ); 80 return true; 81 } 82 } 83 84 /** 85 * Binds an array/hash to this object 86 * @param int $oid optional argument, if not specifed then the value of current key is used 87 * @return any result from the database operation 88 */ 89 function load( $oid=null , $strip = true) { 90 $k = $this->_tbl_key; 91 if ($oid) { 92 $this->$k = intval( $oid ); 93 } 94 $oid = $this->$k; 95 if ($oid === null) { 96 return false; 97 } 98 $this->_query->clear(); 99 $this->_query->addTable($this->_tbl); 100 $this->_query->addWhere("$this->_tbl_key = $oid"); 101 $sql = $this->_query->prepare(); 102 $this->_query->clear(); 103 return db_loadObject( $sql, $this, false, $strip ); 104 } 105 106 /** 107 * Returns an array, keyed by the key field, of all elements that meet 108 * the where clause provided. Ordered by $order key. 109 */ 110 function loadAll($order = null, $where = null) { 111 $this->_query->clear(); 112 $this->_query->addTable($this->_tbl); 113 if ($order) { 114 $this->_query->addOrder($order); 115 } 116 if ($where) { 117 $this->_query->addWhere($where); 118 } 119 $sql = $this->_query->prepare(); 120 $this->_query->clear(); 121 return db_loadHashList($sql, $this->_tbl_key); 122 } 123 124 /** 125 * Return a DBQuery object seeded with the table name. 126 * @param string $alias optional alias for table queries. 127 * @return DBQuery object 128 */ 129 function &getQuery($alias = null) { 130 $this->_query->clear(); 131 $this->_query->addTable($this->_tbl, $alias); 132 return $this->_query; 133 } 134 135 /** 136 * Generic check method 137 * 138 * Can be overloaded/supplemented by the child class 139 * @return null if the object is ok 140 */ 141 function check() { 142 return NULL; 143 } 144 145 /** 146 * Clone the current record 147 * 148 * @author handco <handco@users.sourceforge.net> 149 * @return object The new record object or null if error 150 **/ 151 function duplicate() { 152 $_key = $this->_tbl_key; 153 154 $newObj = $this; 155 // blanking the primary key to ensure that's a new record 156 $newObj->$_key = ''; 157 158 return $newObj; 159 } 160 161 /** 162 * Default trimming method for class variables of type string 163 * 164 * @param object Object to trim class variables for 165 * Can be overloaded/supplemented by the child class 166 * @return none 167 */ 168 function dPTrimAll() { 169 $trim_arr = get_object_vars($this); 170 foreach ($trim_arr as $trim_key => $trim_val) { 171 if (!(strcasecmp(gettype($trim_val), "string"))) { 172 $this->{$trim_key} = trim($trim_val); 173 } 174 } 175 } 176 177 /** 178 * Inserts a new row if id is zero or updates an existing row in the database table 179 * 180 * Can be overloaded/supplemented by the child class 181 * @return null|string null if successful otherwise returns and error message 182 */ 183 function store( $updateNulls = false ) { 184 185 $this->dPTrimAll(); 186 187 $msg = $this->check(); 188 if( $msg ) { 189 return get_class( $this )."::store-check failed<br />$msg"; 190 } 191 $k = $this->_tbl_key; 192 if( $this->$k ) { 193 addHistory($this->_tbl . '_update(' . $this->$k . ')', 0, $this->_tbl); 194 $ret = db_updateObject( $this->_tbl, $this, $this->_tbl_key, $updateNulls ); 195 } 196 else { 197 $ret = db_insertObject( $this->_tbl, $this, $this->_tbl_key ); 198 addHistory($this->_tbl . '_add(' . $this->$k . ')', 0, $this->_tbl); 199 } 200 if( !$ret ) { 201 return get_class( $this )."::store failed <br />" . db_error(); 202 } 203 else { 204 return NULL; 205 } 206 } 207 208 /** 209 * Generic check for whether dependencies exist for this object in the db schema 210 * 211 * Can be overloaded/supplemented by the child class 212 * @param string $msg Error message returned 213 * @param int Optional key index 214 * @param array Optional array to compiles standard joins: format [label=>'Label',name=>'table name',idfield=>'field',joinfield=>'field'] 215 * @return true|false 216 */ 217 function canDelete( &$msg, $oid=null, $joins=null ) { 218 global $AppUI; 219 220 // First things first. Are we allowed to delete? 221 $acl =& $AppUI->acl(); 222 if ( ! $acl->checkModuleItem($this->_tbl, "delete", $oid)) { 223 $msg = $AppUI->_( "noDeletePermission" ); 224 return false; 225 } 226 227 $k = $this->_tbl_key; 228 if ($oid) { 229 $this->$k = intval( $oid ); 230 } 231 if (is_array( $joins )) { 232 $select = "$k"; 233 $join = ""; 234 235 $q = new DBQuery; 236 $q->addTable($this->_tbl); 237 $q->addWhere("$k = '".$this->$k."'"); 238 $q->addGroup($k); 239 foreach( $joins as $table ) { 240 $q->addQuery("COUNT(DISTINCT {$table['idfield']}) AS {$table['idfield']}"); 241 $q->addJoin($table['name'], $table['name'], "{$table['joinfield']} = $k"); 242 } 243 $sql = $q->prepare(); 244 $q->clear(); 245 246 $obj = null; 247 if (!db_loadObject( $sql, $obj )) { 248 $msg = db_error(); 249 return false; 250 } 251 $msg = array(); 252 foreach( $joins as $table ) { 253 $k = $table['idfield']; 254 if ($obj->$k) { 255 $msg[] = $AppUI->_( $table['label'] ); 256 } 257 } 258 259 if (count( $msg )) { 260 $msg = $AppUI->_( "noDeleteRecord" ) . ": " . implode( ', ', $msg ); 261 return false; 262 } 263 else { 264 return true; 265 } 266 } 267 268 return true; 269 } 270 271 /** 272 * Default delete method 273 * 274 * Can be overloaded/supplemented by the child class 275 * @return null|string null if successful otherwise returns and error message 276 */ 277 function delete( $oid=null ) { 278 $k = $this->_tbl_key; 279 if ($oid) { 280 $this->$k = intval( $oid ); 281 } 282 if (!$this->canDelete( $msg )) { 283 return $msg; 284 } 285 286 addHistory($this->_tbl, $this->$k, 'delete'); 287 $q = new DBQuery; 288 $q->setDelete($this->_tbl); 289 $q->addWhere("$this->_tbl_key = '".$this->$k."'"); 290 $result = null; 291 if (!$q->exec()) { 292 $result = db_error(); 293 } 294 $q->clear(); 295 return $result; 296 } 297 298 /** 299 * Get specifically denied records from a table/module based on a user 300 * @param int User id number 301 * @return array 302 */ 303 function getDeniedRecords( $uid ) { 304 $uid = intval( $uid ); 305 $uid || exit ("FATAL ERROR<br />" . get_class( $this ) . "::getDeniedRecords failed, user id = 0" ); 306 307 $perms =& $GLOBALS['AppUI']->acl(); 308 return $perms->getDeniedItems($this->_tbl, $uid); 309 } 310 311 /** 312 * Returns a list of records exposed to the user 313 * @param int User id number 314 * @param string Optional fields to be returned by the query, default is all 315 * @param string Optional sort order for the query 316 * @param string Optional name of field to index the returned array 317 * @param array Optional array of additional sql parameters (from and where supported) 318 * @return array 319 */ 320 // returns a list of records exposed to the user 321 function getAllowedRecords( $uid, $fields='*', $orderby='', $index=null, $extra=null ) { 322 $perms =& $GLOBALS['AppUI']->acl(); 323 $uid = intval( $uid ); 324 $uid || exit ("FATAL ERROR<br />" . get_class( $this ) . "::getAllowedRecords failed" ); 325 $deny =& $perms->getDeniedItems( $this->_tbl, $uid ); 326 $allow =& $perms->getAllowedItems($this->_tbl, $uid); 327 if (! $perms->checkModule($this->_tbl, "view", $uid )) { 328 329 if (! count($allow)) { 330 return array(); // No access, and no allow overrides, so nothing to show. 331 } 332 } 333 else { 334 $allow = array(); // Full access, allow overrides don't mean anything. 335 } 336 $this->_query->clear(); 337 $this->_query->addQuery($fields); 338 $this->_query->addTable($this->_tbl); 339 340 if (@$extra['from']) { 341 $this->_query->addTable($extra['from']); 342 } 343 344 if (count($allow)) { 345 $this->_query->addWhere("$this->_tbl_key IN (" . implode(',', $allow) . ")"); 346 } 347 if (count($deny)) { 348 $this->_query->addWhere("$this->_tbl_key NOT IN (" . implode(",", $deny) . ")"); 349 } 350 if (isset($extra['where'])) { 351 $this->_query->addWhere($extra['where']); 352 } 353 354 if ($orderby) { 355 $this->_query->addOrder($orderby); 356 } 357 358 return $this->_query->loadHashList( $index ); 359 } 360 361 function getAllowedSQL( $uid, $index = null ) { 362 $perms =& $GLOBALS['AppUI']->acl(); 363 $uid = intval( $uid ); 364 $uid || exit ("FATAL ERROR<br />" . get_class( $this ) . "::getAllowedSQL failed" ); 365 $deny =& $perms->getDeniedItems( $this->_tbl, $uid ); 366 $allow =& $perms->getAllowedItems($this->_tbl, $uid); 367 if (! $perms->checkModule($this->_tbl, "view", $uid )) { 368 if (! count($allow)) { 369 return array("1=0"); // No access, and no allow overrides, so nothing to show. 370 } 371 } 372 else { 373 $allow = array(); // Full access, allow overrides don't mean anything. 374 } 375 376 if (! isset($index)) { 377 $index = $this->_tbl_key; 378 } 379 $where = array(); 380 if (count($allow)) { 381 $where[] = "$index IN (" . implode(',', $allow) . ")"; 382 } 383 if (count($deny)) { 384 $where[] = "$index NOT IN (" . implode(",", $deny) . ")"; 385 } 386 return $where; 387 } 388 389 function setAllowedSQL($uid, &$query, $index = null, $key = null) { 390 $perms =& $GLOBALS['AppUI']->acl(); 391 $uid = intval( $uid ); 392 $uid || exit ("FATAL ERROR<br />" . get_class( $this ) . "::getAllowedSQL failed" ); 393 $deny =& $perms->getDeniedItems($this->_tbl, $uid ); 394 $allow =& $perms->getAllowedItems($this->_tbl, $uid); 395 // Make sure that we add the table otherwise dependencies break 396 if (isset($index)) { 397 if (! $key) { 398 $key = substr($this->_tbl, 0, 2); 399 } 400 $query->leftJoin($this->_tbl, $key, "$key.$this->_tbl_key = $index"); 401 } 402 if (! $perms->checkModule($this->_tbl, "view", $uid )) { 403 if (! count($allow)) { 404 // We need to ensure that we don't just break complex SQLs, but 405 // instead limit to a nonsensical value. This assumes that the 406 // key is auto-incremented. 407 $query->addWhere("$this->_tbl_key = 0"); 408 return; 409 } 410 } 411 else { 412 $allow = array(); // Full access, allow overrides don't mean anything. 413 } 414 415 if (count($allow)) { 416 $query->addWhere(((! $key)?'':"$key.")."$this->_tbl_key IN (" . implode(',', $allow) . ")"); 417 } 418 if (count($deny)) { 419 $query->addWhere(((! $key)?'':"$key.")."$this->_tbl_key NOT IN (" . implode(",", $deny) . ")"); 420 } 421 } 422 } 423 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 18 19:46:52 2007 | par Balluche grâce à PHPXref 0.7 |