Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
NotificationsUtil.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 
41  {
48  public static function submit(NotificationMessage $message, NotificationRules $rules)
49  {
50  $users = $rules->getUsers();
51  if (count($users) == 0)
52  {
53  throw new NotSupportedException();
54  }
55  static::processNotification($message, $rules);
56  }
57 
63  public static function getRecentAjaxContentByUser(User $user, $count)
64  {
65  assert('is_int($count)');
66  $content = null;
67  $notification = new Notification(false);
68  $searchAttributes = array(
69  'owner' => array('id' => Yii::app()->user->userModel->id)
70  );
71  $metadataAdapter = new SearchDataProviderMetadataAdapter(
72  $notification,
73  Yii::app()->user->userModel->id,
74  $searchAttributes
75  );
77  $metadataAdapter->getAdaptedMetadata(),
78  'Notification',
79  'RedBeanModelDataProvider',
80  'createdDateTime',
81  true,
82  10
83  );
84  $notifications = $dataProvider->getData();
85  if (count($notifications) > 0)
86  {
87  foreach ($notifications as $notification)
88  {
89  $content .= '<div class="single-notification">';
90  $content .= self::renderShortenedListViewContent($notification);
91  $content .= ZurmoHtml::link("Delete<span class='icon'></span>", "#",
92  array("class" => "remove",
93  "onclick" => "deleteNotificationFromAjaxListView(this, " . $notification->id . ", event)"));
94  $content .= '</div>';
95  }
96  }
97  else
98  {
99  $content .= '<div class="single-notification">' . Zurmo::t('NotificationsModule', 'There are no recent notifications.') . '</div>';
100  }
101  return $content;
102  }
103 
108  public static function renderShortenedListViewContent(Notification $notification)
109  {
110  $content = strval($notification);
111  if ($content != null)
112  {
113  $content = '<h4>' . StringUtil::getChoppedStringContent($content, 68) . '</h4>';
114  }
115  if ($notification->notificationMessage->id > 0)
116  {
117  if ($notification->notificationMessage->htmlContent != null && strlen($notification->notificationMessage->htmlContent) < 136)
118  {
119  $content .= '<div>' . Yii::app()->format->raw($notification->notificationMessage->htmlContent). '</div>';
120  }
121  elseif ($notification->notificationMessage->textContent != null)
122  {
123  $content .= '<div>' . Yii::app()->format->text(StringUtil::
124  getChoppedStringContent($notification->notificationMessage->textContent, 136)) .
125  '</div>';
126  }
127  }
128  return $content;
129  }
130 
135  public static function renderListViewContent(Notification $notification)
136  {
137  $content = strval($notification);
138  if ($content != null)
139  {
140  $content = '<b>' . $content . '</b>';
141  }
142  if ($notification->notificationMessage->id > 0)
143  {
144  if ($notification->notificationMessage->htmlContent != null)
145  {
146  $content .= ZurmoHtml::wrapLabel(Yii::app()->format->
147  raw($notification->notificationMessage->htmlContent),
148  "last-comment");
149  }
150  elseif ($notification->notificationMessage->textContent != null)
151  {
152  $content .= ZurmoHtml::wrapLabel(Yii::app()->format->
153  text($notification->notificationMessage->textContent),
154  "last-comment");
155  }
156  }
157  $content .= ZurmoHtml::tag('span', array('class' => 'list-item-details'),
158  DateTimeUtil::getTimeSinceDisplayContent($notification->createdDateTime));
159  return $content;
160  }
161 
162  protected static function getEmailSubject(Notification $notification, NotificationRules $rules)
163  {
164  try
165  {
166  $subject = $rules->getSubjectForEmailNotification();
167  }
168  catch (NotImplementedException $exception)
169  {
170  $subject = '';
171  }
172  if (!$subject)
173  {
174  $subject = strval($notification);
175  }
176  return $subject;
177  }
178 
179  protected static function processNotification(NotificationMessage $message, NotificationRules $rules)
180  {
181  $notifications = static::resolveAndGetNotifications($message, $rules);
182  if (!$rules->allowSendingEmail())
183  {
184  return;
185  }
186  if (static::resolveShouldSendEmailIfCritical() && $rules->isCritical())
187  {
188  $sendImmediately = true;
189  }
190  else
191  {
192  $sendImmediately = false;
193  }
194  foreach ($notifications as $notification)
195  {
196  static::sendEmail($notification, $sendImmediately, $rules);
197  }
198  }
199 
200  protected static function resolveShouldSendEmailIfCritical()
201  {
202  return true;
203  }
204 
205  protected static function sendEmail(Notification $notification, $sendImmediately, NotificationRules $rules)
206  {
207  $notificationSettingName = static::resolveNotificationSettingNameFromType($notification->type);
208  if ($rules->allowSendingEmail() &&
209  UserNotificationUtil::
210  isEnabledByUserAndNotificationNameAndType($notification->owner, $notificationSettingName, 'email'))
211  {
212  if ($notification->owner->primaryEmail->emailAddress != null)
213  {
214  $emailMessage = static::makeEmailMessage();
215  $emailMessage->subject = static::getEmailSubject($notification, $rules);
216  $emailMessage->content = static::makeEmailContent($notification);
217  $emailMessage->sender = static::makeSender();
218  $emailMessage->recipients->add(static::makeRecipient($notification));
219  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
220  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_DRAFT);
221  if (!$emailMessage->save())
222  {
223  throw new FailedToSaveModelException();
224  }
225  try
226  {
227  if ($sendImmediately)
228  {
229  Yii::app()->emailHelper->sendImmediately($emailMessage);
230  }
231  else
232  {
233  Yii::app()->emailHelper->send($emailMessage);
234  }
235  }
236  catch (CException $e)
237  {
238  //Not sure what to do yet when catching an exception here. Currently ignoring gracefully.
239  }
240  }
241  }
242  }
243 
251  protected static function resolveAndGetNotifications(NotificationMessage $message, NotificationRules $rules)
252  {
253  $notifications = array();
254  foreach ($rules->getUsers() as $user)
255  {
256  //todo: !!!process duplication check
257  if ($rules->allowDuplicates() || Notification::getCountByTypeAndUser($rules->getType(), $user) == 0)
258  {
259  $notification = new Notification();
260  $notification->owner = $user;
261  $notification->type = $rules->getType();
262  $notification->notificationMessage = $message;
263  $notificationSettingName = static::resolveNotificationSettingNameFromType($rules->getType());
264  if (static::resolveToSaveNotification() &&
265  UserNotificationUtil::isEnabledByUserAndNotificationNameAndType($user, $notificationSettingName, 'inbox'))
266  {
267  $saved = $notification->save();
268  if (!$saved)
269  {
270  throw new NotSupportedException();
271  }
272  }
273  $notifications[] = $notification;
274  }
275  }
276  return $notifications;
277  }
278 
283  protected static function resolveToSaveNotification()
284  {
285  return true;
286  }
287 
293  public static function resolveNotificationSettingNameFromType($type)
294  {
295  assert('is_string($type) && $type != ""');
296  return 'enable'.$type.'Notification'; // Not Coding Standard
297  }
298 
302  protected static function makeEmailMessage()
303  {
304  $emailMessage = new EmailMessage();
305  $emailMessage->owner = Yii::app()->user->userModel;
306  return $emailMessage;
307  }
308 
312  protected static function makeSender()
313  {
314  $userToSendMessagesFrom = BaseControlUserConfigUtil::getUserToRunAs();
315  $sender = new EmailMessageSender();
316  $sender->fromAddress = Yii::app()->emailHelper->resolveFromAddressByUser($userToSendMessagesFrom);
317  $sender->fromName = Yii::app()->emailHelper->resolveFromNameForSystemUser($userToSendMessagesFrom);
318  return $sender;
319  }
320 
325  protected static function makeRecipient(Notification $notification)
326  {
327  $recipient = new EmailMessageRecipient();
328  $recipient->toAddress = $notification->owner->primaryEmail->emailAddress;
329  $recipient->toName = strval($notification->owner);
330  $recipient->type = EmailMessageRecipient::TYPE_TO;
331  $recipient->personsOrAccounts->add($notification->owner);
332  return $recipient;
333  }
334 
339  protected static function makeEmailContent(Notification $notification)
340  {
341  $emailContent = new EmailMessageContent();
342  if (is_string($notification->notificationMessage->textContent))
343  {
344  $emailContent->textContent = EmailNotificationUtil::
346  $notification->notificationMessage->textContent, $notification->owner);
347  }
348  if (is_string($notification->notificationMessage->htmlContent))
349  {
350  $emailContent->htmlContent = EmailNotificationUtil::
352  $notification->notificationMessage->htmlContent, $notification->owner);
353  }
354  return $emailContent;
355  }
356  }
357 ?>
static wrapLabel($label, $class= 'z-label')
Definition: ZurmoHtml.php:656
static resolveNotificationTextTemplate($bodyContent, User $user=null, $isTestNotification=null)
static getChoppedStringContent($string, $length, $ellipsis= '...')
Definition: StringUtil.php:51
static getUserToRunAs($createIfMissing=true)
Definition: User.php:37
static getRecentAjaxContentByUser(User $user, $count)
static getTimeSinceDisplayContent($dateTime, $durationDisplayFormat=false)
static renderListViewContent(Notification $notification)
static getCountByTypeAndUser($type, User $user)
static resolveToSaveNotification()
static makeEmailContent(Notification $notification)
static makeRecipient(Notification $notification)
static resolveAndGetNotifications(NotificationMessage $message, NotificationRules $rules)
static resolveNotificationHtmlTemplate($bodyContent, User $user=null, $isTestNotification=null)
static resolveNotificationSettingNameFromType($type)
static renderShortenedListViewContent(Notification $notification)
static submit(NotificationMessage $message, NotificationRules $rules)
static makeDataProvider(array $metadata, $listModelClassName, $dataProviderClassName, $sortAttribute, $sortDescending, $pageSize, $stateMetadataAdapterClassName=null)
Generated on Thu Oct 22 2020 07:10:31
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.