getting started should available for new users
[civicrm-core.git] / CRM / Core / BAO / Dashboard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Class contains Contact dashboard related functions
38 */
39class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
3cfa8e5e 40 /**
fe482240 41 * Add Dashboard.
3cfa8e5e 42 *
6a0b768e
TO
43 * @param array $params
44 * Values.
3cfa8e5e 45 *
3cfa8e5e
EM
46 *
47 * @return object
48 */
00be9182 49 public static function create($params) {
3cfa8e5e
EM
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);
54 return $dao;
55 }
6a488035
TO
56
57 /**
fe482240 58 * Get the list of dashlets enabled by admin.
6a488035 59 *
6a0b768e
TO
60 * @param bool $all
61 * All or only active.
62 * @param bool $checkPermission
63 * All or only authorized for the current user.
6a488035 64 *
a6c01b45
CW
65 * @return array
66 * array of dashlets
6a488035 67 */
00be9182 68 public static function getDashlets($all = TRUE, $checkPermission = TRUE) {
6a488035
TO
69 $dashlets = array();
70 $dao = new CRM_Core_DAO_Dashboard();
71
72 if (!$all) {
73 $dao->is_active = 1;
74 }
75
76 $dao->domain_id = CRM_Core_Config::domainID();
77
78 $dao->find();
79 while ($dao->fetch()) {
ee117e9c 80 if ($checkPermission && !self::checkPermission($dao->permission, $dao->permission_operator)) {
6a488035
TO
81 continue;
82 }
83
84 $values = array();
85 CRM_Core_DAO::storeValues($dao, $values);
86 $dashlets[$dao->id] = $values;
87 }
88
89 return $dashlets;
90 }
91
92 /**
08727453 93 * Get the list of dashlets for the current user or the specified user.
6a488035 94 *
5aa910d4
JM
95 * Additionlly, initializes the dashboard with defaults if this is the
96 * user's first visit to their dashboard.
6a488035 97 *
6a0b768e
TO
98 * @param bool $flatFormat
99 * This is true if you want simple associated.
16b10e64 100 * array of all the contact's dashlets whether or not they are enabled.
6a488035 101 *
6a0b768e
TO
102 * @param int $contactID
103 * Provide the dashlets for the contact id.
16b10e64 104 * passed rather than the current user.
77b97be7 105 *
a6c01b45
CW
106 * @return array
107 * array of dashlets
6a488035 108 */
00be9182 109 public static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
6a488035
TO
110 $dashlets = array();
111
15d9b3ae
N
112 if (!$contactID) {
113 $contactID = CRM_Core_Session::singleton()->get('userID');
114 }
6a488035 115
5aa910d4 116 // Get contact dashboard dashlets.
6a488035
TO
117 $hasDashlets = FALSE;
118 $dao = new CRM_Contact_DAO_DashboardContact();
119 $dao->contact_id = $contactID;
120 $dao->orderBy('column_no asc, weight asc');
121 $dao->find();
122 while ($dao->fetch()) {
5aa910d4
JM
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.
6a488035
TO
126 $hasDashlets = TRUE;
127 if (!$flatFormat) {
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;
131 }
132 }
133 else {
134 $dashlets[$dao->dashboard_id] = $dao->dashboard_id;
135 }
136 }
137
138 if ($flatFormat) {
139 return $dashlets;
140 }
141
5aa910d4 142 // If empty, then initialize contact dashboard for this user.
08727453 143 if (!$hasDashlets) {
98662f1c 144 return self::initializeDashlets($flatFormat);
5aa910d4
JM
145 }
146 return $dashlets;
15d9b3ae
N
147 }
148
b5c2afd0 149 /**
fe482240 150 * Setup default dashlets for new users.
b5c2afd0 151 *
5aa910d4
JM
152 * When a user accesses their dashboard for the first time, set up
153 * the default dashlets.
154 *
a6c01b45 155 * @return array
16b10e64 156 * Array of dashboard_id's
b5c2afd0 157 */
98662f1c 158 public static function initializeDashlets($flatFormat = FALSE) {
5aa910d4 159 $dashlets = array();
353ffa53
TO
160 $getDashlets = civicrm_api3("Dashboard", "get", array(
161 'domain_id' => CRM_Core_Config::domainID(),
408b79bf 162 'option.limit' => 0,
353ffa53 163 ));
15d9b3ae 164 $contactID = CRM_Core_Session::singleton()->get('userID');
15d9b3ae
N
165 $allDashlets = CRM_Utils_Array::index(array('name'), $getDashlets['values']);
166 $defaultDashlets = array();
40c0dd53 167 $defualts = array('blog' => 1, 'getting-started' => '0');
168 foreach ($defualts as $name => $column) {
169 if (!empty($allDashlets[$name])) {
170 $defaultDashlets[$name] = array(
171 'dashboard_id' => $allDashlets[$name]['id'],
172 'is_active' => 1,
173 'column_no' => $column,
174 'contact_id' => $contactID,
175 );
176 }
15d9b3ae
N
177 }
178 CRM_Utils_Hook::dashboard_defaults($allDashlets, $defaultDashlets);
179 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
5aa910d4
JM
180 foreach ($defaultDashlets as $id => $defaultDashlet) {
181 $dashboard_id = $defaultDashlet['dashboard_id'];
182 if (!self::checkPermission($getDashlets['values'][$dashboard_id]['permission'],
5ea06c79 183 CRM_Utils_Array::value('permission_operator', $getDashlets['values'][$dashboard_id]))
353ffa53 184 ) {
15d9b3ae
N
185 continue;
186 }
187 else {
188 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
98662f1c 189 if (!$flatFormat) {
190 $values = $assignDashlets['values'][$assignDashlets['id']];
7dd6f516 191 $dashlets[$values['column_no']][$values['weight'] - $values['dashboard_id']] = $values['is_minimized'];
98662f1c 192 }
193 else {
194 $dashlets[$dashboard_id] = $defaultDashlet['dashboard_id'];
195 }
6a488035 196 }
6a488035
TO
197 }
198 }
5aa910d4 199 return $dashlets;
6a488035 200 }
08727453 201
6a488035
TO
202
203 /**
fe482240 204 * Check dashlet permission for current user.
6a488035 205 *
6a0b768e
TO
206 * @param string $permission
207 * Comma separated list.
c490a46a 208 * @param string $operator
6a488035 209 *
408b79bf 210 * @return bool
a6c01b45 211 * true if use has permission else false
6a488035 212 */
00be9182 213 public static function checkPermission($permission, $operator) {
6a488035
TO
214 if ($permission) {
215 $permissions = explode(',', $permission);
216 $config = CRM_Core_Config::singleton();
217
218 static $allComponents;
219 if (!$allComponents) {
220 $allComponents = CRM_Core_Component::getNames();
221 }
222
223 $hasPermission = FALSE;
224 foreach ($permissions as $key) {
225 $showDashlet = TRUE;
226
227 $componentName = NULL;
228 if (strpos($key, 'access') === 0) {
229 $componentName = trim(substr($key, 6));
230 if (!in_array($componentName, $allComponents)) {
231 $componentName = NULL;
232 }
233 }
234
235 // hack to handle case permissions
236 if (!$componentName && in_array($key, array(
353ffa53 237 'access my cases and activities',
408b79bf 238 'access all cases and activities',
353ffa53
TO
239 ))
240 ) {
6a488035
TO
241 $componentName = 'CiviCase';
242 }
243
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)
248 ) {
249 $showDashlet = FALSE;
250 if ($operator == 'AND') {
251 return $showDashlet;
252 }
253 }
254 else {
255 $hasPermission = TRUE;
256 }
257 }
258 elseif (!CRM_Core_Permission::check($key)) {
259 $showDashlet = FALSE;
260 if ($operator == 'AND') {
261 return $showDashlet;
262 }
263 }
264 else {
265 $hasPermission = TRUE;
266 }
267 }
268
269 if (!$showDashlet && !$hasPermission) {
270 return FALSE;
271 }
272 else {
273 return TRUE;
274 }
275 }
276 else {
277 // if permission is not set consider everyone has permission to access it.
278 return TRUE;
279 }
280 }
281
282 /**
fe482240 283 * Get details of each dashlets.
6a488035 284 *
6a0b768e
TO
285 * @param int $dashletID
286 * Widget ID.
6a488035 287 *
a6c01b45
CW
288 * @return array
289 * associted array title and content
6a488035 290 */
00be9182 291 public static function getDashletInfo($dashletID) {
6a488035
TO
292 $dashletInfo = array();
293
294 $params = array(1 => array($dashletID, 'Integer'));
15d9b3ae 295 $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
6a488035
TO
296 $dashboadDAO = CRM_Core_DAO::executeQuery($query, $params);
297 $dashboadDAO->fetch();
298
299 // build the content
300 $dao = new CRM_Contact_DAO_DashboardContact();
301
353ffa53
TO
302 $session = CRM_Core_Session::singleton();
303 $dao->contact_id = $session->get('userID');
6a488035
TO
304 $dao->dashboard_id = $dashletID;
305 $dao->find(TRUE);
306
307 //reset content based on the cache time set in config
308 $createdDate = strtotime($dao->created_date);
309 $dateDiff = round(abs(time() - $createdDate) / 60);
310
311 $config = CRM_Core_Config::singleton();
312 if ($config->dashboardCacheTimeout <= $dateDiff) {
313 $dao->content = NULL;
314 }
315
316 // if content is empty and url is set, retrieve it from url
317 if (!$dao->content && $dashboadDAO->url) {
318 $url = $dashboadDAO->url;
319
320 // CRM-7087
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') {
36ffcc5f 324 $urlParam = explode('?', $dashboadDAO->url);
6a488035
TO
325 $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
326 }
327
328 //get content from url
329 $dao->content = CRM_Utils_System::getServerResponse($url);
330 $dao->created_date = date("YmdHis");
331 $dao->save();
332 }
333
334 $dashletInfo = array(
335 'title' => $dashboadDAO->label,
15d9b3ae 336 'name' => $dashboadDAO->name,
6a488035
TO
337 'content' => $dao->content,
338 );
339
340 if ($dashboadDAO->is_fullscreen) {
341 $fullscreenUrl = $dashboadDAO->fullscreen_url;
342 if (substr($fullscreenUrl, 0, 4) != 'http') {
36ffcc5f 343 $urlParam = explode('?', $dashboadDAO->fullscreen_url);
6a488035
TO
344 $fullscreenUrl = CRM_Utils_System::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
345 }
346 $dashletInfo['fullscreenUrl'] = $fullscreenUrl;
347 }
348 return $dashletInfo;
349 }
350
351 /**
fe482240 352 * Save changes made by use to the Dashlet.
6a488035 353 *
6a0b768e 354 * @param array $columns
6a488035 355 *
100fef9d 356 * @param int $contactID
77b97be7
EM
357 *
358 * @throws RuntimeException
6a488035 359 * @return void
6a488035 360 */
2aa397bc 361 public static function saveDashletChanges($columns, $contactID = NULL) {
15d9b3ae
N
362 $session = CRM_Core_Session::singleton();
363 if (!$contactID) {
364 $contactID = $session->get('userID');
365 }
366
f583d89b
TO
367 if (empty($contactID)) {
368 throw new RuntimeException("Failed to determine contact ID");
369 }
6a488035 370
dcf56200 371 //we need to get existing dashlets, so we know when to update or insert
15d9b3ae 372 $contactDashlets = self::getContactDashlets(TRUE, $contactID);
6a488035
TO
373
374 $dashletIDs = array();
375 if (is_array($columns)) {
376 foreach ($columns as $colNo => $dashlets) {
408b79bf 377 if (!is_int($colNo)) {
6a488035
TO
378 continue;
379 }
380 $weight = 1;
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} ";
387 }
388 else {
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} )";
392 }
393 // fire update query for each column
394 $dao = CRM_Core_DAO::executeQuery($query);
395
396 $dashletIDs[] = $dashletID;
397 $weight++;
398 }
399 }
400 }
401
402 if (!empty($dashletIDs)) {
403 // we need to disable widget that removed
404 $updateQuery = " UPDATE civicrm_dashboard_contact
405 SET is_active = 0
406 WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}";
407 }
408 else {
409 // this means all widgets are disabled
410 $updateQuery = " UPDATE civicrm_dashboard_contact
411 SET is_active = 0
412 WHERE contact_id = {$contactID}";
413 }
414
415 CRM_Core_DAO::executeQuery($updateQuery);
416 }
417
418 /**
fe482240 419 * Add dashlets.
6a488035 420 *
6a0b768e 421 * @param array $params
6a488035 422 *
a6c01b45
CW
423 * @return object
424 * $dashlet returns dashlet object
6a488035 425 */
00be9182 426 public static function addDashlet(&$params) {
6a488035 427
32c93376 428 // special case to handle duplicate entries for report instances
15d9b3ae
N
429 $dashboardID = CRM_Utils_Array::value('id', $params);
430
a7488080 431 if (!empty($params['instanceURL'])) {
6a488035
TO
432 $query = "SELECT id
433 FROM `civicrm_dashboard`
434 WHERE url LIKE '" . CRM_Utils_Array::value('instanceURL', $params) . "&%'";
435 $dashboardID = CRM_Core_DAO::singleValueQuery($query);
436 }
437
438 $dashlet = new CRM_Core_DAO_Dashboard();
439
440 if (!$dashboardID) {
441 // check url is same as exiting entries, if yes just update existing
a7488080 442 if (!empty($params['name'])) {
15d9b3ae
N
443 $dashlet->name = CRM_Utils_Array::value('name', $params);
444 $dashlet->find(TRUE);
445 }
446 else {
447 $dashlet->url = CRM_Utils_Array::value('url', $params);
448 $dashlet->find(TRUE);
449 }
3cfa8e5e
EM
450 if (empty($params['domain_id'])) {
451 $dashlet->domain_id = CRM_Core_Config::domainID();
452 }
6a488035
TO
453 }
454 else {
455 $dashlet->id = $dashboardID;
456 }
457
458 if (is_array(CRM_Utils_Array::value('permission', $params))) {
459 $params['permission'] = implode(',', $params['permission']);
460 }
461 $dashlet->copyValues($params);
6a488035
TO
462 $dashlet->save();
463
464 // now we need to make dashlet entries for each contact
465 self::addContactDashlet($dashlet);
466
467 return $dashlet;
468 }
469
b5c2afd0
EM
470 /**
471 * @param $url
472 *
473 * @return string
474 */
00be9182 475 public static function getDashletName($url) {
fa4916a4 476 $urlElements = explode('/', $url);
477 if ($urlElements[1] == 'dashlet') {
478 return $urlElements[2];
479 }
480 elseif ($urlElements[1] == 'report') {
481 return 'report/' . $urlElements[3];
482 }
483 return $url;
484 }
353ffa53 485
6a488035 486 /**
fe482240 487 * Update contact dashboard with new dashlet.
6a488035 488 *
353ffa53 489 * @param object : $dashlet
6a488035
TO
490 *
491 * @return void
6a488035 492 */
00be9182 493 public static function addContactDashlet($dashlet) {
6a488035
TO
494 $admin = CRM_Core_Permission::check('administer CiviCRM');
495
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();
499 if ($admin) {
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}
505 )";
506
507 $dao = CRM_Core_DAO::executeQuery($query);
508 while ($dao->fetch()) {
509 $contactIDs[] = $dao->contact_id;
510 }
511 }
512 else {
513 //Get the id of Logged in User
514 $session = CRM_Core_Session::singleton();
155a0ed0 515 $contactID = $session->get('userID');
22e263ad 516 if (!empty($contactID)) {
155a0ed0
JM
517 $contactIDs[] = $session->get('userID');
518 }
6a488035
TO
519 }
520
521 if (!empty($contactIDs)) {
522 foreach ($contactIDs as $contactID) {
523 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
524 }
525
526 $valuesString = implode(',', $valuesArray);
527 $query = "
528 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
529 VALUES {$valuesString}";
530
531 CRM_Core_DAO::executeQuery($query);
532 }
533 }
534
dcf56200 535 /**
6a0b768e
TO
536 * @param array $params
537 * Each item is a spec for a dashlet on the contact's dashboard.
dcf56200
TO
538 * @return bool
539 */
00be9182 540 public static function addContactDashletToDashboard(&$params) {
15d9b3ae
N
541 $valuesString = NULL;
542 $columns = array();
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;
548 }
549 self::saveDashletChanges($columns, $contactID);
550 return TRUE;
551 }
552
6a488035 553 /**
fe482240 554 * Reset dashlet cache.
6a488035 555 *
6a0b768e
TO
556 * @param int $contactID
557 * Reset cache only for specific contact.
6a488035
TO
558 *
559 * @return void
6a488035 560 */
2aa397bc
TO
561 public static function resetDashletCache($contactID = NULL) {
562 $whereClause = NULL;
6a488035
TO
563 $params = array();
564 if ($contactID) {
565 $whereClause = "WHERE contact_id = %1";
566 $params[1] = array($contactID, 'Integer');
567 }
568 $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause";
569 $dao = CRM_Core_DAO::executeQuery($query, $params);
570 }
571
572 /**
fe482240 573 * Delete Dashlet.
6a488035 574 *
100fef9d 575 * @param int $dashletID
2a6da8d7 576 *
6a488035 577 * @return void
6a488035 578 */
00be9182 579 public static function deleteDashlet($dashletID) {
6a488035
TO
580 $dashlet = new CRM_Core_DAO_Dashboard();
581 $dashlet->id = $dashletID;
582 $dashlet->delete();
15d9b3ae 583 return TRUE;
6a488035 584 }
96025800 585
6a488035 586}