All Data Structures Functions Variables Pages
CommentsUtil.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 
37  Yii::import('ext.markdown.Parsedown');
38 
43  {
49  public static function sendNotificationOnCommentCreateOrUpdate(RedBeanModel $relatedModel, Comment $comment, $peopleToSendNotification)
50  {
51  if (count($peopleToSendNotification) > 0)
52  {
53  $notificationRulesClassName = static::resolveNotificationRulesClassByModel($relatedModel);
54  $rules = new $notificationRulesClassName();
55  foreach ($peopleToSendNotification as $people)
56  {
57  $rules->addUser($people);
58  $rules->setModel($relatedModel);
59  }
60  $notificationMessage = static::createNotificationMessage($relatedModel, $comment);
61  NotificationsUtil::submit($notificationMessage, $rules);
62  }
63  else
64  {
65  return;
66  }
67  }
68 
73  public static function getUrlToEmail($model)
74  {
75  if ($model instanceof Conversation)
76  {
78  }
79  elseif ($model instanceof Mission)
80  {
81  return Yii::app()->createAbsoluteUrl('missions/default/details/', array('id' => $model->id));
82  }
83  elseif ($model instanceof Task)
84  {
85  return Yii::app()->createAbsoluteUrl('tasks/default/details/', array('id' => $model->id));
86  }
87  }
88 
95  protected static function createNotificationMessage($model, Comment $comment)
96  {
97  $notificationMessage = new NotificationMessage();
98  $url = static::getUrlToEmail($model);
99  $shortUrl = ShortUrlUtil::createShortUrl($url);
100 
101  $commentDescription = strval($comment);
102 
103  $textContent = Zurmo::t('CommentsModule', 'Hello, {lineBreak} {updaterName} added a new comment to the ' .
104  '{strongStartTag}{modelName}{strongEndTag}: {lineBreak}' .
105  '"{commentDescription}." {lineBreak}{lineBreak} {url} ',
106  array('{lineBreak}' => "\n",
107  '{strongStartTag}' => null,
108  '{strongEndTag}' => null,
109  '{updaterName}' => strval($comment->createdByUser),
110  '{modelName}' => $model->getModelLabelByTypeAndLanguage(
111  'SingularLowerCase'),
112  '{commentDescription}' => self::resolveMarkdownTagsAndConvertToHtml(strval($comment), 'text'),
113  '{url}' => $shortUrl
114  ));
115  $notificationMessage->textContent = $textContent;
116  $htmlContent = Zurmo::t('CommentsModule', 'Hello, {lineBreak} {updaterName} added a new comment to the ' .
117  '{strongStartTag}{url}{strongEndTag}: {lineBreak}' .
118  '"{commentDescription}."',
119  array('{lineBreak}' => "<br/>",
120  '{strongStartTag}' => '<strong>',
121  '{strongEndTag}' => '</strong>',
122  '{updaterName}' => strval($comment->createdByUser),
123  '{commentDescription}' => self::resolveMarkdownTagsAndConvertToHtml(strval($comment), 'html'),
124  '{url}' => ZurmoHtml::link($model->getModelLabelByTypeAndLanguage(
125  'SingularLowerCase'), $url, array('target' => '_blank'))
126  ));
127  $notificationMessage->htmlContent = DataUtil::purifyHtml($htmlContent);
128  return $notificationMessage;
129  }
130 
137  public static function getMentionedUsersForNotification(Comment $comment)
138  {
139  $mentionedUsers = array();
140  preg_match_all("/\[\~(.+?)\]/is", $comment->description, $matches); // Not Coding Standard
141  if (is_array($matches[1]) && !empty($matches[1]))
142  {
143  foreach ($matches[1] as $mentionedUsername)
144  {
145  if ($mentionedUsername == '') continue;
146  try
147  {
148  $user = User::getByUsername(trim(strtolower($mentionedUsername)));
149  // DO not send notification if user mentioned himself
150  if (Yii::app()->user->userModel->id != $user->id)
151  {
152  $mentionedUsers[] = $user;
153  }
154  }
155  catch (NotFoundException $e)
156  {
157  // Just skip this user
158  }
159  }
160  }
161  return $mentionedUsers;
162  }
163 
170  public static function replaceMentionedUsernamesWithFullNamesAndLinksInComments($commentDescription)
171  {
172  preg_match_all("/\[\~(.+?)\]/is", $commentDescription, $matches); // Not Coding Standard
173  if (is_array($matches[1]) && !empty($matches[1]))
174  {
175  foreach ($matches[1] as $mentionedUsername)
176  {
177  if ($mentionedUsername == '') continue;
178  try
179  {
180  $user = User::getByUsername(trim(strtolower($mentionedUsername)));
181  $link = Yii::app()->createAbsoluteUrl('users/default/details/', array('id' => $user->id));
182  $link = ZurmoHtml::link(strval($user), $link, array('target' => '_blank'));
183  $commentDescription = str_replace('[~' . $mentionedUsername . ']', $link, $commentDescription);
184  }
185  catch (NotFoundException $e)
186  {
187  // Just skip replacing this string with user
188  }
189  }
190  }
191  return $commentDescription;
192  }
193 
194  public static function resolveMarkdownTagsAndConvertToHtml($description, $type)
195  {
196  $parsedown = new Parsedown();
197  $text = $parsedown->text($description);
198  if ($type == 'html')
199  {
201  }
202  else
203  {
205  $breaks = array("<br />","<br>","<br/>", "</p>"); // Not Coding Standard
206  $text = str_ireplace($breaks, "\r\n", $text);
207  $text = strip_tags($text);
208  }
209  return $text;
210  }
211 
217  {
218  return 'enable' . get_class($model) . 'CommentNotification';
219  }
220 
225  protected static function resolveNotificationRulesClassByModel(RedBeanModel $model)
226  {
227  return get_class($model) . 'CommentNotificationRules';
228  }
229 
238  public static function hasUserHaveAccessToEditOrDeleteComment(Comment $comment, User $user)
239  {
240  $group = Group::getByName(Group::SUPER_ADMINISTRATORS_GROUP_NAME);
241  if ($comment->createdByUser->id == $user->id ||
242  $group->users->contains($user))
243  {
244  return true;
245  }
246  return false;
247  }
248  }
249 ?>
static getMentionedUsersForNotification(Comment $comment)
static getByUsername($username)
Definition: User.php:49
Definition: User.php:37
static getUrlToEmail($model)
static resolveNotificationRulesClassByModel(RedBeanModel $model)
Definition: Task.php:37
static replaceMentionedUsernamesWithFullNamesAndLinksInComments($commentDescription)
static resolveOnNewCommentNotificationSettingNameByModel(RedBeanModel $model)
static getByName($name)
Definition: Group.php:57
static sendNotificationOnCommentCreateOrUpdate(RedBeanModel $relatedModel, Comment $comment, $peopleToSendNotification)
static hasUserHaveAccessToEditOrDeleteComment(Comment $comment, User $user)
static purifyHtml($text)
Definition: DataUtil.php:165
static createNotificationMessage($model, Comment $comment)
static submit(NotificationMessage $message, NotificationRules $rules)
Generated on Tue May 26 2020 07:10:29