Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-11-16-01-17-32
[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 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2015
31 */
32
33 /**
34 * Recent items utility class.
35 */
36 class CRM_Utils_Recent {
37
38 /**
39 * Max number of items in queue.
40 *
41 * @var int
42 */
43 const MAX_ITEMS = 10, STORE_NAME = 'CRM_Utils_Recent';
44
45 /**
46 * The list of recently viewed items.
47 *
48 * @var array
49 */
50 static private $_recent = NULL;
51
52 /**
53 * Initialize this class and set the static variables.
54 */
55 public static function initialize() {
56 if (!self::$_recent) {
57 $session = CRM_Core_Session::singleton();
58 self::$_recent = $session->get(self::STORE_NAME);
59 if (!self::$_recent) {
60 self::$_recent = array();
61 }
62 }
63 }
64
65 /**
66 * Return the recently viewed array.
67 *
68 * @return array
69 * the recently viewed array
70 */
71 public static function &get() {
72 self::initialize();
73 return self::$_recent;
74 }
75
76 /**
77 * Add an item to the recent stack.
78 *
79 * @param string $title
80 * The title to display.
81 * @param string $url
82 * The link for the above title.
83 * @param string $id
84 * Object id.
85 * @param $type
86 * @param int $contactId
87 * @param string $contactName
88 * @param array $others
89 */
90 public static function add(
91 $title,
92 $url,
93 $id,
94 $type,
95 $contactId,
96 $contactName,
97 $others = array()
98 ) {
99 self::initialize();
100 $session = CRM_Core_Session::singleton();
101
102 // make sure item is not already present in list
103 for ($i = 0; $i < count(self::$_recent); $i++) {
104 if (self::$_recent[$i]['url'] == $url) {
105 // delete item from array
106 array_splice(self::$_recent, $i, 1);
107 break;
108 }
109 }
110
111 if (!is_array($others)) {
112 $others = array();
113 }
114
115 array_unshift(self::$_recent,
116 array(
117 'title' => $title,
118 'url' => $url,
119 'id' => $id,
120 'type' => $type,
121 'contact_id' => $contactId,
122 'contactName' => $contactName,
123 'subtype' => CRM_Utils_Array::value('subtype', $others),
124 'isDeleted' => CRM_Utils_Array::value('isDeleted', $others, FALSE),
125 'image_url' => CRM_Utils_Array::value('imageUrl', $others),
126 'edit_url' => CRM_Utils_Array::value('editUrl', $others),
127 'delete_url' => CRM_Utils_Array::value('deleteUrl', $others),
128 )
129 );
130 if (count(self::$_recent) > self::MAX_ITEMS) {
131 array_pop(self::$_recent);
132 }
133
134 CRM_Utils_Hook::recent(self::$_recent);
135
136 $session->set(self::STORE_NAME, self::$_recent);
137 }
138
139 /**
140 * Delete an item from the recent stack.
141 *
142 * @param array $recentItem
143 * Array of the recent Item to be removed.
144 */
145 public static function del($recentItem) {
146 self::initialize();
147 $tempRecent = self::$_recent;
148
149 self::$_recent = '';
150
151 // make sure item is not already present in list
152 for ($i = 0; $i < count($tempRecent); $i++) {
153 if (!($tempRecent[$i]['id'] == $recentItem['id'] &&
154 $tempRecent[$i]['type'] == $recentItem['type']
155 )
156 ) {
157 self::$_recent[] = $tempRecent[$i];
158 }
159 }
160
161 $session = CRM_Core_Session::singleton();
162 $session->set(self::STORE_NAME, self::$_recent);
163 }
164
165 /**
166 * Delete an item from the recent stack.
167 *
168 * @param string $id
169 * Contact id that had to be removed.
170 */
171 public static function delContact($id) {
172 self::initialize();
173
174 $tempRecent = self::$_recent;
175
176 self::$_recent = '';
177
178 // rebuild recent.
179 for ($i = 0; $i < count($tempRecent); $i++) {
180 // don't include deleted contact in recent.
181 if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) {
182 continue;
183 }
184 self::$_recent[] = $tempRecent[$i];
185 }
186
187 $session = CRM_Core_Session::singleton();
188 $session->set(self::STORE_NAME, self::$_recent);
189 }
190
191 }