IMP_Indices::copy PHP Method

copy() public method

Also handles moves to the tasklist and/or notepad applications.
public copy ( string $targetMbox, string $action, array $opts = [] ) : boolean
$targetMbox string The mailbox to move/copy messages to (UTF-8).
$action string Either 'copy' or 'move'.
$opts array Additional options:
  - create: (boolean) Should the target mailbox be created?
            DEFAULT: false
return boolean True if successful, false if not.
    public function copy($targetMbox, $action, array $opts = array())
    {
        global $notification;
        if (!count($this)) {
            return false;
        }
        $targetMbox = IMP_Mailbox::get($targetMbox);
        /* If the target is a tasklist, handle the move/copy specially. */
        $tasks = new IMP_Indices_Copy_Tasklist();
        if ($tasks->match($targetMbox)) {
            return $tasks->copy($targetMbox, $this, $action == 'copy');
        }
        /* If the target is a notepad, handle the move/copy specially. */
        $note = new IMP_Indices_Copy_Notepad();
        if ($note->match($targetMbox)) {
            return $note->copy($targetMbox, $this, $action == 'copy');
        }
        if (!empty($opts['create']) && !$targetMbox->create()) {
            return false;
        }
        $imap_move = false;
        $return_value = true;
        switch ($action) {
            case 'move':
                $imap_move = true;
                $message = _("There was an error moving messages from \"%s\" to \"%s\". This is what the server said");
                break;
            case 'copy':
                $message = _("There was an error copying messages from \"%s\" to \"%s\". This is what the server said");
                break;
        }
        foreach ($this as $ob) {
            try {
                if ($targetMbox->readonly) {
                    throw new IMP_Exception(_("The target directory is read-only."));
                }
                if ($action == 'move' && $ob->mbox->readonly) {
                    throw new IMP_Exception(_("The source directory is read-only."));
                }
                /* Throws Exception on error. */
                $ob->mbox->uidvalid;
                /* Attempt to copy/move messages to new mailbox. */
                $imp_imap = $ob->mbox->imp_imap;
                $imp_imap->copy($ob->mbox, $targetMbox, array('ids' => $imp_imap->getIdsOb($ob->uids), 'move' => $imap_move));
            } catch (Exception $e) {
                $error_msg = sprintf($message, $ob->mbox->display, $targetMbox->display) . ': ' . $e->getMessage();
                if ($e instanceof IMP_Imap_Exception) {
                    $e->notify($error_msg);
                } else {
                    $notification->push($error_msg, 'horde.error');
                }
                $return_value = false;
            }
        }
        return $return_value;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Moves messages to a mailbox.
  *
  * @param string $mailbox  The name of the source mailbox (UTF-8).
  * @param array $indices   The list of UIDs to move.
  * @param string $target   The name of the target mailbox (UTF-8).
  *
  * @return boolean  True if successful, false if not.
  */
 public function moveMessages($mailbox, $indices, $target)
 {
     $i = new IMP_Indices($mailbox, $indices);
     return $i->copy($target, 'move', array('create' => true));
 }