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