Example #1
0
 /**
  * Update existing record
  *
  * @param array $data The record's new data
  * @return int The amount of updated rows
  */
 public function update(array $data)
 {
     // check if primary key is available
     $prim = $this->_model->info(Zend_Db_Table::PRIMARY);
     if (!is_array($prim)) {
         $prim = array($prim);
     }
     $where = array();
     foreach ($prim as $key) {
         if (!array_key_exists($key, $data)) {
             throw new Garp_Content_Exception('Primary key ' . $key . ' not available in data');
         }
         $where[] = $this->_model->getAdapter()->quoteInto($key . ' = ?', $data[$key]);
         unset($data[$key]);
     }
     $where = implode(' AND ', $where);
     try {
         /**
          * First, see if the user is allowed to update everything
          */
         $this->_checkAcl('update');
     } catch (Garp_Auth_Exception $e) {
         /**
          * If that fails, check if the user is allowed to update her own material
          * AND if the current item is hers.
          */
         $this->_checkAcl('update_own');
         /**
          * Good, the user is allowed to 'update_own'. In that case we have to check if
          * the current item is actually the user's.
          */
         if (!$this->_itemBelongsToUser($data, $where)) {
             throw new Garp_Auth_Exception('You are only allowed to edit your own material.');
         }
     }
     return $this->_model->update($data, $where);
 }