/**
  * @param form_persistentdocument_mail $document
  * @param Integer $parentNodeId Parent node ID where to save the document (optionnal => can be null !).
  * @throws form_ReplyToFieldAlreadyExistsException
  * @return void
  */
 protected function preSave($document, $parentNodeId = null)
 {
     if ($document->getMultiline()) {
         $document->setValidators('emails:true');
     } else {
         $document->setValidators('email:true');
     }
     if ($parentNodeId !== NULL) {
         $form = DocumentHelper::getDocumentInstance($parentNodeId);
     } else {
         $form = $this->getFormOf($document);
     }
     if ($form === null) {
         if (Framework::isWarnEnabled()) {
             Framework::warn(__METHOD__ . ' the mail field document (' . $document->__toString() . ')is not in a form');
         }
     } else {
         if ($document->getUseAsReply()) {
             $oldReplyField = form_BaseFormService::getInstance()->getReplyToField($form);
             if ($oldReplyField !== null && $oldReplyField !== $document) {
                 Framework::error(__METHOD__ . ' Old reply field :' . $oldReplyField->__toString());
                 throw new form_ReplyToFieldAlreadyExistsException(f_Locale::translate('&modules.form.bo.errors.Mail-field-for-replyto-exists'));
             }
         }
     }
     parent::preSave($document, $parentNodeId);
 }
 /**
  * Builds the mail_MessageRecipients according to the recipients selected by
  * the frontoffice user.
  * This method may be overriden via the injection mechanism to allow the
  * developper build a specific mail_MessageRecipients to suit the needs of
  * the project.
  *
  * @param form_persistentdocument_form $form
  * @param mail_MessageRecipients $recipients
  * @param block_BlockRequest $request
  */
 protected function buildMessageRecipients($form, &$recipients, &$request)
 {
     // If there is no recipientGroup, we can exit this method.
     if ($form->getRecipientGroupCount() == 0) {
         return;
     }
     // Init arrays.
     $toArray = $recipients->hasTo() ? $recipients->getTo() : array();
     $ccArray = $recipients->hasCC() ? $recipients->getCC() : array();
     $bccArray = $recipients->hasBCC() ? $recipients->getBCC() : array();
     // The following holds the ID of the recipientGroups selected by the user.
     $selectedGroupArray = array();
     // Retrieve the recipientGroups selected by the user.
     // Depending on the selection type (single or multiple), this value may
     // be either a string or an array.
     if ($request->hasParameter(self::RECIPIENT_GROUP_FIELD_NAME)) {
         $selectedGroupIds = $request->getParameter(self::RECIPIENT_GROUP_FIELD_NAME);
         if (is_string($selectedGroupIds)) {
             $selectedGroupIds = array($selectedGroupIds);
         }
         // Convert each value into an integer.
         $selectedGroupIds = array_map('intval', $selectedGroupIds);
         foreach ($form->getRecipientGroupArray() as $recipientGroup) {
             if (in_array($recipientGroup->getId(), $selectedGroupIds)) {
                 $selectedGroupArray[] = $recipientGroup;
             }
         }
         // If the form holds more than one recipientGroups and if the request
         // contains no recipientGroup selection, we throw an Exception.
         if (count($selectedGroupArray) == 0) {
             throw new form_FormException('Unable to determine the recipients.');
         }
     } else {
         $selectedGroupArray = $form->getRecipientGroupArray();
     }
     // Iterates over the form's recipientGroups and skip the ones that have
     // not been selected by the user.
     foreach ($selectedGroupArray as $recipientGroup) {
         foreach ($recipientGroup->getToArray() as $contact) {
             $toArray = array_merge($toArray, $contact->getEmailAddresses());
         }
         foreach ($recipientGroup->getCcArray() as $contact) {
             $ccArray = array_merge($ccArray, $contact->getEmailAddresses());
         }
         foreach ($recipientGroup->getBccArray() as $contact) {
             $bccArray = array_merge($bccArray, $contact->getEmailAddresses());
         }
     }
     // Update mail_MessageRecipients object.
     $recipients->setTo(array_unique($toArray));
     $recipients->setCC(array_unique($ccArray));
     $recipients->setBCC(array_unique($bccArray));
     if ($recipients->isEmpty() && Framework::isWarnEnabled()) {
         Framework::warn(__METHOD__ . " recipients is empty.");
     }
 }