Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
TasksNotificationUtil.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  {
42  const TASK_NEW = 'TaskNew';
43 
44  const TASK_STATUS_BECOMES_AWAITING_ACCEPTANCE = 'TaskStatusBecomesAwaitingAcceptance';
45 
46  const TASK_STATUS_BECOMES_COMPLETED = 'TaskStatusBecomesCompleted';
47 
48  const TASK_STATUS_BECOMES_REJECTED = 'TaskStatusBecomesAccepted';
49 
50  const TASK_OWNER_CHANGE = 'TaskOwnerChange';
51 
52  const TASK_COMMENT_CREATED_OR_UPDATED = 'TaskCommentCreatedOrUpdated';
53 
63  public static function submitTaskNotificationMessage(Task $task, $action, User $relatedUser = null,
64  Comment $comment = null)
65  {
66  assert('is_string($action)');
67  $message = static::getNotificationMessageByAction($task, $action, $relatedUser, $comment);
68  $notificationRulesClassName = static::resolveNotificationRulesClassByAction($action);
69  $rule = new $notificationRulesClassName();
70  $peopleToSendNotification = static::resolvePeopleToSendNotification($task, $action, $relatedUser);
71  foreach ($peopleToSendNotification as $person)
72  {
73  $rule->addUser($person);
74  }
75  $rule->setModel($task);
76  $rule->setAllowDuplicates(true);
77  static::processTaskNotification($message, $rule, $action);
78  }
79 
86  protected static function processTaskNotification(NotificationMessage $message, TaskNotificationRules $rule, $action)
87  {
88  assert('is_string($action)');
89  $users = $rule->getUsers();
90  //This scenario would be there when there is only one subscriber. In that case users would
91  //be zero
92  if (count($users) == 0)
93  {
94  return;
95  }
96  $notifications = static::resolveAndGetNotifications($message, $rule);
97  if (static::resolveShouldSendEmailIfCritical())
98  {
99  foreach ($notifications as $notification)
100  {
101  static::sendTaskEmail($notification, $rule, $action);
102  }
103  }
104  }
105 
114  protected static function getNotificationMessageByAction(Task $task, $action, User $relatedUser = null,
115  Comment $comment = null)
116  {
117  assert('is_string($action)');
118  $message = new NotificationMessage();
119  $messageContent = static::getEmailMessageContent($task, $action, $relatedUser);
120  $messageContentSecondPart = static::getEmailMessageContentSecondPart($action, $comment);
121  $url = Yii::app()->createAbsoluteUrl('tasks/default/details/',
122  array('id' => $task->id));
123  $message->textContent = $messageContent;
124  if ($messageContentSecondPart != null)
125  {
126  $message->textContent .= "\n" . $messageContentSecondPart;
127  }
128  $message->textContent .= "\n" . ZurmoHtml::link(Zurmo::t('Core', 'Click Here'), $url,
129  array('target' => '_blank'));
130  $message->htmlContent = $messageContent;
131  if ($messageContentSecondPart != null)
132  {
133  $message->htmlContent .= "<br/>" . $messageContentSecondPart;
134  }
135  $message->htmlContent .= "<br/>" . ZurmoHtml::link(Zurmo::t('Core', 'Click Here'), $url,
136  array('target' => '_blank'));
137  return $message;
138  }
139 
147  public static function resolvePeopleToSendNotification(Task $task, $action, User $relatedUser = null)
148  {
149  assert('is_string($action)');
150  $peopleToSendNotification = array();
151  if ($action == self::TASK_NEW)
152  {
153  $peopleToSendNotification[] = $task->owner;
154  }
155  elseif (($action == self::TASK_STATUS_BECOMES_REJECTED) &&
156  (Yii::app()->user->userModel->id != $task->owner->id))
157  {
158  $peopleToSendNotification[] = $task->owner;
159  }
160  elseif (($action == self::TASK_STATUS_BECOMES_COMPLETED) &&
161  (Yii::app()->user->userModel->id != $task->owner->id))
162  {
163  $peopleToSendNotification[] = $task->owner;
164  }
165  elseif ($action == self::TASK_STATUS_BECOMES_AWAITING_ACCEPTANCE ||
166  $action == self::TASK_OWNER_CHANGE)
167  {
168  $peopleToSendNotification[] = $task->requestedByUser;
169  }
170  elseif ($action == self::TASK_COMMENT_CREATED_OR_UPDATED)
171  {
172  $peopleToSendNotification = NotificationSubscriberUtil::getModelSubscribers($task);
173  if ($relatedUser != null)
174  {
175  foreach ($peopleToSendNotification as $key => $person)
176  {
177  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
178  $user = $person->castDown(array($modelDerivationPathToItem));
179  if ($person->getClassId('Item') == $relatedUser->getClassId('Item'))
180  {
181  unset($peopleToSendNotification[$key]);
182  }
183  elseif (!CommentsNotificationUtil::hasPersonHaveRightsAndPermissionsToAccessModelAndIsUserActive($task, $user))
184  {
185  unset($peopleToSendNotification[$key]);
186  }
187  }
188  }
189  }
190  return $peopleToSendNotification;
191  }
192 
200  public static function getEmailMessageContent(Task $task, $action, User $relatedUser = null)
201  {
202  assert('is_string($action)');
203  if ($action == self::TASK_NEW)
204  {
205  return Zurmo::t('TasksModule', "The task, '{task}', is now owned by you.",
206  array('{task}' => strval($task)));
207  }
208  elseif ($action == self::TASK_STATUS_BECOMES_AWAITING_ACCEPTANCE)
209  {
210  return Zurmo::t('TasksModule', "The task you requested, '{task}', has been finished. You can now choose to accept or reject the task.",
211  array('{task}' => strval($task),
212  '{user}' => strval($relatedUser)));
213  }
214  elseif ($action == self::TASK_STATUS_BECOMES_COMPLETED)
215  {
216  return Zurmo::t('TasksModule', "The task, '{task}', was accepted by {user}.",
217  array('{task}' => strval($task),
218  '{user}' => strval($relatedUser)));
219  }
220  elseif ($action == self::TASK_STATUS_BECOMES_REJECTED)
221  {
222  return Zurmo::t('TasksModule', "The task, '{task}', has been rejected by {user}.",
223  array('{task}' => strval($task),
224  '{user}' => strval($relatedUser)));
225  }
226  elseif ($action == self::TASK_OWNER_CHANGE)
227  {
228  return Zurmo::t('TasksModule', "The task you requested, '{task}', has a new owner.",
229  array('{task}' => strval($task)));
230  }
231  elseif ($action == self::TASK_COMMENT_CREATED_OR_UPDATED)
232  {
233  return Zurmo::t('TasksModule', "{user} has commented on the task '{task}':",
234  array('{task}' => strval($task),
235  '{user}' => strval($relatedUser)));
236  }
237  }
238 
244  public static function getEmailMessageContentSecondPart($action, Comment $comment = null)
245  {
246  assert('is_string($action)');
247  if ($action == self::TASK_COMMENT_CREATED_OR_UPDATED)
248  {
249  return strval($comment);
250  }
251  }
252 
259  protected static function sendTaskEmail(Notification $notification, TaskNotificationRules $rule, $action)
260  {
261  assert('is_string($action)');
262  $notificationSettingName = static::resolveNotificationSettingNameFromType($rule->getType());
263  if ($notification->owner->primaryEmail->emailAddress !== null &&
264  UserNotificationUtil::isEnabledByUserAndNotificationNameAndType($notification->owner,
265  $notificationSettingName, 'email'))
266  {
267  $emailMessage = static::makeEmailMessage();
268  $emailMessage->subject = static::getEmailSubject($notification, $rule);
269  $emailMessage->content = static::makeEmailContent($notification);
270  $emailMessage->sender = static::makeSender();
271  $emailMessage->recipients->add(static::makeRecipient($notification));
272  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
273  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_DRAFT);
274  try
275  {
276  Yii::app()->emailHelper->send($emailMessage);
277  }
278  catch (CException $e)
279  {
280  //Not sure what to do yet when catching an exception here. Currently ignoring gracefully.
281  }
282  }
283  }
284 
289  protected static function resolveNotificationRulesClassByAction($action)
290  {
291  switch ($action)
292  {
293  case TasksNotificationUtil::TASK_NEW:
294  return 'NewTaskNotificationRules';
295  break;
296  case TasksNotificationUtil::TASK_STATUS_BECOMES_AWAITING_ACCEPTANCE:
297  return 'DeliveredTaskNotificationRules';
298  break;
299  case TasksNotificationUtil::TASK_STATUS_BECOMES_COMPLETED:
300  return 'AcceptedTaskNotificationRules';
301  break;
302  case TasksNotificationUtil::TASK_STATUS_BECOMES_REJECTED:
303  return 'RejectedTaskNotificationRules';
304  break;
305  case TasksNotificationUtil::TASK_OWNER_CHANGE:
306  return 'TaskOwnerChangeNotificationRules';
307  break;
308  case TasksNotificationUtil::TASK_COMMENT_CREATED_OR_UPDATED:
309  return 'TaskCommentNotificationRules';
310  break;
311  default:
312  throw new NotFoundException();
313  }
314  }
315  }
316 ?>
static sendTaskEmail(Notification $notification, TaskNotificationRules $rule, $action)
Definition: User.php:37
static resolvePeopleToSendNotification(Task $task, $action, User $relatedUser=null)
static processTaskNotification(NotificationMessage $message, TaskNotificationRules $rule, $action)
static getEmailMessageContent(Task $task, $action, User $relatedUser=null)
static resolveNotificationRulesClassByAction($action)
Definition: Task.php:37
static getNotificationMessageByAction(Task $task, $action, User $relatedUser=null, Comment $comment=null)
static getEmailMessageContentSecondPart($action, Comment $comment=null)
static getModelDerivationPathToItem($modelClassName)
Definition: RuntimeUtil.php:79
static submitTaskNotificationMessage(Task $task, $action, User $relatedUser=null, Comment $comment=null)
static getModelSubscribers(OwnedSecurableItem $model)
Generated on Thu Oct 22 2020 07:10:33
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.