CRM-15172 - Fix profile state/county required options
[civicrm-core.git] / CRM / Core / Block.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 */
41class 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,
fd0bc470 169 'cache' => BLOCK_NO_CACHE,
6a488035
TO
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 * @params int $id one of the class constants (ADD, SEARCH, etc.)
186 * @params string $property the desired property
187 *
da6b46f4
EM
188 * @param $id
189 * @param $property
190 *
6a488035
TO
191 * @return string the value of the desired property
192 */
193 static function getProperty($id, $property) {
194 if (!(self::$_properties)) {
195 self::initProperties();
196 }
197 return isset(self::$_properties[$id][$property]) ? self::$_properties[$id][$property] : NULL;
198 }
199
200 /**
201 * sets the desired property in the $_properties array
202 *
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
206 *
da6b46f4
EM
207 * @param $id
208 * @param $property
209 * @param $value
6a488035
TO
210 * @return void
211 */
212 static function setProperty($id, $property, $value) {
213 if (!(self::$_properties)) {
214 self::initProperties();
215 }
216 self::$_properties[$id][$property] = $value;
217 }
218
219 /**
220 * returns the whole $_properties array
221 *
222 * @return array the $_properties array
223 */
224 static function properties() {
225 if (!(self::$_properties)) {
226 self::initProperties();
227 }
228 return self::$_properties;
229 }
230
231 /**
232 * Creates the info block for drupal
233 *
234 * @return array
235 * @access public
236 */
237 static function getInfo() {
238
239 $block = array();
240 foreach (self::properties() as $id => $value) {
241 if ($value['active']) {
242 if (in_array($id, array(
243 self::ADD, self::CREATE_NEW))) {
244 $hasAccess = TRUE;
245 if (!CRM_Core_Permission::check('add contacts') &&
246 !CRM_Core_Permission::check('edit groups')
247 ) {
248 $hasAccess = FALSE;
249 }
250 //validate across edit/view - CRM-5666
251 if ($hasAccess && ($id == self::ADD)) {
252 $hasAccess = CRM_Core_Permission::giveMeAllACLs();
253 }
254 if (!$hasAccess) {
255 continue;
256 }
257 }
258
259 if ($id == self::EVENT &&
260 (!CRM_Core_Permission::access('CiviEvent', FALSE) ||
261 !CRM_Core_Permission::check('view event info')
262 )
263 ) {
264 continue;
265 }
266
267 $block[$id] = array(
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'],
276 );
277 }
278 }
279
280 return $block;
281 }
282
283 /**
284 * set the post action values for the block.
285 *
286 * php is lame and u cannot call functions from static initializers
287 * hence this hack
288 *
dd244018
EM
289 * @param $id
290 *
6a488035
TO
291 * @return void
292 * @access private
293 */
294 private static function setTemplateValues($id) {
295 switch ($id) {
296 case self::CREATE_NEW:
297 self::setTemplateShortcutValues();
298 break;
299
300 case self::DASHBOARD:
301 self::setTemplateDashboardValues();
302 break;
303
304 case self::ADD:
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,
309 );
310
311 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
312 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
313 }
314
315 self::setProperty(self::ADD,
316 'templateValues',
317 $values
318 );
319 break;
320
321 case self::FULLTEXT_SEARCH:
322 $urlArray = array(
323 'fullTextSearchID' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
324 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
325 ));
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 /**
341 * create the list of options to create New objects for the application and format is as a block
342 *
343 * @return void
344 * @access private
345 */
346 private static function setTemplateShortcutValues() {
347 $config = CRM_Core_Config::singleton();
348
349 static $shortCuts = array();
350
351 if (!($shortCuts)) {
352 if (CRM_Core_Permission::check('add contacts')) {
353 if (CRM_Core_Permission::giveMeAllACLs()) {
354 $shortCuts = CRM_Contact_BAO_ContactType::getCreateNewList();
355 }
356 }
357
358 // new activity (select target contact)
359 $shortCuts = array_merge($shortCuts, array(
360 array(
361 'path' => 'civicrm/activity',
362 'query' => 'action=add&reset=1&context=standalone',
363 'ref' => 'new-activity',
364 'title' => ts('Activity'),
365 )));
366
367 $components = CRM_Core_Component::getEnabledComponents();
368
369 if (!empty($config->enableComponents)) {
370 // check if we can process credit card contribs
9be1374d 371 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
fd0bc470 372
6a488035
TO
373 foreach ($components as $componentName => $obj) {
374 if (in_array($componentName, $config->enableComponents)) {
0e6e8724 375 $obj->creatNewShortcut($shortCuts, $newCredit);
6a488035
TO
376 }
377 }
378 }
379
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'),
386 )));
387
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'),
394 )));
395 }
396
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',
401 'ref' => 'new-tag',
402 'title' => ts('Tag'),
403 )));
404 }
405
406 if (empty($shortCuts)) {
407 return NULL;
408 }
409 }
410
411 $values = array();
7926b999 412 foreach ($shortCuts as $key => $short) {
413 $values[$key] = self::setShortCutValues($short);
6a488035
TO
414 }
415
416 // call links hook to add user defined links
417 CRM_Utils_Hook::links('create.new.shorcuts',
418 NULL,
419 CRM_Core_DAO::$_nullObject,
420 $values,
a9b15f31 421 CRM_Core_DAO::$_nullObject,
6a488035
TO
422 CRM_Core_DAO::$_nullObject
423 );
e65cf508 424
727b675c 425 foreach ($values as $key => $val) {
e65cf508 426 if (!empty($val['title'])) {
727b675c
AH
427 $values[$key]['name'] = CRM_Utils_Array::value('name', $val, $val['title']);
428 }
429 }
6a488035
TO
430
431 self::setProperty(self::CREATE_NEW, 'templateValues', array('shortCuts' => $values));
432 }
433
a0ee3941
EM
434 /**
435 * @param $short
436 *
437 * @return array
438 */
7926b999 439 private static function setShortcutValues($short) {
440 $value = array();
441 if (isset($short['url'])) {
442 $value['url'] = $short['url'];
443 }
444 elseif (isset($short['path'])) {
445 $value['url'] = CRM_Utils_System::url($short['path'], $short['query'], FALSE);
446 }
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);
452 }
453 }
454 return $value;
455 }
456
6a488035
TO
457 /**
458 * create the list of dashboard links
459 *
460 * @return void
461 * @access private
462 */
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'),
470 ));
471 }
472
473 if (empty($dashboardLinks)) {
474 return NULL;
475 }
476
477 $values = array();
478 foreach ($dashboardLinks as $dash) {
479 $value = array();
480 if (isset($dash['url'])) {
481 $value['url'] = $dash['url'];
482 }
483 else {
484 $value['url'] = CRM_Utils_System::url($dash['path'], $dash['query'], FALSE);
485 }
486 $value['title'] = $dash['title'];
487 $value['key'] = CRM_Utils_Array::value('key', $dash);
488 $values[] = $value;
489 }
490 self::setProperty(self::DASHBOARD, 'templateValues', array('dashboardLinks' => $values));
491 }
492
493 /**
494 * create the list of mail urls for the application and format is as a block
495 *
496 * @return void
497 * @access private
498 */
499 private static function setTemplateMailValues() {
500 static $shortCuts = NULL;
501
502 if (!($shortCuts)) {
503 $shortCuts = array(
504 array('path' => 'civicrm/mailing/send',
505 'query' => 'reset=1',
506 'title' => ts('Send Mailing'),
507 ),
508 array(
509 'path' => 'civicrm/mailing/browse',
510 'query' => 'reset=1',
511 'title' => ts('Browse Sent Mailings'),
512 ),
513 );
514 }
515
516 $values = array();
517 foreach ($shortCuts as $short) {
518 $value = array();
519 $value['url'] = CRM_Utils_System::url($short['path'], $short['query']);
520 $value['title'] = $short['title'];
521 $values[] = $value;
522 }
523 self::setProperty(self::MAIL, 'templateValues', array('shortCuts' => $values));
524 }
525
526 /**
527 * create the list of shortcuts for the application and format is as a block
528 *
529 * @return void
530 * @access private
531 */
532 private static function setTemplateMenuValues() {
533 $config = CRM_Core_Config::singleton();
534
535 $path = 'navigation';
536 $values = CRM_Core_Menu::getNavigation();
537 if ($values) {
538 self::setProperty(self::MENU, 'templateValues', array('menu' => $values));
539 }
540 }
541
542 /**
543 * create the event blocks for upcoming events
544 *
545 * @return void
546 * @access private
547 */
548 private static function setTemplateEventValues() {
549 $config = CRM_Core_Config::singleton();
550
551 $info = CRM_Event_BAO_Event::getCompleteInfo(date("Ymd"));
552
553 if ($info) {
554 $session = CRM_Core_Session::singleton();
555 // check if registration link should be displayed
556 foreach ($info as $id => $event) {
e2d09ab4
EM
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)
6a488035
TO
559 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event::validRegistrationRequest($event,
560 $session->get('userID')
561 );
562 }
563
564 self::setProperty(self::EVENT, 'templateValues', array('eventBlock' => $info));
565 }
566 }
567
568 /**
569 * Given an id creates a subject/content array
570 *
571 * @param int $id id of the block
572 *
573 * @return array
574 * @access public
575 */
576 static function getContent($id) {
577 // return if upgrade mode
578 $config = CRM_Core_Config::singleton();
579 if ($config->isUpgradeMode()) {
580 return;
581 }
582
583 if (!self::getProperty($id, 'active')) {
584 return NULL;
585 }
586
587 if ($id == self::EVENT &&
588 CRM_Core_Permission::check('view event info')
589 ) {
590 // is CiviEvent enabled?
591 if (!CRM_Core_Permission::access('CiviEvent', FALSE)) {
592 return NULL;
593 }
594 // do nothing
595 }
550b2acf 596 // require 'access CiviCRM' permissons, except for the language switch block
597 elseif (!CRM_Core_Permission::check('access CiviCRM') && $id!=self::LANGSWITCH) {
6a488035
TO
598 return NULL;
599 }
600 elseif ($id == self::ADD) {
601 $hasAccess = TRUE;
602 if (!CRM_Core_Permission::check('add contacts') &&
603 !CRM_Core_Permission::check('edit groups')
604 ) {
605 $hasAccess = FALSE;
606 }
607 //validate across edit/view - CRM-5666
608 if ($hasAccess) {
609 $hasAccess = CRM_Core_Permission::giveMeAllACLs();
610 }
611 if (!$hasAccess) {
612 return NULL;
613 }
614 }
615
616 self::setTemplateValues($id);
617
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)) {
622 return NULL;
623 }
624 }
625
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) {
629 return NULL;
630 }
631
632 $block = array();
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'))
637 );
638 $block['content'] = self::fetch($id, self::getProperty($id, 'template'),
639 self::getProperty($id, 'templateValues')
640 );
641
642
643 return $block;
644 }
645
646 /**
647 * Given an id and a template, fetch the contents
648 *
649 * @param int $id id of the block
650 * @param string $fileName name of the template file
651 * @param array $properties template variables
652 *
653 * @return array
654 * @access public
655 */
656 static function fetch($id, $fileName, $properties) {
657 $template = CRM_Core_Smarty::singleton();
658
659 if ($properties) {
660 $template->assign($properties);
661 }
662
663 return $template->fetch('CRM/Block/' . $fileName);
664 }
665}
666