Merge pull request #6559 from jitendrapurohit/CRM-16877
[civicrm-core.git] / CRM / Utils / Recent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33
34 /**
35 * Recent items utility class.
36 */
37 class CRM_Utils_Recent {
38
39 /**
40 * Max number of items in queue.
41 *
42 * @int
43 */
44 const MAX_ITEMS = 10, STORE_NAME = 'CRM_Utils_Recent';
45
46 /**
47 * The list of recently viewed items.
48 *
49 * @var array
50 */
51 static private $_recent = NULL;
52
53 /**
54 * Initialize this class and set the static variables.
55 */
56 public static function initialize() {
57 if (!self::$_recent) {
58 $session = CRM_Core_Session::singleton();
59 self::$_recent = $session->get(self::STORE_NAME);
60 if (!self::$_recent) {
61 self::$_recent = array();
62 }
63 }
64 }
65
66 /**
67 * Return the recently viewed array.
68 *
69 * @return array
70 * the recently viewed array
71 */
72 public static function &get() {
73 self::initialize();
74 return self::$_recent;
75 }
76
77 /**
78 * Add an item to the recent stack.
79 *
80 * @param string $title
81 * The title to display.
82 * @param string $url
83 * The link for the above title.
84 * @param string $id
85 * Object id.
86 * @param $type
87 * @param int $contactId
88 * @param string $contactName
89 * @param array $others
90 */
91 public static function add(
92 $title,
93 $url,
94 $id,
95 $type,
96 $contactId,
97 $contactName,
98 $others = array()
99 ) {
100 self::initialize();
101 $session = CRM_Core_Session::singleton();
102
103 // make sure item is not already present in list
104 for ($i = 0; $i < count(self::$_recent); $i++) {
105 if (self::$_recent[$i]['url'] == $url) {
106 // delete item from array
107 array_splice(self::$_recent, $i, 1);
108 break;
109 }
110 }
111
112 if (!is_array($others)) {
113 $others = array();
114 }
115
116 array_unshift(self::$_recent,
117 array(
118 'title' => $title,
119 'url' => $url,
120 'id' => $id,
121 'type' => $type,
122 'contact_id' => $contactId,
123 'contactName' => $contactName,
124 'subtype' => CRM_Utils_Array::value('subtype', $others),
125 'isDeleted' => CRM_Utils_Array::value('isDeleted', $others, FALSE),
126 'image_url' => CRM_Utils_Array::value('imageUrl', $others),
127 'edit_url' => CRM_Utils_Array::value('editUrl', $others),
128 'delete_url' => CRM_Utils_Array::value('deleteUrl', $others),
129 )
130 );
131 if (count(self::$_recent) > self::MAX_ITEMS) {
132 array_pop(self::$_recent);
133 }
134
135 CRM_Utils_Hook::recent(self::$_recent);
136
137 $session->set(self::STORE_NAME, self::$_recent);
138 }
139
140 /**
141 * Delete an item from the recent stack.
142 *
143 * @param array $recentItem
144 * Array of the recent Item to be removed.
145 */
146 public static function del($recentItem) {
147 self::initialize();
148 $tempRecent = self::$_recent;
149
150 self::$_recent = '';
151
152 // make sure item is not already present in list
153 for ($i = 0; $i < count($tempRecent); $i++) {
154 if (!($tempRecent[$i]['id'] == $recentItem['id'] &&
155 $tempRecent[$i]['type'] == $recentItem['type']
156 )
157 ) {
158 self::$_recent[] = $tempRecent[$i];
159 }
160 }
161
162 $session = CRM_Core_Session::singleton();
163 $session->set(self::STORE_NAME, self::$_recent);
164 }
165
166 /**
167 * Delete an item from the recent stack.
168 *
169 * @param string $id
170 * Contact id that had to be removed.
171 */
172 public static function delContact($id) {
173 self::initialize();
174
175 $tempRecent = self::$_recent;
176
177 self::$_recent = '';
178
179 // rebuild recent.
180 for ($i = 0; $i < count($tempRecent); $i++) {
181 // don't include deleted contact in recent.
182 if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) {
183 continue;
184 }
185 self::$_recent[] = $tempRecent[$i];
186 }
187
188 $session = CRM_Core_Session::singleton();
189 $session->set(self::STORE_NAME, self::$_recent);
190 }
191
192 }