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