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