3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * Defines a simple implementation of a drupal block.
37 * Blocks definitions and html are in a smarty template file.
39 class CRM_Core_Block
{
42 * The following blocks are supported.
56 * Template file names for the above blocks.
59 public static $_properties = NULL;
64 public function __construct() {
68 * Initialises the $_properties array
70 public static function initProperties() {
71 if (!defined('BLOCK_CACHE_GLOBAL')) {
72 define('BLOCK_CACHE_GLOBAL', 0x0008);
75 if (!defined('BLOCK_CACHE_PER_PAGE')) {
76 define('BLOCK_CACHE_PER_PAGE', 0x0004);
79 if (!defined('BLOCK_NO_CACHE')) {
80 define('BLOCK_NO_CACHE', -1);
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'),
92 'cache' => BLOCK_CACHE_GLOBAL
,
96 'pages' => "civicrm\ncivicrm/*",
97 'region' => $config->userSystem
->getDefaultBlockLocation(),
99 self
::RECENTLY_VIEWED
=> array(
100 'template' => 'RecentlyViewed.tpl',
101 'info' => ts('CiviCRM Recent Items'),
102 'subject' => ts('Recent Items'),
104 'cache' => BLOCK_NO_CACHE
,
108 'pages' => "civicrm\ncivicrm/*",
109 'region' => $config->userSystem
->getDefaultBlockLocation(),
111 self
::DASHBOARD
=> array(
112 'template' => 'Dashboard.tpl',
113 'info' => ts('CiviCRM Contact Dashboard'),
116 'cache' => BLOCK_NO_CACHE
,
120 'pages' => "civicrm\ncivicrm/*",
121 'region' => $config->userSystem
->getDefaultBlockLocation(),
124 'template' => 'Add.tpl',
125 'info' => ts('CiviCRM Quick Add'),
126 'subject' => ts('New Individual'),
128 'cache' => BLOCK_NO_CACHE
,
132 'pages' => "civicrm\ncivicrm/*",
133 'region' => $config->userSystem
->getDefaultBlockLocation(),
135 self
::LANGSWITCH
=> array(
136 'template' => 'LangSwitch.tpl',
137 'info' => ts('CiviCRM Language Switcher'),
139 'templateValues' => array(),
141 'cache' => BLOCK_NO_CACHE
,
145 'pages' => "civicrm\ncivicrm/*",
146 'region' => $config->userSystem
->getDefaultBlockLocation(),
148 self
::EVENT
=> array(
149 'template' => 'Event.tpl',
150 'info' => ts('CiviCRM Upcoming Events'),
151 'subject' => ts('Upcoming Events'),
152 'templateValues' => array(),
154 'cache' => BLOCK_NO_CACHE
,
158 'pages' => "civicrm\ncivicrm/*",
159 'region' => $config->userSystem
->getDefaultBlockLocation(),
161 self
::FULLTEXT_SEARCH
=> array(
162 'template' => 'FullTextSearch.tpl',
163 'info' => ts('CiviCRM Full-text Search'),
164 'subject' => ts('Full-text Search'),
166 'cache' => BLOCK_NO_CACHE
,
170 'pages' => "civicrm\ncivicrm/*",
171 'region' => $config->userSystem
->getDefaultBlockLocation(),
175 ksort(self
::$_properties);
180 * Returns the desired property from the $_properties array
183 * One of the class constants (ADD, SEARCH, etc.).
184 * @param string $property
185 * The desired property.
188 * the value of the desired property
190 public static function getProperty($id, $property) {
191 if (!(self
::$_properties)) {
192 self
::initProperties();
194 return isset(self
::$_properties[$id][$property]) ? self
::$_properties[$id][$property] : NULL;
198 * Sets the desired property in the $_properties array
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.
207 public static function setProperty($id, $property, $value) {
208 if (!(self
::$_properties)) {
209 self
::initProperties();
211 self
::$_properties[$id][$property] = $value;
215 * Returns the whole $_properties array.
218 * the $_properties array
220 public static function properties() {
221 if (!(self
::$_properties)) {
222 self
::initProperties();
224 return self
::$_properties;
228 * Creates the info block for drupal.
232 public static function getInfo() {
235 foreach (self
::properties() as $id => $value) {
236 if ($value['active']) {
237 if (in_array($id, array(
242 if (!CRM_Core_Permission
::check('add contacts') &&
243 !CRM_Core_Permission
::check('edit groups')
247 //validate across edit/view - CRM-5666
248 if ($hasAccess && ($id == self
::ADD
)) {
249 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
256 if ($id == self
::EVENT
&&
257 (!CRM_Core_Permission
::access('CiviEvent', FALSE) ||
258 !CRM_Core_Permission
::check('view event info')
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'],
280 * Set the post action values for the block.
282 * php is lame and u cannot call functions from static initializers
287 private static function setTemplateValues($id) {
289 case self
::CREATE_NEW
:
290 self
::setTemplateShortcutValues();
293 case self
::DASHBOARD
:
294 self
::setTemplateDashboardValues();
298 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
299 $defaultPrimaryLocationId = $defaultLocation->id
;
301 'postURL' => CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&ct=Individual'),
302 'primaryLocationType' => $defaultPrimaryLocationId,
305 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
306 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils
::defaultGreeting('Individual', $greeting);
309 self
::setProperty(self
::ADD
,
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);
321 case self
::FULLTEXT_SEARCH
:
323 'fullTextSearchID' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
324 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
327 self
::setProperty(self
::FULLTEXT_SEARCH
, 'templateValues', $urlArray);
330 case self
::RECENTLY_VIEWED
:
331 $recent = CRM_Utils_Recent
::get();
332 self
::setProperty(self
::RECENTLY_VIEWED
, 'templateValues', array('recentlyViewed' => $recent));
336 self
::setTemplateEventValues();
342 * Create the list of options to create New objects for the application and format is as a block.
344 private static function setTemplateShortcutValues() {
345 $config = CRM_Core_Config
::singleton();
347 static $shortCuts = array();
350 if (CRM_Core_Permission
::check('add contacts')) {
351 if (CRM_Core_Permission
::giveMeAllACLs()) {
352 $shortCuts = CRM_Contact_BAO_ContactType
::getCreateNewList();
356 // new activity (select target contact)
357 $shortCuts = array_merge($shortCuts, array(
359 'path' => 'civicrm/activity',
360 'query' => 'action=add&reset=1&context=standalone',
361 'ref' => 'new-activity',
362 'title' => ts('Activity'),
366 $components = CRM_Core_Component
::getEnabledComponents();
368 if (!empty($config->enableComponents
)) {
369 // check if we can process credit card contribs
370 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
372 foreach ($components as $componentName => $obj) {
373 if (in_array($componentName, $config->enableComponents
)) {
374 $obj->creatNewShortcut($shortCuts, $newCredit);
379 // new email (select recipients)
380 $shortCuts = array_merge($shortCuts, 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'),
389 if (CRM_Core_Permission
::check('edit groups')) {
390 $shortCuts = array_merge($shortCuts, array(
392 'path' => 'civicrm/group/add',
393 'query' => 'reset=1',
394 'ref' => 'new-group',
395 'title' => ts('Group'),
400 if (CRM_Core_Permission
::check('manage tags')) {
401 $shortCuts = array_merge($shortCuts, array(
403 'path' => 'civicrm/tag',
404 'query' => 'reset=1&action=add',
406 'title' => ts('Tag'),
411 if (empty($shortCuts)) {
417 foreach ($shortCuts as $key => $short) {
418 $values[$key] = self
::setShortCutValues($short);
421 // Deprecated hook with typo. Please don't use this!
422 CRM_Utils_Hook
::links('create.new.shorcuts',
424 CRM_Core_DAO
::$_nullObject,
428 // Hook that enables extensions to add user-defined links
429 CRM_Utils_Hook
::links('create.new.shortcuts',
431 CRM_Core_DAO
::$_nullObject,
435 foreach ($values as $key => $val) {
436 if (!empty($val['title'])) {
437 $values[$key]['name'] = CRM_Utils_Array
::value('name', $val, $val['title']);
441 self
::setProperty(self
::CREATE_NEW
, 'templateValues', array('shortCuts' => $values));
449 private static function setShortcutValues($short) {
451 if (isset($short['url'])) {
452 $value['url'] = $short['url'];
454 elseif (isset($short['path'])) {
455 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query'], FALSE);
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);
468 * Create the list of dashboard links.
470 private static function setTemplateDashboardValues() {
471 static $dashboardLinks = array();
472 if (CRM_Core_Permission
::check('access Contact Dashboard')) {
473 $dashboardLinks = array(
475 'path' => 'civicrm/user',
476 'query' => 'reset=1',
477 'title' => ts('My Contact Dashboard'),
482 if (empty($dashboardLinks)) {
487 foreach ($dashboardLinks as $dash) {
489 if (isset($dash['url'])) {
490 $value['url'] = $dash['url'];
493 $value['url'] = CRM_Utils_System
::url($dash['path'], $dash['query'], FALSE);
495 $value['title'] = $dash['title'];
496 $value['key'] = CRM_Utils_Array
::value('key', $dash);
499 self
::setProperty(self
::DASHBOARD
, 'templateValues', array('dashboardLinks' => $values));
503 * Create the list of mail urls for the application and format is as a block.
505 private static function setTemplateMailValues() {
506 static $shortCuts = NULL;
511 'path' => 'civicrm/mailing/send',
512 'query' => 'reset=1',
513 'title' => ts('Send Mailing'),
516 'path' => 'civicrm/mailing/browse',
517 'query' => 'reset=1',
518 'title' => ts('Browse Sent Mailings'),
524 foreach ($shortCuts as $short) {
526 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query']);
527 $value['title'] = $short['title'];
530 self
::setProperty(self
::MAIL
, 'templateValues', array('shortCuts' => $values));
534 * Create the event blocks for upcoming events.
536 private static function setTemplateEventValues() {
537 $config = CRM_Core_Config
::singleton();
539 $info = CRM_Event_BAO_Event
::getCompleteInfo(date("Ymd"));
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')
552 self
::setProperty(self
::EVENT
, 'templateValues', array('eventBlock' => $info));
557 * Given an id creates a subject/content array
564 public static function getContent($id) {
565 // return if upgrade mode
566 $config = CRM_Core_Config
::singleton();
567 if ($config->isUpgradeMode()) {
571 if (!self
::getProperty($id, 'active')) {
575 if ($id == self
::EVENT
&&
576 CRM_Core_Permission
::check('view event info')
578 // is CiviEvent enabled?
579 if (!CRM_Core_Permission
::access('CiviEvent', FALSE)) {
584 // require 'access CiviCRM' permissons, except for the language switch block
585 elseif (!CRM_Core_Permission
::check('access CiviCRM') && $id != self
::LANGSWITCH
) {
588 elseif ($id == self
::ADD
) {
590 if (!CRM_Core_Permission
::check('add contacts') &&
591 !CRM_Core_Permission
::check('edit groups')
595 //validate across edit/view - CRM-5666
597 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
604 self
::setTemplateValues($id);
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)) {
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
) {
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'))
626 $block['content'] = self
::fetch($id, self
::getProperty($id, 'template'),
627 self
::getProperty($id, 'templateValues')
634 * Given an id and a template, fetch the contents
638 * @param string $fileName
639 * Name of the template file.
640 * @param array $properties
641 * Template variables.
645 public static function fetch($id, $fileName, $properties) {
646 $template = CRM_Core_Smarty
::singleton();
649 $template->assign($properties);
652 return $template->fetch('CRM/Block/' . $fileName);