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