private function loadTemplate($message, $code, $publisherId, $publicationId, $identifierBatchId) { // Add configuration helper file require_once JPATH_COMPONENT . '/helpers/configuration.php'; // Check that code is valid if (!ConfigurationHelper::isValidParameterName($code)) { JFactory::getApplication()->enqueueMessage(JText::_('COM_ISBNREGISTRY_ERROR_MESSAGE_INVALID_CODE_PARAMETER'), 'error'); return false; } // Get component parameters $params = JComponentHelper::getParams('com_isbnregistry'); // Get message type id for the given code $messageTypeId = $params->get($code, 0); // Check that the value has been defined if ($messageTypeId == 0) { JFactory::getApplication()->enqueueMessage(JText::_('COM_ISBNREGISTRY_ERROR_MESSAGE_NO_DEFAULT_MESSAGE_TYPE_FOUND'), 'warning'); return false; } // Set message type id $message->message_type_id = $messageTypeId; // Load publisher model $publisherModel = JModelLegacy::getInstance('publisher', 'IsbnregistryModel'); // Load publisher $publisher = $publisherModel->getPublisherById($publisherId); // Check the result if (!$publisher) { JFactory::getApplication()->enqueueMessage(JText::_('COM_ISBNREGISTRY_ERROR_MESSAGE_NO_PUBLISHER_FOUND'), 'warning'); return false; } // Set publisher id $message->publisher_id = $publisherId; // Set publisher language code by default $message->lang_code = $publisher->lang_code; // Update recipient $message->recipient = $publisher->email; // Variables for publication title and subtitle $title = ''; $subtitle = ''; // Check if identifiers are related to a publication $isPublicationIdentifierCreated = ConfigurationHelper::isPublicationIdentifierCreated($code); // If so, load publication if ($isPublicationIdentifierCreated) { // Load publication model $publicationModel = JModelLegacy::getInstance('publication', 'IsbnregistryModel'); // Load publication $publication = $publicationModel->getPublicationById($publicationId); // Check the result if (!$publication) { JFactory::getApplication()->enqueueMessage(JText::_('COM_ISBNREGISTRY_ERROR_MESSAGE_NO_PUBLICATION_FOUND'), 'warning'); return false; } // Set publication id $message->publication_id = $publicationId; // Set publication lang code $message->lang_code = $publication->lang_code; // Set publication title $title = $publication->title; // Set publication subtitle $subtitle = $publication->subtitle; // Get the id of the publisher that represents author publishers $authorPublisherId = $params->get('author_publisher_id_isbn', 0); // Is the publisher author publisher? If true, publisher's name, // address and recipient's email must be read from the publication if ($authorPublisherId == $publisher->id) { // Get publisher name and address - this change is NOT stored // to DB $publisher->official_name = $publication->official_name; $publisher->address = $publication->address; $publisher->zip = $publication->zip; $publisher->city = $publication->city; // Update recipient $message->recipient = $publication->email; } else { if (!empty($publication->email)) { // Update recipient - publication email address is the // primary email address $message->recipient = $publication->email; } } } // Load message template model $messageTemplateModel = JModelLegacy::getInstance('messagetemplate', 'IsbnregistryModel'); // Load template $template = $messageTemplateModel->getMessageTemplateByTypeAndLanguage($messageTypeId, $message->lang_code); // Check that we found a template if (!$template) { JFactory::getApplication()->enqueueMessage(JText::_('COM_ISBNREGISTRY_ERROR_MESSAGE_NO_TEMPLATE_FOUND'), 'warning'); return false; } // Update template id $message->message_template_id = $template->id; // Set subject $message->subject = $template->subject; // Filter subject for title field $message->subject = $this->filterPublicationTitle($message->subject, $title); // Set message $message->message = $template->message; // Check if publication identifiers should be added to the message $addPublicationIdentifiers = ConfigurationHelper::addPublicationIdentifiers($code); // Variable that tells if publisher identifier has been added to the message $publisherIdentifierAddedd = false; // Load publisher identifier // Do we need to load ISBN or ISMN? $type = ConfigurationHelper::isIsbn($code) ? 'isbn' : 'ismn'; // Load model $publisherIdentifierRangeModel = JModelLegacy::getInstance('publisher' . $type . 'range', 'IsbnregistryModel'); // Load active publisher identifier range $publisherIdentifierRange = $publisherIdentifierRangeModel->getActivePublisherIdentifierRange($publisherId); // Check that we have a result if ($publisherIdentifierRange) { // Add publisher identifier to the template $message->message = $this->filterPublisherIdentifier($message->message, $publisherIdentifierRange->publisher_identifier); $publisherIdentifierAddedd = true; } else { if ($addPublicationIdentifiers) { // Publisher identifier range may have been closed after identifier // generation and publisher does not currently have active identifier. // Load identifier batch model so that we can use the identifier // that was used during identifier generation $identifierBatchModel = JModelLegacy::getInstance('identifierbatch', 'IsbnregistryModel'); // Load batch object $identifierBatch = $identifierBatchModel->getItem($identifierBatchId); // Check publisher identifier range id value if ($identifierBatch->publisher_identifier_range_id > 0) { // Get publisher identifier range object used in identifier batch $publisherIdentifierRange = $publisherIdentifierRangeModel->getItem($identifierBatch->publisher_identifier_range_id); // Check that we have a result if ($publisherIdentifierRange) { // Add publisher identifier to the template $message->message = $this->filterPublisherIdentifier($message->message, $publisherIdentifierRange->publisher_identifier); $publisherIdentifierAddedd = true; } } } else { JFactory::getApplication()->enqueueMessage(JText::_('COM_ISBNREGISTRY_ERROR_MESSAGE_NO_ACTIVE_PUBLISHER_IDENTIFIERS_FOUND'), 'warning'); } } // Load language file - translations are used // in "filterPublicationIdentifiers" function later JFactory::getLanguage()->load('com_isbnregistry_email', JPATH_ADMINISTRATOR, $message->lang_code, true); // Add identifiers if needed if ($addPublicationIdentifiers) { // Load identifier model $identifierModel = JModelLegacy::getInstance('identifier', 'IsbnregistryModel'); // Get identifiers $identifiers = $identifierModel->getIdentifiers($identifierBatchId); // Set batch id $message->batch_id = $identifierBatchId; // Get identifiers attachment limit $attachmentLimit = $params->get('identifiers_attachment_limit', 0); // If number of identifiers is greater than the limit, // identifiers must be sent as an attachment $useAttachment = $attachmentLimit > 0 && sizeof($identifiers) > $attachmentLimit ? true : false; // Set has attachment value $message->has_attachment = $useAttachment; // Add identifiers $message->message = $this->filterPublicationIdentifiers($message->message, $identifiers, $useAttachment, strtoupper($type)); // Check if publisher identifier has been added already if (!$publisherIdentifierAddedd) { // Get publisher identifier range object used in the first identifier $publisherIdentifierRange = $publisherIdentifierRangeModel->getItem($identifiers[0]->publisher_identifier_range_id); // Check that we have a result if ($publisherIdentifierRange) { // Add publisher identifier to the template $message->message = $this->filterPublisherIdentifier($message->message, $publisherIdentifierRange->publisher_identifier); $publisherIdentifierAddedd = true; } else { JFactory::getApplication()->enqueueMessage(JText::_('COM_ISBNREGISTRY_ERROR_MESSAGE_NO_PUBLISHER_IDENTIFIERS_FOUND'), 'warning'); } } } // Filter message $message->message = $this->filterMessage($message, $publisher, $title, $subtitle); // Operation was successfull return true; }