Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
NotificationSubscriberUtil.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 getSubscriberData(OwnedSecurableItem $model)
48  {
49  $content = null;
50  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
51  $alreadySubscribedUsers = array();
52  foreach ($model->notificationSubscribers as $subscriber)
53  {
54  $user = $subscriber->person->castDown(array($modelDerivationPathToItem));
55  //Take care of duplicates if any
56  if (!in_array($user->id, $alreadySubscribedUsers))
57  {
58  $content .= static::renderSubscriberImageAndLinkContent($user);
59  $alreadySubscribedUsers[] = $user->id;
60  }
61  }
62  return $content;
63  }
64 
72  public static function renderSubscriberImageAndLinkContent(User $user, $imageSize = 36, $class = null)
73  {
74  assert('is_int($imageSize)');
75  assert('is_string($class) || $class === null');
76  $htmlOptions = array('title' => strval($user));
77  if ($class != null)
78  {
79  $htmlOptions['class'] = $class;
80  }
81  $userUrl = Yii::app()->createUrl('/users/default/details', array('id' => $user->id));
82  return ZurmoHtml::link($user->getAvatarImage($imageSize), $userUrl, $htmlOptions);
83  }
84 
90  public static function getModelSubscribers(OwnedSecurableItem $model)
91  {
92  $subscribers = array();
93  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
94  foreach ($model->notificationSubscribers as $subscriber)
95  {
96  $subscribers[] = $subscriber->person->castDown(array($modelDerivationPathToItem));
97  }
98  return $subscribers;
99  }
100 
108  public static function markUserHasReadLatest(OwnedSecurableItem $model, User $user, $hasReadLatest = true)
109  {
110  assert('$model->id > 0');
111  assert('$user->id > 0');
112  assert('is_bool($hasReadLatest)');
113  $save = false;
114  foreach ($model->notificationSubscribers as $position => $subscriber)
115  {
116  if ($subscriber->person->getClassId('Item') ==
117  $user->getClassId('Item') && $subscriber->hasReadLatest != $hasReadLatest)
118  {
119  $model->notificationSubscribers[$position]->hasReadLatest = $hasReadLatest;
120  $save = true;
121  }
122  }
123 
124  if ($save)
125  {
126  $model->save();
127  }
128  }
129 
135  public static function registerSubscriptionScript($modelClassName, $model = null)
136  {
137  $title = Zurmo::t('Core', 'Unsubscribe');
138  $unsubscribeLink = ZurmoHtml::tag('i', array('class' => 'icon-unsubscribe', 'title' => $title), '');
139 
140  $moduleClassName = $modelClassName::getModuleClassName();
141  $moduleId = $moduleClassName::getDirectoryName();
142 
143  if ($model == null && $modelClassName == 'Task')
144  {
145  $url = Yii::app()->createUrl($moduleId . '/default/addKanbanSubscriber');
146  $script = self::getKanbanSubscriptionScript($url, 'subscribe-model-link', 'unsubscribe-model-link', $unsubscribeLink);
147  Yii::app()->clientScript->registerScript('kanban-subscribe-task-link-script', $script);
148  }
149  else
150  {
151  $url = Yii::app()->createUrl($moduleId . '/default/addSubscriber', array('id' => $model->id));
152  $script = self::getDetailSubscriptionScript($url, 'detail-subscribe-model-link', 'detail-unsubscribe-model-link', $unsubscribeLink, $model->id);
153  Yii::app()->clientScript->registerScript('detail-subscribe-model-link-script', $script);
154  }
155  }
156 
162  public static function registerUnsubscriptionScript($modelClassName, $model = null)
163  {
164  $title = Zurmo::t('Core', 'Subscribe');
165  $subscribeLink = ZurmoHtml::tag('i', array('class' => 'icon-subscribe', 'title' => $title), '');
166 
167  $moduleClassName = $modelClassName::getModuleClassName();
168  $moduleId = $moduleClassName::getDirectoryName();
169 
170  if ($model == null && $modelClassName == 'Task')
171  {
172  $url = Yii::app()->createUrl($moduleId . '/default/removeKanbanSubscriber');
173  $script = self::getKanbanSubscriptionScript($url, 'unsubscribe-model-link', 'subscribe-model-link', $subscribeLink);
174  Yii::app()->clientScript->registerScript('kanban-unsubscribe-task-link-script', $script);
175  }
176  else
177  {
178  $url = Yii::app()->createUrl($moduleId . '/default/removeSubscriber', array('id' => $model->id));
179  $script = self::getDetailSubscriptionScript($url, 'detail-unsubscribe-model-link', 'detail-subscribe-model-link', $subscribeLink, $model->id);
180  Yii::app()->clientScript->registerScript('detail-unsubscribe-model-link-script', $script);
181  }
182  }
183 
192  public static function getKanbanSubscriptionScript($url, $sourceClass, $targetClass, $link)
193  {
194  // Begin Not Coding Standard
195  return "$('body').on('click', '." . $sourceClass . "', function()
196  {
197  var element = $(this).parent().parent().parent();
198  var id = $(element).attr('id');
199  var idParts = id.split('_');
200  var taskId = parseInt(idParts[1]);
201  var linkParent = $(this).parent();
202  console.log(linkParent);
203  $.ajax(
204  {
205  type : 'GET',
206  data : {'id':taskId},
207  url : '" . $url . "',
208  beforeSend : function(){
209  $('.ui-overlay-block').fadeIn(50);
210  $(this).makeLargeLoadingSpinner(true, '.ui-overlay-block');
211  },
212  success : function(data)
213  {
214  $(linkParent).html(data);
215  $(this).makeLargeLoadingSpinner(false, '.ui-overlay-block');
216  $('.ui-overlay-block').fadeOut(100);
217  }
218  }
219  );
220  }
221  );";
222  // End Not Coding Standard
223  }
224 
233  public static function getDetailSubscriptionScript($url, $sourceClass, $targetClass, $link, $taskId)
234  {
235  // Begin Not Coding Standard
236  return "$('body').on('click', '." . $sourceClass . "', function()
237  {
238  $.ajax(
239  {
240  type : 'GET',
241  url : '" . $url . "',
242  beforeSend : function(){
243  $('#subscriberList').html('');
244  $(this).makeLargeLoadingSpinner(true, '#subscriberList');
245  },
246  success : function(data)
247  {
248  $(this).html('" . $link . "');
249  $(this).attr('class', '" . $targetClass . "');
250  if (data == '')
251  {
252  $('#subscriberList').html('');
253  }
254  else
255  {
256  $('#subscriberList').html(data);
257  }
258  $(this).makeLargeLoadingSpinner(false, '#subscriberList');
259  }
260  }
261  );
262  }
263  );";
264  // End Not Coding Standard
265  }
266 
274  public static function getKanbanSubscriptionLink(OwnedSecurableItem $model, $row)
275  {
276  return self::resolveSubscriptionLink($model, 'subscribe-model-link', 'unsubscribe-model-link');
277  }
278 
285  public static function getDetailSubscriptionLink(OwnedSecurableItem $model, $row)
286  {
287  return self::resolveSubscriptionLink($model, 'detail-subscribe-model-link', 'detail-unsubscribe-model-link');
288  }
289 
297  public static function resolveSubscriptionLink(OwnedSecurableItem $model, $subscribeLinkClass, $unsubscribeLinkClass)
298  {
299  assert('is_string($subscribeLinkClass)');
300  assert('is_string($unsubscribeLinkClass)');
301  if ($model->owner->id == Yii::app()->user->userModel->id)
302  {
303  return null;
304  }
305  if ($model instanceof Task &&
306  $model->requestedByUser->id == Yii::app()->user->userModel->id)
307  {
308  return null;
309  }
310  if (static::doNotificationSubscribersContainPerson($model, Yii::app()->user->userModel) === false)
311  {
312  $label = Zurmo::t('Core', 'Subscribe');
313  $class = $subscribeLinkClass;
314  $iconContent = ZurmoHtml::tag('i', array('class' => 'icon-subscribe'), '');
315  }
316  else
317  {
318  $label = Zurmo::t('Core', 'Unsubscribe');
319  $class = $unsubscribeLinkClass;
320  $iconContent = ZurmoHtml::tag('i', array('class' => 'icon-unsubscribe'), '');
321  }
322  return ZurmoHtml::link($iconContent, '#', array('class' => $class, 'title' => $label)) ;
323  }
324 
331  public static function addSubscriber(User $user, OwnedSecurableItem $model, $hasReadLatest = false)
332  {
333  assert('is_bool($hasReadLatest)');
334  if (static::doNotificationSubscribersContainPerson($model, $user) === false)
335  {
336  $notificationSubscriber = new NotificationSubscriber();
337  $notificationSubscriber->person = $user;
338  $notificationSubscriber->hasReadLatest = $hasReadLatest;
339  $model->notificationSubscribers->add($notificationSubscriber);
340  }
341  }
342 
349  {
350  $content = null;
351  $subscribedUsers = static::getModelSubscribers($model);
352  foreach ($subscribedUsers as $user)
353  {
354  if ($user->isSame($model->owner))
355  {
356  $content .= NotificationSubscriberUtil::renderSubscriberImageAndLinkContent($user, 20, 'task-owner');
357  break;
358  }
359  }
360  //To take care of the case of duplicates
361  $addedSubscribers = array();
362  foreach ($subscribedUsers as $user)
363  {
364  if (!$user->isSame($model->owner))
365  {
366  if (!in_array($user->id, $addedSubscribers))
367  {
368  $content .= static::renderSubscriberImageAndLinkContent($user, 20);
369  $addedSubscribers[] = $user->id;
370  }
371  }
372  }
373  return $content;
374  }
375 
385  public static function processSubscriptionRequest(OwnedSecurableItem $model, User $user)
386  {
387  assert('$user instanceof User');
388  if (!static::doNotificationSubscribersContainPerson($model, $user))
389  {
390  $notificationSubscriber = new NotificationSubscriber();
391  $notificationSubscriber->person = $user;
392  $notificationSubscriber->hasReadLatest = false;
393  $model->notificationSubscribers->add($notificationSubscriber);
394  }
395  $model->save(false);
396  return $model;
397  }
398 
409  public static function processUnsubscriptionRequest(OwnedSecurableItem $model, User $user)
410  {
411  assert('$user instanceof User');
412  foreach ($model->notificationSubscribers as $notificationSubscriber)
413  {
414  if ($notificationSubscriber->person->getClassId('Item') == $user->getClassId('Item'))
415  {
416  $model->notificationSubscribers->remove($notificationSubscriber);
417  break;
418  }
419  }
420  $saved = $model->save(false);
421  if (!$saved)
422  {
423  throw new FailedToSaveModelException();
424  }
425  return $model;
426  }
427 
434  public static function doNotificationSubscribersContainPerson(OwnedSecurableItem $model, Item $item)
435  {
436  foreach ($model->notificationSubscribers as $notificationSubscriber)
437  {
438  if ($notificationSubscriber->person->getClassId('Item') == $item->getClassId('Item'))
439  {
440  return true;
441  }
442  }
443  return false;
444  }
445  }
446 ?>
static getDetailSubscriptionScript($url, $sourceClass, $targetClass, $link, $taskId)
static renderSubscriberImageAndLinkContent(User $user, $imageSize=36, $class=null)
static addSubscriber(User $user, OwnedSecurableItem $model, $hasReadLatest=false)
Definition: User.php:37
getClassId($modelClassName)
static processUnsubscriptionRequest(OwnedSecurableItem $model, User $user)
static getDetailSubscriptionLink(OwnedSecurableItem $model, $row)
Definition: Task.php:37
static markUserHasReadLatest(OwnedSecurableItem $model, User $user, $hasReadLatest=true)
static resolveAndRenderTaskCardDetailsSubscribersContent(OwnedSecurableItem $model)
static processSubscriptionRequest(OwnedSecurableItem $model, User $user)
static registerUnsubscriptionScript($modelClassName, $model=null)
static resolveSubscriptionLink(OwnedSecurableItem $model, $subscribeLinkClass, $unsubscribeLinkClass)
Definition: Item.php:37
static getModelDerivationPathToItem($modelClassName)
Definition: RuntimeUtil.php:79
static doNotificationSubscribersContainPerson(OwnedSecurableItem $model, Item $item)
static registerSubscriptionScript($modelClassName, $model=null)
static getKanbanSubscriptionScript($url, $sourceClass, $targetClass, $link)
static getKanbanSubscriptionLink(OwnedSecurableItem $model, $row)
static getSubscriberData(OwnedSecurableItem $model)
static getModelSubscribers(OwnedSecurableItem $model)
Generated on Sat Dec 5 2020 07:10:44
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.