3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * Class contains Contact dashboard related functions
39 class CRM_Core_BAO_Dashboard
extends CRM_Core_DAO_Dashboard
{
43 * @param array $params
49 public static function create($params) {
50 $hook = empty($params['id']) ?
'create' : 'edit';
51 CRM_Utils_Hook
::pre($hook, 'Dashboard', CRM_Utils_Array
::value('id', $params), $params);
52 $dao = self
::addDashlet($params);
53 CRM_Utils_Hook
::post($hook, 'Dashboard', $dao->id
, $dao);
58 * Get the list of dashlets enabled by admin.
62 * @param bool $checkPermission
63 * All or only authorized for the current user.
68 public static function getDashlets($all = TRUE, $checkPermission = TRUE) {
70 $dao = new CRM_Core_DAO_Dashboard();
76 $dao->domain_id
= CRM_Core_Config
::domainID();
79 while ($dao->fetch()) {
80 if ($checkPermission && !self
::checkPermission($dao->permission
, $dao->permission_operator
)) {
85 CRM_Core_DAO
::storeValues($dao, $values);
86 $dashlets[$dao->id
] = $values;
93 * Get the list of dashlets for the current user or the specified user.
95 * Additionlly, initializes the dashboard with defaults if this is the
96 * user's first visit to their dashboard.
98 * @param bool $flatFormat
99 * This is true if you want simple associated.
100 * array of all the contact's dashlets whether or not they are enabled.
102 * @param int $contactID
103 * Provide the dashlets for the contact id.
104 * passed rather than the current user.
109 public static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
113 $contactID = CRM_Core_Session
::singleton()->get('userID');
116 // Get contact dashboard dashlets.
117 $hasDashlets = FALSE;
118 $dao = new CRM_Contact_DAO_DashboardContact();
119 $dao->contact_id
= $contactID;
120 $dao->orderBy('column_no asc, weight asc');
122 while ($dao->fetch()) {
123 // When a dashlet is removed, it stays in the table with status disabled,
124 // so even if a user decides not to have any dashlets show, they will still
125 // have records in the table to indicate that we are not newly initializing.
128 if ($dao->is_active
) {
129 // append weight so that order is preserved.
130 $dashlets[$dao->column_no
]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized
;
134 $dashlets[$dao->dashboard_id
] = $dao->dashboard_id
;
142 // If empty, then initialize contact dashboard for this user.
144 return self
::initializeDashlets($flatFormat);
150 * Setup default dashlets for new users.
152 * When a user accesses their dashboard for the first time, set up
153 * the default dashlets.
156 * Array of dashboard_id's
158 public static function initializeDashlets($flatFormat = FALSE) {
160 $getDashlets = civicrm_api3("Dashboard", "get", array(
161 'domain_id' => CRM_Core_Config
::domainID(),
164 $contactID = CRM_Core_Session
::singleton()->get('userID');
165 $allDashlets = CRM_Utils_Array
::index(array('name'), $getDashlets['values']);
166 $defaultDashlets = array();
167 $defaults = array('blog' => 1, 'getting-started' => '0');
168 foreach ($defaults as $name => $column) {
169 if (!empty($allDashlets[$name])) {
170 $defaultDashlets[$name] = array(
171 'dashboard_id' => $allDashlets[$name]['id'],
173 'column_no' => $column,
174 'contact_id' => $contactID,
178 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
179 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
180 foreach ($defaultDashlets as $id => $defaultDashlet) {
181 $dashboard_id = $defaultDashlet['dashboard_id'];
182 if (!self
::checkPermission($getDashlets['values'][$dashboard_id]['permission'],
183 $getDashlets['values'][$dashboard_id]['permission_operator'])
188 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
190 $values = $assignDashlets['values'][$assignDashlets['id']];
191 $dashlets[$values['column_no']][$values['weight'] - $values['dashboard_id']] = $values['is_minimized'];
194 $dashlets[$dashboard_id] = $defaultDashlet['dashboard_id'];
204 * Check dashlet permission for current user.
206 * @param string $permission
207 * Comma separated list.
208 * @param string $operator
211 * true if use has permission else false
213 public static function checkPermission($permission, $operator) {
215 $permissions = explode(',', $permission);
216 $config = CRM_Core_Config
::singleton();
218 static $allComponents;
219 if (!$allComponents) {
220 $allComponents = CRM_Core_Component
::getNames();
223 $hasPermission = FALSE;
224 foreach ($permissions as $key) {
227 $componentName = NULL;
228 if (strpos($key, 'access') === 0) {
229 $componentName = trim(substr($key, 6));
230 if (!in_array($componentName, $allComponents)) {
231 $componentName = NULL;
235 // hack to handle case permissions
236 if (!$componentName && in_array($key, array(
237 'access my cases and activities',
238 'access all cases and activities',
241 $componentName = 'CiviCase';
244 //hack to determine if it's a component related permission
245 if ($componentName) {
246 if (!in_array($componentName, $config->enableComponents
) ||
247 !CRM_Core_Permission
::check($key)
249 $showDashlet = FALSE;
250 if ($operator == 'AND') {
255 $hasPermission = TRUE;
258 elseif (!CRM_Core_Permission
::check($key)) {
259 $showDashlet = FALSE;
260 if ($operator == 'AND') {
265 $hasPermission = TRUE;
269 if (!$showDashlet && !$hasPermission) {
277 // if permission is not set consider everyone has permission to access it.
283 * Get details of each dashlets.
285 * @param int $dashletID
289 * associted array title and content
291 public static function getDashletInfo($dashletID) {
292 $dashletInfo = array();
294 $params = array(1 => array($dashletID, 'Integer'));
295 $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
296 $dashboadDAO = CRM_Core_DAO
::executeQuery($query, $params);
297 $dashboadDAO->fetch();
300 $dao = new CRM_Contact_DAO_DashboardContact();
302 $session = CRM_Core_Session
::singleton();
303 $dao->contact_id
= $session->get('userID');
304 $dao->dashboard_id
= $dashletID;
307 //reset content based on the cache time set in config
308 $createdDate = strtotime($dao->created_date
);
309 $dateDiff = round(abs(time() - $createdDate) / 60);
311 $config = CRM_Core_Config
::singleton();
312 if ($config->dashboardCacheTimeout
<= $dateDiff) {
313 $dao->content
= NULL;
316 // if content is empty and url is set, retrieve it from url
317 if (!$dao->content
&& $dashboadDAO->url
) {
318 $url = $dashboadDAO->url
;
321 // -lets use relative url for internal use.
322 // -make sure relative url should not be htmlize.
323 if (substr($dashboadDAO->url
, 0, 4) != 'http') {
324 $urlParam = explode('?', $dashboadDAO->url
);
325 $url = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
328 //get content from url
329 $dao->content
= CRM_Utils_System
::getServerResponse($url);
330 $dao->created_date
= date("YmdHis");
334 $dashletInfo = array(
335 'title' => $dashboadDAO->label
,
336 'name' => $dashboadDAO->name
,
337 'content' => $dao->content
,
340 if ($dashboadDAO->is_fullscreen
) {
341 $fullscreenUrl = $dashboadDAO->fullscreen_url
;
342 if (substr($fullscreenUrl, 0, 4) != 'http') {
343 $urlParam = explode('?', $dashboadDAO->fullscreen_url
);
344 $fullscreenUrl = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
346 $dashletInfo['fullscreenUrl'] = $fullscreenUrl;
352 * Save changes made by use to the Dashlet.
354 * @param array $columns
356 * @param int $contactID
358 * @throws RuntimeException
361 public static function saveDashletChanges($columns, $contactID = NULL) {
362 $session = CRM_Core_Session
::singleton();
364 $contactID = $session->get('userID');
367 if (empty($contactID)) {
368 throw new RuntimeException("Failed to determine contact ID");
371 //we need to get existing dashlets, so we know when to update or insert
372 $contactDashlets = self
::getContactDashlets(TRUE, $contactID);
374 $dashletIDs = array();
375 if (is_array($columns)) {
376 foreach ($columns as $colNo => $dashlets) {
377 if (!is_int($colNo)) {
381 foreach ($dashlets as $dashletID => $isMinimized) {
382 $isMinimized = (int) $isMinimized;
383 if (in_array($dashletID, $contactDashlets)) {
384 $query = " UPDATE civicrm_dashboard_contact
385 SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1
386 WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} ";
389 $query = " INSERT INTO civicrm_dashboard_contact
390 ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id )
391 VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )";
393 // fire update query for each column
394 $dao = CRM_Core_DAO
::executeQuery($query);
396 $dashletIDs[] = $dashletID;
402 if (!empty($dashletIDs)) {
403 // we need to disable widget that removed
404 $updateQuery = " UPDATE civicrm_dashboard_contact
406 WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}";
409 // this means all widgets are disabled
410 $updateQuery = " UPDATE civicrm_dashboard_contact
412 WHERE contact_id = {$contactID}";
415 CRM_Core_DAO
::executeQuery($updateQuery);
421 * @param array $params
424 * $dashlet returns dashlet object
426 public static function addDashlet(&$params) {
428 // special case to handle duplicate entries for report instances
429 $dashboardID = CRM_Utils_Array
::value('id', $params);
431 if (!empty($params['instanceURL'])) {
433 FROM `civicrm_dashboard`
434 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
435 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
438 $dashlet = new CRM_Core_DAO_Dashboard();
441 // check url is same as exiting entries, if yes just update existing
442 if (!empty($params['name'])) {
443 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
444 $dashlet->find(TRUE);
447 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
448 $dashlet->find(TRUE);
450 if (empty($params['domain_id'])) {
451 $dashlet->domain_id
= CRM_Core_Config
::domainID();
455 $dashlet->id
= $dashboardID;
458 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
459 $params['permission'] = implode(',', $params['permission']);
461 $dashlet->copyValues($params);
464 // now we need to make dashlet entries for each contact
465 self
::addContactDashlet($dashlet);
475 public static function getDashletName($url) {
476 $urlElements = explode('/', $url);
477 if ($urlElements[1] == 'dashlet') {
478 return $urlElements[2];
480 elseif ($urlElements[1] == 'report') {
481 return 'report/' . $urlElements[3];
487 * Update contact dashboard with new dashlet.
489 * @param object : $dashlet
493 public static function addContactDashlet($dashlet) {
494 $admin = CRM_Core_Permission
::check('administer CiviCRM');
496 // if dashlet is created by admin then you need to add it all contacts.
497 // else just add to contact who is creating this dashlet
498 $contactIDs = array();
500 $query = "SELECT distinct( contact_id )
501 FROM civicrm_dashboard_contact
502 WHERE contact_id NOT IN (
503 SELECT distinct( contact_id )
504 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}
507 $dao = CRM_Core_DAO
::executeQuery($query);
508 while ($dao->fetch()) {
509 $contactIDs[] = $dao->contact_id
;
513 //Get the id of Logged in User
514 $session = CRM_Core_Session
::singleton();
515 $contactID = $session->get('userID');
516 if (!empty($contactID)) {
517 $contactIDs[] = $session->get('userID');
521 if (!empty($contactIDs)) {
522 foreach ($contactIDs as $contactID) {
523 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
526 $valuesString = implode(',', $valuesArray);
528 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
529 VALUES {$valuesString}";
531 CRM_Core_DAO
::executeQuery($query);
536 * @param array $params
537 * Each item is a spec for a dashlet on the contact's dashboard.
540 public static function addContactDashletToDashboard(&$params) {
541 $valuesString = NULL;
543 foreach ($params as $dashboardIDs) {
544 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
545 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
546 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
547 $columns[$column][$dashboardID] = 0;
549 self
::saveDashletChanges($columns, $contactID);
554 * Reset dashlet cache.
556 * @param int $contactID
557 * Reset cache only for specific contact.
561 public static function resetDashletCache($contactID = NULL) {
565 $whereClause = "WHERE contact_id = %1";
566 $params[1] = array($contactID, 'Integer');
568 $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause";
569 $dao = CRM_Core_DAO
::executeQuery($query, $params);
575 * @param int $dashletID
579 public static function deleteDashlet($dashletID) {
580 $dashlet = new CRM_Core_DAO_Dashboard();
581 $dashlet->id
= $dashletID;