3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Redefine the display action.
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 class CRM_Core_QuickForm_Action_Display
extends CRM_Core_QuickForm_Action
{
21 * The template to display the required "red" asterick.
24 public static $_requiredTemplate = NULL;
27 * The template to display error messages inline with the form element.
30 public static $_errorTemplate = NULL;
35 * @param object $stateMachine
36 * Reference to state machine object.
38 * @return \CRM_Core_QuickForm_Action_Display
40 public function __construct(&$stateMachine) {
41 parent
::__construct($stateMachine);
45 * Processes the request.
47 * @param CRM_Core_Form $page
48 * CRM_Core_Form the current form-page.
49 * @param string $actionName
50 * Current action name, as one Action object can serve multiple actions.
54 public function perform(&$page, $actionName) {
55 $pageName = $page->getAttribute('id');
57 // If the original action was 'display' and we have values in container then we load them
58 // BTW, if the page was invalid, we should later call validate() to get the errors
59 list(, $oldName) = $page->controller
->getActionName();
60 if ('display' == $oldName) {
61 // If the controller is "modal" we should not allow direct access to a page
62 // unless all previous pages are valid (see also bug #2323)
63 if ($page->controller
->isModal() && !$page->controller
->isValid($page->getAttribute('id'))) {
64 $target = &$page->controller
->getPage($page->controller
->findInvalid());
65 return $target->handle('jump');
67 $data = &$page->controller
->container();
68 if (!empty($data['values'][$pageName])) {
69 $page->loadValues($data['values'][$pageName]);
70 $validate = FALSE === $data['valid'][$pageName];
74 // set "common" defaults and constants
75 $page->controller
->applyDefaults($pageName);
76 $page->isFormBuilt() or $page->buildForm();
77 // if we had errors we should show them again
78 if (isset($validate) && $validate) {
81 //will this work generally as TRUE (i.e., return output)
82 //was default, i.e., FALSE
83 return $this->renderForm($page);
87 * Render the page using a custom templating system.
89 * @param CRM_Core_Form $page
90 * The CRM_Core_Form page.
92 public function renderForm(&$page) {
93 $this->_setRenderTemplates($page);
94 $template = CRM_Core_Smarty
::singleton();
95 $form = $page->toSmarty();
97 // Deprecated - use snippet=6 instead of json=1
98 $json = CRM_Utils_Request
::retrieve('json', 'Boolean');
100 CRM_Utils_JSON
::output($form);
103 $template->assign('form', $form);
104 $template->assign('isForm', 1);
106 $controller = &$page->controller
;
107 // Stop here if we are in embedded mode. Exception: displaying form errors via ajax
108 if ($controller->getEmbedded() && !(!empty($form['errors']) && $controller->_QFResponseType
== 'json')) {
112 $template->assign('action', $page->getAction());
114 $pageTemplateFile = $page->getHookedTemplateFileName();
115 $template->assign('tplFile', $pageTemplateFile);
117 $content = $template->fetch($controller->getTemplateFile());
119 if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region
::instance('html-header', FALSE)) {
120 CRM_Utils_System
::addHTMLHead($region->render(''));
122 CRM_Utils_System
::appendTPLFile($pageTemplateFile,
124 $page->overrideExtraTemplateFileName()
127 //its time to call the hook.
128 CRM_Utils_Hook
::alterContent($content, 'form', $pageTemplateFile, $page);
130 $print = $controller->getPrint();
135 $html = CRM_Utils_System
::theme($content, $print);
138 if ($controller->_QFResponseType
== 'json') {
139 $response = ['content' => $html];
140 if (!empty($page->ajaxResponse
)) {
141 $response +
= $page->ajaxResponse
;
143 if (!empty($form['errors'])) {
144 $response['status'] = 'form_error';
145 $response['errors'] = $form['errors'];
147 CRM_Core_Page_AJAX
::returnJsonResponse($response);
151 if ($print == CRM_Core_Smarty
::PRINT_PDF
) {
152 CRM_Utils_PDF_Utils
::html2pdf(
154 "{$page->_name}.pdf",
156 ['paper_size' => 'a3', 'orientation' => 'landscape']
162 CRM_Utils_System
::civiExit();
169 * Set the various rendering templates.
171 * @param CRM_Core_Form $page
172 * The CRM_Core_Form page.
174 public function _setRenderTemplates(&$page) {
175 if (self
::$_requiredTemplate === NULL) {
176 $this->initializeTemplates();
179 $renderer = &$page->getRenderer();
181 $renderer->setRequiredTemplate(self
::$_requiredTemplate);
182 $renderer->setErrorTemplate(self
::$_errorTemplate);
186 * Initialize the various templates.
188 public function initializeTemplates() {
189 if (self
::$_requiredTemplate !== NULL) {
193 $config = CRM_Core_Config
::singleton();
195 $templateDir = $config->templateDir
;
196 if (is_array($templateDir)) {
197 $templateDir = array_pop($templateDir);
200 self
::$_requiredTemplate = file_get_contents($templateDir . '/CRM/Form/label.tpl');
201 self
::$_errorTemplate = file_get_contents($templateDir . '/CRM/Form/error.tpl');