public function processDueItem(OwnedModel $item)
 {
     assert('is_object($item)');
     $emailMessageId = null;
     $this->itemId = intval($item->id);
     $this->itemClass = get_class($item);
     assert('$this->itemClass === "AutoresponderItem" || $this->itemClass === "CampaignItem"');
     $contact = $this->resolveContact($item);
     $itemOwnerModel = $this->resolveItemOwnerModel($item);
     if ($itemOwnerModel->id < 0) {
         // the corresponding autoresponder/campaign has been deleted already.
         $item->delete();
         return false;
     }
     $this->personId = $contact->getClassId('Person');
     if ($this->skipMessage($contact, $itemOwnerModel)) {
         $this->createSkipActivity();
     } else {
         $marketingList = $itemOwnerModel->marketingList;
         assert('is_object($marketingList)');
         assert('get_class($marketingList) === "MarketingList"');
         $textContent = $itemOwnerModel->textContent;
         $htmlContent = null;
         if ($this->supportsRichText($itemOwnerModel)) {
             $htmlContent = $itemOwnerModel->htmlContent;
         }
         $invalidTags = array();
         MergeTagsContentResolverUtil::resolveContentsForGlobalFooterAndMergeTagsAndTracking($textContent, $htmlContent, $contact, $this->resolveEmailTemplateType(), $this->resolveErrorOnFirstMissingMergeTag(), $this->resolveLanguageForContent(), $invalidTags, intval($marketingList->id), false, MergeTagsContentResolverUtil::ADD_GLOBAL_FOOTER_MERGE_TAGS_IF_MISSING, (bool) $itemOwnerModel->enableTracking, $this->itemId, $this->itemClass, $this->personId);
         try {
             $item->emailMessage = $this->resolveEmailMessage($textContent, $htmlContent, $itemOwnerModel, $contact, $marketingList);
         } catch (MissingRecipientsForEmailMessageException $e) {
             $this->createSkipActivity();
         }
     }
     $marked = $this->markItemAsProcessedWithSQL($item->emailMessage->id);
     return $marked;
 }
 protected function resolveAndSendEmailMessageByPostSourceDataAndUserOrContact(array $emailData, Item $userOrContact)
 {
     $invalidTags = array();
     $emailMessage = new EmailMessage();
     $emailMessage->subject = $emailData['subject'];
     $emailMessage->folder = $emailData['folder'];
     $emailMessage->owner = $emailData['owner'];
     $emailMessage->sender = $emailData['sender'];
     MergeTagsContentResolverUtil::resolveContentsForGlobalFooterAndMergeTagsAndTracking($emailData['content']['textContent'], $emailData['content']['htmlContent'], $userOrContact, intval($emailData['type']), static::ERROR_ON_FIRST_MISSING_MERGE_TAG, $emailData['language'], $invalidTags, $emailData['marketingListId'], static::PREVIEW_MODE, static::ADD_GLOBAL_FOOTER_DEFAULT, (bool) $emailData['enableTracking'], 0, 'CampaignItem', 0);
     // sending these three just in the case where enableTracking is true
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = $emailData['content']['textContent'];
     if ($emailData['supportsRichText']) {
         $emailContent->htmlContent = $emailData['content']['htmlContent'];
     }
     $emailMessage->content = $emailContent;
     $emailMessage->recipients->add($emailData['recipient']);
     $this->resolveAttachmentsForEmailMessage($emailMessage, $emailData['attachments']);
     Yii::app()->emailHelper->sendImmediately($emailMessage);
     if ($emailMessage->save()) {
         if ($emailData['permissions']) {
             ExplicitReadWriteModelPermissionsUtil::resolveExplicitReadWriteModelPermissions($emailMessage, $emailData['permissions']);
         }
     } else {
         throw new FailedToSaveModelException("Unable to save EmailMessage");
     }
 }
 public function actionDetailsJson($id, $includeFilesInJson = false, $contactId = null)
 {
     $contactId = (int) $contactId;
     $emailTemplate = static::getModelAndCatchNotFoundAndDisplayError('EmailTemplate', intval($id));
     ControllerSecurityUtil::resolveAccessCanCurrentUserReadModel($emailTemplate);
     header('Content-type: application/json');
     if ($contactId != null) {
         $contact = Contact::getById($contactId);
         $textContent = $emailTemplate->textContent;
         $htmlContent = $emailTemplate->htmlContent;
         $invalidTags = array();
         MergeTagsContentResolverUtil::resolveContentsForGlobalFooterAndMergeTagsAndTracking($textContent, $htmlContent, $contact, intval($emailTemplate->type), MergeTagsToModelAttributesAdapter::ERROR_ON_FIRST_INVALID_TAG, $emailTemplate->language, $invalidTags, null, true, MergeTagsContentResolverUtil::REMOVE_GLOBAL_FOOTER_MERGE_TAGS_IF_PRESENT, false);
         $emailTemplate->setTreatCurrentUserAsOwnerForPermissions(true);
         $emailTemplate->textContent = stripslashes($textContent);
         $emailTemplate->htmlContent = stripslashes($htmlContent);
         $emailTemplate->setTreatCurrentUserAsOwnerForPermissions(false);
     }
     $emailTemplate = $this->resolveEmailTemplateAsJson($emailTemplate, $includeFilesInJson);
     echo $emailTemplate;
     Yii::app()->end(0, false);
 }