All Data Structures Functions Variables Pages
AuditEventsRecentlyViewedUtil.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 RECENTLY_VIEWED_COUNT = 10;
43 
51  public static function getRecentlyViewedAjaxContentByUser(User $user, $count)
52  {
53  assert('is_int($count)');
54  $content = null;
55  $recentlyViewedData = self::getRecentlyViewedByUser($user, $count);
56  if (count($recentlyViewedData) > 0)
57  {
58  foreach ($recentlyViewedData as $recentlyViewed)
59  {
60  $moduleClassName = $recentlyViewed[0];
61  $modelId = $recentlyViewed[1];
62  $modelName = $recentlyViewed[2];
63  $linkHtmlOptions = array('style' => 'text-decoration:underline;');
64  $content .= ZurmoHtml::link($modelName,
65  self::getRouteByRecentlyViewed($moduleClassName, $modelId), $linkHtmlOptions);
66  $content .= '&#160;-&#160;<span style="font-size:75%">';
67  $content .= $moduleClassName::getModuleLabelByTypeAndLanguage('Singular') . '</span><br/>';
68  }
69  }
70  else
71  {
72  $content .= Zurmo::t('ZurmoModule', 'There are no recently viewed items.');
73  }
74  return $content;
75  }
76 
84  public static function getRecentlyViewedItemsByUser(User $user, $count)
85  {
86  assert('is_int($count)');
87  $itemLinkPrefix = ZurmoHtml::tag('i', array(), '');
88  $recentlyViewedItems = array();
89  $recentlyViewedData = self::getRecentlyViewedByUser($user, $count);
90  if (count($recentlyViewedData) > 0)
91  {
92  foreach ($recentlyViewedData as $recentlyViewed)
93  {
94  $recentlyViewedItem = array();
95  $moduleClassName = $recentlyViewed[0];
96  $modelId = $recentlyViewed[1];
97  $modelName = self::stripTagsAndMultipleByteEncode($recentlyViewed[2]);
98  $recentlyViewedItem['link'] = ZurmoHtml::link(
99  $itemLinkPrefix . ZurmoHtml::tag('span', array(), $modelName),
100  self::getRouteByRecentlyViewed($moduleClassName, $modelId));
101  $recentlyViewedItem['moduleClassName'] = $moduleClassName;
102  $recentlyViewedItems[] = $recentlyViewedItem;
103  }
104  }
105  return $recentlyViewedItems;
106  }
107 
114  protected static function getRouteByAuditEvent(AuditEvent $auditEvent, $moduleClassName)
115  {
116  assert('is_string($moduleClassName)');
117  return Yii::app()->createUrl($moduleClassName::getDirectoryName() . '/default/details/',
118  array('id' => $auditEvent->modelId));
119  }
120 
127  protected static function getRecentlyViewedByUser(User $user, $count)
128  {
129  assert('is_int($count)');
130  $recentlyViewed = unserialize(ZurmoConfigurationUtil::getByUserAndModuleName($user, 'ZurmoModule', 'recentlyViewed'));
131  if (!is_array($recentlyViewed))
132  {
133  return array();
134  }
135  return array_slice($recentlyViewed, 0, $count);
136  }
137 
144  protected static function getRouteByRecentlyViewed($moduleClassName, $modelId)
145  {
146  assert('is_string($moduleClassName)');
147  assert('$modelId > 0');
148  return Yii::app()->createUrl($moduleClassName::getDirectoryName() . '/default/details/',
149  array('id' => $modelId));
150  }
151 
157  public static function resolveNewRecentlyViewedModel($moduleName, RedBeanModel $model, $count)
158  {
159  assert('strlen($moduleName) > 0 && is_int($model->id)');
160  $newItem = array($moduleName, $model->id, strval($model));
161  $recentlyViewed = unserialize(ZurmoConfigurationUtil::
162  getForCurrentUserByModuleName('ZurmoModule', 'recentlyViewed'));
163  if (!is_array($recentlyViewed))
164  {
165  $recentlyViewed = array();
166  }
167  $inRecentlyKey = null;
168  foreach ($recentlyViewed as $key => $item)
169  {
170  if ($item[1] == $newItem[1] && $item[0] == $newItem[0])
171  {
172  $inRecentlyKey = $key;
173  }
174  }
175  if (isset($inRecentlyKey))
176  {
177  unset($recentlyViewed[$inRecentlyKey]);
178  array_keys($recentlyViewed);
179  }
180  if (array_unshift($recentlyViewed, $newItem) > $count)
181  {
182  array_pop($recentlyViewed);
183  }
185  setForCurrentUserByModuleName('ZurmoModule', 'recentlyViewed', serialize($recentlyViewed));
186  }
187 
192  public static function deleteModelFromRecentlyViewed($moduleName, RedBeanModel $model)
193  {
194  if (!isset($model) || !isset($moduleName))
195  {
196  return;
197  }
198  $newItem = array($moduleName, $model->id, strval($model));
199  $recentlyViewed = unserialize(ZurmoConfigurationUtil::
200  getForCurrentUserByModuleName('ZurmoModule', 'recentlyViewed'));
201  if (!is_array($recentlyViewed))
202  {
203  return;
204  }
205  if (in_array($newItem, $recentlyViewed))
206  {
207  $key = array_search($newItem, $recentlyViewed);
208  unset($recentlyViewed[$key]);
209  array_keys($recentlyViewed);
210  }
212  setForCurrentUserByModuleName('ZurmoModule', 'recentlyViewed', serialize($recentlyViewed));
213  }
214 
215  protected static function stripTagsAndMultipleByteEncode($moduleName)
216  {
217  if ($moduleName == null)
218  {
219  return;
220  }
221  $modelName = strip_tags($moduleName);
222  $modelName = mb_convert_encoding(StringUtil::getChoppedStringContent($modelName, 40, ''), 'HTML-ENTITIES', 'UTF-8');
223  return $modelName;
224  }
225  }
226 ?>
static getRouteByAuditEvent(AuditEvent $auditEvent, $moduleClassName)
static getChoppedStringContent($string, $length, $ellipsis= '...')
Definition: StringUtil.php:51
Definition: User.php:37
static getRecentlyViewedByUser(User $user, $count)
static getRecentlyViewedAjaxContentByUser(User $user, $count)
static getRouteByRecentlyViewed($moduleClassName, $modelId)
static getRecentlyViewedItemsByUser(User $user, $count)
static getByUserAndModuleName($user, $moduleName, $key, $cache=true)
static setForCurrentUserByModuleName($moduleName, $key, $value, $cache=true)
static resolveNewRecentlyViewedModel($moduleName, RedBeanModel $model, $count)
static deleteModelFromRecentlyViewed($moduleName, RedBeanModel $model)
Generated on Thu May 28 2020 07:10:36