X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FBAO%2FDashboard.php;h=c8df7cbdc738afb5735363efd24ddb962cc289af;hb=a60c0bc84a8db9a4123f53879109576524393ff9;hp=a48cd701e537589f1e0988e182399c90147bd6f8;hpb=8bc883dc7e33eb2836a8cb5af2ddae4f32fea0ec;p=civicrm-core.git diff --git a/CRM/Core/BAO/Dashboard.php b/CRM/Core/BAO/Dashboard.php index a48cd701e5..d64620e3f6 100644 --- a/CRM/Core/BAO/Dashboard.php +++ b/CRM/Core/BAO/Dashboard.php @@ -3,7 +3,7 @@ +--------------------------------------------------------------------+ | CiviCRM version 4.7 | +--------------------------------------------------------------------+ - | Copyright CiviCRM LLC (c) 2004-2015 | + | Copyright CiviCRM LLC (c) 2004-2016 | +--------------------------------------------------------------------+ | This file is a part of CiviCRM. | | | @@ -28,13 +28,11 @@ /** * * @package CRM - * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ - * + * @copyright CiviCRM LLC (c) 2004-2016 */ /** - * Class contains Contact dashboard related functions + * Class contains Contact dashboard related functions. */ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { /** @@ -95,57 +93,81 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { * Additionlly, initializes the dashboard with defaults if this is the * user's first visit to their dashboard. * - * @param bool $flatFormat - * This is true if you want simple associated. - * array of all the contact's dashlets whether or not they are enabled. - * * @param int $contactID - * Provide the dashlets for the contact id. - * passed rather than the current user. + * Defaults to the current user. * * @return array * array of dashlets */ - public static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) { + public static function getContactDashlets($contactID = NULL) { + $contactID = $contactID ? $contactID : CRM_Core_Session::singleton()->getLoggedInContactID(); $dashlets = array(); - if (!$contactID) { - $contactID = CRM_Core_Session::singleton()->get('userID'); - } - // Get contact dashboard dashlets. - $hasDashlets = FALSE; - $dao = new CRM_Contact_DAO_DashboardContact(); - $dao->contact_id = $contactID; - $dao->orderBy('column_no asc, weight asc'); - $dao->find(); - while ($dao->fetch()) { - // When a dashlet is removed, it stays in the table with status disabled, - // so even if a user decides not to have any dashlets show, they will still - // have records in the table to indicate that we are not newly initializing. - $hasDashlets = TRUE; - if (!$flatFormat) { - if ($dao->is_active) { - // append weight so that order is preserved. - $dashlets[$dao->column_no]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized; - } - } - else { - $dashlets[$dao->dashboard_id] = $dao->dashboard_id; + $results = civicrm_api3('DashboardContact', 'get', array( + 'contact_id' => $contactID, + 'is_active' => 1, + 'dashboard_id.is_active' => 1, + 'options' => array('sort' => 'weight', 'limit' => 0), + 'return' => array( + 'id', + 'weight', + 'column_no', + 'dashboard_id', + 'dashboard_id.name', + 'dashboard_id.label', + 'dashboard_id.url', + 'dashboard_id.fullscreen_url', + 'dashboard_id.cache_minutes', + 'dashboard_id.permission', + 'dashboard_id.permission_operator', + ), + )); + + foreach ($results['values'] as $item) { + if (self::checkPermission(CRM_Utils_Array::value('dashboard_id.permission', $item), CRM_Utils_Array::value('dashboard_id.permission_operator', $item))) { + $dashlets[$item['id']] = array( + 'dashboard_id' => $item['dashboard_id'], + 'weight' => $item['weight'], + 'column_no' => $item['column_no'], + 'name' => $item['dashboard_id.name'], + 'label' => $item['dashboard_id.label'], + 'url' => $item['dashboard_id.url'], + 'cache_minutes' => $item['dashboard_id.cache_minutes'], + 'fullscreen_url' => CRM_Utils_Array::value('dashboard_id.fullscreen_url', $item), + ); } } - if ($flatFormat) { - return $dashlets; + // If empty, then initialize default dashlets for this user. + if (!$results['count']) { + // They may just have disabled all their dashlets. Check if any records exist for this contact. + if (!civicrm_api3('DashboardContact', 'getcount', array('contact_id' => $contactID))) { + $dashlets = self::initializeDashlets(); + } } - // If empty, then initialize contact dashboard for this user. - if (!$hasDashlets) { - return self::initializeDashlets($flatFormat); - } return $dashlets; } + /** + * @return array + */ + public static function getContactDashletsForJS() { + $data = array(array(), array()); + foreach (self::getContactDashlets() as $item) { + $data[$item['column_no']][] = array( + 'id' => (int) $item['dashboard_id'], + 'name' => $item['name'], + 'title' => $item['label'], + 'url' => self::parseUrl($item['url']), + 'cacheMinutes' => $item['cache_minutes'], + 'fullscreenUrl' => self::parseUrl($item['fullscreen_url']), + ); + } + return $data; + } + /** * Setup default dashlets for new users. * @@ -153,20 +175,21 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { * the default dashlets. * * @return array - * Array of dashboard_id's + * Array of dashboard_id's + * @throws \CiviCRM_API3_Exception */ - public static function initializeDashlets($flatFormat = FALSE) { + public static function initializeDashlets() { $dashlets = array(); $getDashlets = civicrm_api3("Dashboard", "get", array( 'domain_id' => CRM_Core_Config::domainID(), 'option.limit' => 0, )); - $contactID = CRM_Core_Session::singleton()->get('userID'); + $contactID = CRM_Core_Session::singleton()->getLoggedInContactID(); $allDashlets = CRM_Utils_Array::index(array('name'), $getDashlets['values']); $defaultDashlets = array(); $defaults = array('blog' => 1, 'getting-started' => '0'); foreach ($defaults as $name => $column) { - if (!empty($allDashlets[$name])) { + if (!empty($allDashlets[$name]) && !empty($allDashlets[$name]['id'])) { $defaultDashlets[$name] = array( 'dashboard_id' => $allDashlets[$name]['id'], 'is_active' => 1, @@ -179,26 +202,41 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { if (is_array($defaultDashlets) && !empty($defaultDashlets)) { foreach ($defaultDashlets as $id => $defaultDashlet) { $dashboard_id = $defaultDashlet['dashboard_id']; - if (!self::checkPermission($getDashlets['values'][$dashboard_id]['permission'], - CRM_Utils_Array::value('permission_operator', $getDashlets['values'][$dashboard_id])) - ) { + $dashlet = $getDashlets['values'][$dashboard_id]; + if (!self::checkPermission(CRM_Utils_Array::value('permission', $dashlet), CRM_Utils_Array::value('permission_operator', $dashlet))) { continue; } else { $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet); - if (!$flatFormat) { - $values = $assignDashlets['values'][$assignDashlets['id']]; - $dashlets[$values['column_no']][$values['weight'] - $values['dashboard_id']] = $values['is_minimized']; - } - else { - $dashlets[$dashboard_id] = $defaultDashlet['dashboard_id']; - } + $values = $assignDashlets['values'][$assignDashlets['id']]; + $dashlets[$assignDashlets['id']] = array( + 'dashboard_id' => $values['dashboard_id'], + 'weight' => $values['weight'], + 'column_no' => $values['column_no'], + 'name' => $dashlet['name'], + 'label' => $dashlet['label'], + 'cache_minutes' => $dashlet['cache_minutes'], + 'url' => $dashlet['url'], + 'fullscreen_url' => CRM_Utils_Array::value('fullscreen_url', $dashlet), + ); } } } return $dashlets; } + /** + * @param $url + * @return string + */ + public static function parseUrl($url) { + // Check if it is already a fully-formed url + if ($url && substr($url, 0, 4) != 'http' && $url[0] != '/') { + $urlParam = explode('?', $url); + $url = CRM_Utils_System::url($urlParam[0], CRM_Utils_Array::value(1, $urlParam), FALSE, NULL, FALSE); + } + return $url; + } /** * Check dashlet permission for current user. @@ -280,97 +318,23 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { } /** - * Get details of each dashlets. - * - * @param int $dashletID - * Widget ID. - * - * @return array - * associted array title and content - */ - public static function getDashletInfo($dashletID) { - $dashletInfo = array(); - - $params = array(1 => array($dashletID, 'Integer')); - $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1"; - $dashboadDAO = CRM_Core_DAO::executeQuery($query, $params); - $dashboadDAO->fetch(); - - // build the content - $dao = new CRM_Contact_DAO_DashboardContact(); - - $session = CRM_Core_Session::singleton(); - $dao->contact_id = $session->get('userID'); - $dao->dashboard_id = $dashletID; - $dao->find(TRUE); - - //reset content based on the cache time set in config - $createdDate = strtotime($dao->created_date); - $dateDiff = round(abs(time() - $createdDate) / 60); - - $config = CRM_Core_Config::singleton(); - if ($config->dashboardCacheTimeout <= $dateDiff) { - $dao->content = NULL; - } - - // if content is empty and url is set, retrieve it from url - if (!$dao->content && $dashboadDAO->url) { - $url = $dashboadDAO->url; - - // CRM-7087 - // -lets use relative url for internal use. - // -make sure relative url should not be htmlize. - if (substr($dashboadDAO->url, 0, 4) != 'http') { - $urlParam = explode('?', $dashboadDAO->url); - $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE); - } - - //get content from url - $dao->content = CRM_Utils_System::getServerResponse($url); - $dao->created_date = date("YmdHis"); - $dao->save(); - } - - $dashletInfo = array( - 'title' => $dashboadDAO->label, - 'name' => $dashboadDAO->name, - 'content' => $dao->content, - ); - - if ($dashboadDAO->is_fullscreen) { - $fullscreenUrl = $dashboadDAO->fullscreen_url; - if (substr($fullscreenUrl, 0, 4) != 'http') { - $urlParam = explode('?', $dashboadDAO->fullscreen_url); - $fullscreenUrl = CRM_Utils_System::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE); - } - $dashletInfo['fullscreenUrl'] = $fullscreenUrl; - } - return $dashletInfo; - } - - /** - * Save changes made by use to the Dashlet. + * Save changes made by user to the Dashlet. * * @param array $columns * * @param int $contactID * * @throws RuntimeException - * @return void */ public static function saveDashletChanges($columns, $contactID = NULL) { - $session = CRM_Core_Session::singleton(); if (!$contactID) { - $contactID = $session->get('userID'); + $contactID = CRM_Core_Session::getLoggedInContactID(); } if (empty($contactID)) { throw new RuntimeException("Failed to determine contact ID"); } - //we need to get existing dashlets, so we know when to update or insert - $contactDashlets = self::getContactDashlets(TRUE, $contactID); - $dashletIDs = array(); if (is_array($columns)) { foreach ($columns as $colNo => $dashlets) { @@ -379,19 +343,13 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { } $weight = 1; foreach ($dashlets as $dashletID => $isMinimized) { - $isMinimized = (int) $isMinimized; - if (in_array($dashletID, $contactDashlets)) { - $query = " UPDATE civicrm_dashboard_contact - SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1 - WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} "; - } - else { - $query = " INSERT INTO civicrm_dashboard_contact - ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id ) - VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )"; - } + $dashletID = (int) $dashletID; + $query = "INSERT INTO civicrm_dashboard_contact + (weight, column_no, is_active, dashboard_id, contact_id) + VALUES({$weight}, {$colNo}, 1, {$dashletID}, {$contactID}) + ON DUPLICATE KEY UPDATE weight = {$weight}, column_no = {$colNo}, is_active = 1"; // fire update query for each column - $dao = CRM_Core_DAO::executeQuery($query); + CRM_Core_DAO::executeQuery($query); $dashletIDs[] = $dashletID; $weight++; @@ -399,18 +357,11 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { } } - if (!empty($dashletIDs)) { - // we need to disable widget that removed - $updateQuery = " UPDATE civicrm_dashboard_contact - SET is_active = 0 - WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}"; - } - else { - // this means all widgets are disabled - $updateQuery = " UPDATE civicrm_dashboard_contact - SET is_active = 0 - WHERE contact_id = {$contactID}"; - } + // Disable inactive widgets + $dashletClause = $dashletIDs ? "dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)'; + $updateQuery = "UPDATE civicrm_dashboard_contact + SET is_active = 0 + WHERE $dashletClause AND contact_id = {$contactID}"; CRM_Core_DAO::executeQuery($updateQuery); } @@ -467,28 +418,10 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { return $dashlet; } - /** - * @param $url - * - * @return string - */ - public static function getDashletName($url) { - $urlElements = explode('/', $url); - if ($urlElements[1] == 'dashlet') { - return $urlElements[2]; - } - elseif ($urlElements[1] == 'report') { - return 'report/' . $urlElements[3]; - } - return $url; - } - /** * Update contact dashboard with new dashlet. * - * @param object : $dashlet - * - * @return void + * @param object $dashlet */ public static function addContactDashlet($dashlet) { $admin = CRM_Core_Permission::check('administer CiviCRM'); @@ -550,35 +483,19 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { return TRUE; } - /** - * Reset dashlet cache. - * - * @param int $contactID - * Reset cache only for specific contact. - * - * @return void - */ - public static function resetDashletCache($contactID = NULL) { - $whereClause = NULL; - $params = array(); - if ($contactID) { - $whereClause = "WHERE contact_id = %1"; - $params[1] = array($contactID, 'Integer'); - } - $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause"; - $dao = CRM_Core_DAO::executeQuery($query, $params); - } - /** * Delete Dashlet. * * @param int $dashletID * - * @return void + * @return bool */ public static function deleteDashlet($dashletID) { $dashlet = new CRM_Core_DAO_Dashboard(); $dashlet->id = $dashletID; + if (!$dashlet->find(TRUE)) { + return FALSE; + } $dashlet->delete(); return TRUE; }