3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * Class contains Contact dashboard related functions
39 class CRM_Core_BAO_Dashboard
extends CRM_Core_DAO_Dashboard
{
42 * Get the list of dashlets enabled by admin
44 * @param boolean $all all or only active
46 * @return array $widgets array of dashlets
50 static function getDashlets($all = TRUE) {
52 $dao = new CRM_Core_DAO_Dashboard();
58 $dao->domain_id
= CRM_Core_Config
::domainID();
61 while ($dao->fetch()) {
62 if (!self
::checkPermission($dao->permission
, $dao->permission_operator
)) {
67 CRM_Core_DAO
::storeValues($dao, $values);
68 $dashlets[$dao->id
] = $values;
75 * Function to get the list of dashlets for a contact
77 * Initializes the dashboard with defaults if this is the user's first visit to their dashboard
79 * @param boolean $flatFormat this is true if you want simple associated array of contact dashlets
81 * @return array $dashlets array of dashlets
85 static function getContactDashlets($flatFormat = FALSE, $contactID = NULL) {
89 $contactID = CRM_Core_Session
::singleton()->get('userID');
92 // get contact dashboard dashlets
94 $dao = new CRM_Contact_DAO_DashboardContact();
95 $dao->contact_id
= $contactID;
96 $dao->orderBy('column_no asc, weight asc');
98 while ($dao->fetch()) {
101 if ($dao->is_active
) {
102 // append weight so that order is preserved.
103 $dashlets[$dao->column_no
]["{$dao->weight}-{$dao->dashboard_id}"] = $dao->is_minimized
;
107 $dashlets[$dao->dashboard_id
] = $dao->dashboard_id
;
115 // If empty then initialize contact dashboard for this user
116 $defaultDashlet = self
::initializeDashlets($hasDashlets);
117 return $defaultDashlet ?
$defaultDashlet : $dashlets;
120 static function initializeDashlets($hasDashlets) {
121 $getDashlets = civicrm_api3("Dashboard", "get", array('domain_id' => CRM_Core_Config
::domainID()));
122 $contactID = CRM_Core_Session
::singleton()->get('userID');
124 $allDashlets = CRM_Utils_Array
::index(array('name'), $getDashlets['values']);
125 $defaultDashlets = array();
126 if (!$hasDashlets && CRM_Utils_Array
::value('blog', $allDashlets)) {
127 $defaultDashlets['blog'] = array(
128 'dashboard_id' => $allDashlets['blog']['id'],
131 'contact_id' => $contactID,
132 'domain_id' => CRM_Core_Config
::domainID(),
135 CRM_Utils_Hook
::dashboard_defaults($allDashlets, $defaultDashlets);
136 if (is_array($defaultDashlets) && !empty($defaultDashlets)) {
137 foreach ($defaultDashlets as $defaultDashlet) {
138 if (!self
::checkPermission($getDashlets['values'][$defaultDashlet['dashboard_id']]['permission'],
139 $getDashlets['values'][$defaultDashlet['dashboard_id']]['permission_operator'])) {
140 unset($defaultDashlets[$defaultDashlet]);
144 $assignDashlets = civicrm_api3("dashboard_contact", "create", $defaultDashlet);
145 $dashlets[$defaultDashlet['dashboard_id']] = $defaultDashlet['dashboard_id'];
155 * Function to check dashlet permission for current user
157 * @param string permission string
159 * @return boolean true if use has permission else false
161 static function checkPermission($permission, $operator) {
163 $permissions = explode(',', $permission);
164 $config = CRM_Core_Config
::singleton();
166 static $allComponents;
167 if (!$allComponents) {
168 $allComponents = CRM_Core_Component
::getNames();
171 $hasPermission = FALSE;
172 foreach ($permissions as $key) {
175 $componentName = NULL;
176 if (strpos($key, 'access') === 0) {
177 $componentName = trim(substr($key, 6));
178 if (!in_array($componentName, $allComponents)) {
179 $componentName = NULL;
183 // hack to handle case permissions
184 if (!$componentName && in_array($key, array(
185 'access my cases and activities', 'access all cases and activities'))) {
186 $componentName = 'CiviCase';
189 //hack to determine if it's a component related permission
190 if ($componentName) {
191 if (!in_array($componentName, $config->enableComponents
) ||
192 !CRM_Core_Permission
::check($key)
194 $showDashlet = FALSE;
195 if ($operator == 'AND') {
200 $hasPermission = TRUE;
203 elseif (!CRM_Core_Permission
::check($key)) {
204 $showDashlet = FALSE;
205 if ($operator == 'AND') {
210 $hasPermission = TRUE;
214 if (!$showDashlet && !$hasPermission) {
222 // if permission is not set consider everyone has permission to access it.
228 * Function to get details of each dashlets
230 * @param int $dashletID widget ID
232 * @return array associted array title and content
236 static function getDashletInfo($dashletID) {
237 $dashletInfo = array();
239 $params = array(1 => array($dashletID, 'Integer'));
240 $query = "SELECT name, label, url, fullscreen_url, is_fullscreen FROM civicrm_dashboard WHERE id = %1";
241 $dashboadDAO = CRM_Core_DAO
::executeQuery($query, $params);
242 $dashboadDAO->fetch();
245 $dao = new CRM_Contact_DAO_DashboardContact();
247 $session = CRM_Core_Session
::singleton();
248 $dao->contact_id
= $session->get('userID');
249 $dao->dashboard_id
= $dashletID;
252 //reset content based on the cache time set in config
253 $createdDate = strtotime($dao->created_date
);
254 $dateDiff = round(abs(time() - $createdDate) / 60);
256 $config = CRM_Core_Config
::singleton();
257 if ($config->dashboardCacheTimeout
<= $dateDiff) {
258 $dao->content
= NULL;
261 // if content is empty and url is set, retrieve it from url
262 if (!$dao->content
&& $dashboadDAO->url
) {
263 $url = $dashboadDAO->url
;
266 // -lets use relative url for internal use.
267 // -make sure relative url should not be htmlize.
268 if (substr($dashboadDAO->url
, 0, 4) != 'http') {
269 $urlParam = CRM_Utils_System
::explode('&', $dashboadDAO->url
, 2);
270 $url = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
273 //get content from url
274 $dao->content
= CRM_Utils_System
::getServerResponse($url);
275 $dao->created_date
= date("YmdHis");
279 $dashletInfo = array(
280 'title' => $dashboadDAO->label
,
281 'name' => $dashboadDAO->name
,
282 'content' => $dao->content
,
285 if ($dashboadDAO->is_fullscreen
) {
286 $fullscreenUrl = $dashboadDAO->fullscreen_url
;
287 if (substr($fullscreenUrl, 0, 4) != 'http') {
288 $urlParam = CRM_Utils_System
::explode('&', $dashboadDAO->fullscreen_url
, 2);
289 $fullscreenUrl = CRM_Utils_System
::url($urlParam[0], $urlParam[1], TRUE, NULL, FALSE);
291 $dashletInfo['fullscreenUrl'] = $fullscreenUrl;
297 * Function to save changes made by use to the Dashlet
299 * @param array $columns associated array
305 static function saveDashletChanges($columns, $contactID=NULL) {
306 $session = CRM_Core_Session
::singleton();
308 $contactID = $session->get('userID');
311 if (empty($contactID)) {
312 throw new RuntimeException("Failed to determine contact ID");
315 //we need to get existing dashlets, so we know when to update or insert
316 $contactDashlets = self
::getContactDashlets(TRUE, $contactID);
318 $dashletIDs = array();
319 if (is_array($columns)) {
320 foreach ($columns as $colNo => $dashlets) {
321 if (!is_integer($colNo)) {
325 foreach ($dashlets as $dashletID => $isMinimized) {
326 $isMinimized = (int) $isMinimized;
327 if (in_array($dashletID, $contactDashlets)) {
328 $query = " UPDATE civicrm_dashboard_contact
329 SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1
330 WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} ";
333 $query = " INSERT INTO civicrm_dashboard_contact
334 ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id )
335 VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )";
337 // fire update query for each column
338 $dao = CRM_Core_DAO
::executeQuery($query);
340 $dashletIDs[] = $dashletID;
346 if (!empty($dashletIDs)) {
347 // we need to disable widget that removed
348 $updateQuery = " UPDATE civicrm_dashboard_contact
350 WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}";
353 // this means all widgets are disabled
354 $updateQuery = " UPDATE civicrm_dashboard_contact
356 WHERE contact_id = {$contactID}";
359 CRM_Core_DAO
::executeQuery($updateQuery);
363 * Function to add dashlets
365 * @param array $params associated array
367 * @return object $dashlet returns dashlet object
371 static function addDashlet(&$params) {
373 // special case to handle duplicate entires for report instances
374 $dashboardID = CRM_Utils_Array
::value('id', $params);
376 if (CRM_Utils_Array
::value('instanceURL', $params)) {
378 FROM `civicrm_dashboard`
379 WHERE url LIKE '" . CRM_Utils_Array
::value('instanceURL', $params) . "&%'";
380 $dashboardID = CRM_Core_DAO
::singleValueQuery($query);
383 $dashlet = new CRM_Core_DAO_Dashboard();
386 // check url is same as exiting entries, if yes just update existing
387 if (CRM_Utils_Array
::value('name', $params)) {
388 $dashlet->name
= CRM_Utils_Array
::value('name', $params);
389 $dashlet->find(TRUE);
392 $dashlet->url
= CRM_Utils_Array
::value('url', $params);
393 $dashlet->find(TRUE);
397 $dashlet->id
= $dashboardID;
400 if (is_array(CRM_Utils_Array
::value('permission', $params))) {
401 $params['permission'] = implode(',', $params['permission']);
403 $dashlet->copyValues($params);
405 $dashlet->domain_id
= CRM_Core_Config
::domainID();
409 // now we need to make dashlet entries for each contact
410 self
::addContactDashlet($dashlet);
415 static function getDashletName($url) {
416 $urlElements = explode('/', $url);
417 if ($urlElements[1] == 'dashlet') {
418 return $urlElements[2];
420 elseif ($urlElements[1] == 'report') {
421 return 'report/' . $urlElements[3];
426 * Update contact dashboard with new dashlet
428 * @param object: $dashlet
433 static function addContactDashlet($dashlet) {
434 $admin = CRM_Core_Permission
::check('administer CiviCRM');
436 // if dashlet is created by admin then you need to add it all contacts.
437 // else just add to contact who is creating this dashlet
438 $contactIDs = array();
440 $query = "SELECT distinct( contact_id )
441 FROM civicrm_dashboard_contact
442 WHERE contact_id NOT IN (
443 SELECT distinct( contact_id )
444 FROM civicrm_dashboard_contact WHERE dashboard_id = {$dashlet->id}
447 $dao = CRM_Core_DAO
::executeQuery($query);
448 while ($dao->fetch()) {
449 $contactIDs[] = $dao->contact_id
;
453 //Get the id of Logged in User
454 $session = CRM_Core_Session
::singleton();
455 $contactIDs[] = $session->get('userID');
458 if (!empty($contactIDs)) {
459 foreach ($contactIDs as $contactID) {
460 $valuesArray[] = " ( {$dashlet->id}, {$contactID} )";
463 $valuesString = implode(',', $valuesArray);
465 INSERT INTO civicrm_dashboard_contact ( dashboard_id, contact_id )
466 VALUES {$valuesString}";
468 CRM_Core_DAO
::executeQuery($query);
473 * @param array $params each item is a spec for a dashlet on the contact's dashboard
476 static function addContactDashletToDashboard(&$params) {
477 $valuesString = NULL;
479 foreach ($params as $dashboardIDs) {
480 $contactID = CRM_Utils_Array
::value('contact_id', $dashboardIDs);
481 $dashboardID = CRM_Utils_Array
::value('dashboard_id', $dashboardIDs);
482 $column = CRM_Utils_Array
::value('column_no', $dashboardIDs, 0);
483 $columns[$column][$dashboardID] = 0;
485 self
::saveDashletChanges($columns, $contactID);
490 * Function to reset dashlet cache
492 * @param int $contactID reset cache only for specific contact
497 static function resetDashletCache($contactID = null) {
501 $whereClause = "WHERE contact_id = %1";
502 $params[1] = array($contactID, 'Integer');
504 $query = "UPDATE civicrm_dashboard_contact SET content = NULL $whereClause";
505 $dao = CRM_Core_DAO
::executeQuery($query, $params);
514 static function deleteDashlet($dashletID) {
515 $dashlet = new CRM_Core_DAO_Dashboard();
516 $dashlet->id
= $dashletID;