Horde_ActiveSync_Imap_Adapter::createMailbox PHP Method

createMailbox() public method

Create a new mailbox on the server, and subscribe to it.
public createMailbox ( string $name, string $parent = null ) : string
$name string The new mailbox name.
$parent string The parent mailbox, if any. @return string The new serverid for the mailbox. This is the UTF-8 name of the mailbox. @since 2.9.0 @throws Horde_ActiveSync_Exception, Horde_ActiveSync_Exception_FolderExists
return string
    public function createMailbox($name, $parent = null)
    {
        if (!empty($parent)) {
            $ns = $this->_defaultNamespace();
            $name = $parent . $ns['delimiter'] . $name;
        }
        $mbox = new Horde_Imap_Client_Mailbox($this->_prependNamespace($name));
        $imap = $this->_getImapOb();
        try {
            $imap->createMailbox($mbox);
            $imap->subscribeMailbox($mbox, true);
        } catch (Horde_Imap_Client_Exception $e) {
            if ($e->getCode() == Horde_Imap_Client_Exception::ALREADYEXISTS) {
                $this->_logger->warn(sprintf('[%s] Mailbox %s already exists, subscribing to it.', $this->_procid, $name));
                try {
                    $imap->subscribeMailbox($mbox, true);
                } catch (Horde_Imap_Client_Exception $e) {
                    // Exists, but could not subscribe to it, something is
                    // *really* wrong.
                    throw new Horde_ActiveSync_Exception_FolderExists('Folder Exists!');
                }
            }
            throw new Horde_ActiveSync_Exception($e);
        }
        return $mbox->utf8;
    }

Usage Example

Example #1
0
 /**
  * Change a folder on the server.
  *
  * @param string $old_name  The server's existing folder id.
  * @param string $new_name  The new display name.
  * @param string $parent    The folder's parent server id, if needed.
  * @param string $uid       The existing folder uid, if this is an edit.
  *                          @since 2.5.0 (@todo Look at this for H6. It's
  *                          here now to save an extra DB lookup for data
  *                          we already have.)
  * @param  integer $type    The EAS Folder type. @since 2.12.0
  *
  * @return Horde_ActiveSync_Message_Folder
  * @throws Horde_ActiveSync_Exception
  */
 public function changeFolder($old_name, $new_name, $parent, $uid = null, $type = null)
 {
     $this->_logger->info(sprintf('[%s] Horde_Core_ActiveSync_Driver::changeFolder(%s, %s, %s, %s, %s)', getmypid(), $old_name, $new_name, $parent, $uid, $type));
     // For FOLDERUPDATE requests, the EAS Folder type is not passed by
     // the client so we need to figure it out.
     if (empty($type) && !empty($old_name)) {
         $parts = $this->_parseFolderId($old_name, true);
         if (is_array($parts)) {
             $type = $parts[self::FOLDER_PART_CLASS];
         } else {
             $type = $parts;
         }
     }
     switch ($type) {
         case Horde_ActiveSync::CLASS_EMAIL:
         case Horde_ActiveSync::FOLDER_TYPE_USER_MAIL:
             if (!$old_name) {
                 try {
                     $serverid = $this->_imap->createMailbox($new_name, $parent);
                 } catch (Horde_ActiveSync_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     throw $e;
                 }
             } else {
                 try {
                     $serverid = $this->_imap->renameMailbox($old_name, $new_name, $parent);
                     $uid = $this->_getFolderUidForBackendId($serverid, Horde_ActiveSync::FOLDER_TYPE_USER_MAIL, $old_name);
                 } catch (Horde_ActiveSync_Exception $e) {
                     $this->_logger->err($e->getMessage());
                     throw $e;
                 }
             }
             return $this->getFolder($serverid);
         case Horde_ActiveSync::CLASS_TASKS:
         case Horde_ActiveSync::FOLDER_TYPE_USER_TASK:
             $class = Horde_ActiveSync::CLASS_TASKS;
             $type = Horde_ActiveSync::FOLDER_TYPE_USER_TASK;
             break;
         case Horde_ActiveSync::CLASS_CALENDAR:
         case Horde_ActiveSync::FOLDER_TYPE_USER_APPOINTMENT:
             $class = Horde_ActiveSync::CLASS_CALENDAR;
             $type = Horde_ActiveSync::FOLDER_TYPE_USER_APPOINTMENT;
             break;
         case Horde_ActiveSync::CLASS_CONTACTS:
         case Horde_ActiveSync::FOLDER_TYPE_USER_CONTACT:
             $class = Horde_ActiveSync::CLASS_CONTACTS;
             $type = Horde_ActiveSync::FOLDER_TYPE_USER_CONTACT;
             break;
         case Horde_ActiveSync::CLASS_NOTES:
         case Horde_ActiveSync::FOLDER_TYPE_USER_NOTE:
             $class = Horde_ActiveSync::CLASS_NOTES;
             $type = Horde_ActiveSync::FOLDER_TYPE_USER_NOTE;
             break;
         default:
             throw new Horde_ActiveSync_Exception('Unsupported EAS Collection Class.');
     }
     if (!$old_name) {
         try {
             $id = $this->_connector->createFolder($class, $new_name);
         } catch (Horde_Exception $e) {
             $this->_logger->err($e->getMessage());
             throw new Horde_ActiveSync_Exception($e);
         }
     } else {
         if (empty($parts)) {
             $parts = $this->_parseFolderId($old_name);
         }
         if (is_array($parts)) {
             $id = $parts[self::FOLDER_PART_ID];
         } else {
             $id = $parts;
         }
         try {
             $this->_connector->changeFolder($class, $id, $new_name);
         } catch (Horde_Exception $e) {
             $this->_logger->err($e->getMessage());
             throw new Horde_ActiveSync_Exception($e);
         }
     }
     return $this->_buildNonMailFolder($class . ':' . $id, Horde_ActiveSync::FOLDER_ROOT, $type, $new_name);
 }