/** * 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."); } }