CRM-17663 - Cruft removal
[civicrm-core.git] / CRM / Core / BAO / Dashboard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
192d36c5 35 * Class contains Contact dashboard related functions.
6a488035
TO
36 */
37class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
3cfa8e5e 38 /**
fe482240 39 * Add Dashboard.
3cfa8e5e 40 *
6a0b768e
TO
41 * @param array $params
42 * Values.
3cfa8e5e 43 *
3cfa8e5e
EM
44 *
45 * @return object
46 */
00be9182 47 public static function create($params) {
3cfa8e5e
EM
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);
52 return $dao;
53 }
6a488035
TO
54
55 /**
fe482240 56 * Get the list of dashlets enabled by admin.
6a488035 57 *
6a0b768e
TO
58 * @param bool $all
59 * All or only active.
60 * @param bool $checkPermission
61 * All or only authorized for the current user.
6a488035 62 *
a6c01b45
CW
63 * @return array
64 * array of dashlets
6a488035 65 */
00be9182 66 public static function getDashlets($all = TRUE, $checkPermission = TRUE) {
6a488035
TO
67 $dashlets = array();
68 $dao = new CRM_Core_DAO_Dashboard();
69
70 if (!$all) {
71 $dao->is_active = 1;
72 }
73
74 $dao->domain_id = CRM_Core_Config::domainID();
75
76 $dao->find();
77 while ($dao->fetch()) {
ee117e9c 78 if ($checkPermission && !self::checkPermission($dao->permission, $dao->permission_operator)) {
6a488035
TO
79 continue;
80 }
81
82 $values = array();
83 CRM_Core_DAO::storeValues($dao, $values);
84 $dashlets[$dao->id] = $values;
85 }
86
87 return $dashlets;
88 }
89
90 /**
08727453 91 * Get the list of dashlets for the current user or the specified user.
6a488035 92 *
5aa910d4
JM
93 * Additionlly, initializes the dashboard with defaults if this is the
94 * user's first visit to their dashboard.
6a488035 95 *
6a0b768e 96 * @param int $contactID
94864a5f 97 * Defaults to the current user.
77b97be7 98 *
a6c01b45
CW
99 * @return array
100 * array of dashlets
6a488035 101 */
94864a5f
CW
102 public static function getContactDashlets($contactID = NULL) {
103 $contactID = $contactID ? $contactID : CRM_Core_Session::singleton()->getLoggedInContactID();
6a488035
TO
104 $dashlets = array();
105
5aa910d4 106 // Get contact dashboard dashlets.
dd3770bc 107 $results = civicrm_api3('DashboardContact', 'get', array(
94864a5f 108 'contact_id' => $contactID,
dd3770bc
CW
109 'is_active' => 1,
110 'dashboard_id.is_active' => 1,
111 'options' => array('sort' => 'weight'),
112 'return' => array(
113 'id',
114 'weight',
115 'column_no',
dd3770bc
CW
116 'dashboard_id',
117 'dashboard_id.name',
118 'dashboard_id.label',
119 'dashboard_id.url',
120 'dashboard_id.fullscreen_url',
121 'dashboard_id.permission',
122 'dashboard_id.permission_operator',
123 ),
94864a5f 124 ));
5f3f6ec3 125
94864a5f 126 foreach ($results['values'] as $item) {
dd3770bc
CW
127 if (self::checkPermission(CRM_Utils_Array::value('dashboard_id.permission', $item), CRM_Utils_Array::value('dashboard_id.permission_operator', $item))) {
128 $dashlets[$item['id']] = array(
129 'dashboard_id' => $item['dashboard_id'],
130 'weight' => $item['weight'],
131 'column_no' => $item['column_no'],
dd3770bc
CW
132 'name' => $item['dashboard_id.name'],
133 'label' => $item['dashboard_id.label'],
134 'url' => $item['dashboard_id.url'],
135 'fullscreen_url' => $item['dashboard_id.fullscreen_url'],
136 );
6a488035
TO
137 }
138 }
139
dd3770bc 140 // If empty, then initialize default dashlets for this user.
94864a5f 141 if (!$results['count']) {
dd3770bc
CW
142 // They may just have disabled all their dashlets. Check if any records exist for this contact.
143 if (!civicrm_api3('DashboardContact', 'getcount', array('contact_id' => $contactID))) {
144 $dashlets = self::initializeDashlets();
145 }
5aa910d4 146 }
94864a5f 147
5aa910d4 148 return $dashlets;
15d9b3ae
N
149 }
150
242055d3
CW
151 /**
152 * @return array
153 */
dd3770bc
CW
154 public static function getContactDashletsForJS() {
155 $data = array(array(), array());
156 foreach (self::getContactDashlets() as $item) {
157 $data[$item['column_no']][] = array(
158 'id' => (int) $item['dashboard_id'],
dd3770bc
CW
159 'name' => $item['name'],
160 'title' => $item['label'],
161 'url' => self::parseUrl($item['url']),
162 'fullscreenUrl' => self::parseUrl($item['fullscreen_url']),
163 );
164 }
165 return $data;
166 }
167
b5c2afd0 168 /**
fe482240 169 * Setup default dashlets for new users.
b5c2afd0 170 *
5aa910d4
JM
171 * When a user accesses their dashboard for the first time, set up
172 * the default dashlets.
173 *
a6c01b45 174 * @return array
ad37ac8e 175 * Array of dashboard_id's
176 * @throws \CiviCRM_API3_Exception
b5c2afd0 177 */
94864a5f 178 public static function initializeDashlets() {
5aa910d4 179 $dashlets = array();
353ffa53
TO
180 $getDashlets = civicrm_api3("Dashboard", "get", array(
181 'domain_id' => CRM_Core_Config::domainID(),
408b79bf 182 'option.limit' => 0,
353ffa53 183 ));
5f3f6ec3 184 $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
15d9b3ae
N
185 $allDashlets = CRM_Utils_Array::index(array('name'), $getDashlets['values']);
186 $defaultDashlets = array();
c202dd9e 187 $defaults = array('blog' => 1, 'getting-started' => '0');
188 foreach ($defaults as $name => $column) {
fbd19c80 189 if (!empty($allDashlets[$name]) && !empty($allDashlets[$name]['id'])) {
40c0dd53 190 $defaultDashlets[$name] = array(
191 'dashboard_id' => $allDashlets[$name]['id'],
192 'is_active' => 1,
193 'column_no' => $column,
194 'contact_id' => $contactID,
195 );
196 }
15d9b3ae
N
197 }
198 CRM_Utils_Hook::dashboard_defaults($allDashlets, $defaultDashlets);
199 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
5aa910d4
JM
200 foreach ($defaultDashlets as $id => $defaultDashlet) {
201 $dashboard_id = $defaultDashlet['dashboard_id'];
dd3770bc
CW
202 $dashlet = $getDashlets['values'][$dashboard_id];
203 if (!self::checkPermission(CRM_Utils_Array::value('permission', $dashlet), CRM_Utils_Array::value('permission_operator', $dashlet))) {
15d9b3ae
N
204 continue;
205 }
206 else {
207 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
94864a5f 208 $values = $assignDashlets['values'][$assignDashlets['id']];
dd3770bc
CW
209 $dashlets[$assignDashlets['id']] = array(
210 'dashboard_id' => $values['dashboard_id'],
211 'weight' => $values['weight'],
212 'column_no' => $values['column_no'],
dd3770bc
CW
213 'name' => $dashlet['name'],
214 'label' => $dashlet['label'],
215 'url' => $dashlet['url'],
216 'fullscreen_url' => $dashlet['fullscreen_url'],
217 );
6a488035 218 }
6a488035
TO
219 }
220 }
5aa910d4 221 return $dashlets;
6a488035 222 }
08727453 223
dd3770bc
CW
224 /**
225 * @param $url
226 * @return string
227 */
228 public static function parseUrl($url) {
229 if (substr($url, 0, 4) != 'http') {
230 $urlParam = explode('?', $url);
231 $url = CRM_Utils_System::url($urlParam[0], $urlParam[1], FALSE, NULL, FALSE);
232 }
233 return $url;
234 }
6a488035
TO
235
236 /**
fe482240 237 * Check dashlet permission for current user.
6a488035 238 *
6a0b768e
TO
239 * @param string $permission
240 * Comma separated list.
c490a46a 241 * @param string $operator
6a488035 242 *
408b79bf 243 * @return bool
a6c01b45 244 * true if use has permission else false
6a488035 245 */
00be9182 246 public static function checkPermission($permission, $operator) {
6a488035
TO
247 if ($permission) {
248 $permissions = explode(',', $permission);
249 $config = CRM_Core_Config::singleton();
250
251 static $allComponents;
252 if (!$allComponents) {
253 $allComponents = CRM_Core_Component::getNames();
254 }
255
256 $hasPermission = FALSE;
257 foreach ($permissions as $key) {
258 $showDashlet = TRUE;
259
260 $componentName = NULL;
261 if (strpos($key, 'access') === 0) {
262 $componentName = trim(substr($key, 6));
263 if (!in_array($componentName, $allComponents)) {
264 $componentName = NULL;
265 }
266 }
267
268 // hack to handle case permissions
269 if (!$componentName && in_array($key, array(
353ffa53 270 'access my cases and activities',
408b79bf 271 'access all cases and activities',
353ffa53
TO
272 ))
273 ) {
6a488035
TO
274 $componentName = 'CiviCase';
275 }
276
277 //hack to determine if it's a component related permission
278 if ($componentName) {
279 if (!in_array($componentName, $config->enableComponents) ||
280 !CRM_Core_Permission::check($key)
281 ) {
282 $showDashlet = FALSE;
283 if ($operator == 'AND') {
284 return $showDashlet;
285 }
286 }
287 else {
288 $hasPermission = TRUE;
289 }
290 }
291 elseif (!CRM_Core_Permission::check($key)) {
292 $showDashlet = FALSE;
293 if ($operator == 'AND') {
294 return $showDashlet;
295 }
296 }
297 else {
298 $hasPermission = TRUE;
299 }
300 }
301
302 if (!$showDashlet && !$hasPermission) {
303 return FALSE;
304 }
305 else {
306 return TRUE;
307 }
308 }
309 else {
310 // if permission is not set consider everyone has permission to access it.
311 return TRUE;
312 }
313 }
314
315 /**
242055d3 316 * Save changes made by user to the Dashlet.
6a488035 317 *
6a0b768e 318 * @param array $columns
6a488035 319 *
100fef9d 320 * @param int $contactID
77b97be7
EM
321 *
322 * @throws RuntimeException
6a488035 323 */
2aa397bc 324 public static function saveDashletChanges($columns, $contactID = NULL) {
15d9b3ae 325 if (!$contactID) {
ce2cc43e 326 $contactID = CRM_Core_Session::getLoggedInContactID();
15d9b3ae
N
327 }
328
f583d89b
TO
329 if (empty($contactID)) {
330 throw new RuntimeException("Failed to determine contact ID");
331 }
6a488035 332
6a488035
TO
333 $dashletIDs = array();
334 if (is_array($columns)) {
335 foreach ($columns as $colNo => $dashlets) {
408b79bf 336 if (!is_int($colNo)) {
6a488035
TO
337 continue;
338 }
339 $weight = 1;
340 foreach ($dashlets as $dashletID => $isMinimized) {
00c27b41
CW
341 $dashletID = (int) $dashletID;
342 $query = "INSERT INTO civicrm_dashboard_contact
242055d3
CW
343 (weight, column_no, is_active, dashboard_id, contact_id)
344 VALUES({$weight}, {$colNo}, 1, {$dashletID}, {$contactID})
345 ON DUPLICATE KEY UPDATE weight = {$weight}, column_no = {$colNo}, is_active = 1";
6a488035 346 // fire update query for each column
00c27b41 347 CRM_Core_DAO::executeQuery($query);
6a488035
TO
348
349 $dashletIDs[] = $dashletID;
350 $weight++;
351 }
352 }
353 }
354
00c27b41
CW
355 // Disable inactive widgets
356 $dashletClause = $dashletIDs ? "dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)';
357 $updateQuery = "UPDATE civicrm_dashboard_contact
358 SET is_active = 0
359 WHERE $dashletClause AND contact_id = {$contactID}";
6a488035
TO
360
361 CRM_Core_DAO::executeQuery($updateQuery);
362 }
363
364 /**
fe482240 365 * Add dashlets.
6a488035 366 *
6a0b768e 367 * @param array $params
6a488035 368 *
a6c01b45
CW
369 * @return object
370 * $dashlet returns dashlet object
6a488035 371 */
00be9182 372 public static function addDashlet(&$params) {
6a488035 373
32c93376 374 // special case to handle duplicate entries for report instances
15d9b3ae
N
375 $dashboardID = CRM_Utils_Array::value('id', $params);
376
a7488080 377 if (!empty($params['instanceURL'])) {
6a488035
TO
378 $query = "SELECT id
379 FROM `civicrm_dashboard`
380 WHERE url LIKE '" . CRM_Utils_Array::value('instanceURL', $params) . "&%'";
381 $dashboardID = CRM_Core_DAO::singleValueQuery($query);
382 }
383
384 $dashlet = new CRM_Core_DAO_Dashboard();
385
386 if (!$dashboardID) {
387 // check url is same as exiting entries, if yes just update existing
a7488080 388 if (!empty($params['name'])) {
15d9b3ae
N
389 $dashlet->name = CRM_Utils_Array::value('name', $params);
390 $dashlet->find(TRUE);
391 }
392 else {
393 $dashlet->url = CRM_Utils_Array::value('url', $params);
394 $dashlet->find(TRUE);
395 }
3cfa8e5e
EM
396 if (empty($params['domain_id'])) {
397 $dashlet->domain_id = CRM_Core_Config::domainID();
398 }
6a488035
TO
399 }
400 else {
401 $dashlet->id = $dashboardID;
402 }
403
404 if (is_array(CRM_Utils_Array::value('permission', $params))) {
405 $params['permission'] = implode(',', $params['permission']);
406 }
407 $dashlet->copyValues($params);
6a488035
TO
408 $dashlet->save();
409
410 // now we need to make dashlet entries for each contact
411 self::addContactDashlet($dashlet);
412
413 return $dashlet;
414 }
415
416 /**
fe482240 417 * Update contact dashboard with new dashlet.
6a488035 418 *
192d36c5 419 * @param object $dashlet
6a488035 420 */
00be9182 421 public static function addContactDashlet($dashlet) {
6a488035
TO
422 $admin = CRM_Core_Permission::check('administer CiviCRM');
423
424 // if dashlet is created by admin then you need to add it all contacts.
425 // else just add to contact who is creating this dashlet
426 $contactIDs = array();
427 if ($admin) {
428 $query = "SELECT distinct( contact_id )
429 FROM civicrm_dashboard_contact
430 WHERE contact_id NOT IN (
431 SELECT distinct( contact_id )
432 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}
433 )";
434
435 $dao = CRM_Core_DAO::executeQuery($query);
436 while ($dao->fetch()) {
437 $contactIDs[] = $dao->contact_id;
438 }
439 }
440 else {
441 //Get the id of Logged in User
442 $session = CRM_Core_Session::singleton();
155a0ed0 443 $contactID = $session->get('userID');
22e263ad 444 if (!empty($contactID)) {
155a0ed0
JM
445 $contactIDs[] = $session->get('userID');
446 }
6a488035
TO
447 }
448
449 if (!empty($contactIDs)) {
450 foreach ($contactIDs as $contactID) {
451 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
452 }
453
454 $valuesString = implode(',', $valuesArray);
455 $query = "
456 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
457 VALUES {$valuesString}";
458
459 CRM_Core_DAO::executeQuery($query);
460 }
461 }
462
dcf56200 463 /**
6a0b768e
TO
464 * @param array $params
465 * Each item is a spec for a dashlet on the contact's dashboard.
dcf56200
TO
466 * @return bool
467 */
00be9182 468 public static function addContactDashletToDashboard(&$params) {
15d9b3ae
N
469 $valuesString = NULL;
470 $columns = array();
471 foreach ($params as $dashboardIDs) {
472 $contactID = CRM_Utils_Array::value('contact_id', $dashboardIDs);
473 $dashboardID = CRM_Utils_Array::value('dashboard_id', $dashboardIDs);
474 $column = CRM_Utils_Array::value('column_no', $dashboardIDs, 0);
475 $columns[$column][$dashboardID] = 0;
476 }
477 self::saveDashletChanges($columns, $contactID);
478 return TRUE;
479 }
480
6a488035 481 /**
fe482240 482 * Delete Dashlet.
6a488035 483 *
100fef9d 484 * @param int $dashletID
2a6da8d7 485 *
192d36c5 486 * @return bool
6a488035 487 */
00be9182 488 public static function deleteDashlet($dashletID) {
6a488035
TO
489 $dashlet = new CRM_Core_DAO_Dashboard();
490 $dashlet->id = $dashletID;
491 $dashlet->delete();
15d9b3ae 492 return TRUE;
6a488035 493 }
96025800 494
6a488035 495}