All Data Structures Functions Variables Pages
ConversationParticipantsUtil.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  {
47  public static function isCurrentUserAParticipant(Conversation $model)
48  {
49  try
50  {
51  return self::isUserAParticipant($model, Yii::app()->user->userModel);
52  }
54  {
55  return false;
56  }
57  }
58 
65  public static function isUserAParticipant(Conversation $model, User $user)
66  {
67  if ($model->conversationParticipants->count() > 0)
68  {
69  foreach ($model->conversationParticipants as $participant)
70  {
71  if ($participant->person->getClassId('Item') == $user->getClassId('Item'))
72  {
73  return true;
74  }
75  }
76  }
77  return false;
78  }
79 
89  Conversation $conversation, $postData, $explicitReadWriteModelPermissions)
90  {
91  assert('$explicitReadWriteModelPermissions instanceof ExplicitReadWriteModelPermissions');
92  $newPeopleIndexedByItemId = array();
93  if (isset($postData['itemIds']) && strlen($postData['itemIds']) > 0)
94  {
95  $itemIds = explode(",", $postData['itemIds']); // Not Coding Standard
96  foreach ($itemIds as $itemId)
97  {
98  if ($itemId != $conversation->owner->getClassId('Item'))
99  {
100  $newPeopleIndexedByItemId[$itemId] = static::castDownItem(Item::getById((int)$itemId));
101  }
102  }
103  if ($conversation->conversationParticipants->count() > 0)
104  {
105  $participantsToRemove = array();
106  foreach ($conversation->conversationParticipants as $index => $existingParticipantModel)
107  {
108  if (!isset($newPeopleIndexedByItemId[$existingParticipantModel->person->getClassId('Item')]))
109  {
110  $participantsToRemove[] = $existingParticipantModel;
111  }
112  else
113  {
114  unset($newPeopleIndexedByItemId[$existingParticipantModel->person->getClassId('Item')]);
115  }
116  }
117  foreach ($participantsToRemove as $participantModelToRemove)
118  {
119  $conversation->conversationParticipants->remove($participantModelToRemove);
120  $person = static::castDownItem($participantModelToRemove->person);
121  if ($person instanceof Permitable)
122  {
123  $explicitReadWriteModelPermissions->addReadWritePermitableToRemove($person);
124  }
125  }
126  }
127  //Now add missing participants
128  foreach ($newPeopleIndexedByItemId as $personOrUserModel)
129  {
130  $conversation->conversationParticipants->add(static::makeConversationParticipantByPerson($personOrUserModel));
131  if ($personOrUserModel instanceof Permitable)
132  {
133  $explicitReadWriteModelPermissions->addReadWritePermitable($personOrUserModel);
134  }
135  }
136  }
137  else
138  {
139  //remove all participants
140  $conversation->conversationParticipants->removeAll();
141  $explicitReadWriteModelPermissions->removeAllReadWritePermitables();
142  }
143  return $newPeopleIndexedByItemId;
144  }
145 
150  public static function resolveEmailInvitesByPeople($conversation, $people)
151  {
152  assert('$conversation instanceof Conversation && $conversation->id > 0');
153  if (count($people) == 0)
154  {
155  return;
156  }
158  $message = new NotificationMessage();
159  $message->textContent = static::getParticipantInviteEmailTextContent($conversation);
160  $message->htmlContent = static::getParticipantInviteEmailHtmlContent($conversation);
161  foreach ($people as $personOrUserModel)
162  {
163  if ($personOrUserModel instanceof User)
164  {
165  $rules->addUser($personOrUserModel);
166  }
167  else
168  {
169  static::sendEmailInviteToParticipant($conversation, $personOrUserModel);
170  }
171  }
172  NotificationsUtil::submit($message, $rules);
173  }
174 
175  protected static function castDownItem(Item $item)
176  {
177  foreach (array('Contact', 'User') as $modelClassName)
178  {
179  try
180  {
181  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem($modelClassName);
182  return $item->castDown(array($modelDerivationPathToItem));
183  }
184  catch (NotFoundException $e)
185  {
186  }
187  }
188  throw new NotSupportedException();
189  }
190 
191  protected static function makeConversationParticipantByPerson($personOrUserModel)
192  {
193  assert('$personOrUserModel instanceof User || $personOrUserModel instanceof Person');
194  $conversationParticipant = new ConversationParticipant();
195  $conversationParticipant->hasReadLatest = false;
196  $conversationParticipant->person = $personOrUserModel;
197  return $conversationParticipant;
198  }
199 
204  public static function sendEmailInviteToParticipant(Conversation $conversation, $person)
205  {
206  assert('$conversation->id > 0');
207  assert('$person instanceof User || $person instanceof Contact');
208  if ($person->primaryEmail->emailAddress !== null)
209  {
210  $userToSendMessagesFrom = $conversation->owner;
211  $emailMessage = new EmailMessage();
212  $emailMessage->owner = Yii::app()->user->userModel;
213  $emailMessage->subject = Zurmo::t('ConversationsModule', 'You have been invited to participate in a conversation');
214  $emailContent = new EmailMessageContent();
215  $emailContent->textContent = EmailNotificationUtil::
217  static::getParticipantInviteEmailTextContent($conversation));
218  $emailContent->htmlContent = EmailNotificationUtil::
220  static::getParticipantInviteEmailHtmlContent($conversation));
221  $emailMessage->content = $emailContent;
222  $sender = new EmailMessageSender();
223  $sender->fromAddress = Yii::app()->emailHelper->resolveFromAddressByUser($userToSendMessagesFrom);
224  $sender->fromName = strval($userToSendMessagesFrom);
225  $sender->personsOrAccounts->add($userToSendMessagesFrom);
226  $emailMessage->sender = $sender;
227  $recipient = new EmailMessageRecipient();
228  $recipient->toAddress = $person->primaryEmail->emailAddress;
229  $recipient->toName = strval($person);
230  $recipient->type = EmailMessageRecipient::TYPE_TO;
231  $recipient->personsOrAccounts->add($person);
232  $emailMessage->recipients->add($recipient);
233  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
234  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_DRAFT);
235  try
236  {
237  Yii::app()->emailHelper->send($emailMessage);
238  }
239  catch (CException $e)
240  {
241  //Not sure what to do yet when catching an exception here. Currently ignoring gracefully.
242  }
243  }
244  }
245 
246  protected static function getParticipantInviteEmailTextContent(Conversation $conversation)
247  {
248  $url = ShortUrlUtil::createShortUrl(static::getUrlToConversationDetailAndRelationsView($conversation->id));
249  $content = Zurmo::t('ConversationsModule', '{headerStartTag}Join the Conversation{headerEndTag}{headerLineBreak}{ownerName} ' .
250  'would like you to join a conversation {strongStartTag}"{conversationSubject}"{strongEndTag}',
251  array('{headerStartTag}' => null,
252  '{headerEndTag}' => null,
253  '{headerLineBreak}' => "\n\n",
254  '{strongStartTag}' => null,
255  '{strongEndTag}' => null,
256  '{ownerName}' => $conversation->owner,
257  '{conversationSubject}' => $conversation->subject));
258  $content .= "\n\n";
259  $content .= $url;
260  return $content;
261  }
262 
263  protected static function getParticipantInviteEmailHtmlContent(Conversation $conversation)
264  {
265  $url = static::getUrlToConversationDetailAndRelationsView($conversation->id);
266  $content = Zurmo::t('ConversationsModule', '{headerStartTag}Join the Conversation{headerEndTag}{headerLineBreak}{ownerName} ' .
267  'would like you to join a conversation {strongStartTag}"{conversationSubject}"{strongEndTag}',
268  array('{headerStartTag}' => '<h2 class="h2">',
269  '{headerEndTag}' => '</h2>',
270  '{headerLineBreak}' => null,
271  '{strongStartTag}' => '<strong>',
272  '{strongEndTag}' => '</strong>',
273  '{ownerName}' => $conversation->owner,
274  '{conversationSubject}' => $conversation->subject));
275  $content .= "<br/>";
276  $content .= ZurmoHtml::link(Zurmo::t('Core', 'Click Here'), $url, array('target' => '_blank'));
277  return $content;
278  }
279 
284  public static function getUrlToConversationDetailAndRelationsView($id)
285  {
286  assert('is_int($id)');
287  return Yii::app()->createAbsoluteUrl('conversations/default/details/', array('id' => $id));
288  }
289 
294  public static function getConversationParticipants(Conversation $conversation)
295  {
296  $participants = array();
297  foreach ($conversation->conversationParticipants as $participant)
298  {
299  $participants[] = static::castDownItem($participant->person);
300  }
301  return $participants;
302  }
303  }
304 ?>
static resolveNotificationTextTemplate($bodyContent, User $user=null, $isTestNotification=null)
Definition: User.php:37
getClassId($modelClassName)
static sendEmailInviteToParticipant(Conversation $conversation, $person)
static getConversationParticipants(Conversation $conversation)
static resolveConversationHasManyParticipantsFromPost(Conversation $conversation, $postData, $explicitReadWriteModelPermissions)
static isUserAParticipant(Conversation $model, User $user)
static resolveNotificationHtmlTemplate($bodyContent, User $user=null, $isTestNotification=null)
Definition: Item.php:37
static getModelDerivationPathToItem($modelClassName)
Definition: RuntimeUtil.php:79
static getById($id, $modelClassName=null)
static isCurrentUserAParticipant(Conversation $model)
castDown(array $derivedModelClassNames)
static resolveEmailInvitesByPeople($conversation, $people)
static submit(NotificationMessage $message, NotificationRules $rules)
Generated on Thu Apr 9 2020 07:10:40