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
{
42 * @param array $params
48 public static function create($params) {
49 $hook = empty($params['id']) ?
'create' : 'edit';
50 CRM_Utils_Hook
::pre($hook, 'Dashboard', CRM_Utils_Array
::value('id', $params), $params);
51 $dao = self
::addDashlet($params);
52 CRM_Utils_Hook
::post($hook, 'Dashboard', $dao->id
, $dao);
57 * Get the list of dashlets enabled by admin.
61 * @param bool $checkPermission
62 * All or only authorized for the current user.
67 public static function getDashlets($all = TRUE, $checkPermission = TRUE) {
69 $dao = new CRM_Core_DAO_Dashboard();
75 $dao->domain_id
= CRM_Core_Config
::domainID();
78 while ($dao->fetch()) {
79 if ($checkPermission && !self
::checkPermission($dao->permission
, $dao->permission_operator
)) {
84 CRM_Core_DAO
::storeValues($dao, $values);
85 $dashlets[$dao->id
] = $values;
92 * Get the list of dashlets for the current user or the specified user.
94 * Additionlly, initializes the dashboard with defaults if this is the
95 * user's first visit to their dashboard.
97 * @param int $contactID
98 * Defaults to the current user.
103 public static function getContactDashlets($contactID = NULL) {
104 $contactID = $contactID ?
$contactID : CRM_Core_Session
::getLoggedInContactID();
107 // Get contact dashboard dashlets.
108 $results = civicrm_api3('DashboardContact', 'get', [
109 'contact_id' => $contactID,
111 'dashboard_id.is_active' => 1,
112 'options' => ['sort' => 'weight', 'limit' => 0],
119 'dashboard_id.label',
121 'dashboard_id.fullscreen_url',
122 'dashboard_id.cache_minutes',
123 'dashboard_id.permission',
124 'dashboard_id.permission_operator',
128 foreach ($results['values'] as $item) {
129 if (self
::checkPermission(CRM_Utils_Array
::value('dashboard_id.permission', $item), CRM_Utils_Array
::value('dashboard_id.permission_operator', $item))) {
130 $dashlets[$item['id']] = [
131 'dashboard_id' => $item['dashboard_id'],
132 'weight' => $item['weight'],
133 'column_no' => $item['column_no'],
134 'name' => $item['dashboard_id.name'],
135 'label' => $item['dashboard_id.label'],
136 'url' => $item['dashboard_id.url'],
137 'cache_minutes' => $item['dashboard_id.cache_minutes'],
138 'fullscreen_url' => CRM_Utils_Array
::value('dashboard_id.fullscreen_url', $item),
143 // If empty, then initialize default dashlets for this user.
144 if (!$results['count']) {
145 // They may just have disabled all their dashlets. Check if any records exist for this contact.
146 if (!civicrm_api3('DashboardContact', 'getcount', ['contact_id' => $contactID])) {
147 $dashlets = self
::initializeDashlets();
157 public static function getContactDashletsForJS() {
159 foreach (self
::getContactDashlets() as $item) {
160 $data[$item['column_no']][] = [
161 'id' => (int) $item['dashboard_id'],
162 'name' => $item['name'],
163 'title' => $item['label'],
164 'url' => self
::parseUrl($item['url']),
165 'cacheMinutes' => $item['cache_minutes'],
166 'fullscreenUrl' => self
::parseUrl($item['fullscreen_url']),
173 * Setup default dashlets for new users.
175 * When a user accesses their dashboard for the first time, set up
176 * the default dashlets.
179 * Array of dashboard_id's
180 * @throws \CiviCRM_API3_Exception
182 public static function initializeDashlets() {
184 $getDashlets = civicrm_api3("Dashboard", "get", [
185 'domain_id' => CRM_Core_Config
::domainID(),
188 $contactID = CRM_Core_Session
::getLoggedInContactID();
189 $allDashlets = CRM_Utils_Array
::index(['name'], $getDashlets['values']);
190 $defaultDashlets = [];
191 $defaults = ['blog' => 1, 'getting-started' => '0'];
192 foreach ($defaults as $name => $column) {
193 if (!empty($allDashlets[$name]) && !empty($allDashlets[$name]['id'])) {
194 $defaultDashlets[$name] = [
195 'dashboard_id' => $allDashlets[$name]['id'],
197 'column_no' => $column,
198 'contact_id' => $contactID,
202 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
203 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
204 foreach ($defaultDashlets as $id => $defaultDashlet) {
205 $dashboard_id = $defaultDashlet['dashboard_id'];
206 $dashlet = $getDashlets['values'][$dashboard_id];
207 if (!self
::checkPermission(CRM_Utils_Array
::value('permission', $dashlet), CRM_Utils_Array
::value('permission_operator', $dashlet))) {
211 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
212 $values = $assignDashlets['values'][$assignDashlets['id']];
213 $dashlets[$assignDashlets['id']] = [
214 'dashboard_id' => $values['dashboard_id'],
215 'weight' => $values['weight'],
216 'column_no' => $values['column_no'],
217 'name' => $dashlet['name'],
218 'label' => $dashlet['label'],
219 'cache_minutes' => $dashlet['cache_minutes'],
220 'url' => $dashlet['url'],
221 'fullscreen_url' => CRM_Utils_Array
::value('fullscreen_url', $dashlet),
233 public static function parseUrl($url) {
234 // Check if it is already a fully-formed url
235 if ($url && substr($url, 0, 4) != 'http' && $url[0] != '/') {
236 $urlParam = explode('?', $url);
237 $url = CRM_Utils_System
::url($urlParam[0], CRM_Utils_Array
::value(1, $urlParam), FALSE, NULL, FALSE);
243 * Check dashlet permission for current user.
245 * @param string $permission
246 * Comma separated list.
247 * @param string $operator
250 * true if use has permission else false
252 public static function checkPermission($permission, $operator) {
254 $permissions = explode(',', $permission);
255 $config = CRM_Core_Config
::singleton();
257 static $allComponents;
258 if (!$allComponents) {
259 $allComponents = CRM_Core_Component
::getNames();
262 $hasPermission = FALSE;
263 foreach ($permissions as $key) {
266 $componentName = NULL;
267 if (strpos($key, 'access') === 0) {
268 $componentName = trim(substr($key, 6));
269 if (!in_array($componentName, $allComponents)) {
270 $componentName = NULL;
274 // hack to handle case permissions
276 && in_array($key, ['access my cases and activities', 'access all cases and activities'])
278 $componentName = 'CiviCase';
281 //hack to determine if it's a component related permission
282 if ($componentName) {
283 if (!in_array($componentName, $config->enableComponents
) ||
284 !CRM_Core_Permission
::check($key)
286 $showDashlet = FALSE;
287 if ($operator == 'AND') {
292 $hasPermission = TRUE;
295 elseif (!CRM_Core_Permission
::check($key)) {
296 $showDashlet = FALSE;
297 if ($operator == 'AND') {
302 $hasPermission = TRUE;
306 if (!$showDashlet && !$hasPermission) {
314 // if permission is not set consider everyone has permission to access it.
320 * Save changes made by user to the Dashlet.
322 * @param array $columns
324 * @param int $contactID
326 * @throws RuntimeException
328 public static function saveDashletChanges($columns, $contactID = NULL) {
330 $contactID = CRM_Core_Session
::getLoggedInContactID();
333 if (empty($contactID)) {
334 throw new RuntimeException("Failed to determine contact ID");
338 if (is_array($columns)) {
339 foreach ($columns as $colNo => $dashlets) {
340 if (!is_int($colNo)) {
344 foreach ($dashlets as $dashletID => $isMinimized) {
345 $dashletID = (int) $dashletID;
346 $query = "INSERT INTO civicrm_dashboard_contact
347 (weight, column_no, is_active, dashboard_id, contact_id)
348 VALUES({$weight}, {$colNo}, 1, {$dashletID}, {$contactID})
349 ON DUPLICATE KEY UPDATE weight = {$weight}, column_no = {$colNo}, is_active = 1";
350 // fire update query for each column
351 CRM_Core_DAO
::executeQuery($query);
353 $dashletIDs[] = $dashletID;
359 // Disable inactive widgets
360 $dashletClause = $dashletIDs ?
"dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)';
361 $updateQuery = "UPDATE civicrm_dashboard_contact
363 WHERE $dashletClause AND contact_id = {$contactID}";
365 CRM_Core_DAO
::executeQuery($updateQuery);
371 * @param array $params
374 * $dashlet returns dashlet object
376 public static function addDashlet(&$params) {
378 // special case to handle duplicate entries for report instances
379 $dashboardID = CRM_Utils_Array
::value('id', $params);
381 if (!empty($params['instanceURL'])) {
383 FROM `civicrm_dashboard`
384 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
385 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
388 $dashlet = new CRM_Core_DAO_Dashboard();
391 // check url is same as exiting entries, if yes just update existing
392 if (!empty($params['name'])) {
393 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
394 $dashlet->find(TRUE);
397 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
398 $dashlet->find(TRUE);
400 if (empty($params['domain_id'])) {
401 $dashlet->domain_id
= CRM_Core_Config
::domainID();
405 $dashlet->id
= $dashboardID;
408 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
409 $params['permission'] = implode(',', $params['permission']);
411 $dashlet->copyValues($params);
414 // now we need to make dashlet entries for each contact
415 self
::addContactDashlet($dashlet);
421 * Update contact dashboard with new dashlet.
423 * @param object $dashlet
425 public static function addContactDashlet($dashlet) {
426 $admin = CRM_Core_Permission
::check('administer CiviCRM');
428 // if dashlet is created by admin then you need to add it all contacts.
429 // else just add to contact who is creating this dashlet
432 $query = "SELECT distinct( contact_id )
433 FROM civicrm_dashboard_contact";
434 $dao = CRM_Core_DAO
::executeQuery($query);
435 while ($dao->fetch()) {
436 $contactIDs[$dao->contact_id
] = NULL;
440 //Get the id of Logged in User
441 $contactID = CRM_Core_Session
::getLoggedInContactID();
442 if (!empty($contactID)) {
443 $contactIDs[$contactID] = NULL;
447 // Remove contact ids that already have this dashlet to avoid DB
448 // constraint violation.
449 $query = "SELECT distinct( contact_id )
450 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}";
451 $dao = CRM_Core_DAO
::executeQuery($query);
452 while ($dao->fetch()) {
453 if (array_key_exists($dao->contact_id
, $contactIDs)) {
454 unset($contactIDs[$dao->contact_id
]);
457 if (!empty($contactIDs)) {
458 foreach ($contactIDs as $contactID => $value) {
459 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
461 $valuesString = implode(',', $valuesArray);
463 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
464 VALUES {$valuesString}";
466 CRM_Core_DAO
::executeQuery($query);
471 * @param array $params
472 * Each item is a spec for a dashlet on the contact's dashboard.
475 public static function addContactDashletToDashboard(&$params) {
476 $valuesString = NULL;
478 foreach ($params as $dashboardIDs) {
479 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
480 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
481 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
482 $columns[$column][$dashboardID] = 0;
484 self
::saveDashletChanges($columns, $contactID);
491 * @param int $dashletID
495 public static function deleteDashlet($dashletID) {
496 $dashlet = new CRM_Core_DAO_Dashboard();
497 $dashlet->id
= $dashletID;
498 if (!$dashlet->find(TRUE)) {