1b14cdd2ca89f9d02910e61231c2fc297a658388
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
35 * Class contains Contact dashboard related functions.
37 class CRM_Core_BAO_Dashboard
extends CRM_Core_DAO_Dashboard
{
41 * @param array $params
47 public static function create($params) {
48 $hook = empty($params['id']) ?
'create' : 'edit';
49 CRM_Utils_Hook
::pre($hook, 'Dashboard', CRM_Utils_Array
::value('id', $params), $params);
50 $dao = self
::addDashlet($params);
51 CRM_Utils_Hook
::post($hook, 'Dashboard', $dao->id
, $dao);
56 * Get the list of dashlets enabled by admin.
60 * @param bool $checkPermission
61 * All or only authorized for the current user.
66 public static function getDashlets($all = TRUE, $checkPermission = TRUE) {
68 $dao = new CRM_Core_DAO_Dashboard();
74 $dao->domain_id
= CRM_Core_Config
::domainID();
77 while ($dao->fetch()) {
78 if ($checkPermission && !self
::checkPermission($dao->permission
, $dao->permission_operator
)) {
83 CRM_Core_DAO
::storeValues($dao, $values);
84 $dashlets[$dao->id
] = $values;
91 * Get the list of dashlets for the current user or the specified user.
93 * Additionlly, initializes the dashboard with defaults if this is the
94 * user's first visit to their dashboard.
96 * @param int $contactID
97 * Defaults to the current user.
102 public static function getContactDashlets($contactID = NULL) {
103 $contactID = $contactID ?
$contactID : CRM_Core_Session
::singleton()->getLoggedInContactID();
106 // Get contact dashboard dashlets.
107 $results = civicrm_api3('DashboardContact', 'get', array(
108 'contact_id' => $contactID,
110 'dashboard_id.is_active' => 1,
111 'options' => array('sort' => 'weight'),
119 'dashboard_id.label',
121 'dashboard_id.fullscreen_url',
122 'dashboard_id.permission',
123 'dashboard_id.permission_operator',
127 foreach ($results['values'] as $item) {
128 if (self
::checkPermission(CRM_Utils_Array
::value('dashboard_id.permission', $item), CRM_Utils_Array
::value('dashboard_id.permission_operator', $item))) {
129 $dashlets[$item['id']] = array(
130 'dashboard_id' => $item['dashboard_id'],
131 'weight' => $item['weight'],
132 'column_no' => $item['column_no'],
133 'is_minimized' => $item['is_minimized'],
134 'name' => $item['dashboard_id.name'],
135 'label' => $item['dashboard_id.label'],
136 'url' => $item['dashboard_id.url'],
137 'fullscreen_url' => $item['dashboard_id.fullscreen_url'],
142 // If empty, then initialize default dashlets for this user.
143 if (!$results['count']) {
144 // They may just have disabled all their dashlets. Check if any records exist for this contact.
145 if (!civicrm_api3('DashboardContact', 'getcount', array('contact_id' => $contactID))) {
146 $dashlets = self
::initializeDashlets();
153 public static function getContactDashletsForJS() {
154 $data = array(array(), array());
155 foreach (self
::getContactDashlets() as $item) {
156 $data[$item['column_no']][] = array(
157 'id' => (int) $item['dashboard_id'],
158 'name' => $item['name'],
159 'title' => $item['label'],
160 'url' => self
::parseUrl($item['url']),
161 'fullscreenUrl' => self
::parseUrl($item['fullscreen_url']),
168 * Setup default dashlets for new users.
170 * When a user accesses their dashboard for the first time, set up
171 * the default dashlets.
174 * Array of dashboard_id's
175 * @throws \CiviCRM_API3_Exception
177 public static function initializeDashlets() {
179 $getDashlets = civicrm_api3("Dashboard", "get", array(
180 'domain_id' => CRM_Core_Config
::domainID(),
183 $contactID = CRM_Core_Session
::singleton()->getLoggedInContactID();
184 $allDashlets = CRM_Utils_Array
::index(array('name'), $getDashlets['values']);
185 $defaultDashlets = array();
186 $defaults = array('blog' => 1, 'getting-started' => '0');
187 foreach ($defaults as $name => $column) {
188 if (!empty($allDashlets[$name]) && !empty($allDashlets[$name]['id'])) {
189 $defaultDashlets[$name] = array(
190 'dashboard_id' => $allDashlets[$name]['id'],
192 'column_no' => $column,
193 'contact_id' => $contactID,
197 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
198 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
199 foreach ($defaultDashlets as $id => $defaultDashlet) {
200 $dashboard_id = $defaultDashlet['dashboard_id'];
201 $dashlet = $getDashlets['values'][$dashboard_id];
202 if (!self
::checkPermission(CRM_Utils_Array
::value('permission', $dashlet), CRM_Utils_Array
::value('permission_operator', $dashlet))) {
206 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
207 $values = $assignDashlets['values'][$assignDashlets['id']];
208 $dashlets[$assignDashlets['id']] = array(
209 'dashboard_id' => $values['dashboard_id'],
210 'weight' => $values['weight'],
211 'column_no' => $values['column_no'],
212 'is_minimized' => $values['is_minimized'],
213 'name' => $dashlet['name'],
214 'label' => $dashlet['label'],
215 'url' => $dashlet['url'],
216 'fullscreen_url' => $dashlet['fullscreen_url'],
228 public static function parseUrl($url) {
229 if (substr($url, 0, 4) != 'http') {
230 $urlParam = explode('?', $url);
231 $url = CRM_Utils_System
::url($urlParam[0], $urlParam[1], FALSE, NULL, FALSE);
237 * Check dashlet permission for current user.
239 * @param string $permission
240 * Comma separated list.
241 * @param string $operator
244 * true if use has permission else false
246 public static function checkPermission($permission, $operator) {
248 $permissions = explode(',', $permission);
249 $config = CRM_Core_Config
::singleton();
251 static $allComponents;
252 if (!$allComponents) {
253 $allComponents = CRM_Core_Component
::getNames();
256 $hasPermission = FALSE;
257 foreach ($permissions as $key) {
260 $componentName = NULL;
261 if (strpos($key, 'access') === 0) {
262 $componentName = trim(substr($key, 6));
263 if (!in_array($componentName, $allComponents)) {
264 $componentName = NULL;
268 // hack to handle case permissions
269 if (!$componentName && in_array($key, array(
270 'access my cases and activities',
271 'access all cases and activities',
274 $componentName = 'CiviCase';
277 //hack to determine if it's a component related permission
278 if ($componentName) {
279 if (!in_array($componentName, $config->enableComponents
) ||
280 !CRM_Core_Permission
::check($key)
282 $showDashlet = FALSE;
283 if ($operator == 'AND') {
288 $hasPermission = TRUE;
291 elseif (!CRM_Core_Permission
::check($key)) {
292 $showDashlet = FALSE;
293 if ($operator == 'AND') {
298 $hasPermission = TRUE;
302 if (!$showDashlet && !$hasPermission) {
310 // if permission is not set consider everyone has permission to access it.
316 * Get details of each dashlets.
318 * @param int $dashletID
322 * associted array title and content
324 public static function getDashletInfo($dashletID) {
325 $dashletInfo = array();
327 $params = array(1 => array($dashletID, 'Integer'));
328 $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
329 $dashboadDAO = CRM_Core_DAO
::executeQuery($query, $params);
330 $dashboadDAO->fetch();
333 $dao = new CRM_Contact_DAO_DashboardContact();
335 $session = CRM_Core_Session
::singleton();
336 $dao->contact_id
= $session->get('userID');
337 $dao->dashboard_id
= $dashletID;
340 //reset content based on the cache time set in config
341 $createdDate = strtotime($dao->created_date
);
342 $dateDiff = round(abs(time() - $createdDate) / 60);
344 $config = CRM_Core_Config
::singleton();
345 if ($config->dashboardCacheTimeout
<= $dateDiff) {
346 $dao->content
= NULL;
349 // if content is empty and url is set, retrieve it from url
350 if (!$dao->content
&& $dashboadDAO->url
) {
351 $url = $dashboadDAO->url
;
354 // -lets use relative url for internal use.
355 // -make sure relative url should not be htmlize.
356 if (substr($dashboadDAO->url
, 0, 4) != 'http') {
357 $urlParam = explode('?', $dashboadDAO->url
);
358 $url = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
361 //get content from url
362 $dao->content
= CRM_Utils_System
::getServerResponse($url);
363 $dao->created_date
= date("YmdHis");
367 $dashletInfo = array(
368 'title' => $dashboadDAO->label
,
369 'name' => $dashboadDAO->name
,
370 'content' => $dao->content
,
373 if ($dashboadDAO->is_fullscreen
) {
374 $fullscreenUrl = $dashboadDAO->fullscreen_url
;
375 if (substr($fullscreenUrl, 0, 4) != 'http') {
376 $urlParam = explode('?', $dashboadDAO->fullscreen_url
);
377 $fullscreenUrl = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
379 $dashletInfo['fullscreenUrl'] = $fullscreenUrl;
385 * Save changes made by use to the Dashlet.
387 * @param array $columns
389 * @param int $contactID
391 * @throws RuntimeException
393 public static function saveDashletChanges($columns, $contactID = NULL) {
395 $contactID = CRM_Core_Session
::getLoggedInContactID();
398 if (empty($contactID)) {
399 throw new RuntimeException("Failed to determine contact ID");
402 $dashletIDs = array();
403 if (is_array($columns)) {
404 foreach ($columns as $colNo => $dashlets) {
405 if (!is_int($colNo)) {
409 foreach ($dashlets as $dashletID => $isMinimized) {
410 $isMinimized = (int) $isMinimized;
411 $dashletID = (int) $dashletID;
412 $query = "INSERT INTO civicrm_dashboard_contact
413 (weight, is_minimized, column_no, is_active, dashboard_id, contact_id)
414 VALUES({$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID})
415 ON DUPLICATE KEY UPDATE weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1";
416 // fire update query for each column
417 CRM_Core_DAO
::executeQuery($query);
419 $dashletIDs[] = $dashletID;
425 // Disable inactive widgets
426 $dashletClause = $dashletIDs ?
"dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)';
427 $updateQuery = "UPDATE civicrm_dashboard_contact
429 WHERE $dashletClause AND contact_id = {$contactID}";
431 CRM_Core_DAO
::executeQuery($updateQuery);
437 * @param array $params
440 * $dashlet returns dashlet object
442 public static function addDashlet(&$params) {
444 // special case to handle duplicate entries for report instances
445 $dashboardID = CRM_Utils_Array
::value('id', $params);
447 if (!empty($params['instanceURL'])) {
449 FROM `civicrm_dashboard`
450 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
451 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
454 $dashlet = new CRM_Core_DAO_Dashboard();
457 // check url is same as exiting entries, if yes just update existing
458 if (!empty($params['name'])) {
459 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
460 $dashlet->find(TRUE);
463 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
464 $dashlet->find(TRUE);
466 if (empty($params['domain_id'])) {
467 $dashlet->domain_id
= CRM_Core_Config
::domainID();
471 $dashlet->id
= $dashboardID;
474 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
475 $params['permission'] = implode(',', $params['permission']);
477 $dashlet->copyValues($params);
480 // now we need to make dashlet entries for each contact
481 self
::addContactDashlet($dashlet);
491 public static function getDashletName($url) {
492 $urlElements = explode('/', $url);
493 if ($urlElements[1] == 'dashlet') {
494 return $urlElements[2];
496 elseif ($urlElements[1] == 'report') {
497 return 'report/' . $urlElements[3];
503 * Update contact dashboard with new dashlet.
505 * @param object $dashlet
507 public static function addContactDashlet($dashlet) {
508 $admin = CRM_Core_Permission
::check('administer CiviCRM');
510 // if dashlet is created by admin then you need to add it all contacts.
511 // else just add to contact who is creating this dashlet
512 $contactIDs = array();
514 $query = "SELECT distinct( contact_id )
515 FROM civicrm_dashboard_contact
516 WHERE contact_id NOT IN (
517 SELECT distinct( contact_id )
518 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}
521 $dao = CRM_Core_DAO
::executeQuery($query);
522 while ($dao->fetch()) {
523 $contactIDs[] = $dao->contact_id
;
527 //Get the id of Logged in User
528 $session = CRM_Core_Session
::singleton();
529 $contactID = $session->get('userID');
530 if (!empty($contactID)) {
531 $contactIDs[] = $session->get('userID');
535 if (!empty($contactIDs)) {
536 foreach ($contactIDs as $contactID) {
537 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
540 $valuesString = implode(',', $valuesArray);
542 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
543 VALUES {$valuesString}";
545 CRM_Core_DAO
::executeQuery($query);
550 * @param array $params
551 * Each item is a spec for a dashlet on the contact's dashboard.
554 public static function addContactDashletToDashboard(&$params) {
555 $valuesString = NULL;
557 foreach ($params as $dashboardIDs) {
558 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
559 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
560 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
561 $columns[$column][$dashboardID] = 0;
563 self
::saveDashletChanges($columns, $contactID);
568 * Reset dashlet cache.
570 * @param int $contactID
571 * Reset cache only for specific contact.
573 public static function resetDashletCache($contactID = NULL) {
577 $whereClause = "WHERE contact_id = %1";
578 $params[1] = array($contactID, 'Integer');
580 $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause";
581 $dao = CRM_Core_DAO
::executeQuery($query, $params);
587 * @param int $dashletID
591 public static function deleteDashlet($dashletID) {
592 $dashlet = new CRM_Core_DAO_Dashboard();
593 $dashlet->id
= $dashletID;