PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Core/QuickForm/Action/Display.php

https://github.com/michaelmcandrew/lbc
PHP | 213 lines | 88 code | 32 blank | 93 comment | 18 complexity | b5a25e5aec30da3d383eb0b0fd5ca885 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. * Redefine the display action.
  29. *
  30. * @package CRM
  31. * @copyright CiviCRM LLC (c) 2004-2011
  32. * $Id$
  33. *
  34. */
  35. require_once 'CRM/Core/QuickForm/Action.php';
  36. require_once 'CRM/Core/Config.php';
  37. class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action {
  38. /**
  39. * the template to display the required "red" asterick
  40. * @var string
  41. */
  42. static $_requiredTemplate = null;
  43. /**
  44. * the template to display error messages inline with the form element
  45. * @var string
  46. */
  47. static $_errorTemplate = null;
  48. /**
  49. * class constructor
  50. *
  51. * @param object $stateMachine reference to state machine object
  52. *
  53. * @return object
  54. * @access public
  55. */
  56. function __construct( &$stateMachine ) {
  57. parent::__construct( $stateMachine );
  58. }
  59. /**
  60. * Processes the request.
  61. *
  62. * @param object $page CRM_Core_Form the current form-page
  63. * @param string $actionName Current action name, as one Action object can serve multiple actions
  64. *
  65. * @return void
  66. * @access public
  67. */
  68. function perform(&$page, $actionName) {
  69. $pageName = $page->getAttribute('id');
  70. // If the original action was 'display' and we have values in container then we load them
  71. // BTW, if the page was invalid, we should later call validate() to get the errors
  72. list(, $oldName) = $page->controller->getActionName();
  73. if ('display' == $oldName) {
  74. // If the controller is "modal" we should not allow direct access to a page
  75. // unless all previous pages are valid (see also bug #2323)
  76. if ($page->controller->isModal() && !$page->controller->isValid($page->getAttribute('id'))) {
  77. $target =& $page->controller->getPage($page->controller->findInvalid());
  78. return $target->handle('jump');
  79. }
  80. $data =& $page->controller->container();
  81. if (!empty($data['values'][$pageName])) {
  82. $page->loadValues($data['values'][$pageName]);
  83. $validate = false === $data['valid'][$pageName];
  84. }
  85. }
  86. // set "common" defaults and constants
  87. $page->controller->applyDefaults($pageName);
  88. $page->isFormBuilt() or $page->buildForm();
  89. // if we had errors we should show them again
  90. if (isset($validate) && $validate) {
  91. $page->validate();
  92. }
  93. //will this work generally as TRUE (i.e., return output)
  94. //was default, i.e., FALSE
  95. return $this->renderForm($page);
  96. }
  97. /**
  98. * render the page using a custom templating
  99. * system
  100. *
  101. * @param object $page the CRM_Core_Form page
  102. * @param boolean $ret should we echo or return output
  103. *
  104. * @return void
  105. * @access public
  106. */
  107. function renderForm(&$page, $ret = false) {
  108. $this->_setRenderTemplates($page);
  109. $template = CRM_Core_Smarty::singleton( );
  110. $template->assign( 'form' , $page->toSmarty());
  111. $template->assign( 'isForm' , 1 );
  112. $controller =& $page->controller;
  113. if ( $controller->getEmbedded( ) ) {
  114. return;
  115. }
  116. $template->assign( 'action' , $page->getAction( ) );
  117. $pageTemplateFile = $page->getTemplateFileName( );
  118. $template->assign( 'tplFile', $pageTemplateFile );
  119. $content = $template->fetch( $controller->getTemplateFile( ) );
  120. CRM_Utils_System::appendTPLFile( $pageTemplateFile, $content );
  121. //its time to call the hook.
  122. require_once 'CRM/Utils/Hook.php';
  123. CRM_Utils_Hook::alterContent( $content, 'form', $pageTemplateFile, $page );
  124. $print = $controller->getPrint( );
  125. if ( $print ) {
  126. $html =& $content;
  127. } else {
  128. $html = CRM_Utils_System::theme( 'page', $content, true, $print, $ret );
  129. }
  130. if ( $ret ) {
  131. return $html;
  132. }
  133. if ( $print ) {
  134. if ( $print == CRM_Core_Smarty::PRINT_PDF ) {
  135. require_once 'CRM/Utils/PDF/Utils.php';
  136. CRM_Utils_PDF_Utils::html2pdf( $content, "{$page->_name}.pdf", false,
  137. array( 'paper_size' => 'a3', 'orientation' => 'landscape' ) );
  138. } else {
  139. echo $html;
  140. }
  141. CRM_Utils_System::civiExit( );
  142. }
  143. print $html;
  144. }
  145. /**
  146. * set the various rendering templates
  147. *
  148. * @param object $page the CRM_Core_Form page
  149. *
  150. * @return void
  151. * @access public
  152. */
  153. function _setRenderTemplates(&$page) {
  154. if ( self::$_requiredTemplate === null ) {
  155. $this->initializeTemplates();
  156. }
  157. $renderer =& $page->getRenderer();
  158. $renderer->setRequiredTemplate( self::$_requiredTemplate );
  159. $renderer->setErrorTemplate ( self::$_errorTemplate );
  160. }
  161. /**
  162. * initialize the various templates
  163. *
  164. * @param object $page the CRM_Core_Form page
  165. *
  166. * @return void
  167. * @access public
  168. */
  169. function initializeTemplates() {
  170. if ( self::$_requiredTemplate !== null ) {
  171. return;
  172. }
  173. $config = CRM_Core_Config::singleton();
  174. $templateDir = $config->templateDir;
  175. if ( is_array( $templateDir ) ) {
  176. $templateDir = array_pop( $templateDir );
  177. }
  178. self::$_requiredTemplate = file_get_contents( $templateDir . '/CRM/Form/label.tpl' );
  179. self::$_errorTemplate = file_get_contents( $templateDir . '/CRM/Form/error.tpl' );
  180. }
  181. }