Merge pull request #3580 from monishdeb/CRM-14701
[civicrm-core.git] / CRM / Utils / Recent.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
232624b1 5 | CiviCRM version 4.4 |
6a488035
TO
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36
37/**
38 *
39 */
40class CRM_Utils_Recent {
41
42 /**
43 * max number of items in queue
44 *
45 * @int
46 */
47 CONST MAX_ITEMS = 10, STORE_NAME = 'CRM_Utils_Recent';
48
49 /**
50 * The list of recently viewed items
51 *
52 * @var array
53 * @static
54 */
55 static private $_recent = NULL;
56
57 /**
58 * initialize this class and set the static variables
59 *
60 * @return void
61 * @access public
62 * @static
63 */
64 static function initialize() {
65 if (!self::$_recent) {
66 $session = CRM_Core_Session::singleton();
67 self::$_recent = $session->get(self::STORE_NAME);
68 if (!self::$_recent) {
69 self::$_recent = array();
70 }
71 }
72 }
73
74 /**
75 * return the recently viewed array
76 *
77 * @return array the recently viewed array
78 * @access public
79 * @static
80 */
81 static function &get() {
82 self::initialize();
83 return self::$_recent;
84 }
85
86 /**
87 * add an item to the recent stack
88 *
89 * @param string $title the title to display
90 * @param string $url the link for the above title
91 * @param string $icon a link to a graphical image
92 * @param string $id object id
93 *
94 * @return void
95 * @access public
96 * @static
97 */
98 static function add($title,
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 /**
147 * delete an item from the recent stack
148 *
149 * @param array $recentItem array of the recent Item to be removed
150 *
151 * @return void
152 * @access public
153 * @static
154 */
155 static function del($recentItem) {
156 self::initialize();
157 $tempRecent = self::$_recent;
158
159 self::$_recent = '';
160
161 // make sure item is not already present in list
162 for ($i = 0; $i < count($tempRecent); $i++) {
163 if (!($tempRecent[$i]['id'] == $recentItem['id'] &&
164 $tempRecent[$i]['type'] == $recentItem['type']
165 )) {
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 /**
175 * delete an item from the recent stack
176 *
177 * @param string $id contact id that had to be removed
178 *
179 * @return void
180 * @access public
181 * @static
182 */
183 static function delContact($id) {
184 self::initialize();
185
186 $tempRecent = self::$_recent;
187
188 self::$_recent = '';
189
190 // rebuild recent.
191 for ($i = 0; $i < count($tempRecent); $i++) {
192 // don't include deleted contact in recent.
193 if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) {
194 continue;
195 }
196 self::$_recent[] = $tempRecent[$i];
197 }
198
199 $session = CRM_Core_Session::singleton();
200 $session->set(self::STORE_NAME, self::$_recent);
201 }
202}
203