Merge pull request #4693 from jaapjansma/CRM-15702
[civicrm-core.git] / CRM / Core / Block.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * defines a simple implemenation of a drupal block.
38 * blocks definitions and html are in a smarty template file
39 *
40 */
41 class CRM_Core_Block {
42
43 /**
44 * The following blocks are supported
45 *
46 * @var int
47 */
48 CONST
49 CREATE_NEW = 1,
50 RECENTLY_VIEWED = 2,
51 DASHBOARD = 3,
52 ADD = 4,
53 LANGSWITCH = 5,
54 EVENT = 6,
55 FULLTEXT_SEARCH = 7;
56
57 /**
58 * Template file names for the above blocks
59 */
60 static $_properties = NULL;
61
62 /**
63 * Class constructor
64 *
65 */
66 function __construct() {}
67
68 /**
69 * Initialises the $_properties array
70 *
71 * @return void
72 */
73 static function initProperties() {
74 if (!defined('BLOCK_CACHE_GLOBAL')) {
75 define('BLOCK_CACHE_GLOBAL', 0x0008);
76 }
77
78 if (!defined('BLOCK_CACHE_PER_PAGE')) {
79 define('BLOCK_CACHE_PER_PAGE', 0x0004);
80 }
81
82 if (!defined('BLOCK_NO_CACHE')) {
83 define('BLOCK_NO_CACHE', -1);
84 }
85
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'),
93 'subject' => ts(''),
94 'active' => TRUE,
95 'cache' => BLOCK_CACHE_GLOBAL,
96 'visibility' => 1,
97 'weight' => -100,
98 'status' => 1,
99 'pages' => "civicrm\ncivicrm/*",
100 'region' => $config->userSystem->getDefaultBlockLocation(),
101 ),
102 self::RECENTLY_VIEWED => array(
103 'template' => 'RecentlyViewed.tpl',
104 'info' => ts('CiviCRM Recent Items'),
105 'subject' => ts('Recent Items'),
106 'active' => TRUE,
107 'cache' => BLOCK_NO_CACHE,
108 'visibility' => 1,
109 'weight' => -99,
110 'status' => 1,
111 'pages' => "civicrm\ncivicrm/*",
112 'region' => $config->userSystem->getDefaultBlockLocation(),
113 ),
114 self::DASHBOARD => array(
115 'template' => 'Dashboard.tpl',
116 'info' => ts('CiviCRM Contact Dashboard'),
117 'subject' => '',
118 'active' => TRUE,
119 'cache' => BLOCK_NO_CACHE,
120 'visibility' => 1,
121 'weight' => -98,
122 'status' => 1,
123 'pages' => "civicrm\ncivicrm/*",
124 'region' => $config->userSystem->getDefaultBlockLocation(),
125 ),
126 self::ADD => array(
127 'template' => 'Add.tpl',
128 'info' => ts('CiviCRM Quick Add'),
129 'subject' => ts('New Individual'),
130 'active' => TRUE,
131 'cache' => BLOCK_NO_CACHE,
132 'visibility' => 1,
133 'weight' => -97,
134 'status' => 1,
135 'pages' => "civicrm\ncivicrm/*",
136 'region' => $config->userSystem->getDefaultBlockLocation(),
137 ),
138 self::LANGSWITCH => array(
139 'template' => 'LangSwitch.tpl',
140 'info' => ts('CiviCRM Language Switcher'),
141 'subject' => '',
142 'templateValues' => array(),
143 'active' => TRUE,
144 'cache' => BLOCK_NO_CACHE,
145 'visibility' => 1,
146 'weight' => -96,
147 'status' => 1,
148 'pages' => "civicrm\ncivicrm/*",
149 'region' => $config->userSystem->getDefaultBlockLocation(),
150 ),
151 self::EVENT => array(
152 'template' => 'Event.tpl',
153 'info' => ts('CiviCRM Upcoming Events'),
154 'subject' => ts('Upcoming Events'),
155 'templateValues' => array(),
156 'active' => TRUE,
157 'cache' => BLOCK_NO_CACHE,
158 'visibility' => 1,
159 'weight' => -95,
160 'status' => 0,
161 'pages' => "civicrm\ncivicrm/*",
162 'region' => $config->userSystem->getDefaultBlockLocation(),
163 ),
164 self::FULLTEXT_SEARCH => array(
165 'template' => 'FullTextSearch.tpl',
166 'info' => ts('CiviCRM Full-text Search'),
167 'subject' => ts('Full-text Search'),
168 'active' => TRUE,
169 'cache' => BLOCK_NO_CACHE,
170 'visibility' => 1,
171 'weight' => -94,
172 'status' => 0,
173 'pages' => "civicrm\ncivicrm/*",
174 'region' => $config->userSystem->getDefaultBlockLocation(),
175 ),
176 );
177
178 ksort(self::$_properties);
179 }
180 }
181
182 /**
183 * Returns the desired property from the $_properties array
184 *
185 * @param int $id one of the class constants (ADD, SEARCH, etc.)
186 * @param string $property the desired property
187 *
188 * @return string the value of the desired property
189 */
190 static function getProperty($id, $property) {
191 if (!(self::$_properties)) {
192 self::initProperties();
193 }
194 return isset(self::$_properties[$id][$property]) ? self::$_properties[$id][$property] : NULL;
195 }
196
197 /**
198 * Sets the desired property in the $_properties array
199 *
200 * @param int $id one of the class constants (ADD, SEARCH, etc.)
201 * @param string $property the desired property
202 * @param string $value the value of the desired property
203 *
204 * @return void
205 */
206 static function setProperty($id, $property, $value) {
207 if (!(self::$_properties)) {
208 self::initProperties();
209 }
210 self::$_properties[$id][$property] = $value;
211 }
212
213 /**
214 * Returns the whole $_properties array
215 *
216 * @return array the $_properties array
217 */
218 static function properties() {
219 if (!(self::$_properties)) {
220 self::initProperties();
221 }
222 return self::$_properties;
223 }
224
225 /**
226 * Creates the info block for drupal
227 *
228 * @return array
229 * @access public
230 */
231 static function getInfo() {
232
233 $block = array();
234 foreach (self::properties() as $id => $value) {
235 if ($value['active']) {
236 if (in_array($id, array(
237 self::ADD, self::CREATE_NEW))) {
238 $hasAccess = TRUE;
239 if (!CRM_Core_Permission::check('add contacts') &&
240 !CRM_Core_Permission::check('edit groups')
241 ) {
242 $hasAccess = FALSE;
243 }
244 //validate across edit/view - CRM-5666
245 if ($hasAccess && ($id == self::ADD)) {
246 $hasAccess = CRM_Core_Permission::giveMeAllACLs();
247 }
248 if (!$hasAccess) {
249 continue;
250 }
251 }
252
253 if ($id == self::EVENT &&
254 (!CRM_Core_Permission::access('CiviEvent', FALSE) ||
255 !CRM_Core_Permission::check('view event info')
256 )
257 ) {
258 continue;
259 }
260
261 $block[$id] = array(
262 'info' => $value['info'],
263 'cache' => $value['cache'],
264 'status' => $value['active'],
265 'region' => $value['region'],
266 'visibility' => $value['visibility'],
267 'pages' => $value['pages'],
268 'status' => $value['status'],
269 'weight' => $value['weight'],
270 );
271 }
272 }
273
274 return $block;
275 }
276
277 /**
278 * Set the post action values for the block.
279 *
280 * php is lame and u cannot call functions from static initializers
281 * hence this hack
282 *
283 * @param int $id
284 *
285 * @return void
286 * @access private
287 */
288 private static function setTemplateValues($id) {
289 switch ($id) {
290 case self::CREATE_NEW:
291 self::setTemplateShortcutValues();
292 break;
293
294 case self::DASHBOARD:
295 self::setTemplateDashboardValues();
296 break;
297
298 case self::ADD:
299 $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
300 $defaultPrimaryLocationId = $defaultLocation->id;
301 $values = array('postURL' => CRM_Utils_System::url('civicrm/contact/add', 'reset=1&ct=Individual'),
302 'primaryLocationType' => $defaultPrimaryLocationId,
303 );
304
305 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
306 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
307 }
308
309 self::setProperty(self::ADD,
310 'templateValues',
311 $values
312 );
313 break;
314
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);
319 break;
320 case self::FULLTEXT_SEARCH:
321 $urlArray = array(
322 'fullTextSearchID' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
323 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
324 ));
325 self::setProperty(self::FULLTEXT_SEARCH, 'templateValues', $urlArray);
326 break;
327
328 case self::RECENTLY_VIEWED:
329 $recent = CRM_Utils_Recent::get();
330 self::setProperty(self::RECENTLY_VIEWED, 'templateValues', array('recentlyViewed' => $recent));
331 break;
332
333 case self::EVENT:
334 self::setTemplateEventValues();
335 break;
336 }
337 }
338
339 /**
340 * Create the list of options to create New objects for the application and format is as a block
341 *
342 * @return void
343 * @access private
344 */
345 private static function setTemplateShortcutValues() {
346 $config = CRM_Core_Config::singleton();
347
348 static $shortCuts = array();
349
350 if (!($shortCuts)) {
351 if (CRM_Core_Permission::check('add contacts')) {
352 if (CRM_Core_Permission::giveMeAllACLs()) {
353 $shortCuts = CRM_Contact_BAO_ContactType::getCreateNewList();
354 }
355 }
356
357 // new activity (select target contact)
358 $shortCuts = array_merge($shortCuts, array(
359 array(
360 'path' => 'civicrm/activity',
361 'query' => 'action=add&reset=1&context=standalone',
362 'ref' => 'new-activity',
363 'title' => ts('Activity'),
364 )));
365
366 $components = CRM_Core_Component::getEnabledComponents();
367
368 if (!empty($config->enableComponents)) {
369 // check if we can process credit card contribs
370 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
371
372 foreach ($components as $componentName => $obj) {
373 if (in_array($componentName, $config->enableComponents)) {
374 $obj->creatNewShortcut($shortCuts, $newCredit);
375 }
376 }
377 }
378
379 // new email (select recipients)
380 $shortCuts = array_merge($shortCuts, array(
381 array('path' => 'civicrm/activity/email/add',
382 'query' => 'atype=3&action=add&reset=1&context=standalone',
383 'ref' => 'new-email',
384 'title' => ts('Email'),
385 )));
386
387 if (CRM_Core_Permission::check('edit groups')) {
388 $shortCuts = array_merge($shortCuts, array(
389 array('path' => 'civicrm/group/add',
390 'query' => 'reset=1',
391 'ref' => 'new-group',
392 'title' => ts('Group'),
393 )));
394 }
395
396 if (CRM_Core_Permission::check('administer CiviCRM')) {
397 $shortCuts = array_merge($shortCuts, array(
398 array('path' => 'civicrm/admin/tag',
399 'query' => 'reset=1&action=add',
400 'ref' => 'new-tag',
401 'title' => ts('Tag'),
402 )));
403 }
404
405 if (empty($shortCuts)) {
406 return NULL;
407 }
408 }
409
410 $values = array();
411 foreach ($shortCuts as $key => $short) {
412 $values[$key] = self::setShortCutValues($short);
413 }
414
415 // call links hook to add user defined links
416 CRM_Utils_Hook::links('create.new.shorcuts',
417 NULL,
418 CRM_Core_DAO::$_nullObject,
419 $values,
420 CRM_Core_DAO::$_nullObject,
421 CRM_Core_DAO::$_nullObject
422 );
423
424 foreach ($values as $key => $val) {
425 if (!empty($val['title'])) {
426 $values[$key]['name'] = CRM_Utils_Array::value('name', $val, $val['title']);
427 }
428 }
429
430 self::setProperty(self::CREATE_NEW, 'templateValues', array('shortCuts' => $values));
431 }
432
433 /**
434 * @param $short
435 *
436 * @return array
437 */
438 private static function setShortcutValues($short) {
439 $value = array();
440 if (isset($short['url'])) {
441 $value['url'] = $short['url'];
442 }
443 elseif (isset($short['path'])) {
444 $value['url'] = CRM_Utils_System::url($short['path'], $short['query'], FALSE);
445 }
446 $value['title'] = $short['title'];
447 $value['ref'] = $short['ref'];
448 if (!empty($short['shortCuts'])) {
449 foreach ($short['shortCuts'] as $shortCut) {
450 $value['shortCuts'][] = self::setShortcutValues($shortCut);
451 }
452 }
453 return $value;
454 }
455
456 /**
457 * Create the list of dashboard links
458 *
459 * @return void
460 * @access private
461 */
462 private static function setTemplateDashboardValues() {
463 static $dashboardLinks = array();
464 if (CRM_Core_Permission::check('access Contact Dashboard')) {
465 $dashboardLinks = array(
466 array('path' => 'civicrm/user',
467 'query' => 'reset=1',
468 'title' => ts('My Contact Dashboard'),
469 ));
470 }
471
472 if (empty($dashboardLinks)) {
473 return NULL;
474 }
475
476 $values = array();
477 foreach ($dashboardLinks as $dash) {
478 $value = array();
479 if (isset($dash['url'])) {
480 $value['url'] = $dash['url'];
481 }
482 else {
483 $value['url'] = CRM_Utils_System::url($dash['path'], $dash['query'], FALSE);
484 }
485 $value['title'] = $dash['title'];
486 $value['key'] = CRM_Utils_Array::value('key', $dash);
487 $values[] = $value;
488 }
489 self::setProperty(self::DASHBOARD, 'templateValues', array('dashboardLinks' => $values));
490 }
491
492 /**
493 * Create the list of mail urls for the application and format is as a block
494 *
495 * @return void
496 * @access private
497 */
498 private static function setTemplateMailValues() {
499 static $shortCuts = NULL;
500
501 if (!($shortCuts)) {
502 $shortCuts = array(
503 array('path' => 'civicrm/mailing/send',
504 'query' => 'reset=1',
505 'title' => ts('Send Mailing'),
506 ),
507 array(
508 'path' => 'civicrm/mailing/browse',
509 'query' => 'reset=1',
510 'title' => ts('Browse Sent Mailings'),
511 ),
512 );
513 }
514
515 $values = array();
516 foreach ($shortCuts as $short) {
517 $value = array();
518 $value['url'] = CRM_Utils_System::url($short['path'], $short['query']);
519 $value['title'] = $short['title'];
520 $values[] = $value;
521 }
522 self::setProperty(self::MAIL, 'templateValues', array('shortCuts' => $values));
523 }
524
525 /**
526 * Create the list of shortcuts for the application and format is as a block
527 *
528 * @return void
529 * @access private
530 */
531 private static function setTemplateMenuValues() {
532 $config = CRM_Core_Config::singleton();
533
534 $path = 'navigation';
535 $values = CRM_Core_Menu::getNavigation();
536 if ($values) {
537 self::setProperty(self::MENU, 'templateValues', array('menu' => $values));
538 }
539 }
540
541 /**
542 * Create the event blocks for upcoming events
543 *
544 * @return void
545 * @access private
546 */
547 private static function setTemplateEventValues() {
548 $config = CRM_Core_Config::singleton();
549
550 $info = CRM_Event_BAO_Event::getCompleteInfo(date("Ymd"));
551
552 if ($info) {
553 $session = CRM_Core_Session::singleton();
554 // check if registration link should be displayed
555 foreach ($info as $id => $event) {
556 //@todo FIXME - validRegistraionRequest takes eventID not contactID as a param
557 // this is called via an obscure patch from Joomla event block rendering (only)
558 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event::validRegistrationRequest($event,
559 $session->get('userID')
560 );
561 }
562
563 self::setProperty(self::EVENT, 'templateValues', array('eventBlock' => $info));
564 }
565 }
566
567 /**
568 * Given an id creates a subject/content array
569 *
570 * @param int $id id of the block
571 *
572 * @return array
573 * @access public
574 */
575 static function getContent($id) {
576 // return if upgrade mode
577 $config = CRM_Core_Config::singleton();
578 if ($config->isUpgradeMode()) {
579 return;
580 }
581
582 if (!self::getProperty($id, 'active')) {
583 return NULL;
584 }
585
586 if ($id == self::EVENT &&
587 CRM_Core_Permission::check('view event info')
588 ) {
589 // is CiviEvent enabled?
590 if (!CRM_Core_Permission::access('CiviEvent', FALSE)) {
591 return NULL;
592 }
593 // do nothing
594 }
595 // require 'access CiviCRM' permissons, except for the language switch block
596 elseif (!CRM_Core_Permission::check('access CiviCRM') && $id!=self::LANGSWITCH) {
597 return NULL;
598 }
599 elseif ($id == self::ADD) {
600 $hasAccess = TRUE;
601 if (!CRM_Core_Permission::check('add contacts') &&
602 !CRM_Core_Permission::check('edit groups')
603 ) {
604 $hasAccess = FALSE;
605 }
606 //validate across edit/view - CRM-5666
607 if ($hasAccess) {
608 $hasAccess = CRM_Core_Permission::giveMeAllACLs();
609 }
610 if (!$hasAccess) {
611 return NULL;
612 }
613 }
614
615 self::setTemplateValues($id);
616
617 // Suppress Recent Items block if it's empty - CRM-5188
618 if ($id == self::RECENTLY_VIEWED) {
619 $recent = self::getProperty($id, 'templateValues');
620 if (CRM_Utils_Array::crmIsEmptyArray($recent)) {
621 return NULL;
622 }
623 }
624
625 // Suppress Language switcher if language is inherited from CMS - CRM-9971
626 $config = CRM_Core_Config::singleton();
627 if ($id == self::LANGSWITCH && property_exists($config, "inheritLocale") && $config->inheritLocale) {
628 return NULL;
629 }
630
631 $block = array();
632 $block['name'] = 'block-civicrm';
633 $block['id'] = $block['name'] . '_' . $id;
634 $block['subject'] = self::fetch($id, 'Subject.tpl',
635 array('subject' => self::getProperty($id, 'subject'))
636 );
637 $block['content'] = self::fetch($id, self::getProperty($id, 'template'),
638 self::getProperty($id, 'templateValues')
639 );
640
641
642 return $block;
643 }
644
645 /**
646 * Given an id and a template, fetch the contents
647 *
648 * @param int $id id of the block
649 * @param string $fileName name of the template file
650 * @param array $properties template variables
651 *
652 * @return array
653 * @access public
654 */
655 static function fetch($id, $fileName, $properties) {
656 $template = CRM_Core_Smarty::singleton();
657
658 if ($properties) {
659 $template->assign($properties);
660 }
661
662 return $template->fetch('CRM/Block/' . $fileName);
663 }
664 }
665