/**
  * @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.");
     }
 }
 /**
  * @param block_BlockContext $context
  * @param block_BlockRequest $request
  * @return String the view name
  */
 public function execute($context, $request)
 {
     $id = $this->getFormId();
     if (empty($id)) {
         return block_BlockView::NONE;
     }
     $form = DocumentHelper::getDocumentInstance($id);
     if (!$form->isPublished()) {
         return block_BlockView::NONE;
     }
     $form->getDocumentNode()->getDescendents();
     $this->setParameter('form', $form);
     if ($context->inBackofficeMode()) {
         return block_BlockView::DUMMY;
     }
     if ($request->hasParameter("receiverIds")) {
         $receiverIds = explode(",", $request->getParameter("receiverIds"));
         $receiverLabels = array();
         foreach ($receiverIds as $receiverId) {
             if (is_numeric($receiverId)) {
                 try {
                     $receiver = DocumentHelper::getDocumentInstance($receiverId);
                     $receiverLabels[] = $receiver->getLabel();
                 } catch (Exception $e) {
                     Framework::exception($e);
                     $receiverLabels[] = $receiverId;
                 }
             } elseif (f_util_StringUtils::isNotEmpty($receiverId)) {
                 $receiverLabels[] = $receiverId;
             }
         }
         $this->setParameter("receiverLabels", $receiverLabels);
     }
     if ($this->isSuccess($context, $request)) {
         $view = block_BlockView::SUCCESS;
     } else {
         if ($this->isFormPosted($request)) {
             $fs = form_FormService::getInstance();
             try {
                 $fs->saveFormData($form, $request);
                 $user = $context->getGlobalContext()->getUser();
                 $user->setAttribute('form_success_parameters_' . $form->getId(), $request->getParameters());
                 $view = block_BlockView::SUCCESS;
             } catch (form_FormValidationException $e) {
                 $this->setParameter('errors', $e->getErrorCollection());
                 $view = block_BlockView::INPUT;
             }
         } else {
             $view = block_BlockView::INPUT;
         }
     }
     // Calls the BlockFormDecorator if present.
     $formId = $form->getFormid();
     $matches = null;
     if (preg_match('#^modules_([a-z]+)/([a-z]+)$#', $formId, $matches)) {
         $extendClassName = $matches[1] . '_BlockForm' . ucfirst($matches[2]) . 'Decorator';
         if (f_util_ClassUtils::classExists($extendClassName)) {
             $instance = new $extendClassName($this);
             if ($instance instanceof form_BlockFormDecorator) {
                 $instance->execute($context, $request);
             } else {
                 Framework::warn("\"{$extendClassName}\" is not an instance of \"block_BlockDecorator\": form \"{$formId}\" won't be decorated.");
             }
         }
     }
     return $view;
 }