| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | * Redefine the display action. |
| 30 | * |
| 31 | * @package CRM |
| 32 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 33 | */ |
| 34 | class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action { |
| 35 | |
| 36 | /** |
| 37 | * The template to display the required "red" asterick. |
| 38 | * @var string |
| 39 | */ |
| 40 | static $_requiredTemplate = NULL; |
| 41 | |
| 42 | /** |
| 43 | * The template to display error messages inline with the form element. |
| 44 | * @var string |
| 45 | */ |
| 46 | static $_errorTemplate = NULL; |
| 47 | |
| 48 | /** |
| 49 | * Class constructor. |
| 50 | * |
| 51 | * @param object $stateMachine |
| 52 | * Reference to state machine object. |
| 53 | * |
| 54 | * @return \CRM_Core_QuickForm_Action_Display |
| 55 | */ |
| 56 | public function __construct(&$stateMachine) { |
| 57 | parent::__construct($stateMachine); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Processes the request. |
| 62 | * |
| 63 | * @param CRM_Core_Form $page |
| 64 | * CRM_Core_Form the current form-page. |
| 65 | * @param string $actionName |
| 66 | * Current action name, as one Action object can serve multiple actions. |
| 67 | * |
| 68 | * @return object|void |
| 69 | */ |
| 70 | public function perform(&$page, $actionName) { |
| 71 | $pageName = $page->getAttribute('id'); |
| 72 | |
| 73 | // If the original action was 'display' and we have values in container then we load them |
| 74 | // BTW, if the page was invalid, we should later call validate() to get the errors |
| 75 | list(, $oldName) = $page->controller->getActionName(); |
| 76 | if ('display' == $oldName) { |
| 77 | // If the controller is "modal" we should not allow direct access to a page |
| 78 | // unless all previous pages are valid (see also bug #2323) |
| 79 | if ($page->controller->isModal() && !$page->controller->isValid($page->getAttribute('id'))) { |
| 80 | $target = &$page->controller->getPage($page->controller->findInvalid()); |
| 81 | return $target->handle('jump'); |
| 82 | } |
| 83 | $data = &$page->controller->container(); |
| 84 | if (!empty($data['values'][$pageName])) { |
| 85 | $page->loadValues($data['values'][$pageName]); |
| 86 | $validate = FALSE === $data['valid'][$pageName]; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // set "common" defaults and constants |
| 91 | $page->controller->applyDefaults($pageName); |
| 92 | $page->isFormBuilt() or $page->buildForm(); |
| 93 | // if we had errors we should show them again |
| 94 | if (isset($validate) && $validate) { |
| 95 | $page->validate(); |
| 96 | } |
| 97 | //will this work generally as TRUE (i.e., return output) |
| 98 | //was default, i.e., FALSE |
| 99 | return $this->renderForm($page); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Render the page using a custom templating system. |
| 104 | * |
| 105 | * @param CRM_Core_Form $page |
| 106 | * The CRM_Core_Form page. |
| 107 | */ |
| 108 | public function renderForm(&$page) { |
| 109 | $this->_setRenderTemplates($page); |
| 110 | $template = CRM_Core_Smarty::singleton(); |
| 111 | $form = $page->toSmarty(); |
| 112 | |
| 113 | // Deprecated - use snippet=6 instead of json=1 |
| 114 | $json = CRM_Utils_Request::retrieve('json', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 115 | if ($json) { |
| 116 | CRM_Utils_JSON::output($form); |
| 117 | } |
| 118 | |
| 119 | $template->assign('form', $form); |
| 120 | $template->assign('isForm', 1); |
| 121 | |
| 122 | $controller = &$page->controller; |
| 123 | // Stop here if we are in embedded mode. Exception: displaying form errors via ajax |
| 124 | if ($controller->getEmbedded() && !(!empty($form['errors']) && $controller->_QFResponseType == 'json')) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $template->assign('action', $page->getAction()); |
| 129 | |
| 130 | $pageTemplateFile = $page->getHookedTemplateFileName(); |
| 131 | $template->assign('tplFile', $pageTemplateFile); |
| 132 | |
| 133 | $content = $template->fetch($controller->getTemplateFile()); |
| 134 | |
| 135 | if (!defined('CIVICRM_UF_HEAD') && $region = CRM_Core_Region::instance('html-header', FALSE)) { |
| 136 | CRM_Utils_System::addHTMLHead($region->render('')); |
| 137 | } |
| 138 | CRM_Utils_System::appendTPLFile($pageTemplateFile, |
| 139 | $content, |
| 140 | $page->overrideExtraTemplateFileName() |
| 141 | ); |
| 142 | |
| 143 | //its time to call the hook. |
| 144 | CRM_Utils_Hook::alterContent($content, 'form', $pageTemplateFile, $page); |
| 145 | |
| 146 | $print = $controller->getPrint(); |
| 147 | if ($print) { |
| 148 | $html = &$content; |
| 149 | } |
| 150 | else { |
| 151 | $html = CRM_Utils_System::theme($content, $print); |
| 152 | } |
| 153 | |
| 154 | if ($controller->_QFResponseType == 'json') { |
| 155 | $response = array('content' => $html); |
| 156 | if (!empty($page->ajaxResponse)) { |
| 157 | $response += $page->ajaxResponse; |
| 158 | } |
| 159 | if (!empty($form['errors'])) { |
| 160 | $response['status'] = 'form_error'; |
| 161 | $response['errors'] = $form['errors']; |
| 162 | } |
| 163 | CRM_Core_Page_AJAX::returnJsonResponse($response); |
| 164 | } |
| 165 | |
| 166 | if ($print) { |
| 167 | if ($print == CRM_Core_Smarty::PRINT_PDF) { |
| 168 | CRM_Utils_PDF_Utils::html2pdf( |
| 169 | $content, |
| 170 | "{$page->_name}.pdf", |
| 171 | FALSE, |
| 172 | array('paper_size' => 'a3', 'orientation' => 'landscape') |
| 173 | ); |
| 174 | } |
| 175 | else { |
| 176 | echo $html; |
| 177 | } |
| 178 | CRM_Utils_System::civiExit(); |
| 179 | } |
| 180 | |
| 181 | print $html; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Set the various rendering templates. |
| 186 | * |
| 187 | * @param CRM_Core_Form $page |
| 188 | * The CRM_Core_Form page. |
| 189 | */ |
| 190 | public function _setRenderTemplates(&$page) { |
| 191 | if (self::$_requiredTemplate === NULL) { |
| 192 | $this->initializeTemplates(); |
| 193 | } |
| 194 | |
| 195 | $renderer = &$page->getRenderer(); |
| 196 | |
| 197 | $renderer->setRequiredTemplate(self::$_requiredTemplate); |
| 198 | $renderer->setErrorTemplate(self::$_errorTemplate); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Initialize the various templates. |
| 203 | */ |
| 204 | public function initializeTemplates() { |
| 205 | if (self::$_requiredTemplate !== NULL) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | $config = CRM_Core_Config::singleton(); |
| 210 | |
| 211 | $templateDir = $config->templateDir; |
| 212 | if (is_array($templateDir)) { |
| 213 | $templateDir = array_pop($templateDir); |
| 214 | } |
| 215 | |
| 216 | self::$_requiredTemplate = file_get_contents($templateDir . '/CRM/Form/label.tpl'); |
| 217 | self::$_errorTemplate = file_get_contents($templateDir . '/CRM/Form/error.tpl'); |
| 218 | } |
| 219 | |
| 220 | } |