3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Class contains Contact dashboard related functions
39 class CRM_Core_BAO_Dashboard
extends CRM_Core_DAO_Dashboard
{
42 * Get the list of dashlets enabled by admin
44 * @param boolean $all all or only active
45 * @param boolean $checkPermission all or only authorized for the current user
47 * @return array $widgets array of dashlets
51 static function getDashlets($all = TRUE, $checkPermission = TRUE) {
53 $dao = new CRM_Core_DAO_Dashboard();
59 $dao->domain_id
= CRM_Core_Config
::domainID();
62 while ($dao->fetch()) {
63 if ($checkPermission && !self
::checkPermission($dao->permission
, $dao->permission_operator
)) {
68 CRM_Core_DAO
::storeValues($dao, $values);
69 $dashlets[$dao->id
] = $values;
76 * Function to get the list of dashlets for a contact
78 * Initializes the dashboard with defaults if this is the user's first visit to their dashboard
80 * @param boolean $flatFormat this is true if you want simple associated array of contact dashlets
82 * @param null $contactID
84 * @return array $dashlets array of dashlets
88 static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
92 $contactID = CRM_Core_Session
::singleton()->get('userID');
95 // get contact dashboard dashlets
97 $dao = new CRM_Contact_DAO_DashboardContact();
98 $dao->contact_id
= $contactID;
99 $dao->orderBy('column_no asc, weight asc');
101 while ($dao->fetch()) {
104 if ($dao->is_active
) {
105 // append weight so that order is preserved.
106 $dashlets[$dao->column_no
]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized
;
110 $dashlets[$dao->dashboard_id
] = $dao->dashboard_id
;
118 // If empty then initialize contact dashboard for this user
119 $defaultDashlet = self
::initializeDashlets($hasDashlets);
120 return $defaultDashlet ?
$defaultDashlet : $dashlets;
124 * @param $hasDashlets
127 * @throws CiviCRM_API3_Exception
129 static function initializeDashlets($hasDashlets) {
130 $getDashlets = civicrm_api3("Dashboard", "get", array('domain_id' => CRM_Core_Config
::domainID()));
131 $contactID = CRM_Core_Session
::singleton()->get('userID');
133 $allDashlets = CRM_Utils_Array
::index(array('name'), $getDashlets['values']);
134 $defaultDashlets = array();
135 if (!$hasDashlets && !empty($allDashlets['blog'])) {
136 $defaultDashlets['blog'] = array(
137 'dashboard_id' => $allDashlets['blog']['id'],
140 'contact_id' => $contactID,
141 'domain_id' => CRM_Core_Config
::domainID(),
144 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
145 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
146 foreach ($defaultDashlets as $defaultDashlet) {
147 if (!self
::checkPermission($getDashlets['values'][$defaultDashlet['dashboard_id']]['permission'],
148 $getDashlets['values'][$defaultDashlet['dashboard_id']]['permission_operator'])) {
149 unset($defaultDashlets[$defaultDashlet]);
153 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
154 $dashlets[$defaultDashlet['dashboard_id']] = $defaultDashlet['dashboard_id'];
164 * Function to check dashlet permission for current user
169 * @internal param \permission $string string
171 * @return boolean true if use has permission else false
173 static function checkPermission($permission, $operator) {
175 $permissions = explode(',', $permission);
176 $config = CRM_Core_Config
::singleton();
178 static $allComponents;
179 if (!$allComponents) {
180 $allComponents = CRM_Core_Component
::getNames();
183 $hasPermission = FALSE;
184 foreach ($permissions as $key) {
187 $componentName = NULL;
188 if (strpos($key, 'access') === 0) {
189 $componentName = trim(substr($key, 6));
190 if (!in_array($componentName, $allComponents)) {
191 $componentName = NULL;
195 // hack to handle case permissions
196 if (!$componentName && in_array($key, array(
197 'access my cases and activities', 'access all cases and activities'))) {
198 $componentName = 'CiviCase';
201 //hack to determine if it's a component related permission
202 if ($componentName) {
203 if (!in_array($componentName, $config->enableComponents
) ||
204 !CRM_Core_Permission
::check($key)
206 $showDashlet = FALSE;
207 if ($operator == 'AND') {
212 $hasPermission = TRUE;
215 elseif (!CRM_Core_Permission
::check($key)) {
216 $showDashlet = FALSE;
217 if ($operator == 'AND') {
222 $hasPermission = TRUE;
226 if (!$showDashlet && !$hasPermission) {
234 // if permission is not set consider everyone has permission to access it.
240 * Function to get details of each dashlets
242 * @param int $dashletID widget ID
244 * @return array associted array title and content
248 static function getDashletInfo($dashletID) {
249 $dashletInfo = array();
251 $params = array(1 => array($dashletID, 'Integer'));
252 $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
253 $dashboadDAO = CRM_Core_DAO
::executeQuery($query, $params);
254 $dashboadDAO->fetch();
257 $dao = new CRM_Contact_DAO_DashboardContact();
259 $session = CRM_Core_Session
::singleton();
260 $dao->contact_id
= $session->get('userID');
261 $dao->dashboard_id
= $dashletID;
264 //reset content based on the cache time set in config
265 $createdDate = strtotime($dao->created_date
);
266 $dateDiff = round(abs(time() - $createdDate) / 60);
268 $config = CRM_Core_Config
::singleton();
269 if ($config->dashboardCacheTimeout
<= $dateDiff) {
270 $dao->content
= NULL;
273 // if content is empty and url is set, retrieve it from url
274 if (!$dao->content
&& $dashboadDAO->url
) {
275 $url = $dashboadDAO->url
;
278 // -lets use relative url for internal use.
279 // -make sure relative url should not be htmlize.
280 if (substr($dashboadDAO->url
, 0, 4) != 'http') {
281 $urlParam = CRM_Utils_System
::explode('&', $dashboadDAO->url
, 2);
282 $url = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
285 //get content from url
286 $dao->content
= CRM_Utils_System
::getServerResponse($url);
287 $dao->created_date
= date("YmdHis");
291 $dashletInfo = array(
292 'title' => $dashboadDAO->label
,
293 'name' => $dashboadDAO->name
,
294 'content' => $dao->content
,
297 if ($dashboadDAO->is_fullscreen
) {
298 $fullscreenUrl = $dashboadDAO->fullscreen_url
;
299 if (substr($fullscreenUrl, 0, 4) != 'http') {
300 $urlParam = CRM_Utils_System
::explode('&', $dashboadDAO->fullscreen_url
, 2);
301 $fullscreenUrl = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
303 $dashletInfo['fullscreenUrl'] = $fullscreenUrl;
309 * Function to save changes made by use to the Dashlet
311 * @param array $columns associated array
313 * @param null $contactID
315 * @throws RuntimeException
320 static function saveDashletChanges($columns, $contactID=NULL) {
321 $session = CRM_Core_Session
::singleton();
323 $contactID = $session->get('userID');
326 if (empty($contactID)) {
327 throw new RuntimeException("Failed to determine contact ID");
330 //we need to get existing dashlets, so we know when to update or insert
331 $contactDashlets = self
::getContactDashlets(TRUE, $contactID);
333 $dashletIDs = array();
334 if (is_array($columns)) {
335 foreach ($columns as $colNo => $dashlets) {
336 if (!is_integer($colNo)) {
340 foreach ($dashlets as $dashletID => $isMinimized) {
341 $isMinimized = (int) $isMinimized;
342 if (in_array($dashletID, $contactDashlets)) {
343 $query = " UPDATE civicrm_dashboard_contact
344 SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1
345 WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} ";
348 $query = " INSERT INTO civicrm_dashboard_contact
349 ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id )
350 VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )";
352 // fire update query for each column
353 $dao = CRM_Core_DAO
::executeQuery($query);
355 $dashletIDs[] = $dashletID;
361 if (!empty($dashletIDs)) {
362 // we need to disable widget that removed
363 $updateQuery = " UPDATE civicrm_dashboard_contact
365 WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}";
368 // this means all widgets are disabled
369 $updateQuery = " UPDATE civicrm_dashboard_contact
371 WHERE contact_id = {$contactID}";
374 CRM_Core_DAO
::executeQuery($updateQuery);
378 * Function to add dashlets
380 * @param array $params associated array
382 * @return object $dashlet returns dashlet object
386 static function addDashlet(&$params) {
388 // special case to handle duplicate entires for report instances
389 $dashboardID = CRM_Utils_Array
::value('id', $params);
391 if (!empty($params['instanceURL'])) {
393 FROM `civicrm_dashboard`
394 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
395 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
398 $dashlet = new CRM_Core_DAO_Dashboard();
401 // check url is same as exiting entries, if yes just update existing
402 if (!empty($params['name'])) {
403 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
404 $dashlet->find(TRUE);
407 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
408 $dashlet->find(TRUE);
412 $dashlet->id
= $dashboardID;
415 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
416 $params['permission'] = implode(',', $params['permission']);
418 $dashlet->copyValues($params);
420 $dashlet->domain_id
= CRM_Core_Config
::domainID();
424 // now we need to make dashlet entries for each contact
425 self
::addContactDashlet($dashlet);
435 static function getDashletName($url) {
436 $urlElements = explode('/', $url);
437 if ($urlElements[1] == 'dashlet') {
438 return $urlElements[2];
440 elseif ($urlElements[1] == 'report') {
441 return 'report/' . $urlElements[3];
446 * Update contact dashboard with new dashlet
448 * @param object: $dashlet
453 static function addContactDashlet($dashlet) {
454 $admin = CRM_Core_Permission
::check('administer CiviCRM');
456 // if dashlet is created by admin then you need to add it all contacts.
457 // else just add to contact who is creating this dashlet
458 $contactIDs = array();
460 $query = "SELECT distinct( contact_id )
461 FROM civicrm_dashboard_contact
462 WHERE contact_id NOT IN (
463 SELECT distinct( contact_id )
464 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}
467 $dao = CRM_Core_DAO
::executeQuery($query);
468 while ($dao->fetch()) {
469 $contactIDs[] = $dao->contact_id
;
473 //Get the id of Logged in User
474 $session = CRM_Core_Session
::singleton();
475 $contactIDs[] = $session->get('userID');
478 if (!empty($contactIDs)) {
479 foreach ($contactIDs as $contactID) {
480 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
483 $valuesString = implode(',', $valuesArray);
485 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
486 VALUES {$valuesString}";
488 CRM_Core_DAO
::executeQuery($query);
493 * @param array $params each item is a spec for a dashlet on the contact's dashboard
496 static function addContactDashletToDashboard(&$params) {
497 $valuesString = NULL;
499 foreach ($params as $dashboardIDs) {
500 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
501 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
502 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
503 $columns[$column][$dashboardID] = 0;
505 self
::saveDashletChanges($columns, $contactID);
510 * Function to reset dashlet cache
512 * @param int $contactID reset cache only for specific contact
517 static function resetDashletCache($contactID = null) {
521 $whereClause = "WHERE contact_id = %1";
522 $params[1] = array($contactID, 'Integer');
524 $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause";
525 $dao = CRM_Core_DAO
::executeQuery($query, $params);
536 static function deleteDashlet($dashletID) {
537 $dashlet = new CRM_Core_DAO_Dashboard();
538 $dashlet->id
= $dashletID;