3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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
::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', 'limit' => 0),
118 'dashboard_id.label',
120 'dashboard_id.fullscreen_url',
121 'dashboard_id.cache_minutes',
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 'name' => $item['dashboard_id.name'],
134 'label' => $item['dashboard_id.label'],
135 'url' => $item['dashboard_id.url'],
136 'cache_minutes' => $item['dashboard_id.cache_minutes'],
137 'fullscreen_url' => CRM_Utils_Array
::value('dashboard_id.fullscreen_url', $item),
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();
156 public static function getContactDashletsForJS() {
157 $data = array(array(), array());
158 foreach (self
::getContactDashlets() as $item) {
159 $data[$item['column_no']][] = array(
160 'id' => (int) $item['dashboard_id'],
161 'name' => $item['name'],
162 'title' => $item['label'],
163 'url' => self
::parseUrl($item['url']),
164 'cacheMinutes' => $item['cache_minutes'],
165 'fullscreenUrl' => self
::parseUrl($item['fullscreen_url']),
172 * Setup default dashlets for new users.
174 * When a user accesses their dashboard for the first time, set up
175 * the default dashlets.
178 * Array of dashboard_id's
179 * @throws \CiviCRM_API3_Exception
181 public static function initializeDashlets() {
183 $getDashlets = civicrm_api3("Dashboard", "get", array(
184 'domain_id' => CRM_Core_Config
::domainID(),
187 $contactID = CRM_Core_Session
::getLoggedInContactID();
188 $allDashlets = CRM_Utils_Array
::index(array('name'), $getDashlets['values']);
189 $defaultDashlets = array();
190 $defaults = array('blog' => 1, 'getting-started' => '0');
191 foreach ($defaults as $name => $column) {
192 if (!empty($allDashlets[$name]) && !empty($allDashlets[$name]['id'])) {
193 $defaultDashlets[$name] = array(
194 'dashboard_id' => $allDashlets[$name]['id'],
196 'column_no' => $column,
197 'contact_id' => $contactID,
201 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
202 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
203 foreach ($defaultDashlets as $id => $defaultDashlet) {
204 $dashboard_id = $defaultDashlet['dashboard_id'];
205 $dashlet = $getDashlets['values'][$dashboard_id];
206 if (!self
::checkPermission(CRM_Utils_Array
::value('permission', $dashlet), CRM_Utils_Array
::value('permission_operator', $dashlet))) {
210 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
211 $values = $assignDashlets['values'][$assignDashlets['id']];
212 $dashlets[$assignDashlets['id']] = array(
213 'dashboard_id' => $values['dashboard_id'],
214 'weight' => $values['weight'],
215 'column_no' => $values['column_no'],
216 'name' => $dashlet['name'],
217 'label' => $dashlet['label'],
218 'cache_minutes' => $dashlet['cache_minutes'],
219 'url' => $dashlet['url'],
220 'fullscreen_url' => CRM_Utils_Array
::value('fullscreen_url', $dashlet),
232 public static function parseUrl($url) {
233 // Check if it is already a fully-formed url
234 if ($url && substr($url, 0, 4) != 'http' && $url[0] != '/') {
235 $urlParam = explode('?', $url);
236 $url = CRM_Utils_System
::url($urlParam[0], CRM_Utils_Array
::value(1, $urlParam), FALSE, NULL, FALSE);
242 * Check dashlet permission for current user.
244 * @param string $permission
245 * Comma separated list.
246 * @param string $operator
249 * true if use has permission else false
251 public static function checkPermission($permission, $operator) {
253 $permissions = explode(',', $permission);
254 $config = CRM_Core_Config
::singleton();
256 static $allComponents;
257 if (!$allComponents) {
258 $allComponents = CRM_Core_Component
::getNames();
261 $hasPermission = FALSE;
262 foreach ($permissions as $key) {
265 $componentName = NULL;
266 if (strpos($key, 'access') === 0) {
267 $componentName = trim(substr($key, 6));
268 if (!in_array($componentName, $allComponents)) {
269 $componentName = NULL;
273 // hack to handle case permissions
274 if (!$componentName && in_array($key, array(
275 'access my cases and activities',
276 'access all cases and activities',
279 $componentName = 'CiviCase';
282 //hack to determine if it's a component related permission
283 if ($componentName) {
284 if (!in_array($componentName, $config->enableComponents
) ||
285 !CRM_Core_Permission
::check($key)
287 $showDashlet = FALSE;
288 if ($operator == 'AND') {
293 $hasPermission = TRUE;
296 elseif (!CRM_Core_Permission
::check($key)) {
297 $showDashlet = FALSE;
298 if ($operator == 'AND') {
303 $hasPermission = TRUE;
307 if (!$showDashlet && !$hasPermission) {
315 // if permission is not set consider everyone has permission to access it.
321 * Save changes made by user to the Dashlet.
323 * @param array $columns
325 * @param int $contactID
327 * @throws RuntimeException
329 public static function saveDashletChanges($columns, $contactID = NULL) {
331 $contactID = CRM_Core_Session
::getLoggedInContactID();
334 if (empty($contactID)) {
335 throw new RuntimeException("Failed to determine contact ID");
338 $dashletIDs = array();
339 if (is_array($columns)) {
340 foreach ($columns as $colNo => $dashlets) {
341 if (!is_int($colNo)) {
345 foreach ($dashlets as $dashletID => $isMinimized) {
346 $dashletID = (int) $dashletID;
347 $query = "INSERT INTO civicrm_dashboard_contact
348 (weight, column_no, is_active, dashboard_id, contact_id)
349 VALUES({$weight}, {$colNo}, 1, {$dashletID}, {$contactID})
350 ON DUPLICATE KEY UPDATE weight = {$weight}, column_no = {$colNo}, is_active = 1";
351 // fire update query for each column
352 CRM_Core_DAO
::executeQuery($query);
354 $dashletIDs[] = $dashletID;
360 // Disable inactive widgets
361 $dashletClause = $dashletIDs ?
"dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)';
362 $updateQuery = "UPDATE civicrm_dashboard_contact
364 WHERE $dashletClause AND contact_id = {$contactID}";
366 CRM_Core_DAO
::executeQuery($updateQuery);
372 * @param array $params
375 * $dashlet returns dashlet object
377 public static function addDashlet(&$params) {
379 // special case to handle duplicate entries for report instances
380 $dashboardID = CRM_Utils_Array
::value('id', $params);
382 if (!empty($params['instanceURL'])) {
384 FROM `civicrm_dashboard`
385 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
386 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
389 $dashlet = new CRM_Core_DAO_Dashboard();
392 // check url is same as exiting entries, if yes just update existing
393 if (!empty($params['name'])) {
394 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
395 $dashlet->find(TRUE);
398 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
399 $dashlet->find(TRUE);
401 if (empty($params['domain_id'])) {
402 $dashlet->domain_id
= CRM_Core_Config
::domainID();
406 $dashlet->id
= $dashboardID;
409 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
410 $params['permission'] = implode(',', $params['permission']);
412 $dashlet->copyValues($params);
415 // now we need to make dashlet entries for each contact
416 self
::addContactDashlet($dashlet);
422 * Update contact dashboard with new dashlet.
424 * @param object $dashlet
426 public static function addContactDashlet($dashlet) {
427 $admin = CRM_Core_Permission
::check('administer CiviCRM');
429 // if dashlet is created by admin then you need to add it all contacts.
430 // else just add to contact who is creating this dashlet
431 $contactIDs = array();
433 $query = "SELECT distinct( contact_id )
434 FROM civicrm_dashboard_contact";
435 $dao = CRM_Core_DAO
::executeQuery($query);
436 while ($dao->fetch()) {
437 $contactIDs[$dao->contact_id
] = NULL;
441 //Get the id of Logged in User
442 $contactID = CRM_Core_Session
::getLoggedInContactID();
443 if (!empty($contactID)) {
444 $contactIDs[$contactID] = NULL;
448 // Remove contact ids that already have this dashlet to avoid DB
449 // constraint violation.
450 $query = "SELECT distinct( contact_id )
451 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}";
452 $dao = CRM_Core_DAO
::executeQuery($query);
453 while ($dao->fetch()) {
454 if (array_key_exists($dao->contact_id
, $contactIDs)) {
455 unset($contactIDs[$dao->contact_id
]);
458 if (!empty($contactIDs)) {
459 foreach ($contactIDs as $contactID => $value) {
460 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
462 $valuesString = implode(',', $valuesArray);
464 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
465 VALUES {$valuesString}";
467 CRM_Core_DAO
::executeQuery($query);
472 * @param array $params
473 * Each item is a spec for a dashlet on the contact's dashboard.
476 public static function addContactDashletToDashboard(&$params) {
477 $valuesString = NULL;
479 foreach ($params as $dashboardIDs) {
480 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
481 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
482 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
483 $columns[$column][$dashboardID] = 0;
485 self
::saveDashletChanges($columns, $contactID);
492 * @param int $dashletID
496 public static function deleteDashlet($dashletID) {
497 $dashlet = new CRM_Core_DAO_Dashboard();
498 $dashlet->id
= $dashletID;
499 if (!$dashlet->find(TRUE)) {