3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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.
58 static $_properties = NULL;
63 public function __construct() {
67 * Initialises the $_properties array
69 public static function initProperties() {
70 if (!defined('BLOCK_CACHE_GLOBAL')) {
71 define('BLOCK_CACHE_GLOBAL', 0x0008);
74 if (!defined('BLOCK_CACHE_PER_PAGE')) {
75 define('BLOCK_CACHE_PER_PAGE', 0x0004);
78 if (!defined('BLOCK_NO_CACHE')) {
79 define('BLOCK_NO_CACHE', -1);
82 if (!(self
::$_properties)) {
83 $config = CRM_Core_Config
::singleton();
84 self
::$_properties = array(
85 // set status item to 0 to disable block by default (at install)
86 self
::CREATE_NEW
=> array(
87 'template' => 'CreateNew.tpl',
88 'info' => ts('CiviCRM Create New Record'),
91 'cache' => BLOCK_CACHE_GLOBAL
,
95 'pages' => "civicrm\ncivicrm/*",
96 'region' => $config->userSystem
->getDefaultBlockLocation(),
98 self
::RECENTLY_VIEWED
=> array(
99 'template' => 'RecentlyViewed.tpl',
100 'info' => ts('CiviCRM Recent Items'),
101 'subject' => ts('Recent Items'),
103 'cache' => BLOCK_NO_CACHE
,
107 'pages' => "civicrm\ncivicrm/*",
108 'region' => $config->userSystem
->getDefaultBlockLocation(),
110 self
::DASHBOARD
=> array(
111 'template' => 'Dashboard.tpl',
112 'info' => ts('CiviCRM Contact Dashboard'),
115 'cache' => BLOCK_NO_CACHE
,
119 'pages' => "civicrm\ncivicrm/*",
120 'region' => $config->userSystem
->getDefaultBlockLocation(),
123 'template' => 'Add.tpl',
124 'info' => ts('CiviCRM Quick Add'),
125 'subject' => ts('New Individual'),
127 'cache' => BLOCK_NO_CACHE
,
131 'pages' => "civicrm\ncivicrm/*",
132 'region' => $config->userSystem
->getDefaultBlockLocation(),
134 self
::LANGSWITCH
=> array(
135 'template' => 'LangSwitch.tpl',
136 'info' => ts('CiviCRM Language Switcher'),
138 'templateValues' => array(),
140 'cache' => BLOCK_NO_CACHE
,
144 'pages' => "civicrm\ncivicrm/*",
145 'region' => $config->userSystem
->getDefaultBlockLocation(),
147 self
::EVENT
=> array(
148 'template' => 'Event.tpl',
149 'info' => ts('CiviCRM Upcoming Events'),
150 'subject' => ts('Upcoming Events'),
151 'templateValues' => array(),
153 'cache' => BLOCK_NO_CACHE
,
157 'pages' => "civicrm\ncivicrm/*",
158 'region' => $config->userSystem
->getDefaultBlockLocation(),
160 self
::FULLTEXT_SEARCH
=> array(
161 'template' => 'FullTextSearch.tpl',
162 'info' => ts('CiviCRM Full-text Search'),
163 'subject' => ts('Full-text Search'),
165 'cache' => BLOCK_NO_CACHE
,
169 'pages' => "civicrm\ncivicrm/*",
170 'region' => $config->userSystem
->getDefaultBlockLocation(),
174 ksort(self
::$_properties);
179 * Returns the desired property from the $_properties array
182 * One of the class constants (ADD, SEARCH, etc.).
183 * @param string $property
184 * The desired property.
187 * the value of the desired property
189 public static function getProperty($id, $property) {
190 if (!(self
::$_properties)) {
191 self
::initProperties();
193 return isset(self
::$_properties[$id][$property]) ? self
::$_properties[$id][$property] : NULL;
197 * Sets the desired property in the $_properties array
200 * One of the class constants (ADD, SEARCH, etc.).
201 * @param string $property
202 * The desired property.
203 * @param string $value
204 * The value of the desired property.
206 public static function setProperty($id, $property, $value) {
207 if (!(self
::$_properties)) {
208 self
::initProperties();
210 self
::$_properties[$id][$property] = $value;
214 * Returns the whole $_properties array.
217 * the $_properties array
219 public static function properties() {
220 if (!(self
::$_properties)) {
221 self
::initProperties();
223 return self
::$_properties;
227 * Creates the info block for drupal.
231 public static function getInfo() {
234 foreach (self
::properties() as $id => $value) {
235 if ($value['active']) {
236 if (in_array($id, array(
241 if (!CRM_Core_Permission
::check('add contacts') &&
242 !CRM_Core_Permission
::check('edit groups')
246 //validate across edit/view - CRM-5666
247 if ($hasAccess && ($id == self
::ADD
)) {
248 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
255 if ($id == self
::EVENT
&&
256 (!CRM_Core_Permission
::access('CiviEvent', FALSE) ||
257 !CRM_Core_Permission
::check('view event info')
264 'info' => $value['info'],
265 'cache' => $value['cache'],
266 'region' => $value['region'],
267 'visibility' => $value['visibility'],
268 'pages' => $value['pages'],
269 'status' => $value['status'],
270 'weight' => $value['weight'],
279 * Set the post action values for the block.
281 * php is lame and u cannot call functions from static initializers
286 private static function setTemplateValues($id) {
288 case self
::CREATE_NEW
:
289 self
::setTemplateShortcutValues();
292 case self
::DASHBOARD
:
293 self
::setTemplateDashboardValues();
297 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
298 $defaultPrimaryLocationId = $defaultLocation->id
;
300 'postURL' => CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&ct=Individual'),
301 'primaryLocationType' => $defaultPrimaryLocationId,
304 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
305 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils
::defaultGreeting('Individual', $greeting);
308 self
::setProperty(self
::ADD
,
314 case self
::LANGSWITCH
:
315 // gives the currentPath without trailing empty lcMessages to be completed
316 $values = array('queryString' => CRM_Utils_System
::getLinksUrl('lcMessages', TRUE, FALSE, FALSE));
317 self
::setProperty(self
::LANGSWITCH
, 'templateValues', $values);
320 case self
::FULLTEXT_SEARCH
:
322 'fullTextSearchID' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
323 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
326 self
::setProperty(self
::FULLTEXT_SEARCH
, 'templateValues', $urlArray);
329 case self
::RECENTLY_VIEWED
:
330 $recent = CRM_Utils_Recent
::get();
331 self
::setProperty(self
::RECENTLY_VIEWED
, 'templateValues', array('recentlyViewed' => $recent));
335 self
::setTemplateEventValues();
341 * Create the list of options to create New objects for the application and format is as a block.
343 private static function setTemplateShortcutValues() {
344 $config = CRM_Core_Config
::singleton();
346 static $shortCuts = array();
349 if (CRM_Core_Permission
::check('add contacts')) {
350 if (CRM_Core_Permission
::giveMeAllACLs()) {
351 $shortCuts = CRM_Contact_BAO_ContactType
::getCreateNewList();
355 // new activity (select target contact)
356 $shortCuts = array_merge($shortCuts, array(
358 'path' => 'civicrm/activity',
359 'query' => 'action=add&reset=1&context=standalone',
360 'ref' => 'new-activity',
361 'title' => ts('Activity'),
365 $components = CRM_Core_Component
::getEnabledComponents();
367 if (!empty($config->enableComponents
)) {
368 // check if we can process credit card contribs
369 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
371 foreach ($components as $componentName => $obj) {
372 if (in_array($componentName, $config->enableComponents
)) {
373 $obj->creatNewShortcut($shortCuts, $newCredit);
378 // new email (select recipients)
379 $shortCuts = array_merge($shortCuts, array(
381 'path' => 'civicrm/activity/email/add',
382 'query' => 'atype=3&action=add&reset=1&context=standalone',
383 'ref' => 'new-email',
384 'title' => ts('Email'),
388 if (CRM_Core_Permission
::check('edit groups')) {
389 $shortCuts = array_merge($shortCuts, array(
391 'path' => 'civicrm/group/add',
392 'query' => 'reset=1',
393 'ref' => 'new-group',
394 'title' => ts('Group'),
399 if (CRM_Core_Permission
::check('manage tags')) {
400 $shortCuts = array_merge($shortCuts, array(
402 'path' => 'civicrm/tag',
403 'query' => 'reset=1&action=add',
405 'title' => ts('Tag'),
410 if (empty($shortCuts)) {
416 foreach ($shortCuts as $key => $short) {
417 $values[$key] = self
::setShortCutValues($short);
420 // call links hook to add user defined links
421 CRM_Utils_Hook
::links('create.new.shorcuts',
423 CRM_Core_DAO
::$_nullObject,
427 foreach ($values as $key => $val) {
428 if (!empty($val['title'])) {
429 $values[$key]['name'] = CRM_Utils_Array
::value('name', $val, $val['title']);
433 self
::setProperty(self
::CREATE_NEW
, 'templateValues', array('shortCuts' => $values));
441 private static function setShortcutValues($short) {
443 if (isset($short['url'])) {
444 $value['url'] = $short['url'];
446 elseif (isset($short['path'])) {
447 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query'], FALSE);
449 $value['title'] = $short['title'];
450 $value['ref'] = isset($short['ref']) ?
$short['ref'] : '';
451 if (!empty($short['shortCuts'])) {
452 foreach ($short['shortCuts'] as $shortCut) {
453 $value['shortCuts'][] = self
::setShortcutValues($shortCut);
460 * Create the list of dashboard links.
462 private static function setTemplateDashboardValues() {
463 static $dashboardLinks = array();
464 if (CRM_Core_Permission
::check('access Contact Dashboard')) {
465 $dashboardLinks = array(
467 'path' => 'civicrm/user',
468 'query' => 'reset=1',
469 'title' => ts('My Contact Dashboard'),
474 if (empty($dashboardLinks)) {
479 foreach ($dashboardLinks as $dash) {
481 if (isset($dash['url'])) {
482 $value['url'] = $dash['url'];
485 $value['url'] = CRM_Utils_System
::url($dash['path'], $dash['query'], FALSE);
487 $value['title'] = $dash['title'];
488 $value['key'] = CRM_Utils_Array
::value('key', $dash);
491 self
::setProperty(self
::DASHBOARD
, 'templateValues', array('dashboardLinks' => $values));
495 * Create the list of mail urls for the application and format is as a block.
497 private static function setTemplateMailValues() {
498 static $shortCuts = NULL;
503 'path' => 'civicrm/mailing/send',
504 'query' => 'reset=1',
505 'title' => ts('Send Mailing'),
508 'path' => 'civicrm/mailing/browse',
509 'query' => 'reset=1',
510 'title' => ts('Browse Sent Mailings'),
516 foreach ($shortCuts as $short) {
518 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query']);
519 $value['title'] = $short['title'];
522 self
::setProperty(self
::MAIL
, 'templateValues', array('shortCuts' => $values));
526 * Create the list of shortcuts for the application and format is as a block.
528 private static function setTemplateMenuValues() {
529 $config = CRM_Core_Config
::singleton();
531 $path = 'navigation';
532 $values = CRM_Core_Menu
::getNavigation();
534 self
::setProperty(self
::MENU
, 'templateValues', array('menu' => $values));
539 * Create the event blocks for upcoming events.
541 private static function setTemplateEventValues() {
542 $config = CRM_Core_Config
::singleton();
544 $info = CRM_Event_BAO_Event
::getCompleteInfo(date("Ymd"));
547 $session = CRM_Core_Session
::singleton();
548 // check if registration link should be displayed
549 foreach ($info as $id => $event) {
550 //@todo FIXME - validRegistraionRequest takes eventID not contactID as a param
551 // this is called via an obscure patch from Joomla event block rendering (only)
552 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event
::validRegistrationRequest($event,
553 $session->get('userID')
557 self
::setProperty(self
::EVENT
, 'templateValues', array('eventBlock' => $info));
562 * Given an id creates a subject/content array
569 public static function getContent($id) {
570 // return if upgrade mode
571 $config = CRM_Core_Config
::singleton();
572 if ($config->isUpgradeMode()) {
576 if (!self
::getProperty($id, 'active')) {
580 if ($id == self
::EVENT
&&
581 CRM_Core_Permission
::check('view event info')
583 // is CiviEvent enabled?
584 if (!CRM_Core_Permission
::access('CiviEvent', FALSE)) {
589 // require 'access CiviCRM' permissons, except for the language switch block
590 elseif (!CRM_Core_Permission
::check('access CiviCRM') && $id != self
::LANGSWITCH
) {
593 elseif ($id == self
::ADD
) {
595 if (!CRM_Core_Permission
::check('add contacts') &&
596 !CRM_Core_Permission
::check('edit groups')
600 //validate across edit/view - CRM-5666
602 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
609 self
::setTemplateValues($id);
611 // Suppress Recent Items block if it's empty - CRM-5188
612 if ($id == self
::RECENTLY_VIEWED
) {
613 $recent = self
::getProperty($id, 'templateValues');
614 if (CRM_Utils_Array
::crmIsEmptyArray($recent)) {
619 // Suppress Language switcher if language is inherited from CMS - CRM-9971
620 $config = CRM_Core_Config
::singleton();
621 if ($id == self
::LANGSWITCH
&& $config->inheritLocale
) {
626 $block['name'] = 'block-civicrm';
627 $block['id'] = $block['name'] . '_' . $id;
628 $block['subject'] = self
::fetch($id, 'Subject.tpl',
629 array('subject' => self
::getProperty($id, 'subject'))
631 $block['content'] = self
::fetch($id, self
::getProperty($id, 'template'),
632 self
::getProperty($id, 'templateValues')
639 * Given an id and a template, fetch the contents
643 * @param string $fileName
644 * Name of the template file.
645 * @param array $properties
646 * Template variables.
650 public static function fetch($id, $fileName, $properties) {
651 $template = CRM_Core_Smarty
::singleton();
654 $template->assign($properties);
657 return $template->fetch('CRM/Block/' . $fileName);