From dad574d62d7e3285bd980fb913bcd208ce22a90f Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 1 Oct 2010 11:38:54 -0600 Subject: [PATCH] IMP_Compose implements ArrayAccess and Iterator --- imp/compose-mimp.php | 2 +- imp/compose.php | 18 ++--- imp/lib/Ajax/Application.php | 5 +- imp/lib/Compose.php | 179 ++++++++++++++++++++++--------------------- imp/lib/Dimp.php | 2 +- imp/lib/Views/Compose.php | 2 +- 6 files changed, 104 insertions(+), 104 deletions(-) diff --git a/imp/compose-mimp.php b/imp/compose-mimp.php index 0cd9fdc58..718b911d4 100644 --- a/imp/compose-mimp.php +++ b/imp/compose-mimp.php @@ -382,7 +382,7 @@ if ($_SESSION['imp']['file_upload']) { try { if (Horde::callHook('mimp_advanced', array('compose_attach'), 'imp')) { $t->set('attach', true); - if ($atc_list = $imp_compose->getAttachments()) { + if (count($imp_compose)) { $imp_ui_mbox = new IMP_Ui_Mailbox(); $t->set('attach_data', sprintf("%s [%s] - %s", htmlspecialchars($atc_list[0]['part']->getName()), htmlspecialchars($atc_list[0]['part']->getType()), $imp_ui_mbox->getSize($atc_list[0]['part']->getBytes()))); } diff --git a/imp/compose.php b/imp/compose.php index 1a4430c8b..0495bf79c 100644 --- a/imp/compose.php +++ b/imp/compose.php @@ -184,21 +184,19 @@ if ($_SESSION['imp']['file_upload']) { $deleteList = Horde_Util::getPost('delattachments', array()); /* Update the attachment information. */ - foreach (array_keys($imp_compose->getAttachments()) as $i) { - if (!in_array($i, $deleteList)) { - $description = $vars->get('file_description_' . $i); - $imp_compose->updateAttachment($i, array('description' => $description)); + foreach ($imp_compose as $key => $val) { + if (!in_array($key, $deleteList)) { + $val['part']->setDescription($vars->get('file_description_' . $key)); + $imp_compose[$key] = $val; } } /* Delete attachments. */ - if (!empty($deleteList)) { - $filenames = $imp_compose->deleteAttachment($deleteList); + foreach ($deleteList as $val) { if ($notify) { - foreach ($filenames as $val) { - $notification->push(sprintf(_("Deleted the attachment \"%s\"."), Horde_Mime::decode($val)), 'horde.success'); - } + $notification->push(sprintf(_("Deleted attachment \"%s\"."), Horde_Mime::decode($imp_compose[$val]['part']->getName(true))), 'horde.success'); } + unset($imp_compose[$val]); } /* Add new attachments. */ @@ -1025,7 +1023,7 @@ if ($redirect) { if ($t->get('numberattach')) { $atc = array(); $v = $injector->getInstance('Horde_Mime_Viewer'); - foreach ($imp_compose->getAttachments() as $atc_num => $data) { + foreach ($imp_compose as $atc_num => $data) { $mime = $data['part']; $type = $mime->getType(); diff --git a/imp/lib/Ajax/Application.php b/imp/lib/Ajax/Application.php index 85c848fdd..8b90fbc1d 100644 --- a/imp/lib/Ajax/Application.php +++ b/imp/lib/Ajax/Application.php @@ -1259,8 +1259,9 @@ class IMP_Ajax_Application extends Horde_Core_Ajax_Application { if (isset($this->_vars->atc_indices)) { $imp_compose = $GLOBALS['injector']->getInstance('IMP_Compose')->getOb($this->_vars->imp_compose); - foreach ($imp_compose->deleteAttachment($this->_vars->atc_indices) as $val) { - $GLOBALS['notification']->push(sprintf(_("Deleted attachment \"%s\"."), Horde_Mime::decode($val)), 'horde.success'); + foreach ($this->_vars->atc_indices as $val) { + $GLOBALS['notification']->push(sprintf(_("Deleted attachment \"%s\"."), Horde_Mime::decode($imp_compose[$val]['part']->getName(true))), 'horde.success'); + unset($imp_compose[$val]); } } diff --git a/imp/lib/Compose.php b/imp/lib/Compose.php index 15b30a4ac..7c4a0de37 100644 --- a/imp/lib/Compose.php +++ b/imp/lib/Compose.php @@ -13,7 +13,7 @@ * @license http://www.fsf.org/copyleft/gpl.html GPL * @package IMP */ -class IMP_Compose implements Countable +class IMP_Compose implements ArrayAccess, Countable, Iterator { /* The virtual path to use for VFS data. */ const VFS_ATTACH_PATH = '.horde/imp/compose'; @@ -1132,7 +1132,7 @@ class IMP_Compose implements Countable $base = new Horde_Mime_Part(); $base->setType('multipart/mixed'); $base->addPart($textpart); - foreach (array_keys($this->_cache) as $id) { + foreach ($this as $id => $val) { $base->addPart($this->buildAttachment($id)); } } @@ -2052,92 +2052,16 @@ class IMP_Compose implements Countable } /** - * Delete attached files. - * - * @param mixed $number Either a single integer or an array of integers - * corresponding to the attachment position. - * - * @return array The list of deleted filenames (MIME encoded). - */ - public function deleteAttachment($number) - { - $names = array(); - - if (!is_array($number)) { - $number = array($number); - } - - foreach ($number as $val) { - if (!isset($this->_cache[$val])) { - continue; - } - - $atc = &$this->_cache[$val]; - - switch ($atc['filetype']) { - case 'vfs': - /* Delete from VFS. */ - try { - $vfs = $GLOBALS['injector']->getInstance('Horde_Vfs')->getVfs(); - $vfs->deleteFile(self::VFS_ATTACH_PATH, $atc['filename']); - } catch (VFS_Exception $e) {} - break; - - case 'file': - /* Delete from filesystem. */ - @unlink($filename); - break; - } - - $names[] = $atc['part']->getName(true); - - /* Remove the size information from the counter. */ - $this->_size -= $atc['part']->getBytes(); - - unset($this->_cache[$val]); - - $this->changed = 'changed'; - } - - return $names; - } - - /** * Deletes all attachments. */ public function deleteAllAttachments() { - $this->deleteAttachment(array_keys($this->_cache)); - } - - /** - * Updates information in a specific attachment. - * - * @param integer $number The attachment to update. - * @param array $params An array of update information. - *
-     * 'description'  --  The Content-Description value.
-     * 
- */ - public function updateAttachment($number, $params) - { - if (isset($this->_cache[$number])) { - $this->_cache[$number]['part']->setDescription($params['description']); - $this->changed = 'changed'; + foreach ($this as $key => $val) { + unset($this[$key]); } } /** - * Returns the list of current attachments. - * - * @return array The list of attachments. - */ - public function getAttachments() - { - return $this->_cache; - } - - /** * Returns the size of the attachments in bytes. * * @return integer The size of the attachments (in bytes). @@ -2156,24 +2080,23 @@ class IMP_Compose implements Countable */ public function buildAttachment($id) { - $part = $this->_cache[$id]['part']; + $atc = $this[$id]; - switch ($this->_cache[$id]['filetype']) { + switch ($atc['filetype']) { case 'vfs': - // TODO: Use streams try { $vfs = $GLOBALS['injector']->getInstance('Horde_Vfs')->getVfs(); - $part->setContents($vfs->read(self::VFS_ATTACH_PATH, $this->_cache[$id]['filename'])); + $atc['part']->setContents($vfs->readFile(self::VFS_ATTACH_PATH, $atc['filename'])); } catch (VFS_Exception $e) {} break; case 'file': - $fp = fopen($this->_cache[$id]['filename'], 'r'); - $part->setContents($fp); + $fp = fopen($atc['filename'], 'r'); + $atc['part']->setContents($fp); fclose($fp); } - return $part; + return $atc['part']; } /** @@ -2460,7 +2383,7 @@ class IMP_Compose implements Countable $trailer .= Horde_String::convertCharset(' (' . sprintf(_("Links will expire on %s"), strftime('%x', $del_time)) . ')', $GLOBALS['registry']->getCharset(), $charset); } - foreach ($this->getAttachments() as $att) { + foreach ($this as $att) { $trailer .= "\n" . $baseurl->copy()->add(array('u' => $auth, 't' => $ts, 'f' => $att['part']->getName())); try { @@ -2774,7 +2697,7 @@ class IMP_Compose implements Countable if ($vfs->exists(self::VFS_DRAFTS_PATH, $filename)) { try { - $data = $vfs->read(self::VFS_DRAFTS_PATH, $filename); + $data = $vfs->readFile(self::VFS_DRAFTS_PATH, $filename); $vfs->deleteFile(self::VFS_DRAFTS_PATH, $filename); } catch (VFS_Exception $e) { return; @@ -2897,6 +2820,57 @@ class IMP_Compose implements Countable return $search; } + /* ArrayAccess methods. */ + + public function offsetExists($offset) + { + return isset($this->_cache[$offset]); + } + + public function offsetGet($offset) + { + return isset($this->_cache[$offset]) + ? $this->_cache[$offset] + : null; + } + + public function offsetSet($offset, $value) + { + $this->_cache[$offset] = $value; + $this->changed = 'changed'; + } + + public function offsetUnset($offset) + { + if (!isset($this->_cache[$offset])) { + return; + } + + $atc = &$this->_cache[$offset]; + + switch ($atc['filetype']) { + case 'file': + /* Delete from filesystem. */ + @unlink($filename); + break; + + case 'vfs': + /* Delete from VFS. */ + try { + $vfs = $GLOBALS['injector']->getInstance('Horde_Vfs')->getVfs(); + $vfs->deleteFile(self::VFS_ATTACH_PATH, $atc['filename']); + } catch (VFS_Exception $e) {} + break; + } + + /* Remove the size information from the counter. */ + $this->_size -= $atc['part']->getBytes(); + + unset($this->_cache[$offset]); + + $this->changed = 'changed'; + } + /* Countable methods. */ /** @@ -2909,4 +2883,31 @@ class IMP_Compose implements Countable return count($this->_cache); } + /* Iterator methods. */ + + public function current() + { + return current($this->_cache); + } + + public function key() + { + return key($this->_cache); + } + + public function next() + { + next($this->_cache); + } + + public function rewind() + { + reset($this->_cache); + } + + public function valid() + { + return (key($this->_cache) !== null); + } + } diff --git a/imp/lib/Dimp.php b/imp/lib/Dimp.php index 888423e3c..0eba4f2b1 100644 --- a/imp/lib/Dimp.php +++ b/imp/lib/Dimp.php @@ -110,7 +110,7 @@ class IMP_Dimp $fwd_list = array(); if (count($imp_compose)) { - foreach ($imp_compose->getAttachments() as $atc_num => $data) { + foreach ($imp_compose as $atc_num => $data) { $mime = $data['part']; $fwd_list[] = array( diff --git a/imp/lib/Views/Compose.php b/imp/lib/Views/Compose.php index 3130c47a6..60beca655 100644 --- a/imp/lib/Views/Compose.php +++ b/imp/lib/Views/Compose.php @@ -59,7 +59,7 @@ class IMP_Views_Compose $result['js'] = array_merge($result['js'], $imp_ui->identityJs()); if ($t->get('composeCache') && count($imp_compose)) { - foreach ($imp_compose->getAttachments() as $num => $atc) { + foreach ($imp_compose as $num => $atc) { $mime = $atc['part']; $opts = Horde_Serialize::serialize(array( 'name' => $mime->getName(true), -- 2.11.0