3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * defines a simple implemenation of a drupal block.
38 * blocks definitions and html are in a smarty template file
41 class CRM_Core_Block
{
44 * The following blocks are supported.
58 * Template file names for the above blocks.
60 static $_properties = NULL;
65 public function __construct() {
69 * Initialises the $_properties array
73 public static function initProperties() {
74 if (!defined('BLOCK_CACHE_GLOBAL')) {
75 define('BLOCK_CACHE_GLOBAL', 0x0008);
78 if (!defined('BLOCK_CACHE_PER_PAGE')) {
79 define('BLOCK_CACHE_PER_PAGE', 0x0004);
82 if (!defined('BLOCK_NO_CACHE')) {
83 define('BLOCK_NO_CACHE', -1);
86 if (!(self
::$_properties)) {
87 $config = CRM_Core_Config
::singleton();
88 self
::$_properties = array(
89 // set status item to 0 to disable block by default (at install)
90 self
::CREATE_NEW
=> array(
91 'template' => 'CreateNew.tpl',
92 'info' => ts('CiviCRM Create New Record'),
95 'cache' => BLOCK_CACHE_GLOBAL
,
99 'pages' => "civicrm\ncivicrm/*",
100 'region' => $config->userSystem
->getDefaultBlockLocation(),
102 self
::RECENTLY_VIEWED
=> array(
103 'template' => 'RecentlyViewed.tpl',
104 'info' => ts('CiviCRM Recent Items'),
105 'subject' => ts('Recent Items'),
107 'cache' => BLOCK_NO_CACHE
,
111 'pages' => "civicrm\ncivicrm/*",
112 'region' => $config->userSystem
->getDefaultBlockLocation(),
114 self
::DASHBOARD
=> array(
115 'template' => 'Dashboard.tpl',
116 'info' => ts('CiviCRM Contact Dashboard'),
119 'cache' => BLOCK_NO_CACHE
,
123 'pages' => "civicrm\ncivicrm/*",
124 'region' => $config->userSystem
->getDefaultBlockLocation(),
127 'template' => 'Add.tpl',
128 'info' => ts('CiviCRM Quick Add'),
129 'subject' => ts('New Individual'),
131 'cache' => BLOCK_NO_CACHE
,
135 'pages' => "civicrm\ncivicrm/*",
136 'region' => $config->userSystem
->getDefaultBlockLocation(),
138 self
::LANGSWITCH
=> array(
139 'template' => 'LangSwitch.tpl',
140 'info' => ts('CiviCRM Language Switcher'),
142 'templateValues' => array(),
144 'cache' => BLOCK_NO_CACHE
,
148 'pages' => "civicrm\ncivicrm/*",
149 'region' => $config->userSystem
->getDefaultBlockLocation(),
151 self
::EVENT
=> array(
152 'template' => 'Event.tpl',
153 'info' => ts('CiviCRM Upcoming Events'),
154 'subject' => ts('Upcoming Events'),
155 'templateValues' => array(),
157 'cache' => BLOCK_NO_CACHE
,
161 'pages' => "civicrm\ncivicrm/*",
162 'region' => $config->userSystem
->getDefaultBlockLocation(),
164 self
::FULLTEXT_SEARCH
=> array(
165 'template' => 'FullTextSearch.tpl',
166 'info' => ts('CiviCRM Full-text Search'),
167 'subject' => ts('Full-text Search'),
169 'cache' => BLOCK_NO_CACHE
,
173 'pages' => "civicrm\ncivicrm/*",
174 'region' => $config->userSystem
->getDefaultBlockLocation(),
178 ksort(self
::$_properties);
183 * Returns the desired property from the $_properties array
186 * One of the class constants (ADD, SEARCH, etc.).
187 * @param string $property
188 * The desired property.
191 * the value of the desired property
193 public static function getProperty($id, $property) {
194 if (!(self
::$_properties)) {
195 self
::initProperties();
197 return isset(self
::$_properties[$id][$property]) ? self
::$_properties[$id][$property] : NULL;
201 * Sets the desired property in the $_properties array
204 * One of the class constants (ADD, SEARCH, etc.).
205 * @param string $property
206 * The desired property.
207 * @param string $value
208 * The value of the desired property.
212 public static function setProperty($id, $property, $value) {
213 if (!(self
::$_properties)) {
214 self
::initProperties();
216 self
::$_properties[$id][$property] = $value;
220 * Returns the whole $_properties array
223 * the $_properties array
225 public static function properties() {
226 if (!(self
::$_properties)) {
227 self
::initProperties();
229 return self
::$_properties;
233 * Creates the info block for drupal.
237 public static function getInfo() {
240 foreach (self
::properties() as $id => $value) {
241 if ($value['active']) {
242 if (in_array($id, array(
247 if (!CRM_Core_Permission
::check('add contacts') &&
248 !CRM_Core_Permission
::check('edit groups')
252 //validate across edit/view - CRM-5666
253 if ($hasAccess && ($id == self
::ADD
)) {
254 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
261 if ($id == self
::EVENT
&&
262 (!CRM_Core_Permission
::access('CiviEvent', FALSE) ||
263 !CRM_Core_Permission
::check('view event info')
270 'info' => $value['info'],
271 'cache' => $value['cache'],
272 'region' => $value['region'],
273 'visibility' => $value['visibility'],
274 'pages' => $value['pages'],
275 'status' => $value['status'],
276 'weight' => $value['weight'],
285 * Set the post action values for the block.
287 * php is lame and u cannot call functions from static initializers
294 private static function setTemplateValues($id) {
296 case self
::CREATE_NEW
:
297 self
::setTemplateShortcutValues();
300 case self
::DASHBOARD
:
301 self
::setTemplateDashboardValues();
305 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
306 $defaultPrimaryLocationId = $defaultLocation->id
;
308 'postURL' => CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&ct=Individual'),
309 'primaryLocationType' => $defaultPrimaryLocationId,
312 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
313 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils
::defaultGreeting('Individual', $greeting);
316 self
::setProperty(self
::ADD
,
322 case self
::LANGSWITCH
:
323 // gives the currentPath without trailing empty lcMessages to be completed
324 $values = array('queryString' => CRM_Utils_System
::getLinksUrl('lcMessages', TRUE, FALSE, FALSE));
325 self
::setProperty(self
::LANGSWITCH
, 'templateValues', $values);
328 case self
::FULLTEXT_SEARCH
:
330 'fullTextSearchID' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
331 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
334 self
::setProperty(self
::FULLTEXT_SEARCH
, 'templateValues', $urlArray);
337 case self
::RECENTLY_VIEWED
:
338 $recent = CRM_Utils_Recent
::get();
339 self
::setProperty(self
::RECENTLY_VIEWED
, 'templateValues', array('recentlyViewed' => $recent));
343 self
::setTemplateEventValues();
349 * Create the list of options to create New objects for the application and format is as a block.
353 private static function setTemplateShortcutValues() {
354 $config = CRM_Core_Config
::singleton();
356 static $shortCuts = array();
359 if (CRM_Core_Permission
::check('add contacts')) {
360 if (CRM_Core_Permission
::giveMeAllACLs()) {
361 $shortCuts = CRM_Contact_BAO_ContactType
::getCreateNewList();
365 // new activity (select target contact)
366 $shortCuts = array_merge($shortCuts, array(
368 'path' => 'civicrm/activity',
369 'query' => 'action=add&reset=1&context=standalone',
370 'ref' => 'new-activity',
371 'title' => ts('Activity'),
375 $components = CRM_Core_Component
::getEnabledComponents();
377 if (!empty($config->enableComponents
)) {
378 // check if we can process credit card contribs
379 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
381 foreach ($components as $componentName => $obj) {
382 if (in_array($componentName, $config->enableComponents
)) {
383 $obj->creatNewShortcut($shortCuts, $newCredit);
388 // new email (select recipients)
389 $shortCuts = array_merge($shortCuts, array(
391 'path' => 'civicrm/activity/email/add',
392 'query' => 'atype=3&action=add&reset=1&context=standalone',
393 'ref' => 'new-email',
394 'title' => ts('Email'),
398 if (CRM_Core_Permission
::check('edit groups')) {
399 $shortCuts = array_merge($shortCuts, array(
401 'path' => 'civicrm/group/add',
402 'query' => 'reset=1',
403 'ref' => 'new-group',
404 'title' => ts('Group'),
409 if (CRM_Core_Permission
::check('administer CiviCRM')) {
410 $shortCuts = array_merge($shortCuts, array(
412 'path' => 'civicrm/admin/tag',
413 'query' => 'reset=1&action=add',
415 'title' => ts('Tag'),
420 if (empty($shortCuts)) {
426 foreach ($shortCuts as $key => $short) {
427 $values[$key] = self
::setShortCutValues($short);
430 // call links hook to add user defined links
431 CRM_Utils_Hook
::links('create.new.shorcuts',
433 CRM_Core_DAO
::$_nullObject,
435 CRM_Core_DAO
::$_nullObject,
436 CRM_Core_DAO
::$_nullObject
439 foreach ($values as $key => $val) {
440 if (!empty($val['title'])) {
441 $values[$key]['name'] = CRM_Utils_Array
::value('name', $val, $val['title']);
445 self
::setProperty(self
::CREATE_NEW
, 'templateValues', array('shortCuts' => $values));
453 private static function setShortcutValues($short) {
455 if (isset($short['url'])) {
456 $value['url'] = $short['url'];
458 elseif (isset($short['path'])) {
459 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query'], FALSE);
461 $value['title'] = $short['title'];
462 $value['ref'] = $short['ref'];
463 if (!empty($short['shortCuts'])) {
464 foreach ($short['shortCuts'] as $shortCut) {
465 $value['shortCuts'][] = self
::setShortcutValues($shortCut);
472 * Create the list of dashboard links.
476 private static function setTemplateDashboardValues() {
477 static $dashboardLinks = array();
478 if (CRM_Core_Permission
::check('access Contact Dashboard')) {
479 $dashboardLinks = array(
481 'path' => 'civicrm/user',
482 'query' => 'reset=1',
483 'title' => ts('My Contact Dashboard'),
488 if (empty($dashboardLinks)) {
493 foreach ($dashboardLinks as $dash) {
495 if (isset($dash['url'])) {
496 $value['url'] = $dash['url'];
499 $value['url'] = CRM_Utils_System
::url($dash['path'], $dash['query'], FALSE);
501 $value['title'] = $dash['title'];
502 $value['key'] = CRM_Utils_Array
::value('key', $dash);
505 self
::setProperty(self
::DASHBOARD
, 'templateValues', array('dashboardLinks' => $values));
509 * Create the list of mail urls for the application and format is as a block.
513 private static function setTemplateMailValues() {
514 static $shortCuts = NULL;
519 'path' => 'civicrm/mailing/send',
520 'query' => 'reset=1',
521 'title' => ts('Send Mailing'),
524 'path' => 'civicrm/mailing/browse',
525 'query' => 'reset=1',
526 'title' => ts('Browse Sent Mailings'),
532 foreach ($shortCuts as $short) {
534 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query']);
535 $value['title'] = $short['title'];
538 self
::setProperty(self
::MAIL
, 'templateValues', array('shortCuts' => $values));
542 * Create the list of shortcuts for the application and format is as a block.
546 private static function setTemplateMenuValues() {
547 $config = CRM_Core_Config
::singleton();
549 $path = 'navigation';
550 $values = CRM_Core_Menu
::getNavigation();
552 self
::setProperty(self
::MENU
, 'templateValues', array('menu' => $values));
557 * Create the event blocks for upcoming events.
561 private static function setTemplateEventValues() {
562 $config = CRM_Core_Config
::singleton();
564 $info = CRM_Event_BAO_Event
::getCompleteInfo(date("Ymd"));
567 $session = CRM_Core_Session
::singleton();
568 // check if registration link should be displayed
569 foreach ($info as $id => $event) {
570 //@todo FIXME - validRegistraionRequest takes eventID not contactID as a param
571 // this is called via an obscure patch from Joomla event block rendering (only)
572 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event
::validRegistrationRequest($event,
573 $session->get('userID')
577 self
::setProperty(self
::EVENT
, 'templateValues', array('eventBlock' => $info));
582 * Given an id creates a subject/content array
589 public static function getContent($id) {
590 // return if upgrade mode
591 $config = CRM_Core_Config
::singleton();
592 if ($config->isUpgradeMode()) {
596 if (!self
::getProperty($id, 'active')) {
600 if ($id == self
::EVENT
&&
601 CRM_Core_Permission
::check('view event info')
603 // is CiviEvent enabled?
604 if (!CRM_Core_Permission
::access('CiviEvent', FALSE)) {
609 // require 'access CiviCRM' permissons, except for the language switch block
610 elseif (!CRM_Core_Permission
::check('access CiviCRM') && $id != self
::LANGSWITCH
) {
613 elseif ($id == self
::ADD
) {
615 if (!CRM_Core_Permission
::check('add contacts') &&
616 !CRM_Core_Permission
::check('edit groups')
620 //validate across edit/view - CRM-5666
622 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
629 self
::setTemplateValues($id);
631 // Suppress Recent Items block if it's empty - CRM-5188
632 if ($id == self
::RECENTLY_VIEWED
) {
633 $recent = self
::getProperty($id, 'templateValues');
634 if (CRM_Utils_Array
::crmIsEmptyArray($recent)) {
639 // Suppress Language switcher if language is inherited from CMS - CRM-9971
640 $config = CRM_Core_Config
::singleton();
641 if ($id == self
::LANGSWITCH
&& property_exists($config, "inheritLocale") && $config->inheritLocale
) {
646 $block['name'] = 'block-civicrm';
647 $block['id'] = $block['name'] . '_' . $id;
648 $block['subject'] = self
::fetch($id, 'Subject.tpl',
649 array('subject' => self
::getProperty($id, 'subject'))
651 $block['content'] = self
::fetch($id, self
::getProperty($id, 'template'),
652 self
::getProperty($id, 'templateValues')
659 * Given an id and a template, fetch the contents
663 * @param string $fileName
664 * Name of the template file.
665 * @param array $properties
666 * Template variables.
670 public static function fetch($id, $fileName, $properties) {
671 $template = CRM_Core_Smarty
::singleton();
674 $template->assign($properties);
677 return $template->fetch('CRM/Block/' . $fileName);