3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
{
43 * @param array $params
50 public static function create($params) {
51 $hook = empty($params['id']) ?
'create' : 'edit';
52 CRM_Utils_Hook
::pre($hook, 'Dashboard', CRM_Utils_Array
::value('id', $params), $params);
53 $dao = self
::addDashlet($params);
54 CRM_Utils_Hook
::post($hook, 'Dashboard', $dao->id
, $dao);
59 * Get the list of dashlets enabled by admin
63 * @param bool $checkPermission
64 * All or only authorized for the current user.
66 * @return array $widgets array of dashlets
69 public static function getDashlets($all = TRUE, $checkPermission = TRUE) {
71 $dao = new CRM_Core_DAO_Dashboard();
77 $dao->domain_id
= CRM_Core_Config
::domainID();
80 while ($dao->fetch()) {
81 if ($checkPermission && !self
::checkPermission($dao->permission
, $dao->permission_operator
)) {
86 CRM_Core_DAO
::storeValues($dao, $values);
87 $dashlets[$dao->id
] = $values;
94 * Get the list of dashlets for the current user or the specified user.
96 * Additionlly, initializes the dashboard with defaults if this is the
97 * user's first visit to their dashboard.
99 * @param bool $flatFormat
100 * This is true if you want simple associated.
101 * array of all the contact's dashlets whether or not they are enabled.
103 * @param int $contactID
104 * Provide the dashlets for the contact id.
105 * passed rather than the current user.
107 * @return array $dashlets array of dashlets
110 public static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
114 $contactID = CRM_Core_Session
::singleton()->get('userID');
117 // Get contact dashboard dashlets.
118 $hasDashlets = FALSE;
119 $dao = new CRM_Contact_DAO_DashboardContact();
120 $dao->contact_id
= $contactID;
121 $dao->orderBy('column_no asc, weight asc');
123 while ($dao->fetch()) {
124 // When a dashlet is removed, it stays in the table with status disabled,
125 // so even if a user decides not to have any dashlets show, they will still
126 // have records in the table to indicate that we are not newly initializing.
129 if ($dao->is_active
) {
130 // append weight so that order is preserved.
131 $dashlets[$dao->column_no
]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized
;
135 $dashlets[$dao->dashboard_id
] = $dao->dashboard_id
;
143 // If empty, then initialize contact dashboard for this user.
145 return self
::initializeDashlets();
151 * Setup default dashlets for new users
153 * When a user accesses their dashboard for the first time, set up
154 * the default dashlets.
156 * @return array of dashboard_id's
158 public static function initializeDashlets() {
160 $getDashlets = civicrm_api3("Dashboard", "get", array('domain_id' => CRM_Core_Config
::domainID(), 'option.limit' => 0));
161 $contactID = CRM_Core_Session
::singleton()->get('userID');
162 $allDashlets = CRM_Utils_Array
::index(array('name'), $getDashlets['values']);
163 $defaultDashlets = array();
164 if (CRM_Utils_Array
::value('blog', $allDashlets)) {
165 $defaultDashlets['blog'] = array(
166 'dashboard_id' => $allDashlets['blog']['id'],
169 'contact_id' => $contactID,
172 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
173 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
174 foreach ($defaultDashlets as $id => $defaultDashlet) {
175 $dashboard_id = $defaultDashlet['dashboard_id'];
176 if (!self
::checkPermission($getDashlets['values'][$dashboard_id]['permission'],
177 $getDashlets['values'][$dashboard_id]['permission_operator'])) {
181 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
182 $dashlets[$dashboard_id] = $defaultDashlet['dashboard_id'];
191 * Check dashlet permission for current user
193 * @param string $permission
194 * Comma separated list.
195 * @param string $operator
197 * @return boolean true if use has permission else false
199 public static function checkPermission($permission, $operator) {
201 $permissions = explode(',', $permission);
202 $config = CRM_Core_Config
::singleton();
204 static $allComponents;
205 if (!$allComponents) {
206 $allComponents = CRM_Core_Component
::getNames();
209 $hasPermission = FALSE;
210 foreach ($permissions as $key) {
213 $componentName = NULL;
214 if (strpos($key, 'access') === 0) {
215 $componentName = trim(substr($key, 6));
216 if (!in_array($componentName, $allComponents)) {
217 $componentName = NULL;
221 // hack to handle case permissions
222 if (!$componentName && in_array($key, array(
223 'access my cases and activities', 'access all cases and activities'))) {
224 $componentName = 'CiviCase';
227 //hack to determine if it's a component related permission
228 if ($componentName) {
229 if (!in_array($componentName, $config->enableComponents
) ||
230 !CRM_Core_Permission
::check($key)
232 $showDashlet = FALSE;
233 if ($operator == 'AND') {
238 $hasPermission = TRUE;
241 elseif (!CRM_Core_Permission
::check($key)) {
242 $showDashlet = FALSE;
243 if ($operator == 'AND') {
248 $hasPermission = TRUE;
252 if (!$showDashlet && !$hasPermission) {
260 // if permission is not set consider everyone has permission to access it.
266 * Get details of each dashlets
268 * @param int $dashletID
271 * @return array associted array title and content
274 public static function getDashletInfo($dashletID) {
275 $dashletInfo = array();
277 $params = array(1 => array($dashletID, 'Integer'));
278 $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
279 $dashboadDAO = CRM_Core_DAO
::executeQuery($query, $params);
280 $dashboadDAO->fetch();
283 $dao = new CRM_Contact_DAO_DashboardContact();
285 $session = CRM_Core_Session
::singleton();
286 $dao->contact_id
= $session->get('userID');
287 $dao->dashboard_id
= $dashletID;
290 //reset content based on the cache time set in config
291 $createdDate = strtotime($dao->created_date
);
292 $dateDiff = round(abs(time() - $createdDate) / 60);
294 $config = CRM_Core_Config
::singleton();
295 if ($config->dashboardCacheTimeout
<= $dateDiff) {
296 $dao->content
= NULL;
299 // if content is empty and url is set, retrieve it from url
300 if (!$dao->content
&& $dashboadDAO->url
) {
301 $url = $dashboadDAO->url
;
304 // -lets use relative url for internal use.
305 // -make sure relative url should not be htmlize.
306 if (substr($dashboadDAO->url
, 0, 4) != 'http') {
307 $urlParam = explode('?', $dashboadDAO->url
);
308 $url = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
311 //get content from url
312 $dao->content
= CRM_Utils_System
::getServerResponse($url);
313 $dao->created_date
= date("YmdHis");
317 $dashletInfo = array(
318 'title' => $dashboadDAO->label
,
319 'name' => $dashboadDAO->name
,
320 'content' => $dao->content
,
323 if ($dashboadDAO->is_fullscreen
) {
324 $fullscreenUrl = $dashboadDAO->fullscreen_url
;
325 if (substr($fullscreenUrl, 0, 4) != 'http') {
326 $urlParam = explode('?', $dashboadDAO->fullscreen_url
);
327 $fullscreenUrl = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
329 $dashletInfo['fullscreenUrl'] = $fullscreenUrl;
335 * Save changes made by use to the Dashlet
337 * @param array $columns
340 * @param int $contactID
342 * @throws RuntimeException
346 public static function saveDashletChanges($columns, $contactID = NULL) {
347 $session = CRM_Core_Session
::singleton();
349 $contactID = $session->get('userID');
352 if (empty($contactID)) {
353 throw new RuntimeException("Failed to determine contact ID");
356 //we need to get existing dashlets, so we know when to update or insert
357 $contactDashlets = self
::getContactDashlets(TRUE, $contactID);
359 $dashletIDs = array();
360 if (is_array($columns)) {
361 foreach ($columns as $colNo => $dashlets) {
362 if (!is_integer($colNo)) {
366 foreach ($dashlets as $dashletID => $isMinimized) {
367 $isMinimized = (int) $isMinimized;
368 if (in_array($dashletID, $contactDashlets)) {
369 $query = " UPDATE civicrm_dashboard_contact
370 SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1
371 WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} ";
374 $query = " INSERT INTO civicrm_dashboard_contact
375 ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id )
376 VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )";
378 // fire update query for each column
379 $dao = CRM_Core_DAO
::executeQuery($query);
381 $dashletIDs[] = $dashletID;
387 if (!empty($dashletIDs)) {
388 // we need to disable widget that removed
389 $updateQuery = " UPDATE civicrm_dashboard_contact
391 WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}";
394 // this means all widgets are disabled
395 $updateQuery = " UPDATE civicrm_dashboard_contact
397 WHERE contact_id = {$contactID}";
400 CRM_Core_DAO
::executeQuery($updateQuery);
406 * @param array $params
409 * @return object $dashlet returns dashlet object
412 public static function addDashlet(&$params) {
414 // special case to handle duplicate entries for report instances
415 $dashboardID = CRM_Utils_Array
::value('id', $params);
417 if (!empty($params['instanceURL'])) {
419 FROM `civicrm_dashboard`
420 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
421 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
424 $dashlet = new CRM_Core_DAO_Dashboard();
427 // check url is same as exiting entries, if yes just update existing
428 if (!empty($params['name'])) {
429 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
430 $dashlet->find(TRUE);
433 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
434 $dashlet->find(TRUE);
436 if (empty($params['domain_id'])) {
437 $dashlet->domain_id
= CRM_Core_Config
::domainID();
441 $dashlet->id
= $dashboardID;
444 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
445 $params['permission'] = implode(',', $params['permission']);
447 $dashlet->copyValues($params);
450 // now we need to make dashlet entries for each contact
451 self
::addContactDashlet($dashlet);
461 public static function getDashletName($url) {
462 $urlElements = explode('/', $url);
463 if ($urlElements[1] == 'dashlet') {
464 return $urlElements[2];
466 elseif ($urlElements[1] == 'report') {
467 return 'report/' . $urlElements[3];
472 * Update contact dashboard with new dashlet
474 * @param object: $dashlet
479 public static function addContactDashlet($dashlet) {
480 $admin = CRM_Core_Permission
::check('administer CiviCRM');
482 // if dashlet is created by admin then you need to add it all contacts.
483 // else just add to contact who is creating this dashlet
484 $contactIDs = array();
486 $query = "SELECT distinct( contact_id )
487 FROM civicrm_dashboard_contact
488 WHERE contact_id NOT IN (
489 SELECT distinct( contact_id )
490 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}
493 $dao = CRM_Core_DAO
::executeQuery($query);
494 while ($dao->fetch()) {
495 $contactIDs[] = $dao->contact_id
;
499 //Get the id of Logged in User
500 $session = CRM_Core_Session
::singleton();
501 $contactID = $session->get('userID');
502 if (!empty($contactID)) {
503 $contactIDs[] = $session->get('userID');
507 if (!empty($contactIDs)) {
508 foreach ($contactIDs as $contactID) {
509 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
512 $valuesString = implode(',', $valuesArray);
514 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
515 VALUES {$valuesString}";
517 CRM_Core_DAO
::executeQuery($query);
522 * @param array $params
523 * Each item is a spec for a dashlet on the contact's dashboard.
526 public static function addContactDashletToDashboard(&$params) {
527 $valuesString = NULL;
529 foreach ($params as $dashboardIDs) {
530 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
531 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
532 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
533 $columns[$column][$dashboardID] = 0;
535 self
::saveDashletChanges($columns, $contactID);
540 * Reset dashlet cache
542 * @param int $contactID
543 * Reset cache only for specific contact.
548 public static function resetDashletCache($contactID = NULL) {
552 $whereClause = "WHERE contact_id = %1";
553 $params[1] = array($contactID, 'Integer');
555 $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause";
556 $dao = CRM_Core_DAO
::executeQuery($query, $params);
562 * @param int $dashletID
567 public static function deleteDashlet($dashletID) {
568 $dashlet = new CRM_Core_DAO_Dashboard();
569 $dashlet->id
= $dashletID;