* 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();
// Get contact dashboard dashlets.
- $hasDashlets = FALSE;
- $dao = new CRM_Contact_DAO_DashboardContact();
- $dao->contact_id = $contactID ? $contactID : CRM_Core_Session::singleton()->getLoggedInContactID();
- $dao->orderBy('column_no asc, weight asc');
- $dao->find();
-
- // The available list will only include those which are valid for the domain.
- $availableDashlets = self::getDashlets();
- 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.
- if ((!empty($availableDashlets[$dao->dashboard_id]) && $availableDashlets[$dao->dashboard_id]['is_active'])) {
- $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.
*
* When a user accesses their dashboard for the first time, set up
* the default dashlets.
*
- * @param bool $flatFormat
- *
* @return array
* 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(),
$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,
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.
}
/**
- * 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
*
* @throws RuntimeException
*/
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) {
}
$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++;
}
}
- 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);
}
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.
*
return TRUE;
}
- /**
- * Reset dashlet cache.
- *
- * @param int $contactID
- * Reset cache only for specific contact.
- */
- 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.
*
public static function deleteDashlet($dashletID) {
$dashlet = new CRM_Core_DAO_Dashboard();
$dashlet->id = $dashletID;
+ if (!$dashlet->find(TRUE)) {
+ return FALSE;
+ }
$dashlet->delete();
return TRUE;
}