public function flag(array $add = array(), array $remove = array(), array $opts = array())
{
global $injector, $notification;
if (!count($this)) {
return false;
}
$opts = array_merge(array('unchangedsince' => array()), $opts);
$ajax_queue = $injector->getInstance('IMP_Ajax_Queue');
$ret = true;
foreach ($this as $ob) {
try {
if ($ob->mbox->readonly) {
throw new IMP_Exception(_("This mailbox is read-only."));
}
$ob->mbox->uidvalid;
$unchangedsince = isset($opts['unchangedsince'][strval($ob->mbox)]) ? $opts['unchangedsince'][strval($ob->mbox)] : null;
/* Flag/unflag the messages now. */
$imp_imap = $ob->mbox->imp_imap;
$res = $imp_imap->store($ob->mbox, array_filter(array('add' => $add, 'ids' => $imp_imap->getIdsOb($ob->uids), 'remove' => $remove, 'unchangedsince' => $unchangedsince)));
$flag_change = $ob->mbox->getIndicesOb($ob->uids);
if ($unchangedsince && count($res)) {
foreach ($res as $val) {
unset($flag_change[$val]);
}
if (empty($opts['silent'])) {
$notification->push(sprintf(_("Flags were not changed for at least one message in the mailbox \"%s\" because the flags were altered by another connection to the mailbox prior to this request. You may redo the flag action if desired; this warning is precautionary to ensure you don't overwrite flag changes."), $ob->mbox->display), 'horde.warning');
$ret = false;
}
}
foreach (array('add' => $add, 'remove' => $remove) as $key => $val) {
if (!empty($val)) {
$ajax_queue->flag($val, $key == 'add', $flag_change);
if ($this instanceof IMP_Indices_Mailbox) {
$ajax_queue->flag($val, $key == 'add', $this->mailbox->toBuids($flag_change));
}
}
}
} catch (Exception $e) {
if (empty($opts['silent'])) {
$notification->push(sprintf(_("There was an error flagging messages in the mailbox \"%s\": %s."), $ob->mbox->display, $e->getMessage()), 'horde.error');
}
$ret = false;
}
}
return $ret;
}