Wicked_Driver::removeAttachment PHP Method

removeAttachment() public method

Remove a single version or all versions of an attachment to $pageId from the VFS backend.
public removeAttachment ( integer $pageId, string $attachment, string $version = null )
$pageId integer The Id of the page the file is attached to.
$attachment string The name of the file.
$version string If specified, the version to delete. If null, then all versions of $attachment will be removed.
    public function removeAttachment($pageId, $attachment, $version = null)
    {
        $vfs = $this->getVFS();
        $path = Wicked::VFS_ATTACH_PATH . '/' . $pageId;
        $fileList = $this->getAttachedFiles($pageId, true);
        foreach ($fileList as $file) {
            $fileversion = $file['attachment_version'];
            if ($file['attachment_name'] == $attachment && (is_null($version) || $fileversion == $version)) {
                /* Skip any attachments that don't exist so they can
                 * be cleared out of the backend. */
                if (!$vfs->exists($path, $attachment . ';' . $fileversion)) {
                    continue;
                }
                try {
                    $vfs->deleteFile($path, $attachment . ';' . $fileversion);
                } catch (Horde_Vfs_Exception $e) {
                    throw new Wicked_Exception($e);
                }
            }
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * Removes a single version or all versions of an attachment from
  * $pageId.
  *
  * @param integer $pageId     The Id of the page the file is attached to.
  * @param string $attachment  The name of the file.
  * @param string $version     If specified, the version to delete. If null,
  *                            then all versions of $attachment will be
  *                            removed.
  *
  * @throws Wicked_Exception
  */
 public function removeAttachment($pageId, $attachment, $version = null)
 {
     /* Try to delete from the VFS first. */
     parent::removeAttachment($pageId, $attachment, $version);
     /* First try against the current attachments table. */
     $sql = 'DELETE FROM ' . $this->_params['attachmenttable'] . ' WHERE page_id = ? AND attachment_name = ?';
     $params = array((int) $pageId, $attachment);
     if (!is_null($version)) {
         $sql .= ' AND attachment_version = ?';
         $params[] = (int) $version;
     }
     try {
         $this->_db->beginDbTransaction();
         $result = $this->_db->delete($sql, $params);
         /* Now try against the attachment history table. $params is
          * unchanged. */
         $sql = 'DELETE FROM ' . $this->_params['attachmenthistorytable'] . ' WHERE page_id = ? AND attachment_name = ?';
         if (!is_null($version)) {
             $sql .= ' AND attachment_version = ?';
         }
         $this->_db->delete($sql, $params);
         $this->_db->commitDbTransaction();
     } catch (Horde_Db_Exception $e) {
         $this->_db->rollbackDbTransaction();
         throw new Wicked_Exception($e);
     }
 }