/**
  * Ensure everything works as expected
  */
 public function testCreateUsingMappingItself()
 {
     $account = $this->accountRepository->findById(1);
     $mapping = new \Conjoon\Data\Entity\Mail\DefaultFolderMappingEntity();
     $mapping->setType('SENT');
     $mapping->setGlobalName('INBOX/Sent');
     $mapping->setMailAccount($account);
     $this->repository->register($mapping);
     $this->assertEquals(3, $this->getConnection()->getRowCount('groupware_email_imap_mapping'), "Post-Condition");
     $this->repository->flush($mapping);
     $this->assertEquals(4, $this->getConnection()->getRowCount('groupware_email_imap_mapping'), "Post-Condition");
 }
 /**
  * Adds an email account to the database, assigning it to the currently
  * logged in user.
  * The following key/value pairs will be submitted via POST:
  *
  * <ul>
  * <li>name           : The name of the account</li>
  * <li>address        : The email address for this account</li>
  * <li>isStandard    : <tt>true</tt>, if this should become the standard email account,
  * otherwise <tt>false</tt></li>
  * <li>server_inbox   : The address of the inbox-server</li>
  * <li>server_outbox  : The address of the outbox server</li>
  * <li>username_inbox : The username for the inbox-server</li>
  * <li>username_outbox: The username for the outbox-server. If <tt>outbox_auth</tt>
  * equals to <tt>false</tt>, the value will be empty</li>
  * <li>user_name      : The full name of the user who owns this account</li>
  * <li>outbox_auth    : <tt>true</tt> if the outbox-server needs authentication,
  * otherwise <tt>false</tt></li>
  * <li>password_inbox : The password for the inbox-server</li>
  * <li>password_outbox: The password for the outbox-server. If <tt>outbox_auth</tt>
  * equals to <tt>false</tt>, the value will be empty</li>
  * </ul>
  * <li>inbox_connection_type: Secure connection type for the incoming
  * mail server. Empty for unsecure connection, or SSL or TLS</li>
  * <li>outbox_connection_type: Secure connection type for the outgoing mail
  * server. Empty for unsecure connection, or SSL or TLS. If <tt>outbox_auth</tt>
  * equals to <tt>false</tt>, the value will be empty</li>
  * </ul>
  *
  * Upon success, the following view variables will be assigned:
  * <ul>
  *  <li>success: <tt>true</tt>, if the account was added to the database,
  *  otherwise <tt>false></tt></li>
  *  <li>account: a fully configured instance of <tt>Conjoon_Groupware_Email_Account</tt>.
  * <br /><strong>NOTE:</strong> If the user submitted passwords, those will be replaced by strings
  * containing only blanks, matching the length of the originally submitted
  * passwords.</li>
  *  <li>rootFolder: The root folder that saves the tree hierarchy for this
  * account.</li>
  * <li>error: An object of the type <tt>Conjoon_Groupware_ErrorObject</tt>, if any error
  * occured, otherwise <tt>null</tt></li>
  * <ul>
  *
  * <strong>Note:</strong> The properties <tt>account</tt> and <tt>error</tt> will
  * be returned in the format based on the passed context the action was called.
  * For example, if an array was assigned to <tt>account</tt> and the context is <tt>json</tt>,
  * this array will become json-encoded and returned as a string. This happens transparently.
  *
  * @todo FACADE!
  */
 public function addEmailAccountAction()
 {
     require_once 'Conjoon/Util/Array.php';
     require_once 'Conjoon/Keys.php';
     require_once 'Conjoon/BeanContext/Inspector.php';
     require_once 'Conjoon/Modules/Groupware/Email/Account/Model/Account.php';
     require_once 'Conjoon/Modules/Groupware/Email/Account/Filter/Account.php';
     $model = new Conjoon_Modules_Groupware_Email_Account_Model_Account();
     $filter = new Conjoon_Modules_Groupware_Email_Account_Filter_Account(array(), Conjoon_Filter_Input::CONTEXT_CREATE);
     $auth = Zend_Registry::get(Conjoon_Keys::REGISTRY_AUTH_OBJECT);
     $userId = $auth->getIdentity()->getId();
     /**
      * @see Conjoon_Builder_Factory
      */
     require_once 'Conjoon/Builder/Factory.php';
     // clean cache in any case
     Conjoon_Builder_Factory::getBuilder(Conjoon_Keys::CACHE_EMAIL_ACCOUNTS, Zend_Registry::get(Conjoon_Keys::REGISTRY_CONFIG_OBJECT)->toArray())->cleanCacheForTags(array('userId' => $userId));
     $classToCreate = 'Conjoon_Modules_Groupware_Email_Account';
     $this->view->success = true;
     $this->view->error = null;
     try {
         $filter->setData($_POST);
         $processedData = $filter->getProcessedData();
     } catch (Zend_Filter_Exception $e) {
         /**
          * @see Conjoon_Error
          */
         require_once 'Conjoon/Error.php';
         $error = Conjoon_Error::fromFilter($filter, $e);
         $accountData = $_POST;
         $accountData['passwordOutbox'] = isset($accountData['passwordOutbox']) ? str_pad("", strlen($accountData['passwordOutbox']), '*') : '';
         $accountData['passwordInbox'] = isset($accountData['passwordInbox']) ? str_pad("", strlen($accountData['passwordInbox']), '*') : '';
         $this->view->account = Conjoon_BeanContext_Inspector::create($classToCreate, $accountData)->getDto();
         $this->view->success = false;
         $this->view->error = $error->getDto();
         return;
     }
     $data = $processedData;
     // check for duplicates
     $duplicates = $model->getAccountWithNameForUser($data['name'], $userId);
     if (!empty($duplicates)) {
         /**
          * @see Conjoon_Error
          */
         require_once 'Conjoon/Error.php';
         $error = new Conjoon_Error();
         $error->setMessage("There is already an account with " . "the name \"" . $data['name'] . "\"!");
         $error->setLevel(Conjoon_Error::LEVEL_WARNING);
         $this->view->success = false;
         $this->view->error = $error->getDto();
         return;
     }
     // add account here
     Conjoon_Util_Array::underscoreKeys($data);
     $addedId = $model->addAccount($userId, $data);
     /**
      * @see Conjoon_BeanContext_Decorator
      */
     require_once 'Conjoon/BeanContext/Decorator.php';
     $decoratedModel = new Conjoon_BeanContext_Decorator('Conjoon_Modules_Groupware_Email_Account_Model_Account');
     $dto = $decoratedModel->getAccountAsDto($addedId, $userId);
     $dto->folderMappings = array();
     // ADD FOLDER MAPPINGS
     if ($dto->protocol == 'IMAP') {
         /**
          * @see \Conjoon\Data\Entity\Mail\DefaultFolderMappingEntity
          */
         require_once 'Conjoon/Data/Entity/Mail/DefaultFolderMappingEntity.php';
         $entityManager = Zend_Registry::get(Conjoon_Keys::DOCTRINE_ENTITY_MANAGER);
         $accRep = $entityManager->getRepository('\\Conjoon\\Data\\Entity\\Mail\\DefaultMailAccountEntity');
         $fmRep = $entityManager->getRepository('\\Conjoon\\Data\\Entity\\Mail\\DefaultFolderMappingEntity');
         $types = array('INBOX', 'SENT', 'JUNK', 'DRAFT', 'TRASH', 'OUTBOX');
         $accountEnt = $accRep->findById($addedId);
         foreach ($types as $type) {
             $newEnt = new \Conjoon\Data\Entity\Mail\DefaultFolderMappingEntity();
             $newEnt->setType($type);
             $newEnt->setGlobalName("");
             $newEnt->setMailAccount($accountEnt);
             $fmRep->register($newEnt);
             $fmRep->flush();
             $dto->folderMappings[] = array('id' => $newEnt->getId(), 'type' => $type, 'globalName' => "", 'path' => array());
         }
     }
     if (!$dto->isOutboxAuth) {
         $dto->usernameOutbox = "";
         $dto->passwordOutbox = "";
     }
     $dto->passwordOutbox = str_pad("", strlen($dto->passwordOutbox), '*');
     $dto->passwordInbox = str_pad("", strlen($dto->passwordInbox), '*');
     // read out root folder for account
     require_once 'Conjoon/BeanContext/Decorator.php';
     $decoratedFolderModel = new Conjoon_BeanContext_Decorator('Conjoon_Modules_Groupware_Email_Folder_Model_Folder', null, false);
     $rootId = $decoratedFolderModel->getAccountsRootOrRootFolderId($addedId, $userId);
     $dto->localRootMailFolder = array();
     if ($rootId != 0) {
         $dto->localRootMailFolder = $decoratedFolderModel->getFolderBaseDataAsDto($rootId);
     }
     $this->view->account = $dto;
 }