Merge pull request #22316 from braders/core-3003-preserve-tab-between-pageloads
[civicrm-core.git] / CRM / Core / Block.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Defines a simple implementation of a drupal block.
20 *
21 * Blocks definitions and html are in a smarty template file.
22 */
23 class CRM_Core_Block {
24
25 /**
26 * The following blocks are supported.
27 *
28 * @var int
29 */
30 const
31 CREATE_NEW = 1,
32 RECENTLY_VIEWED = 2,
33 DASHBOARD = 3,
34 ADD = 4,
35 LANGSWITCH = 5,
36 EVENT = 6,
37 FULLTEXT_SEARCH = 7;
38
39 /**
40 * Template file names for the above blocks.
41 * @var array
42 */
43 public static $_properties = NULL;
44
45 /**
46 * Class constructor.
47 */
48 public function __construct() {
49 }
50
51 /**
52 * Initialises the $_properties array
53 */
54 public static function initProperties() {
55 if (!defined('BLOCK_CACHE_GLOBAL')) {
56 define('BLOCK_CACHE_GLOBAL', 0x0008);
57 }
58
59 if (!defined('BLOCK_CACHE_PER_PAGE')) {
60 define('BLOCK_CACHE_PER_PAGE', 0x0004);
61 }
62
63 if (!defined('BLOCK_NO_CACHE')) {
64 define('BLOCK_NO_CACHE', -1);
65 }
66
67 if (!(self::$_properties)) {
68 $config = CRM_Core_Config::singleton();
69 self::$_properties = array(
70 // set status item to 0 to disable block by default (at install)
71 self::CREATE_NEW => array(
72 'template' => 'CreateNew.tpl',
73 'info' => ts('CiviCRM Create New Record'),
74 'subject' => '',
75 'active' => TRUE,
76 'cache' => BLOCK_CACHE_GLOBAL,
77 'visibility' => 1,
78 'weight' => -100,
79 'status' => 1,
80 'pages' => "civicrm\ncivicrm/*",
81 'region' => $config->userSystem->getDefaultBlockLocation(),
82 ),
83 self::RECENTLY_VIEWED => array(
84 'template' => 'RecentlyViewed.tpl',
85 'info' => ts('CiviCRM Recent Items'),
86 'subject' => ts('Recent Items'),
87 'active' => TRUE,
88 'cache' => BLOCK_NO_CACHE,
89 'visibility' => 1,
90 'weight' => -99,
91 'status' => 1,
92 'pages' => "civicrm\ncivicrm/*",
93 'region' => $config->userSystem->getDefaultBlockLocation(),
94 ),
95 self::DASHBOARD => array(
96 'template' => 'Dashboard.tpl',
97 'info' => ts('CiviCRM Contact Dashboard'),
98 'subject' => '',
99 'active' => TRUE,
100 'cache' => BLOCK_NO_CACHE,
101 'visibility' => 1,
102 'weight' => -98,
103 'status' => 1,
104 'pages' => "civicrm\ncivicrm/*",
105 'region' => $config->userSystem->getDefaultBlockLocation(),
106 ),
107 self::ADD => array(
108 'template' => 'Add.tpl',
109 'info' => ts('CiviCRM Quick Add'),
110 'subject' => ts('New Individual'),
111 'active' => TRUE,
112 'cache' => BLOCK_NO_CACHE,
113 'visibility' => 1,
114 'weight' => -97,
115 'status' => 1,
116 'pages' => "civicrm\ncivicrm/*",
117 'region' => $config->userSystem->getDefaultBlockLocation(),
118 ),
119 self::LANGSWITCH => array(
120 'template' => 'LangSwitch.tpl',
121 'info' => ts('CiviCRM Language Switcher'),
122 'subject' => '',
123 'templateValues' => [],
124 'active' => TRUE,
125 'cache' => BLOCK_NO_CACHE,
126 'visibility' => 1,
127 'weight' => -96,
128 'status' => 1,
129 'pages' => "civicrm\ncivicrm/*",
130 'region' => $config->userSystem->getDefaultBlockLocation(),
131 ),
132 self::EVENT => array(
133 'template' => 'Event.tpl',
134 'info' => ts('CiviCRM Upcoming Events'),
135 'subject' => ts('Upcoming Events'),
136 'templateValues' => [],
137 'active' => TRUE,
138 'cache' => BLOCK_NO_CACHE,
139 'visibility' => 1,
140 'weight' => -95,
141 'status' => 0,
142 'pages' => "civicrm\ncivicrm/*",
143 'region' => $config->userSystem->getDefaultBlockLocation(),
144 ),
145 self::FULLTEXT_SEARCH => array(
146 'template' => 'FullTextSearch.tpl',
147 'info' => ts('CiviCRM Full-text Search'),
148 'subject' => ts('Full-text Search'),
149 'active' => TRUE,
150 'cache' => BLOCK_NO_CACHE,
151 'visibility' => 1,
152 'weight' => -94,
153 'status' => 0,
154 'pages' => "civicrm\ncivicrm/*",
155 'region' => $config->userSystem->getDefaultBlockLocation(),
156 ),
157 );
158
159 ksort(self::$_properties);
160 }
161 }
162
163 /**
164 * Returns the desired property from the $_properties array
165 *
166 * @param int $id
167 * One of the class constants (ADD, SEARCH, etc.).
168 * @param string $property
169 * The desired property.
170 *
171 * @return string
172 * the value of the desired property
173 */
174 public static function getProperty($id, $property) {
175 if (!(self::$_properties)) {
176 self::initProperties();
177 }
178 return self::$_properties[$id][$property] ?? NULL;
179 }
180
181 /**
182 * Sets the desired property in the $_properties array
183 *
184 * @param int $id
185 * One of the class constants (ADD, SEARCH, etc.).
186 * @param string $property
187 * The desired property.
188 * @param string $value
189 * The value of the desired property.
190 */
191 public static function setProperty($id, $property, $value) {
192 if (!(self::$_properties)) {
193 self::initProperties();
194 }
195 self::$_properties[$id][$property] = $value;
196 }
197
198 /**
199 * Returns the whole $_properties array.
200 *
201 * @return array
202 * the $_properties array
203 */
204 public static function properties() {
205 if (!(self::$_properties)) {
206 self::initProperties();
207 }
208 return self::$_properties;
209 }
210
211 /**
212 * Creates the info block for drupal.
213 *
214 * @return array
215 */
216 public static function getInfo() {
217
218 $block = [];
219 foreach (self::properties() as $id => $value) {
220 if ($value['active']) {
221 if (in_array($id, array(
222 self::ADD,
223 self::CREATE_NEW,
224 ))) {
225 $hasAccess = TRUE;
226 if (!CRM_Core_Permission::check('add contacts') &&
227 !CRM_Core_Permission::check('edit groups')
228 ) {
229 $hasAccess = FALSE;
230 }
231 //validate across edit/view - CRM-5666
232 if ($hasAccess && ($id == self::ADD)) {
233 $hasAccess = CRM_Core_Permission::giveMeAllACLs();
234 }
235 if (!$hasAccess) {
236 continue;
237 }
238 }
239
240 if ($id == self::EVENT &&
241 (!CRM_Core_Permission::access('CiviEvent', FALSE) ||
242 !CRM_Core_Permission::check('view event info')
243 )
244 ) {
245 continue;
246 }
247
248 $block[$id] = array(
249 'info' => $value['info'],
250 'cache' => $value['cache'],
251 'region' => $value['region'],
252 'visibility' => $value['visibility'],
253 'pages' => $value['pages'],
254 'status' => $value['status'],
255 'weight' => $value['weight'],
256 );
257 }
258 }
259
260 return $block;
261 }
262
263 /**
264 * Set the post action values for the block.
265 *
266 * php is lame and u cannot call functions from static initializers
267 * hence this hack
268 *
269 * @param int $id
270 */
271 private static function setTemplateValues($id) {
272 switch ($id) {
273 case self::CREATE_NEW:
274 self::setTemplateShortcutValues();
275 break;
276
277 case self::DASHBOARD:
278 self::setTemplateDashboardValues();
279 break;
280
281 case self::ADD:
282 $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
283 $defaultPrimaryLocationId = $defaultLocation->id;
284 $values = array(
285 'postURL' => CRM_Utils_System::url('civicrm/contact/add', 'reset=1&ct=Individual'),
286 'primaryLocationType' => $defaultPrimaryLocationId,
287 );
288
289 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
290 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
291 }
292
293 self::setProperty(self::ADD,
294 'templateValues',
295 $values
296 );
297 break;
298
299 case self::LANGSWITCH:
300 // gives the currentPath without trailing empty lcMessages to be completed
301 $values = array('queryString' => CRM_Utils_System::getLinksUrl('lcMessages', TRUE, FALSE, FALSE));
302 self::setProperty(self::LANGSWITCH, 'templateValues', $values);
303 break;
304
305 case self::FULLTEXT_SEARCH:
306 $urlArray = array(
307 'fullTextSearchID' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
308 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
309 ),
310 );
311 self::setProperty(self::FULLTEXT_SEARCH, 'templateValues', $urlArray);
312 break;
313
314 case self::RECENTLY_VIEWED:
315 $recent = CRM_Utils_Recent::get();
316 self::setProperty(self::RECENTLY_VIEWED, 'templateValues', array('recentlyViewed' => $recent));
317 break;
318
319 case self::EVENT:
320 self::setTemplateEventValues();
321 break;
322 }
323 }
324
325 /**
326 * Create the list of options to create New objects for the application and format is as a block.
327 */
328 private static function setTemplateShortcutValues() {
329 static $shortCuts = [];
330
331 if (!($shortCuts)) {
332 if (CRM_Core_Permission::check('add contacts')) {
333 if (CRM_Core_Permission::giveMeAllACLs()) {
334 $shortCuts = CRM_Contact_BAO_ContactType::getCreateNewList();
335 }
336 }
337
338 // new activity (select target contact)
339 $shortCuts = array_merge($shortCuts, array(
340 array(
341 'path' => 'civicrm/activity',
342 'query' => 'action=add&reset=1&context=standalone',
343 'ref' => 'new-activity',
344 'title' => ts('Activity'),
345 ),
346 ));
347
348 $components = CRM_Core_Component::getEnabledComponents();
349
350 if ($components) {
351 // check if we can process credit card contribs
352 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
353
354 foreach ($components as $obj) {
355 $obj->creatNewShortcut($shortCuts, $newCredit);
356 }
357 }
358
359 // new email (select recipients)
360 $shortCuts = array_merge($shortCuts, array(
361 array(
362 'path' => 'civicrm/activity/email/add',
363 'query' => 'atype=3&action=add&reset=1&context=standalone',
364 'ref' => 'new-email',
365 'title' => ts('Email'),
366 ),
367 ));
368
369 if (CRM_Core_Permission::check('edit groups')) {
370 $shortCuts = array_merge($shortCuts, array(
371 array(
372 'path' => 'civicrm/group/add',
373 'query' => 'reset=1',
374 'ref' => 'new-group',
375 'title' => ts('Group'),
376 ),
377 ));
378 }
379
380 if (CRM_Core_Permission::check('manage tags')) {
381 $shortCuts = array_merge($shortCuts, array(
382 array(
383 'path' => 'civicrm/tag',
384 'query' => 'reset=1&action=add',
385 'ref' => 'new-tag',
386 'title' => ts('Tag'),
387 ),
388 ));
389 }
390
391 if (empty($shortCuts)) {
392 return NULL;
393 }
394 }
395
396 $values = [];
397 foreach ($shortCuts as $key => $short) {
398 $values[$key] = self::setShortCutValues($short);
399 }
400
401 // Deprecated hook with typo. Please don't use this!
402 CRM_Utils_Hook::links('create.new.shorcuts',
403 NULL,
404 CRM_Core_DAO::$_nullObject,
405 $values
406 );
407
408 // Hook that enables extensions to add user-defined links
409 CRM_Utils_Hook::links('create.new.shortcuts',
410 NULL,
411 CRM_Core_DAO::$_nullObject,
412 $values
413 );
414
415 foreach ($values as $key => $val) {
416 if (!empty($val['title'])) {
417 $values[$key]['name'] = CRM_Utils_Array::value('name', $val, $val['title']);
418 }
419 }
420
421 self::setProperty(self::CREATE_NEW, 'templateValues', array('shortCuts' => $values));
422 }
423
424 /**
425 * @param array $short
426 *
427 * @return array
428 */
429 private static function setShortcutValues($short) {
430 $value = [];
431 if (isset($short['url'])) {
432 $value['url'] = $short['url'];
433 }
434 elseif (isset($short['path'])) {
435 $value['url'] = CRM_Utils_System::url($short['path'], $short['query'], FALSE);
436 }
437 $value['title'] = $short['title'];
438 $value['ref'] = $short['ref'] ?? '';
439 $value['shortCuts'] = [];
440 if (!empty($short['shortCuts'])) {
441 foreach ($short['shortCuts'] as $shortCut) {
442 $value['shortCuts'][] = self::setShortcutValues($shortCut);
443 }
444 }
445 return $value;
446 }
447
448 /**
449 * Create the list of dashboard links.
450 */
451 private static function setTemplateDashboardValues() {
452 static $dashboardLinks = [];
453 if (CRM_Core_Permission::check('access Contact Dashboard')) {
454 $dashboardLinks = array(
455 array(
456 'path' => 'civicrm/user',
457 'query' => 'reset=1',
458 'title' => ts('My Contact Dashboard'),
459 ),
460 );
461 }
462
463 $values = [];
464 foreach ($dashboardLinks as $dash) {
465 $value = [];
466 if (isset($dash['url'])) {
467 $value['url'] = $dash['url'];
468 }
469 else {
470 $value['url'] = CRM_Utils_System::url($dash['path'], $dash['query'], FALSE);
471 }
472 $value['title'] = $dash['title'];
473 $value['key'] = $dash['key'] ?? NULL;
474 $values[] = $value;
475 }
476 self::setProperty(self::DASHBOARD, 'templateValues', array('dashboardLinks' => $values));
477 }
478
479 /**
480 * Create the list of mail urls for the application and format is as a block.
481 */
482 private static function setTemplateMailValues() {
483 static $shortCuts = NULL;
484
485 if (!($shortCuts)) {
486 $shortCuts = array(
487 array(
488 'path' => 'civicrm/mailing/send',
489 'query' => 'reset=1',
490 'title' => ts('Send Mailing'),
491 ),
492 array(
493 'path' => 'civicrm/mailing/browse',
494 'query' => 'reset=1',
495 'title' => ts('Browse Sent Mailings'),
496 ),
497 );
498 }
499
500 $values = [];
501 foreach ($shortCuts as $short) {
502 $value = [];
503 $value['url'] = CRM_Utils_System::url($short['path'], $short['query']);
504 $value['title'] = $short['title'];
505 $values[] = $value;
506 }
507 self::setProperty(self::MAIL, 'templateValues', array('shortCuts' => $values));
508 }
509
510 /**
511 * Create the event blocks for upcoming events.
512 */
513 private static function setTemplateEventValues() {
514 $config = CRM_Core_Config::singleton();
515
516 $info = CRM_Event_BAO_Event::getCompleteInfo(date("Ymd"));
517
518 if ($info) {
519 $session = CRM_Core_Session::singleton();
520 // check if registration link should be displayed
521 foreach ($info as $id => $event) {
522 //@todo FIXME - validRegistraionRequest takes eventID not contactID as a param
523 // this is called via an obscure patch from Joomla event block rendering (only)
524 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event::validRegistrationRequest($event,
525 $session->get('userID')
526 );
527 }
528
529 self::setProperty(self::EVENT, 'templateValues', array('eventBlock' => $info));
530 }
531 }
532
533 /**
534 * Given an id creates a subject/content array
535 *
536 * @param int $id
537 * Id of the block.
538 *
539 * @return array
540 */
541 public static function getContent($id) {
542 // return if upgrade mode
543 $config = CRM_Core_Config::singleton();
544 if ($config->isUpgradeMode()) {
545 return NULL;
546 }
547 CRM_Core_Smarty::singleton()->ensureVariablesAreAssigned(['langSwitch', 'breadcrumb', 'pageTitle']);
548
549 if (!self::getProperty($id, 'active')) {
550 return NULL;
551 }
552
553 if ($id == self::EVENT &&
554 CRM_Core_Permission::check('view event info')
555 ) {
556 // is CiviEvent enabled?
557 if (!CRM_Core_Permission::access('CiviEvent', FALSE)) {
558 return NULL;
559 }
560 // do nothing
561 }
562 // require 'access CiviCRM' permissons, except for the language switch block
563 elseif (!CRM_Core_Permission::check('access CiviCRM') && $id != self::LANGSWITCH) {
564 return NULL;
565 }
566 elseif ($id == self::ADD) {
567 $hasAccess = TRUE;
568 if (!CRM_Core_Permission::check('add contacts') &&
569 !CRM_Core_Permission::check('edit groups')
570 ) {
571 $hasAccess = FALSE;
572 }
573 //validate across edit/view - CRM-5666
574 if ($hasAccess) {
575 $hasAccess = CRM_Core_Permission::giveMeAllACLs();
576 }
577 if (!$hasAccess) {
578 return NULL;
579 }
580 }
581
582 self::setTemplateValues($id);
583
584 // Suppress Recent Items block if it's empty - CRM-5188
585 if ($id == self::RECENTLY_VIEWED) {
586 $recent = self::getProperty($id, 'templateValues');
587 if (CRM_Utils_Array::crmIsEmptyArray($recent)) {
588 return NULL;
589 }
590 }
591
592 // Suppress Language switcher if language is inherited from CMS - CRM-9971
593 $config = CRM_Core_Config::singleton();
594 if ($id == self::LANGSWITCH && $config->inheritLocale) {
595 return NULL;
596 }
597
598 $block = [];
599 $block['name'] = 'block-civicrm';
600 $block['id'] = $block['name'] . '_' . $id;
601 $block['subject'] = self::fetch($id, 'Subject.tpl',
602 array('subject' => self::getProperty($id, 'subject'))
603 );
604 $block['content'] = self::fetch($id, self::getProperty($id, 'template'),
605 self::getProperty($id, 'templateValues')
606 );
607
608 return $block;
609 }
610
611 /**
612 * Given an id and a template, fetch the contents
613 *
614 * @param int $id
615 * Id of the block.
616 * @param string $fileName
617 * Name of the template file.
618 * @param array $properties
619 * Template variables.
620 *
621 * @return array
622 */
623 public static function fetch($id, $fileName, $properties) {
624 $template = CRM_Core_Smarty::singleton();
625
626 if ($properties) {
627 $template->assign($properties);
628 }
629
630 return $template->fetch('CRM/Block/' . $fileName);
631 }
632
633 }