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;
66 public function __construct() {
70 * Initialises the $_properties array
74 public static function initProperties() {
75 if (!defined('BLOCK_CACHE_GLOBAL')) {
76 define('BLOCK_CACHE_GLOBAL', 0x0008);
79 if (!defined('BLOCK_CACHE_PER_PAGE')) {
80 define('BLOCK_CACHE_PER_PAGE', 0x0004);
83 if (!defined('BLOCK_NO_CACHE')) {
84 define('BLOCK_NO_CACHE', -1);
87 if (!(self
::$_properties)) {
88 $config = CRM_Core_Config
::singleton();
89 self
::$_properties = array(
90 // set status item to 0 to disable block by default (at install)
91 self
::CREATE_NEW
=> array(
92 'template' => 'CreateNew.tpl',
93 'info' => ts('CiviCRM Create New Record'),
96 'cache' => BLOCK_CACHE_GLOBAL
,
100 'pages' => "civicrm\ncivicrm/*",
101 'region' => $config->userSystem
->getDefaultBlockLocation(),
103 self
::RECENTLY_VIEWED
=> array(
104 'template' => 'RecentlyViewed.tpl',
105 'info' => ts('CiviCRM Recent Items'),
106 'subject' => ts('Recent Items'),
108 'cache' => BLOCK_NO_CACHE
,
112 'pages' => "civicrm\ncivicrm/*",
113 'region' => $config->userSystem
->getDefaultBlockLocation(),
115 self
::DASHBOARD
=> array(
116 'template' => 'Dashboard.tpl',
117 'info' => ts('CiviCRM Contact Dashboard'),
120 'cache' => BLOCK_NO_CACHE
,
124 'pages' => "civicrm\ncivicrm/*",
125 'region' => $config->userSystem
->getDefaultBlockLocation(),
128 'template' => 'Add.tpl',
129 'info' => ts('CiviCRM Quick Add'),
130 'subject' => ts('New Individual'),
132 'cache' => BLOCK_NO_CACHE
,
136 'pages' => "civicrm\ncivicrm/*",
137 'region' => $config->userSystem
->getDefaultBlockLocation(),
139 self
::LANGSWITCH
=> array(
140 'template' => 'LangSwitch.tpl',
141 'info' => ts('CiviCRM Language Switcher'),
143 'templateValues' => array(),
145 'cache' => BLOCK_NO_CACHE
,
149 'pages' => "civicrm\ncivicrm/*",
150 'region' => $config->userSystem
->getDefaultBlockLocation(),
152 self
::EVENT
=> array(
153 'template' => 'Event.tpl',
154 'info' => ts('CiviCRM Upcoming Events'),
155 'subject' => ts('Upcoming Events'),
156 'templateValues' => array(),
158 'cache' => BLOCK_NO_CACHE
,
162 'pages' => "civicrm\ncivicrm/*",
163 'region' => $config->userSystem
->getDefaultBlockLocation(),
165 self
::FULLTEXT_SEARCH
=> array(
166 'template' => 'FullTextSearch.tpl',
167 'info' => ts('CiviCRM Full-text Search'),
168 'subject' => ts('Full-text Search'),
170 'cache' => BLOCK_NO_CACHE
,
174 'pages' => "civicrm\ncivicrm/*",
175 'region' => $config->userSystem
->getDefaultBlockLocation(),
179 ksort(self
::$_properties);
184 * Returns the desired property from the $_properties array
187 * One of the class constants (ADD, SEARCH, etc.).
188 * @param string $property
189 * The desired property.
191 * @return string 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
222 * @return array the $_properties array
224 public static function properties() {
225 if (!(self
::$_properties)) {
226 self
::initProperties();
228 return self
::$_properties;
232 * Creates the info block for drupal
236 public static function getInfo() {
239 foreach (self
::properties() as $id => $value) {
240 if ($value['active']) {
241 if (in_array($id, array(
242 self
::ADD
, self
::CREATE_NEW
))) {
244 if (!CRM_Core_Permission
::check('add contacts') &&
245 !CRM_Core_Permission
::check('edit groups')
249 //validate across edit/view - CRM-5666
250 if ($hasAccess && ($id == self
::ADD
)) {
251 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
258 if ($id == self
::EVENT
&&
259 (!CRM_Core_Permission
::access('CiviEvent', FALSE) ||
260 !CRM_Core_Permission
::check('view event info')
267 'info' => $value['info'],
268 'cache' => $value['cache'],
269 'status' => $value['active'],
270 'region' => $value['region'],
271 'visibility' => $value['visibility'],
272 'pages' => $value['pages'],
273 'status' => $value['status'],
274 'weight' => $value['weight'],
283 * Set the post action values for the block.
285 * php is lame and u cannot call functions from static initializers
292 private static function setTemplateValues($id) {
294 case self
::CREATE_NEW
:
295 self
::setTemplateShortcutValues();
298 case self
::DASHBOARD
:
299 self
::setTemplateDashboardValues();
303 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
304 $defaultPrimaryLocationId = $defaultLocation->id
;
306 'postURL' => CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&ct=Individual'),
307 'primaryLocationType' => $defaultPrimaryLocationId,
310 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
311 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils
::defaultGreeting('Individual', $greeting);
314 self
::setProperty(self
::ADD
,
320 case self
::LANGSWITCH
:
321 // gives the currentPath without trailing empty lcMessages to be completed
322 $values = array('queryString' => CRM_Utils_System
::getLinksUrl('lcMessages', TRUE, FALSE, FALSE));
323 self
::setProperty(self
::LANGSWITCH
, 'templateValues', $values);
326 case self
::FULLTEXT_SEARCH
:
328 'fullTextSearchID' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
329 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
331 self
::setProperty(self
::FULLTEXT_SEARCH
, 'templateValues', $urlArray);
334 case self
::RECENTLY_VIEWED
:
335 $recent = CRM_Utils_Recent
::get();
336 self
::setProperty(self
::RECENTLY_VIEWED
, 'templateValues', array('recentlyViewed' => $recent));
340 self
::setTemplateEventValues();
346 * Create the list of options to create New objects for the application and format is as a block
350 private static function setTemplateShortcutValues() {
351 $config = CRM_Core_Config
::singleton();
353 static $shortCuts = array();
356 if (CRM_Core_Permission
::check('add contacts')) {
357 if (CRM_Core_Permission
::giveMeAllACLs()) {
358 $shortCuts = CRM_Contact_BAO_ContactType
::getCreateNewList();
362 // new activity (select target contact)
363 $shortCuts = array_merge($shortCuts, array(
365 'path' => 'civicrm/activity',
366 'query' => 'action=add&reset=1&context=standalone',
367 'ref' => 'new-activity',
368 'title' => ts('Activity'),
371 $components = CRM_Core_Component
::getEnabledComponents();
373 if (!empty($config->enableComponents
)) {
374 // check if we can process credit card contribs
375 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
377 foreach ($components as $componentName => $obj) {
378 if (in_array($componentName, $config->enableComponents
)) {
379 $obj->creatNewShortcut($shortCuts, $newCredit);
384 // new email (select recipients)
385 $shortCuts = array_merge($shortCuts, array(
387 'path' => 'civicrm/activity/email/add',
388 'query' => 'atype=3&action=add&reset=1&context=standalone',
389 'ref' => 'new-email',
390 'title' => ts('Email'),
393 if (CRM_Core_Permission
::check('edit groups')) {
394 $shortCuts = array_merge($shortCuts, array(
396 'path' => 'civicrm/group/add',
397 'query' => 'reset=1',
398 'ref' => 'new-group',
399 'title' => ts('Group'),
403 if (CRM_Core_Permission
::check('administer CiviCRM')) {
404 $shortCuts = array_merge($shortCuts, array(
406 'path' => 'civicrm/admin/tag',
407 'query' => 'reset=1&action=add',
409 'title' => ts('Tag'),
413 if (empty($shortCuts)) {
419 foreach ($shortCuts as $key => $short) {
420 $values[$key] = self
::setShortCutValues($short);
423 // call links hook to add user defined links
424 CRM_Utils_Hook
::links('create.new.shorcuts',
426 CRM_Core_DAO
::$_nullObject,
428 CRM_Core_DAO
::$_nullObject,
429 CRM_Core_DAO
::$_nullObject
432 foreach ($values as $key => $val) {
433 if (!empty($val['title'])) {
434 $values[$key]['name'] = CRM_Utils_Array
::value('name', $val, $val['title']);
438 self
::setProperty(self
::CREATE_NEW
, 'templateValues', array('shortCuts' => $values));
446 private static function setShortcutValues($short) {
448 if (isset($short['url'])) {
449 $value['url'] = $short['url'];
451 elseif (isset($short['path'])) {
452 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query'], FALSE);
454 $value['title'] = $short['title'];
455 $value['ref'] = $short['ref'];
456 if (!empty($short['shortCuts'])) {
457 foreach ($short['shortCuts'] as $shortCut) {
458 $value['shortCuts'][] = self
::setShortcutValues($shortCut);
465 * Create the list of dashboard links
469 private static function setTemplateDashboardValues() {
470 static $dashboardLinks = array();
471 if (CRM_Core_Permission
::check('access Contact Dashboard')) {
472 $dashboardLinks = array(
474 'path' => 'civicrm/user',
475 'query' => 'reset=1',
476 'title' => ts('My Contact Dashboard'),
480 if (empty($dashboardLinks)) {
485 foreach ($dashboardLinks as $dash) {
487 if (isset($dash['url'])) {
488 $value['url'] = $dash['url'];
491 $value['url'] = CRM_Utils_System
::url($dash['path'], $dash['query'], FALSE);
493 $value['title'] = $dash['title'];
494 $value['key'] = CRM_Utils_Array
::value('key', $dash);
497 self
::setProperty(self
::DASHBOARD
, 'templateValues', array('dashboardLinks' => $values));
501 * 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 list of shortcuts for the application and format is as a block
538 private static function setTemplateMenuValues() {
539 $config = CRM_Core_Config
::singleton();
541 $path = 'navigation';
542 $values = CRM_Core_Menu
::getNavigation();
544 self
::setProperty(self
::MENU
, 'templateValues', array('menu' => $values));
549 * Create the event blocks for upcoming events
553 private static function setTemplateEventValues() {
554 $config = CRM_Core_Config
::singleton();
556 $info = CRM_Event_BAO_Event
::getCompleteInfo(date("Ymd"));
559 $session = CRM_Core_Session
::singleton();
560 // check if registration link should be displayed
561 foreach ($info as $id => $event) {
562 //@todo FIXME - validRegistraionRequest takes eventID not contactID as a param
563 // this is called via an obscure patch from Joomla event block rendering (only)
564 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event
::validRegistrationRequest($event,
565 $session->get('userID')
569 self
::setProperty(self
::EVENT
, 'templateValues', array('eventBlock' => $info));
574 * Given an id creates a subject/content array
581 public static function getContent($id) {
582 // return if upgrade mode
583 $config = CRM_Core_Config
::singleton();
584 if ($config->isUpgradeMode()) {
588 if (!self
::getProperty($id, 'active')) {
592 if ($id == self
::EVENT
&&
593 CRM_Core_Permission
::check('view event info')
595 // is CiviEvent enabled?
596 if (!CRM_Core_Permission
::access('CiviEvent', FALSE)) {
601 // require 'access CiviCRM' permissons, except for the language switch block
602 elseif (!CRM_Core_Permission
::check('access CiviCRM') && $id != self
::LANGSWITCH
) {
605 elseif ($id == self
::ADD
) {
607 if (!CRM_Core_Permission
::check('add contacts') &&
608 !CRM_Core_Permission
::check('edit groups')
612 //validate across edit/view - CRM-5666
614 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
621 self
::setTemplateValues($id);
623 // Suppress Recent Items block if it's empty - CRM-5188
624 if ($id == self
::RECENTLY_VIEWED
) {
625 $recent = self
::getProperty($id, 'templateValues');
626 if (CRM_Utils_Array
::crmIsEmptyArray($recent)) {
631 // Suppress Language switcher if language is inherited from CMS - CRM-9971
632 $config = CRM_Core_Config
::singleton();
633 if ($id == self
::LANGSWITCH
&& property_exists($config, "inheritLocale") && $config->inheritLocale
) {
638 $block['name'] = 'block-civicrm';
639 $block['id'] = $block['name'] . '_' . $id;
640 $block['subject'] = self
::fetch($id, 'Subject.tpl',
641 array('subject' => self
::getProperty($id, 'subject'))
643 $block['content'] = self
::fetch($id, self
::getProperty($id, 'template'),
644 self
::getProperty($id, 'templateValues')
651 * Given an id and a template, fetch the contents
655 * @param string $fileName
656 * Name of the template file.
657 * @param array $properties
658 * Template variables.
662 public static function fetch($id, $fileName, $properties) {
663 $template = CRM_Core_Smarty
::singleton();
666 $template->assign($properties);
669 return $template->fetch('CRM/Block/' . $fileName);