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