Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2013-12-16-21-42-04
[civicrm-core.git] / CRM / Core / Block.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * @params int $id one of the class constants (ADD, SEARCH, etc.)
186 * @params 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 * @params int $id one of the class constants (ADD, SEARCH, etc.)
201 * @params string $property the desired property
202 * @params 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 * @return void
284 * @access private
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;
299 $values = array('postURL' => CRM_Utils_System::url('civicrm/contact/add', 'reset=1&ct=Individual'),
300 'primaryLocationType' => $defaultPrimaryLocationId,
301 );
302
303 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
304 $values[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
305 }
306
307 self::setProperty(self::ADD,
308 'templateValues',
309 $values
310 );
311 break;
312
313 case self::FULLTEXT_SEARCH:
314 $urlArray = array(
315 'fullTextSearchID' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
316 'CRM_Contact_Form_Search_Custom_FullText', 'value', 'name'
317 ));
318 self::setProperty(self::FULLTEXT_SEARCH, 'templateValues', $urlArray);
319 break;
320
321 case self::RECENTLY_VIEWED:
322 $recent = CRM_Utils_Recent::get();
323 self::setProperty(self::RECENTLY_VIEWED, 'templateValues', array('recentlyViewed' => $recent));
324 break;
325
326 case self::EVENT:
327 self::setTemplateEventValues();
328 break;
329 }
330 }
331
332 /**
333 * create the list of options to create New objects for the application and format is as a block
334 *
335 * @return void
336 * @access private
337 */
338 private static function setTemplateShortcutValues() {
339 $config = CRM_Core_Config::singleton();
340
341 static $shortCuts = array();
342
343 if (!($shortCuts)) {
344 if (CRM_Core_Permission::check('add contacts')) {
345 if (CRM_Core_Permission::giveMeAllACLs()) {
346 $shortCuts = CRM_Contact_BAO_ContactType::getCreateNewList();
347 }
348 }
349
350 // new activity (select target contact)
351 $shortCuts = array_merge($shortCuts, array(
352 array(
353 'path' => 'civicrm/activity',
354 'query' => 'action=add&reset=1&context=standalone',
355 'ref' => 'new-activity',
356 'title' => ts('Activity'),
357 )));
358
359 $components = CRM_Core_Component::getEnabledComponents();
360
361 if (!empty($config->enableComponents)) {
362 // check if we can process credit card contribs
363 $newCredit = CRM_Core_Payment::allowBackofficeCreditCard();
364
365 foreach ($components as $componentName => $obj) {
366 if (in_array($componentName, $config->enableComponents)) {
367 $obj->creatNewShortcut($shortCuts, $newCredit);
368 }
369 }
370 }
371
372 // new email (select recipients)
373 $shortCuts = array_merge($shortCuts, array(
374 array('path' => 'civicrm/activity/email/add',
375 'query' => 'atype=3&action=add&reset=1&context=standalone',
376 'ref' => 'new-email',
377 'title' => ts('Email'),
378 )));
379
380 if (CRM_Core_Permission::check('edit groups')) {
381 $shortCuts = array_merge($shortCuts, array(
382 array('path' => 'civicrm/group/add',
383 'query' => 'reset=1',
384 'ref' => 'new-group',
385 'title' => ts('Group'),
386 )));
387 }
388
389 if (CRM_Core_Permission::check('administer CiviCRM')) {
390 $shortCuts = array_merge($shortCuts, array(
391 array('path' => 'civicrm/admin/tag',
392 'query' => 'reset=1&action=add',
393 'ref' => 'new-tag',
394 'title' => ts('Tag'),
395 )));
396 }
397
398 if (empty($shortCuts)) {
399 return NULL;
400 }
401 }
402
403 $values = array();
404 foreach ($shortCuts as $short) {
405 $value = array();
406 if (isset($short['url'])) {
407 $value['url'] = $short['url'];
408 }
409 else {
410 $value['url'] = CRM_Utils_System::url($short['path'], $short['query'], FALSE);
411 }
412 $value['title'] = $short['title'];
413 $value['ref'] = $short['ref'];
414 $values[] = $value;
415 }
416
417 // call links hook to add user defined links
418 CRM_Utils_Hook::links('create.new.shorcuts',
419 NULL,
420 CRM_Core_DAO::$_nullObject,
421 $values,
422 CRM_Core_DAO::$_nullObject,
423 CRM_Core_DAO::$_nullObject
424 );
425
426 foreach ($values as $key => $val) {
427 if (!empty($val['title'])) {
428 $values[$key]['name'] = CRM_Utils_Array::value('name', $val, $val['title']);
429 }
430 }
431
432 self::setProperty(self::CREATE_NEW, 'templateValues', array('shortCuts' => $values));
433 }
434
435 /**
436 * create the list of dashboard links
437 *
438 * @return void
439 * @access private
440 */
441 private static function setTemplateDashboardValues() {
442 static $dashboardLinks = array();
443 if (CRM_Core_Permission::check('access Contact Dashboard')) {
444 $dashboardLinks = array(
445 array('path' => 'civicrm/user',
446 'query' => 'reset=1',
447 'title' => ts('My Contact Dashboard'),
448 ));
449 }
450
451 if (empty($dashboardLinks)) {
452 return NULL;
453 }
454
455 $values = array();
456 foreach ($dashboardLinks as $dash) {
457 $value = array();
458 if (isset($dash['url'])) {
459 $value['url'] = $dash['url'];
460 }
461 else {
462 $value['url'] = CRM_Utils_System::url($dash['path'], $dash['query'], FALSE);
463 }
464 $value['title'] = $dash['title'];
465 $value['key'] = CRM_Utils_Array::value('key', $dash);
466 $values[] = $value;
467 }
468 self::setProperty(self::DASHBOARD, 'templateValues', array('dashboardLinks' => $values));
469 }
470
471 /**
472 * create the list of mail urls for the application and format is as a block
473 *
474 * @return void
475 * @access private
476 */
477 private static function setTemplateMailValues() {
478 static $shortCuts = NULL;
479
480 if (!($shortCuts)) {
481 $shortCuts = array(
482 array('path' => 'civicrm/mailing/send',
483 'query' => 'reset=1',
484 'title' => ts('Send Mailing'),
485 ),
486 array(
487 'path' => 'civicrm/mailing/browse',
488 'query' => 'reset=1',
489 'title' => ts('Browse Sent Mailings'),
490 ),
491 );
492 }
493
494 $values = array();
495 foreach ($shortCuts as $short) {
496 $value = array();
497 $value['url'] = CRM_Utils_System::url($short['path'], $short['query']);
498 $value['title'] = $short['title'];
499 $values[] = $value;
500 }
501 self::setProperty(self::MAIL, 'templateValues', array('shortCuts' => $values));
502 }
503
504 /**
505 * create the list of shortcuts for the application and format is as a block
506 *
507 * @return void
508 * @access private
509 */
510 private static function setTemplateMenuValues() {
511 $config = CRM_Core_Config::singleton();
512
513 $path = 'navigation';
514 $values = CRM_Core_Menu::getNavigation();
515 if ($values) {
516 self::setProperty(self::MENU, 'templateValues', array('menu' => $values));
517 }
518 }
519
520 /**
521 * create the event blocks for upcoming events
522 *
523 * @return void
524 * @access private
525 */
526 private static function setTemplateEventValues() {
527 $config = CRM_Core_Config::singleton();
528
529 $info = CRM_Event_BAO_Event::getCompleteInfo(date("Ymd"));
530
531 if ($info) {
532 $session = CRM_Core_Session::singleton();
533 // check if registration link should be displayed
534 foreach ($info as $id => $event) {
535 $info[$id]['onlineRegistration'] = CRM_Event_BAO_Event::validRegistrationRequest($event,
536 $session->get('userID')
537 );
538 }
539
540 self::setProperty(self::EVENT, 'templateValues', array('eventBlock' => $info));
541 }
542 }
543
544 /**
545 * Given an id creates a subject/content array
546 *
547 * @param int $id id of the block
548 *
549 * @return array
550 * @access public
551 */
552 static function getContent($id) {
553 // return if upgrade mode
554 $config = CRM_Core_Config::singleton();
555 if ($config->isUpgradeMode()) {
556 return;
557 }
558
559 if (!self::getProperty($id, 'active')) {
560 return NULL;
561 }
562
563 if ($id == self::EVENT &&
564 CRM_Core_Permission::check('view event info')
565 ) {
566 // is CiviEvent enabled?
567 if (!CRM_Core_Permission::access('CiviEvent', FALSE)) {
568 return NULL;
569 }
570 // do nothing
571 }
572 // require 'access CiviCRM' permissons, except for the language switch block
573 elseif (!CRM_Core_Permission::check('access CiviCRM') && $id!=self::LANGSWITCH) {
574 return NULL;
575 }
576 elseif ($id == self::ADD) {
577 $hasAccess = TRUE;
578 if (!CRM_Core_Permission::check('add contacts') &&
579 !CRM_Core_Permission::check('edit groups')
580 ) {
581 $hasAccess = FALSE;
582 }
583 //validate across edit/view - CRM-5666
584 if ($hasAccess) {
585 $hasAccess = CRM_Core_Permission::giveMeAllACLs();
586 }
587 if (!$hasAccess) {
588 return NULL;
589 }
590 }
591
592 self::setTemplateValues($id);
593
594 // Suppress Recent Items block if it's empty - CRM-5188
595 if ($id == self::RECENTLY_VIEWED) {
596 $recent = self::getProperty($id, 'templateValues');
597 if (CRM_Utils_Array::crmIsEmptyArray($recent)) {
598 return NULL;
599 }
600 }
601
602 // Suppress Language switcher if language is inherited from CMS - CRM-9971
603 $config = CRM_Core_Config::singleton();
604 if ($id == self::LANGSWITCH && property_exists($config, "inheritLocale") && $config->inheritLocale) {
605 return NULL;
606 }
607
608 $block = array();
609 $block['name'] = 'block-civicrm';
610 $block['id'] = $block['name'] . '_' . $id;
611 $block['subject'] = self::fetch($id, 'Subject.tpl',
612 array('subject' => self::getProperty($id, 'subject'))
613 );
614 $block['content'] = self::fetch($id, self::getProperty($id, 'template'),
615 self::getProperty($id, 'templateValues')
616 );
617
618
619 return $block;
620 }
621
622 /**
623 * Given an id and a template, fetch the contents
624 *
625 * @param int $id id of the block
626 * @param string $fileName name of the template file
627 * @param array $properties template variables
628 *
629 * @return array
630 * @access public
631 */
632 static function fetch($id, $fileName, $properties) {
633 $template = CRM_Core_Smarty::singleton();
634
635 if ($properties) {
636 $template->assign($properties);
637 }
638
639 return $template->fetch('CRM/Block/' . $fileName);
640 }
641 }
642