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