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