CRM/Report add missing comment blocks
[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 * @params int $id one of the class constants (ADD, SEARCH, etc.)
186 * @params string $property the desired property
187 *
188 * @param $id
189 * @param $property
190 *
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 *
207 * @param $id
208 * @param $property
209 * @param $value
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 *
289 * @param $id
290 *
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
371 $newCredit = CRM_Core_Config::isEnabledBackOfficeCreditCardPayments();
372
373 foreach ($components as $componentName => $obj) {
374 if (in_array($componentName, $config->enableComponents)) {
375 $obj->creatNewShortcut($shortCuts, $newCredit);
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();
412 foreach ($shortCuts as $key => $short) {
413 $values[$key] = self::setShortCutValues($short);
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,
421 CRM_Core_DAO::$_nullObject,
422 CRM_Core_DAO::$_nullObject
423 );
424
425 foreach ($values as $key => $val) {
426 if (!empty($val['title'])) {
427 $values[$key]['name'] = CRM_Utils_Array::value('name', $val, $val['title']);
428 }
429 }
430
431 self::setProperty(self::CREATE_NEW, 'templateValues', array('shortCuts' => $values));
432 }
433
434 private static function setShortcutValues($short) {
435 $value = array();
436 if (isset($short['url'])) {
437 $value['url'] = $short['url'];
438 }
439 elseif (isset($short['path'])) {
440 $value['url'] = CRM_Utils_System::url($short['path'], $short['query'], FALSE);
441 }
442 $value['title'] = $short['title'];
443 $value['ref'] = $short['ref'];
444 if (!empty($short['shortCuts'])) {
445 foreach ($short['shortCuts'] as $shortCut) {
446 $value['shortCuts'][] = self::setShortcutValues($shortCut);
447 }
448 }
449 return $value;
450 }
451
452 /**
453 * create the list of dashboard links
454 *
455 * @return void
456 * @access private
457 */
458 private static function setTemplateDashboardValues() {
459 static $dashboardLinks = array();
460 if (CRM_Core_Permission::check('access Contact Dashboard')) {
461 $dashboardLinks = array(
462 array('path' => 'civicrm/user',
463 'query' => 'reset=1',
464 'title' => ts('My Contact Dashboard'),
465 ));
466 }
467
468 if (empty($dashboardLinks)) {
469 return NULL;
470 }
471
472 $values = array();
473 foreach ($dashboardLinks as $dash) {
474 $value = array();
475 if (isset($dash['url'])) {
476 $value['url'] = $dash['url'];
477 }
478 else {
479 $value['url'] = CRM_Utils_System::url($dash['path'], $dash['query'], FALSE);
480 }
481 $value['title'] = $dash['title'];
482 $value['key'] = CRM_Utils_Array::value('key', $dash);
483 $values[] = $value;
484 }
485 self::setProperty(self::DASHBOARD, 'templateValues', array('dashboardLinks' => $values));
486 }
487
488 /**
489 * create the list of mail urls for the application and format is as a block
490 *
491 * @return void
492 * @access private
493 */
494 private static function setTemplateMailValues() {
495 static $shortCuts = NULL;
496
497 if (!($shortCuts)) {
498 $shortCuts = array(
499 array('path' => 'civicrm/mailing/send',
500 'query' => 'reset=1',
501 'title' => ts('Send Mailing'),
502 ),
503 array(
504 'path' => 'civicrm/mailing/browse',
505 'query' => 'reset=1',
506 'title' => ts('Browse Sent Mailings'),
507 ),
508 );
509 }
510
511 $values = array();
512 foreach ($shortCuts as $short) {
513 $value = array();
514 $value['url'] = CRM_Utils_System::url($short['path'], $short['query']);
515 $value['title'] = $short['title'];
516 $values[] = $value;
517 }
518 self::setProperty(self::MAIL, 'templateValues', array('shortCuts' => $values));
519 }
520
521 /**
522 * create the list of shortcuts for the application and format is as a block
523 *
524 * @return void
525 * @access private
526 */
527 private static function setTemplateMenuValues() {
528 $config = CRM_Core_Config::singleton();
529
530 $path = 'navigation';
531 $values = CRM_Core_Menu::getNavigation();
532 if ($values) {
533 self::setProperty(self::MENU, 'templateValues', array('menu' => $values));
534 }
535 }
536
537 /**
538 * create the event blocks for upcoming events
539 *
540 * @return void
541 * @access private
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) {
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)
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 *
566 * @param int $id id of the block
567 *
568 * @return array
569 * @access public
570 */
571 static function getContent($id) {
572 // return if upgrade mode
573 $config = CRM_Core_Config::singleton();
574 if ($config->isUpgradeMode()) {
575 return;
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 }
591 // require 'access CiviCRM' permissons, except for the language switch block
592 elseif (!CRM_Core_Permission::check('access CiviCRM') && $id!=self::LANGSWITCH) {
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();
623 if ($id == self::LANGSWITCH && property_exists($config, "inheritLocale") && $config->inheritLocale) {
624 return NULL;
625 }
626
627 $block = array();
628 $block['name'] = 'block-civicrm';
629 $block['id'] = $block['name'] . '_' . $id;
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
637
638 return $block;
639 }
640
641 /**
642 * Given an id and a template, fetch the contents
643 *
644 * @param int $id id of the block
645 * @param string $fileName name of the template file
646 * @param array $properties template variables
647 *
648 * @return array
649 * @access public
650 */
651 static function fetch($id, $fileName, $properties) {
652 $template = CRM_Core_Smarty::singleton();
653
654 if ($properties) {
655 $template->assign($properties);
656 }
657
658 return $template->fetch('CRM/Block/' . $fileName);
659 }
660 }
661