Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
AutoresponderAndCampaignItemsUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
43  {
44  protected $itemTableName = null;
45 
46  protected $itemClass = null;
47 
48  protected $itemId = null;
49 
50  protected $personId = null;
51 
52  protected $_instance = null;
53 
54  public function processDueItem(OwnedModel $item)
55  {
56  assert('is_object($item)');
57  $emailMessageId = null;
58  $this->itemId = intval($item->id);
59  $this->itemClass = get_class($item);
60  assert('$this->itemClass === "AutoresponderItem" || $this->itemClass === "CampaignItem"');
61  $contact = $this->resolveContact($item);
62  $itemOwnerModel = $this->resolveItemOwnerModel($item);
63  if ($itemOwnerModel->id < 0)
64  {
65  // the corresponding autoresponder/campaign has been deleted already.
66  $item->delete();
67  return false;
68  }
69  $this->personId = $contact->getClassId('Person');
70 
71  if ($this->skipMessage($contact, $itemOwnerModel))
72  {
73  $this->createSkipActivity();
74  }
75  else
76  {
77  $marketingList = $itemOwnerModel->marketingList;
78  assert('is_object($marketingList)');
79  assert('get_class($marketingList) === "MarketingList"');
80  $textContent = $itemOwnerModel->textContent;
81  $htmlContent = null;
82  if ($this->supportsRichText($itemOwnerModel))
83  {
84  $htmlContent = $itemOwnerModel->htmlContent;
85  }
86  $invalidTags = array();
88  $htmlContent, $contact, $this->resolveEmailTemplateType(),
89  $this->resolveErrorOnFirstMissingMergeTag(),
90  $this->resolveLanguageForContent(),
91  $invalidTags, intval($marketingList->id),
92  false, MergeTagsContentResolverUtil::ADD_GLOBAL_FOOTER_MERGE_TAGS_IF_MISSING,
93  (bool)$itemOwnerModel->enableTracking,
94  $this->itemId, $this->itemClass, $this->personId);
95  try
96  {
97  $item->emailMessage = $this->resolveEmailMessage($textContent, $htmlContent, $itemOwnerModel,
98  $contact, $marketingList);
99  }
101  {
102  $this->createSkipActivity();
103  }
104  }
105  $marked = $this->markItemAsProcessedWithSQL($item->emailMessage->id);
106  return $marked;
107  }
108 
109  protected function resolveContact(OwnedModel $item)
110  {
111  $contact = $item->contact;
112  if (empty($contact) || $contact->id < 0)
113  {
114  throw new NotFoundException();
115  }
116  return $contact;
117  }
118 
119  protected function resolveItemOwnerModel(OwnedModel $item)
120  {
121  $itemOwnerModel = $item->{$this->resolveItemOwnerModelRelationName()};
122  assert('is_object($itemOwnerModel)');
123  assert('get_class($itemOwnerModel) === "Autoresponder" || get_class($itemOwnerModel) === "Campaign"');
124  return $itemOwnerModel;
125  }
126 
127  public function skipMessage(Contact $contact, Item $itemOwnerModel)
128  {
129  // TODO: @Shoaibi: Critical0: We could use SQL for getByMarketingListIdContactIdandUnsubscribed to save further performance here.
130  if ($contact->primaryEmail->optOut)
131  {
132  return true;
133  }
134  $marketingListMember = MarketingListMember::getByMarketingListIdAndContactId(
135  $itemOwnerModel->marketingList->id,
136  $contact->id);
137  if ($marketingListMember === false)
138  {
139  return true;
140  }
141  else
142  {
143  $marketingListMemberObject = $marketingListMember[0];
144  $itemOwnerModelClassName = get_class($itemOwnerModel);
145  if ($itemOwnerModelClassName === "Campaign" && $marketingListMemberObject->unsubscribed == true)
146  {
147  return true;
148  }
149  elseif ($itemOwnerModelClassName === "Autoresponder")
150  {
151  if ($itemOwnerModel->operationType == Autoresponder::OPERATION_SUBSCRIBE &&
152  $marketingListMemberObject->unsubscribed == true)
153  {
154  return true;
155  }
156  elseif ($itemOwnerModel->operationType == Autoresponder::OPERATION_UNSUBSCRIBE &&
157  $marketingListMemberObject->unsubscribed == false)
158  {
159  return true;
160  }
161  }
162  }
163  return false;
164  }
165 
166  protected function supportsRichText(Item $itemOwnerModel)
167  {
168  return (($this->itemClass == 'CampaignItem' && $itemOwnerModel->supportsRichText) ||
169  ($this->itemClass == 'AutoresponderItem'));
170  }
171 
172  protected function createSkipActivity()
173  {
174  $activityClass = $this->itemClass . 'Activity';
175  $type = $activityClass::TYPE_SKIP;
176  $activityClass::createNewActivity($type, $this->itemId, $this->personId);
177  }
178 
179  protected function resolveLanguageForContent()
180  {
181  // TODO: @Shoaibi/@Jason: Low: we might add support for language
182  return null;
183  }
184 
185  protected function resolveEmailTemplateType()
186  {
187  return EmailTemplate::TYPE_CONTACT;
188  }
189 
190  protected function resolveErrorOnFirstMissingMergeTag()
191  {
192  return MergeTagsToModelAttributesAdapter::ERROR_ON_FIRST_INVALID_TAG;
193  }
194 
195  protected function resolveEmailMessage($textContent, $htmlContent, Item $itemOwnerModel,
196  Contact $contact, MarketingList $marketingList)
197  {
198  $emailMessage = $this->saveEmailMessage($textContent, $htmlContent, $itemOwnerModel,
199  $contact, $marketingList);
200  $this->sendEmailMessage($emailMessage);
201  $this->resolveExplicitPermissionsForEmailMessage($emailMessage, $marketingList);
202  return $emailMessage;
203  }
204 
205  protected function saveEmailMessage($textContent, $htmlContent, Item $itemOwnerModel,
206  Contact $contact, MarketingList $marketingList)
207  {
208  $folder = $this->resolveEmailFolder();
209  $emailMessage = AutoresponderAndCampaignItemsEmailMessageUtil::resolveAndSaveEmailMessage($textContent,
210  $htmlContent,
211  $itemOwnerModel,
212  $contact,
213  $marketingList,
214  $this->itemId,
215  $folder->id,
216  $this->itemClass,
217  $this->personId);
218  return $emailMessage;
219  }
220 
221  protected function sendEmailMessage(EmailMessage & $emailMessage)
222  {
223  Yii::app()->emailHelper->send($emailMessage, true, false);
224  }
225 
226  protected function resolveExplicitPermissionsForEmailMessage(EmailMessage & $emailMessage, MarketingList $marketingList)
227  {
228  $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($marketingList);
230  $explicitReadWriteModelPermissions);
231  }
232 
233  protected function markItemAsProcessedWithSQL($emailMessageId = null)
234  {
235  $className = $this->itemClass;
236  $itemTableName = $className::getTableName();
237  $sql = "UPDATE " . DatabaseCompatibilityUtil::quoteString($itemTableName);
238  $sql .= " SET " . DatabaseCompatibilityUtil::quoteString('processed') . ' = 1';
239  if ($emailMessageId)
240  {
241  $emailMessageForeignKey = RedBeanModel::getForeignKeyName($this->itemClass, 'emailMessage');
242  $sql .= ", " . DatabaseCompatibilityUtil::quoteString($emailMessageForeignKey);
243  $sql .= " = ${emailMessageId}";
244  }
245  $sql .= " WHERE " . DatabaseCompatibilityUtil::quoteString('id') . " = {$this->itemId};";
246  $effectedRows = ZurmoRedBean::exec($sql);
247  return ($effectedRows == 1);
248  }
249 
250  protected function resolveItemOwnerModelRelationName()
251  {
252  if ($this->itemClass == 'AutoresponderItem')
253  {
254  return 'autoresponder';
255  }
256  else
257  {
258  return 'campaign';
259  }
260  }
261 
262  protected function resolveEmailBoxName()
263  {
264  if ($this->itemClass == "AutoresponderItem")
265  {
266  return EmailBox::AUTORESPONDERS_NAME;
267  }
268  else
269  {
270  return EmailBox::CAMPAIGNS_NAME;
271  }
272  }
273 
274  protected function resolveEmailFolder()
275  {
276  $boxName = $this->resolveEmailBoxName();
277  $box = EmailBox::resolveAndGetByName($boxName);
278  $folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_DRAFT);
279  return $folder;
280  }
281  }
282 ?>
static makeBySecurableItem(SecurableItem $securableItem)
static resolveContentsForGlobalFooterAndMergeTagsAndTracking(&$textContent, &$htmlContent, RedBeanModel $attachedModel=null, $emailTemplateType=EmailTemplate::TYPE_CONTACT, $errorOnFirstMissing=MergeTagsToModelAttributesAdapter::ERROR_ON_FIRST_INVALID_TAG, $language=null, array &$invalidTags=array(), $marketingListId=null, $preview=false, $addGlobalFooterMergeTags=self::ADD_GLOBAL_FOOTER_MERGE_TAGS_IF_MISSING, $enableTracking=true, $modelId=null, $modelType=null, $personId=null)
static resolveExplicitReadWriteModelPermissions(SecurableItem $securableItem, ExplicitReadWriteModelPermissions $explicitReadWriteModelPermissions, $validate=false)
static getForeignKeyName($modelClassName, $relationName)
Definition: Item.php:37
Generated on Tue Jul 14 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.