Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
39de6fd5 | 5 | | CiviCRM version 4.6 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * | |
39 | */ | |
40 | class CRM_Utils_Recent { | |
41 | ||
42 | /** | |
100fef9d | 43 | * Max number of items in queue |
6a488035 TO |
44 | * |
45 | * @int | |
46 | */ | |
7da04cde | 47 | const MAX_ITEMS = 10, STORE_NAME = 'CRM_Utils_Recent'; |
6a488035 TO |
48 | |
49 | /** | |
50 | * The list of recently viewed items | |
51 | * | |
52 | * @var array | |
53 | * @static | |
54 | */ | |
55 | static private $_recent = NULL; | |
56 | ||
57 | /** | |
100fef9d | 58 | * Initialize this class and set the static variables |
6a488035 TO |
59 | * |
60 | * @return void | |
6a488035 TO |
61 | * @static |
62 | */ | |
00be9182 | 63 | public static function initialize() { |
6a488035 TO |
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 | /** | |
100fef9d | 74 | * Return the recently viewed array |
6a488035 | 75 | * |
a6c01b45 CW |
76 | * @return array |
77 | * the recently viewed array | |
6a488035 TO |
78 | * @static |
79 | */ | |
00be9182 | 80 | public static function &get() { |
6a488035 TO |
81 | self::initialize(); |
82 | return self::$_recent; | |
83 | } | |
84 | ||
85 | /** | |
100fef9d | 86 | * Add an item to the recent stack |
6a488035 | 87 | * |
77855840 TO |
88 | * @param string $title |
89 | * The title to display. | |
90 | * @param string $url | |
91 | * The link for the above title. | |
92 | * @param string $id | |
93 | * Object id. | |
f4aaa82a | 94 | * @param $type |
100fef9d CW |
95 | * @param int $contactId |
96 | * @param string $contactName | |
f4aaa82a EM |
97 | * @param array $others |
98 | * | |
6a488035 | 99 | * @return void |
6a488035 TO |
100 | * @static |
101 | */ | |
a3e55d9c TO |
102 | static function add( |
103 | $title, | |
6a488035 TO |
104 | $url, |
105 | $id, | |
106 | $type, | |
107 | $contactId, | |
108 | $contactName, | |
109 | $others = array() | |
110 | ) { | |
111 | self::initialize(); | |
112 | $session = CRM_Core_Session::singleton(); | |
113 | ||
114 | // make sure item is not already present in list | |
115 | for ($i = 0; $i < count(self::$_recent); $i++) { | |
116 | if (self::$_recent[$i]['url'] == $url) { | |
117 | // delete item from array | |
118 | array_splice(self::$_recent, $i, 1); | |
119 | break; | |
120 | } | |
121 | } | |
122 | ||
123 | if (!is_array($others)) { | |
124 | $others = array(); | |
125 | } | |
126 | ||
127 | array_unshift(self::$_recent, | |
128 | array( | |
129 | 'title' => $title, | |
130 | 'url' => $url, | |
131 | 'id' => $id, | |
132 | 'type' => $type, | |
133 | 'contact_id' => $contactId, | |
134 | 'contactName' => $contactName, | |
135 | 'subtype' => CRM_Utils_Array::value('subtype', $others), | |
136 | 'isDeleted' => CRM_Utils_Array::value('isDeleted', $others, FALSE), | |
137 | 'image_url' => CRM_Utils_Array::value('imageUrl', $others), | |
138 | 'edit_url' => CRM_Utils_Array::value('editUrl', $others), | |
139 | 'delete_url' => CRM_Utils_Array::value('deleteUrl', $others), | |
140 | ) | |
141 | ); | |
142 | if (count(self::$_recent) > self::MAX_ITEMS) { | |
143 | array_pop(self::$_recent); | |
144 | } | |
145 | ||
146 | CRM_Utils_Hook::recent(self::$_recent); | |
147 | ||
148 | $session->set(self::STORE_NAME, self::$_recent); | |
149 | } | |
150 | ||
151 | /** | |
100fef9d | 152 | * Delete an item from the recent stack |
6a488035 | 153 | * |
77855840 TO |
154 | * @param array $recentItem |
155 | * Array of the recent Item to be removed. | |
6a488035 TO |
156 | * |
157 | * @return void | |
6a488035 TO |
158 | * @static |
159 | */ | |
00be9182 | 160 | public static function del($recentItem) { |
6a488035 TO |
161 | self::initialize(); |
162 | $tempRecent = self::$_recent; | |
163 | ||
164 | self::$_recent = ''; | |
165 | ||
166 | // make sure item is not already present in list | |
167 | for ($i = 0; $i < count($tempRecent); $i++) { | |
168 | if (!($tempRecent[$i]['id'] == $recentItem['id'] && | |
169 | $tempRecent[$i]['type'] == $recentItem['type'] | |
170 | )) { | |
171 | self::$_recent[] = $tempRecent[$i]; | |
172 | } | |
173 | } | |
174 | ||
175 | $session = CRM_Core_Session::singleton(); | |
176 | $session->set(self::STORE_NAME, self::$_recent); | |
177 | } | |
178 | ||
179 | /** | |
100fef9d | 180 | * Delete an item from the recent stack |
6a488035 | 181 | * |
77855840 TO |
182 | * @param string $id |
183 | * Contact id that had to be removed. | |
6a488035 TO |
184 | * |
185 | * @return void | |
6a488035 TO |
186 | * @static |
187 | */ | |
00be9182 | 188 | public static function delContact($id) { |
6a488035 TO |
189 | self::initialize(); |
190 | ||
191 | $tempRecent = self::$_recent; | |
192 | ||
193 | self::$_recent = ''; | |
194 | ||
195 | // rebuild recent. | |
196 | for ($i = 0; $i < count($tempRecent); $i++) { | |
197 | // don't include deleted contact in recent. | |
198 | if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) { | |
199 | continue; | |
200 | } | |
201 | self::$_recent[] = $tempRecent[$i]; | |
202 | } | |
203 | ||
204 | $session = CRM_Core_Session::singleton(); | |
205 | $session->set(self::STORE_NAME, self::$_recent); | |
206 | } | |
207 | } |