| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * @package CRM |
| 30 | * @copyright CiviCRM LLC (c) 2004-2017 |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * Recent items utility class. |
| 35 | */ |
| 36 | class CRM_Utils_Recent { |
| 37 | |
| 38 | /** |
| 39 | * Store name |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | const MAX_ITEMS = 30, STORE_NAME = 'CRM_Utils_Recent'; |
| 44 | |
| 45 | /** |
| 46 | * The list of recently viewed items. |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | static private $_recent = NULL; |
| 51 | |
| 52 | /** |
| 53 | * Maximum stack size |
| 54 | * @var int |
| 55 | */ |
| 56 | static private $_maxItems = 10; |
| 57 | |
| 58 | /** |
| 59 | * Initialize this class and set the static variables. |
| 60 | */ |
| 61 | public static function initialize() { |
| 62 | $maxItemsSetting = Civi::settings()->get('recentItemsMaxCount'); |
| 63 | if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self::MAX_ITEMS) { |
| 64 | self::$_maxItems = $maxItemsSetting; |
| 65 | } |
| 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 |
| 79 | * the recently viewed array |
| 80 | */ |
| 81 | public 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 |
| 90 | * The title to display. |
| 91 | * @param string $url |
| 92 | * The link for the above title. |
| 93 | * @param string $id |
| 94 | * Object id. |
| 95 | * @param $type |
| 96 | * @param int $contactId |
| 97 | * @param string $contactName |
| 98 | * @param array $others |
| 99 | */ |
| 100 | public static function add( |
| 101 | $title, |
| 102 | $url, |
| 103 | $id, |
| 104 | $type, |
| 105 | $contactId, |
| 106 | $contactName, |
| 107 | $others = array() |
| 108 | ) { |
| 109 | self::initialize(); |
| 110 | |
| 111 | if (!self::isProviderEnabled($type)) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $session = CRM_Core_Session::singleton(); |
| 116 | |
| 117 | // make sure item is not already present in list |
| 118 | for ($i = 0; $i < count(self::$_recent); $i++) { |
| 119 | if (self::$_recent[$i]['url'] == $url) { |
| 120 | // delete item from array |
| 121 | array_splice(self::$_recent, $i, 1); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (!is_array($others)) { |
| 127 | $others = array(); |
| 128 | } |
| 129 | |
| 130 | array_unshift(self::$_recent, |
| 131 | array( |
| 132 | 'title' => $title, |
| 133 | 'url' => $url, |
| 134 | 'id' => $id, |
| 135 | 'type' => $type, |
| 136 | 'contact_id' => $contactId, |
| 137 | 'contactName' => $contactName, |
| 138 | 'subtype' => CRM_Utils_Array::value('subtype', $others), |
| 139 | 'isDeleted' => CRM_Utils_Array::value('isDeleted', $others, FALSE), |
| 140 | 'image_url' => CRM_Utils_Array::value('imageUrl', $others), |
| 141 | 'edit_url' => CRM_Utils_Array::value('editUrl', $others), |
| 142 | 'delete_url' => CRM_Utils_Array::value('deleteUrl', $others), |
| 143 | ) |
| 144 | ); |
| 145 | |
| 146 | if (count(self::$_recent) > self::$_maxItems) { |
| 147 | array_pop(self::$_recent); |
| 148 | } |
| 149 | |
| 150 | CRM_Utils_Hook::recent(self::$_recent); |
| 151 | |
| 152 | $session->set(self::STORE_NAME, self::$_recent); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Delete an item from the recent stack. |
| 157 | * |
| 158 | * @param array $recentItem |
| 159 | * Array of the recent Item to be removed. |
| 160 | */ |
| 161 | public static function del($recentItem) { |
| 162 | self::initialize(); |
| 163 | $tempRecent = self::$_recent; |
| 164 | |
| 165 | self::$_recent = ''; |
| 166 | |
| 167 | // make sure item is not already present in list |
| 168 | for ($i = 0; $i < count($tempRecent); $i++) { |
| 169 | if (!($tempRecent[$i]['id'] == $recentItem['id'] && |
| 170 | $tempRecent[$i]['type'] == $recentItem['type'] |
| 171 | ) |
| 172 | ) { |
| 173 | self::$_recent[] = $tempRecent[$i]; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | $session = CRM_Core_Session::singleton(); |
| 178 | $session->set(self::STORE_NAME, self::$_recent); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Delete an item from the recent stack. |
| 183 | * |
| 184 | * @param string $id |
| 185 | * Contact id that had to be removed. |
| 186 | */ |
| 187 | public static function delContact($id) { |
| 188 | self::initialize(); |
| 189 | |
| 190 | $tempRecent = self::$_recent; |
| 191 | |
| 192 | self::$_recent = ''; |
| 193 | |
| 194 | // rebuild recent. |
| 195 | for ($i = 0; $i < count($tempRecent); $i++) { |
| 196 | // don't include deleted contact in recent. |
| 197 | if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) { |
| 198 | continue; |
| 199 | } |
| 200 | self::$_recent[] = $tempRecent[$i]; |
| 201 | } |
| 202 | |
| 203 | $session = CRM_Core_Session::singleton(); |
| 204 | $session->set(self::STORE_NAME, self::$_recent); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check if a provider is allowed to add stuff. |
| 209 | * If correspondig setting is empty, all are allowed |
| 210 | * |
| 211 | * @param string $providerName |
| 212 | */ |
| 213 | public static function isProviderEnabled($providerName) { |
| 214 | |
| 215 | // Join contact types to providerName 'Contact' |
| 216 | $contactTypes = CRM_Contact_BAO_ContactType::contactTypes(TRUE); |
| 217 | if (in_array($providerName, $contactTypes)) { |
| 218 | $providerName = 'Contact'; |
| 219 | } |
| 220 | $allowed = TRUE; |
| 221 | |
| 222 | // Use core setting recentItemsProviders if configured |
| 223 | $providersPermitted = Civi::settings()->get('recentItemsProviders'); |
| 224 | if ($providersPermitted) { |
| 225 | $allowed = in_array($providerName, $providersPermitted); |
| 226 | } |
| 227 | // Else allow |
| 228 | return $allowed; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Gets the list of available providers to civi's recent items stack |
| 233 | */ |
| 234 | public static function getProviders() { |
| 235 | $providers = array( |
| 236 | 'Contact' => ts('Contacts'), |
| 237 | 'Relationship' => ts('Relationships'), |
| 238 | 'Activity' => ts('Activities'), |
| 239 | 'Note' => ts('Notes'), |
| 240 | 'Group' => ts('Groups'), |
| 241 | 'Case' => ts('Cases'), |
| 242 | 'Contribution' => ts('Contributions'), |
| 243 | 'Participant' => ts('Participants'), |
| 244 | 'Grant' => ts('Grants'), |
| 245 | 'Membership' => ts('Memberships'), |
| 246 | 'Pledge' => ts('Pledges'), |
| 247 | 'Event' => ts('Events'), |
| 248 | 'Campaign' => ts('Campaigns'), |
| 249 | ); |
| 250 | |
| 251 | return $providers; |
| 252 | } |
| 253 | |
| 254 | } |