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