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