Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
b8c71ffa | 35 | * Recent items utility class. |
6a488035 TO |
36 | */ |
37 | class CRM_Utils_Recent { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Max number of items in queue. |
6a488035 TO |
41 | * |
42 | * @int | |
43 | */ | |
7da04cde | 44 | const MAX_ITEMS = 10, STORE_NAME = 'CRM_Utils_Recent'; |
6a488035 TO |
45 | |
46 | /** | |
fe482240 | 47 | * The list of recently viewed items. |
6a488035 TO |
48 | * |
49 | * @var array | |
6a488035 TO |
50 | */ |
51 | static private $_recent = NULL; | |
52 | ||
53 | /** | |
fe482240 | 54 | * Initialize this class and set the static variables. |
6a488035 | 55 | */ |
00be9182 | 56 | public static function initialize() { |
6a488035 TO |
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 | /** | |
fe482240 | 67 | * Return the recently viewed array. |
6a488035 | 68 | * |
a6c01b45 CW |
69 | * @return array |
70 | * the recently viewed array | |
6a488035 | 71 | */ |
00be9182 | 72 | public static function &get() { |
6a488035 TO |
73 | self::initialize(); |
74 | return self::$_recent; | |
75 | } | |
76 | ||
77 | /** | |
fe482240 | 78 | * Add an item to the recent stack. |
6a488035 | 79 | * |
77855840 TO |
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. | |
f4aaa82a | 86 | * @param $type |
100fef9d CW |
87 | * @param int $contactId |
88 | * @param string $contactName | |
f4aaa82a | 89 | * @param array $others |
6a488035 | 90 | */ |
608e6658 | 91 | public static function add( |
a3e55d9c | 92 | $title, |
6a488035 TO |
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 | /** | |
fe482240 | 141 | * Delete an item from the recent stack. |
6a488035 | 142 | * |
77855840 TO |
143 | * @param array $recentItem |
144 | * Array of the recent Item to be removed. | |
6a488035 | 145 | */ |
00be9182 | 146 | public static function del($recentItem) { |
6a488035 TO |
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'] && | |
353ffa53 TO |
155 | $tempRecent[$i]['type'] == $recentItem['type'] |
156 | ) | |
157 | ) { | |
6a488035 TO |
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 | /** | |
fe482240 | 167 | * Delete an item from the recent stack. |
6a488035 | 168 | * |
77855840 TO |
169 | * @param string $id |
170 | * Contact id that had to be removed. | |
6a488035 | 171 | */ |
00be9182 | 172 | public static function delContact($id) { |
6a488035 TO |
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 | } | |
96025800 | 191 | |
6a488035 | 192 | } |