3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 function __construct() {}
69 * initialises the $_properties array
73 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
185 * @params int $id one of the class constants (ADD, SEARCH, etc.)
186 * @params string $property the desired property
191 * @return string the value of the desired property
193 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
203 * @params int $id one of the class constants (ADD, SEARCH, etc.)
204 * @params string $property the desired property
205 * @params string $value the value of the desired property
212 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 static function properties() {
225 if (!(self
::$_properties)) {
226 self
::initProperties();
228 return self
::$_properties;
232 * Creates the info block for drupal
237 static function getInfo() {
240 foreach (self
::properties() as $id => $value) {
241 if ($value['active']) {
242 if (in_array($id, array(
243 self
::ADD
, self
::CREATE_NEW
))) {
245 if (!CRM_Core_Permission
::check('add contacts') &&
246 !CRM_Core_Permission
::check('edit groups')
250 //validate across edit/view - CRM-5666
251 if ($hasAccess && ($id == self
::ADD
)) {
252 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
259 if ($id == self
::EVENT
&&
260 (!CRM_Core_Permission
::access('CiviEvent', FALSE) ||
261 !CRM_Core_Permission
::check('view event info')
268 'info' => $value['info'],
269 'cache' => $value['cache'],
270 'status' => $value['active'],
271 'region' => $value['region'],
272 'visibility' => $value['visibility'],
273 'pages' => $value['pages'],
274 'status' => $value['status'],
275 'weight' => $value['weight'],
284 * set the post action values for the block.
286 * 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
;
307 $values = array('postURL' => CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&ct=Individual'),
308 'primaryLocationType' => $defaultPrimaryLocationId,
311 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
312 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils
::defaultGreeting('Individual', $greeting);
315 self
::setProperty(self
::ADD
,
321 case self
::FULLTEXT_SEARCH
:
323 'fullTextSearchID' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
324 '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
346 private static function setTemplateShortcutValues() {
347 $config = CRM_Core_Config
::singleton();
349 static $shortCuts = array();
352 if (CRM_Core_Permission
::check('add contacts')) {
353 if (CRM_Core_Permission
::giveMeAllACLs()) {
354 $shortCuts = CRM_Contact_BAO_ContactType
::getCreateNewList();
358 // new activity (select target contact)
359 $shortCuts = array_merge($shortCuts, array(
361 'path' => 'civicrm/activity',
362 'query' => 'action=add&reset=1&context=standalone',
363 'ref' => 'new-activity',
364 'title' => ts('Activity'),
367 $components = CRM_Core_Component
::getEnabledComponents();
369 if (!empty($config->enableComponents
)) {
370 // check if we can process credit card contribs
371 $newCredit = CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments();
373 foreach ($components as $componentName => $obj) {
374 if (in_array($componentName, $config->enableComponents
)) {
375 $obj->creatNewShortcut($shortCuts, $newCredit);
380 // new email (select recipients)
381 $shortCuts = array_merge($shortCuts, array(
382 array('path' => 'civicrm/activity/email/add',
383 'query' => 'atype=3&action=add&reset=1&context=standalone',
384 'ref' => 'new-email',
385 'title' => ts('Email'),
388 if (CRM_Core_Permission
::check('edit groups')) {
389 $shortCuts = array_merge($shortCuts, array(
390 array('path' => 'civicrm/group/add',
391 'query' => 'reset=1',
392 'ref' => 'new-group',
393 'title' => ts('Group'),
397 if (CRM_Core_Permission
::check('administer CiviCRM')) {
398 $shortCuts = array_merge($shortCuts, array(
399 array('path' => 'civicrm/admin/tag',
400 'query' => 'reset=1&action=add',
402 'title' => ts('Tag'),
406 if (empty($shortCuts)) {
412 foreach ($shortCuts as $key => $short) {
413 $values[$key] = self
::setShortCutValues($short);
416 // call links hook to add user defined links
417 CRM_Utils_Hook
::links('create.new.shorcuts',
419 CRM_Core_DAO
::$_nullObject,
421 CRM_Core_DAO
::$_nullObject,
422 CRM_Core_DAO
::$_nullObject
425 foreach ($values as $key => $val) {
426 if (!empty($val['title'])) {
427 $values[$key]['name'] = CRM_Utils_Array
::value('name', $val, $val['title']);
431 self
::setProperty(self
::CREATE_NEW
, 'templateValues', array('shortCuts' => $values));
439 private static function setShortcutValues($short) {
441 if (isset($short['url'])) {
442 $value['url'] = $short['url'];
444 elseif (isset($short['path'])) {
445 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query'], FALSE);
447 $value['title'] = $short['title'];
448 $value['ref'] = $short['ref'];
449 if (!empty($short['shortCuts'])) {
450 foreach ($short['shortCuts'] as $shortCut) {
451 $value['shortCuts'][] = self
::setShortcutValues($shortCut);
458 * create the list of dashboard links
463 private static function setTemplateDashboardValues() {
464 static $dashboardLinks = array();
465 if (CRM_Core_Permission
::check('access Contact Dashboard')) {
466 $dashboardLinks = array(
467 array('path' => 'civicrm/user',
468 'query' => 'reset=1',
469 'title' => ts('My Contact Dashboard'),
473 if (empty($dashboardLinks)) {
478 foreach ($dashboardLinks as $dash) {
480 if (isset($dash['url'])) {
481 $value['url'] = $dash['url'];
484 $value['url'] = CRM_Utils_System
::url($dash['path'], $dash['query'], FALSE);
486 $value['title'] = $dash['title'];
487 $value['key'] = CRM_Utils_Array
::value('key', $dash);
490 self
::setProperty(self
::DASHBOARD
, 'templateValues', array('dashboardLinks' => $values));
494 * create the list of mail urls for the application and format is as a block
499 private static function setTemplateMailValues() {
500 static $shortCuts = NULL;
504 array('path' => 'civicrm/mailing/send',
505 'query' => 'reset=1',
506 'title' => ts('Send Mailing'),
509 'path' => 'civicrm/mailing/browse',
510 'query' => 'reset=1',
511 'title' => ts('Browse Sent Mailings'),
517 foreach ($shortCuts as $short) {
519 $value['url'] = CRM_Utils_System
::url($short['path'], $short['query']);
520 $value['title'] = $short['title'];
523 self
::setProperty(self
::MAIL
, 'templateValues', array('shortCuts' => $values));
527 * create the list of shortcuts for the application and format is as a block
532 private static function setTemplateMenuValues() {
533 $config = CRM_Core_Config
::singleton();
535 $path = 'navigation';
536 $values = CRM_Core_Menu
::getNavigation();
538 self
::setProperty(self
::MENU
, 'templateValues', array('menu' => $values));
543 * create the event blocks for upcoming events
548 private static function setTemplateEventValues() {
549 $config = CRM_Core_Config
::singleton();
551 $info = CRM_Event_BAO_Event
::getCompleteInfo(date("Ymd"));
554 $session = CRM_Core_Session
::singleton();
555 // check if registration link should be displayed
556 foreach ($info as $id => $event) {
557 //@todo FIXME - validRegistraionRequest takes eventID not contactID as a param
558 // this is called via an obscure patch from Joomla event block rendering (only)
559 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event
::validRegistrationRequest($event,
560 $session->get('userID')
564 self
::setProperty(self
::EVENT
, 'templateValues', array('eventBlock' => $info));
569 * Given an id creates a subject/content array
571 * @param int $id id of the block
576 static function getContent($id) {
577 // return if upgrade mode
578 $config = CRM_Core_Config
::singleton();
579 if ($config->isUpgradeMode()) {
583 if (!self
::getProperty($id, 'active')) {
587 if ($id == self
::EVENT
&&
588 CRM_Core_Permission
::check('view event info')
590 // is CiviEvent enabled?
591 if (!CRM_Core_Permission
::access('CiviEvent', FALSE)) {
596 // require 'access CiviCRM' permissons, except for the language switch block
597 elseif (!CRM_Core_Permission
::check('access CiviCRM') && $id!=self
::LANGSWITCH
) {
600 elseif ($id == self
::ADD
) {
602 if (!CRM_Core_Permission
::check('add contacts') &&
603 !CRM_Core_Permission
::check('edit groups')
607 //validate across edit/view - CRM-5666
609 $hasAccess = CRM_Core_Permission
::giveMeAllACLs();
616 self
::setTemplateValues($id);
618 // Suppress Recent Items block if it's empty - CRM-5188
619 if ($id == self
::RECENTLY_VIEWED
) {
620 $recent = self
::getProperty($id, 'templateValues');
621 if (CRM_Utils_Array
::crmIsEmptyArray($recent)) {
626 // Suppress Language switcher if language is inherited from CMS - CRM-9971
627 $config = CRM_Core_Config
::singleton();
628 if ($id == self
::LANGSWITCH
&& property_exists($config, "inheritLocale") && $config->inheritLocale
) {
633 $block['name'] = 'block-civicrm';
634 $block['id'] = $block['name'] . '_' . $id;
635 $block['subject'] = self
::fetch($id, 'Subject.tpl',
636 array('subject' => self
::getProperty($id, 'subject'))
638 $block['content'] = self
::fetch($id, self
::getProperty($id, 'template'),
639 self
::getProperty($id, 'templateValues')
647 * Given an id and a template, fetch the contents
649 * @param int $id id of the block
650 * @param string $fileName name of the template file
651 * @param array $properties template variables
656 static function fetch($id, $fileName, $properties) {
657 $template = CRM_Core_Smarty
::singleton();
660 $template->assign($properties);
663 return $template->fetch('CRM/Block/' . $fileName);