home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / dotproject / classes / dp.class.php < prev    next >
PHP Script  |  2004-01-28  |  7KB  |  267 lines

  1. <?php /* CLASSES $Id: dp.class.php,v 1.12 2004/01/28 06:27:16 ajdonnison Exp $ */
  2.  
  3. /**
  4.  *    @package dotproject
  5.  *    @subpackage modules
  6.  *    @version $Revision: 1.12 $
  7.  */
  8.  
  9. /**
  10.  *    CDpObject Abstract Class.
  11.  *
  12.  *    Parent class to all database table derived objects
  13.  *    @author Andrew Eddie <eddieajau@users.sourceforge.net>
  14.  *    @abstract
  15.  */
  16. class CDpObject {
  17. /**
  18.  *    @var string Name of the table in the db schema relating to child class
  19.  */
  20.     var $_tbl = '';
  21. /**
  22.  *    @var string Name of the primary key field in the table
  23.  */
  24.     var $_tbl_key = '';
  25. /**
  26.  *    @var string Error message
  27.  */
  28.     var $_error = '';
  29.  
  30. /**
  31.  *    Object constructor to set table and key field
  32.  *
  33.  *    Can be overloaded/supplemented by the child class
  34.  *    @param string $table name of the table in the db schema relating to child class
  35.  *    @param string $key name of the primary key field in the table
  36.  */
  37.     function CDpObject( $table, $key ) {
  38.         $this->_tbl = $table;
  39.         $this->_tbl_key = $key;
  40.     }
  41. /**
  42.  *    @return string Returns the error message
  43.  */
  44.     function getError() {
  45.         return $this->_error;
  46.     }
  47. /**
  48.  *    Binds a named array/hash to this object
  49.  *
  50.  *    can be overloaded/supplemented by the child class
  51.  *    @param array $hash named array
  52.  *    @return null|string    null is operation was satisfactory, otherwise returns an error
  53.  */
  54.     function bind( $hash ) {
  55.         if (!is_array( $hash )) {
  56.             $this->_error = get_class( $this )."::bind failed.";
  57.             return false;
  58.         } else {
  59.             bindHashToObject( $hash, $this );
  60.             return true;
  61.         }
  62.     }
  63.  
  64. /**
  65.  *    Binds an array/hash to this object
  66.  *    @param int $oid optional argument, if not specifed then the value of current key is used
  67.  *    @return any result from the database operation
  68.  */
  69.     function load( $oid=null , $strip = true) {
  70.         $k = $this->_tbl_key;
  71.         if ($oid) {
  72.             $this->$k = intval( $oid );
  73.         }
  74.         $oid = $this->$k;
  75.         if ($oid === null) {
  76.             return false;
  77.         }
  78.         $sql = "SELECT * FROM $this->_tbl WHERE $this->_tbl_key=$oid";
  79.         return db_loadObject( $sql, $this, false, $strip );
  80.     }
  81.  
  82. /**
  83.  *    Generic check method
  84.  *
  85.  *    Can be overloaded/supplemented by the child class
  86.  *    @return null if the object is ok
  87.  */
  88.     function check() {
  89.         return NULL;
  90.     }
  91.     
  92. /**
  93. *    Clone de current record
  94. *
  95. *    @author    handco <handco@users.sourceforge.net>
  96. *    @return    object    The new record object or null if error
  97. **/
  98.     function clone() {
  99.         $_key = $this->_tbl_key;
  100.         
  101.         $newObj = $this;
  102.         // blanking the primary key to ensure that's a new record
  103.         $newObj->$_key = '';
  104.         
  105.         return $newObj;
  106.     }
  107.  
  108.  
  109. /**
  110.  *    Inserts a new row if id is zero or updates an existing row in the database table
  111.  *
  112.  *    Can be overloaded/supplemented by the child class
  113.  *    @return null|string null if successful otherwise returns and error message
  114.  */
  115.     function store( $updateNulls = false ) {
  116.         $msg = $this->check();
  117.         if( $msg ) {
  118.             return get_class( $this )."::store-check failed<br />$msg";
  119.         }
  120.         $k = $this->_tbl_key;
  121.         if( $this->$k ) {
  122.             $ret = db_updateObject( $this->_tbl, $this, $this->_tbl_key, $updateNulls );
  123.         } else {
  124.             $ret = db_insertObject( $this->_tbl, $this, $this->_tbl_key );
  125.         }
  126.         if( !$ret ) {
  127.             return get_class( $this )."::store failed <br />" . db_error();
  128.         } else {
  129.             return NULL;
  130.         }
  131.     }
  132.  
  133. /**
  134.  *    Generic check for whether dependancies exist for this object in the db schema
  135.  *
  136.  *    Can be overloaded/supplemented by the child class
  137.  *    @param string $msg Error message returned
  138.  *    @param int Optional key index
  139.  *    @param array Optional array to compiles standard joins: format [label=>'Label',name=>'table name',idfield=>'field',joinfield=>'field']
  140.  *    @return true|false
  141.  */
  142.     function canDelete( &$msg, $oid=null, $joins=null ) {
  143.         global $AppUI;
  144.         $k = $this->_tbl_key;
  145.         if ($oid) {
  146.             $this->$k = intval( $oid );
  147.         }
  148.         if (is_array( $joins )) {
  149.             $select = "$k";
  150.             $join = "";
  151.             foreach( $joins as $table ) {
  152.                 $select .= ",\nCOUNT(DISTINCT {$table['idfield']}) AS {$table['idfield']}";
  153.                 $join .= "\nLEFT JOIN {$table['name']} ON {$table['joinfield']} = $k";
  154.             }
  155.             $sql = "SELECT $select\nFROM $this->_tbl\n$join\nWHERE $k = ".$this->$k." GROUP BY $k";
  156.  
  157.             $obj = null;
  158.             if (!db_loadObject( $sql, $obj )) {
  159.                 $msg = db_error();
  160.                 return false;
  161.             }
  162.             $msg = array();
  163.             foreach( $joins as $table ) {
  164.                 $k = $table['idfield'];
  165.                 if ($obj->$k) {
  166.                     $msg[] = $AppUI->_( $table['label'] );
  167.                 }
  168.             }
  169.  
  170.             if (count( $msg )) {
  171.                 $msg = $AppUI->_( "noDeleteRecord" ) . ": " . implode( ', ', $msg );
  172.                 return false;
  173.             } else {
  174.                 return true;
  175.             }
  176.         }
  177.  
  178.         return true;
  179.     }
  180.  
  181. /**
  182.  *    Default delete method
  183.  *
  184.  *    Can be overloaded/supplemented by the child class
  185.  *    @return null|string null if successful otherwise returns and error message
  186.  */
  187.     function delete( $oid=null ) {
  188.         $k = $this->_tbl_key;
  189.         if ($oid) {
  190.             $this->$k = intval( $oid );
  191.         }
  192.         if (!$this->canDelete( $msg )) {
  193.             return $msg;
  194.         }
  195.  
  196.         $sql = "DELETE FROM $this->_tbl WHERE $this->_tbl_key = '".$this->$k."'";
  197.         if (!db_exec( $sql )) {
  198.             return db_error();
  199.         } else {
  200.             return NULL;
  201.         }
  202.     }
  203.  
  204. /**
  205.  *    Get specifically denied records from a table/module based on a user
  206.  *    @param int User id number
  207.  *    @return array
  208.  */
  209.     function getDeniedRecords( $uid ) {
  210.         $uid = intval( $uid );
  211.         $uid || exit ("FATAL ERROR<br />" . get_class( $this ) . "::getDeniedRecords failed, user id = 0" );
  212.  
  213.         // get read denied projects
  214.         $deny = array();
  215.         $sql = "
  216.         SELECT $this->_tbl_key
  217.         FROM $this->_tbl, permissions
  218.         WHERE permission_user = $uid
  219.             AND permission_grant_on = '$this->_tbl'
  220.             AND permission_item = $this->_tbl_key
  221.             AND permission_value = 0
  222.         ";
  223.         return db_loadColumn( $sql );
  224.     }
  225.  
  226. /**
  227.  *    Returns a list of records exposed to the user
  228.  *    @param int User id number
  229.  *    @param string Optional fields to be returned by the query, default is all
  230.  *    @param string Optional sort order for the query
  231.  *    @param string Optional name of field to index the returned array
  232.  *    @param array Optional array of additional sql parameters (from and where supported)
  233.  *    @return array
  234.  */
  235. // returns a list of records exposed to the user
  236.     function getAllowedRecords( $uid, $fields='*', $orderby='', $index=null, $extra=null ) {
  237.         $uid = intval( $uid );
  238.         $uid || exit ("FATAL ERROR<br />" . get_class( $this ) . "::getAllowedRecords failed" );
  239.         $deny = $this->getDeniedRecords( $uid );
  240.  
  241.         $sql = "SELECT $fields"
  242.             . "\nFROM $this->_tbl, permissions";
  243.  
  244.         if (@$extra['from']) {
  245.             $sql .= ',' . $extra['from'];
  246.         }
  247.         
  248.         $sql .= "\nWHERE permission_user = $uid"
  249.             . "\n    AND permission_value <> 0"
  250.             . "\n    AND ("
  251.             . "\n        (permission_grant_on = 'all')"
  252.             . "\n        OR (permission_grant_on = '$this->_tbl' AND permission_item = -1)"
  253.             . "\n        OR (permission_grant_on = '$this->_tbl' AND permission_item = $this->_tbl_key)"
  254.             . "\n    )"
  255.             . (count($deny) > 0 ? "\n\tAND $this->_tbl_key NOT IN (" . implode( ',', $deny ) . ')' : '');
  256.         
  257.         if (@$extra['where']) {
  258.             $sql .= "\n\t" . $extra['where'];
  259.         }
  260.  
  261.         $sql .= ($orderby ? "\nORDER BY $orderby" : '');
  262.  
  263.         return db_loadHashList( $sql, $index );
  264.     }
  265. }
  266. ?>
  267.