| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | use Civi\Api4\MessageTemplate; |
| 13 | use Civi\Token\TokenProcessor; |
| 14 | |
| 15 | /** |
| 16 | * |
| 17 | * @package CRM |
| 18 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * This class generates form components for Message templates |
| 23 | * used by membership, contributions, event registrations, etc. |
| 24 | */ |
| 25 | class CRM_Admin_Form_MessageTemplates extends CRM_Core_Form { |
| 26 | /** |
| 27 | * which (and whether) mailing workflow this template belongs to |
| 28 | * @var int |
| 29 | */ |
| 30 | protected $_workflow_id = NULL; |
| 31 | |
| 32 | /** |
| 33 | * Is document file is already loaded as default value? |
| 34 | * |
| 35 | * @var bool |
| 36 | */ |
| 37 | protected $_is_document = FALSE; |
| 38 | |
| 39 | /** |
| 40 | * @var bool |
| 41 | */ |
| 42 | public $submitOnce = TRUE; |
| 43 | |
| 44 | /** |
| 45 | * PreProcess form - load existing values. |
| 46 | * |
| 47 | * @throws \API_Exception |
| 48 | * @throws \CRM_Core_Exception |
| 49 | * @throws \Civi\API\Exception\UnauthorizedException |
| 50 | */ |
| 51 | public function preProcess() { |
| 52 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 53 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
| 54 | $this->assign('action', $this->_action); |
| 55 | $this->_values = []; |
| 56 | if ($this->_id) { |
| 57 | $this->_values = (array) MessageTemplate::get()->addWhere('id', '=', $this->_id)->setSelect(['*'])->execute()->first(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Set default values for the form. |
| 63 | * |
| 64 | * The default values are retrieved from the database. |
| 65 | */ |
| 66 | public function setDefaultValues() { |
| 67 | $defaults = $this->_values; |
| 68 | |
| 69 | if (empty($defaults['pdf_format_id'])) { |
| 70 | $defaults['pdf_format_id'] = 'null'; |
| 71 | } |
| 72 | if (empty($defaults['file_type'])) { |
| 73 | $defaults['file_type'] = 0; |
| 74 | } |
| 75 | |
| 76 | if ($this->_action & CRM_Core_Action::ADD) { |
| 77 | $defaults['is_active'] = 1; |
| 78 | } |
| 79 | |
| 80 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 81 | $documentInfo = CRM_Core_BAO_File::getEntityFile('civicrm_msg_template', $this->_id, TRUE); |
| 82 | if (!empty($documentInfo)) { |
| 83 | $defaults['file_type'] = 1; |
| 84 | $this->_is_document = TRUE; |
| 85 | $this->assign('attachment', $documentInfo); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return $defaults; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Build the form object. |
| 94 | */ |
| 95 | public function buildQuickForm() { |
| 96 | // For VIEW we only want Done button |
| 97 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 98 | // currently, the above action is used solely for previewing default workflow templates |
| 99 | $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1'); |
| 100 | $cancelURL = str_replace('&', '&', $cancelURL); |
| 101 | $this->addButtons([ |
| 102 | [ |
| 103 | 'type' => 'cancel', |
| 104 | 'name' => ts('Done'), |
| 105 | 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"], |
| 106 | 'isDefault' => TRUE, |
| 107 | ], |
| 108 | ]); |
| 109 | } |
| 110 | else { |
| 111 | $this->_workflow_id = $this->_values['workflow_id'] ?? NULL; |
| 112 | $this->checkUserPermission($this->_workflow_id); |
| 113 | $this->assign('isWorkflow', (bool) ($this->_values['workflow_id'] ?? NULL)); |
| 114 | |
| 115 | if ($this->_workflow_id) { |
| 116 | $selectedChild = 'workflow'; |
| 117 | } |
| 118 | else { |
| 119 | $selectedChild = 'user'; |
| 120 | } |
| 121 | |
| 122 | $cancelURL = CRM_Utils_System::url('civicrm/admin/messageTemplates', "selectedChild={$selectedChild}&reset=1"); |
| 123 | $cancelURL = str_replace('&', '&', $cancelURL); |
| 124 | $buttons[] = [ |
| 125 | 'type' => 'upload', |
| 126 | 'name' => $this->_action & CRM_Core_Action::DELETE ? ts('Delete') : ts('Save'), |
| 127 | 'isDefault' => TRUE, |
| 128 | ]; |
| 129 | if (!($this->_action & CRM_Core_Action::DELETE)) { |
| 130 | $buttons[] = [ |
| 131 | 'type' => 'upload', |
| 132 | 'name' => ts('Save and Done'), |
| 133 | 'subName' => 'done', |
| 134 | ]; |
| 135 | } |
| 136 | $buttons[] = [ |
| 137 | 'type' => 'cancel', |
| 138 | 'name' => ts('Cancel'), |
| 139 | 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"], |
| 140 | ]; |
| 141 | $this->addButtons($buttons); |
| 142 | } |
| 143 | |
| 144 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 145 | $this->assign('msg_title', $this->_values['msg_title']); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | $breadCrumb = [ |
| 150 | [ |
| 151 | 'title' => ts('Message Templates'), |
| 152 | 'url' => CRM_Utils_System::url('civicrm/admin/messageTemplates', 'action=browse&reset=1'), |
| 153 | ], |
| 154 | ]; |
| 155 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
| 156 | |
| 157 | $this->applyFilter('__ALL__', 'trim'); |
| 158 | $this->add('text', 'msg_title', ts('Message Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_title'), TRUE); |
| 159 | |
| 160 | $options = [ts('Compose On-screen'), ts('Upload Document')]; |
| 161 | $element = $this->addRadio('file_type', ts('Source'), $options); |
| 162 | if ($this->_id) { |
| 163 | $element->freeze(); |
| 164 | } |
| 165 | |
| 166 | $this->addElement('file', "file_id", ts('Upload Document'), 'size=30 maxlength=255'); |
| 167 | $this->addUploadElement("file_id"); |
| 168 | |
| 169 | $this->add('text', 'msg_subject', |
| 170 | ts('Message Subject'), |
| 171 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject') |
| 172 | ); |
| 173 | |
| 174 | $tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contactId']]); |
| 175 | $tokens = $tokenProcessor->listTokens(); |
| 176 | |
| 177 | $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens)); |
| 178 | |
| 179 | // if not a system message use a wysiwyg editor, CRM-5971 |
| 180 | if ($this->_id && |
| 181 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_MessageTemplate', |
| 182 | $this->_id, |
| 183 | 'workflow_id' |
| 184 | ) |
| 185 | ) { |
| 186 | $this->add('textarea', 'msg_html', ts('HTML Message'), |
| 187 | ['cols' => 50, 'rows' => 6] |
| 188 | ); |
| 189 | } |
| 190 | else { |
| 191 | $this->add('wysiwyg', 'msg_html', ts('HTML Message'), |
| 192 | [ |
| 193 | 'cols' => '80', |
| 194 | 'rows' => '8', |
| 195 | 'onkeyup' => "return verify(this)", |
| 196 | 'preset' => 'civimail', |
| 197 | ] |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | $this->add('textarea', 'msg_text', ts('Text Message'), |
| 202 | ['cols' => 50, 'rows' => 6] |
| 203 | ); |
| 204 | |
| 205 | $this->add('select', 'pdf_format_id', ts('PDF Page Format'), |
| 206 | [ |
| 207 | 'null' => ts('- default -'), |
| 208 | ] + CRM_Core_BAO_PdfFormat::getList(TRUE), FALSE |
| 209 | ); |
| 210 | |
| 211 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
| 212 | $this->addFormRule([__CLASS__, 'formRule'], $this); |
| 213 | |
| 214 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 215 | $this->freeze(); |
| 216 | $this->setTitle(ts('View System Default Message Template')); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Restrict users access based on permission |
| 222 | * |
| 223 | * @param int $workflowId |
| 224 | */ |
| 225 | private function checkUserPermission($workflowId) { |
| 226 | if (isset($workflowId)) { |
| 227 | $canView = CRM_Core_Permission::check('edit system workflow message templates'); |
| 228 | } |
| 229 | else { |
| 230 | $canView = CRM_Core_Permission::check('edit user-driven message templates'); |
| 231 | } |
| 232 | |
| 233 | if (!$canView && !CRM_Core_Permission::check('edit message templates')) { |
| 234 | CRM_Core_Session::setStatus(ts('You do not have permission to view requested page.'), ts('Access Denied')); |
| 235 | $url = CRM_Utils_System::url('civicrm/admin/messageTemplates', "reset=1"); |
| 236 | CRM_Utils_System::redirect($url); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Global form rule. |
| 242 | * |
| 243 | * @param array $params |
| 244 | * The input form values. |
| 245 | * @param array $files |
| 246 | * The uploaded files if any. |
| 247 | * @param self $self |
| 248 | * |
| 249 | * @return array |
| 250 | * array of errors |
| 251 | */ |
| 252 | public static function formRule($params, $files, $self) { |
| 253 | // If user uploads non-document file other than odt/docx |
| 254 | if (!empty($files['file_id']['tmp_name']) && |
| 255 | array_search($files['file_id']['type'], CRM_Core_SelectValues::documentApplicationType()) == NULL |
| 256 | ) { |
| 257 | $errors['file_id'] = ts('Invalid document file format'); |
| 258 | } |
| 259 | // If default is not set and no document file is uploaded |
| 260 | elseif (empty($files['file_id']['tmp_name']) && !empty($params['file_type']) && !$self->_is_document) { |
| 261 | //On edit page of docx/odt message template if user changes file type but forgot to upload document |
| 262 | $errors['file_id'] = ts('Please upload document'); |
| 263 | } |
| 264 | |
| 265 | return empty($errors) ? TRUE : $errors; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Process the form submission. |
| 270 | * |
| 271 | * @throws \API_Exception |
| 272 | * @throws \CRM_Core_Exception |
| 273 | * @throws \Civi\API\Exception\UnauthorizedException |
| 274 | */ |
| 275 | public function postProcess() { |
| 276 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 277 | CRM_Core_BAO_MessageTemplate::del($this->_id); |
| 278 | |
| 279 | $this->postProcessHook(); |
| 280 | } |
| 281 | elseif ($this->_action & CRM_Core_Action::VIEW) { |
| 282 | // currently, the above action is used solely for previewing default workflow templates |
| 283 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); |
| 284 | } |
| 285 | else { |
| 286 | // store the submitted values in an array |
| 287 | $params = $this->controller->exportValues(); |
| 288 | |
| 289 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 290 | $params['id'] = $this->_id; |
| 291 | } |
| 292 | |
| 293 | if (!empty($params['file_type'])) { |
| 294 | unset($params['msg_html']); |
| 295 | unset($params['msg_text']); |
| 296 | CRM_Utils_File::formatFile($params, 'file_id'); |
| 297 | } |
| 298 | // delete related file references if html/text/pdf template are chosen over document |
| 299 | elseif (!empty($this->_id)) { |
| 300 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); |
| 301 | $entityFileDAO->entity_id = $this->_id; |
| 302 | $entityFileDAO->entity_table = 'civicrm_msg_template'; |
| 303 | if ($entityFileDAO->find(TRUE)) { |
| 304 | $fileDAO = new CRM_Core_DAO_File(); |
| 305 | $fileDAO->id = $entityFileDAO->file_id; |
| 306 | $fileDAO->find(TRUE); |
| 307 | $entityFileDAO->delete(); |
| 308 | $fileDAO->delete(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | $this->_workflow_id = $this->_values['workflow_id'] ?? NULL; |
| 313 | if ($this->_workflow_id) { |
| 314 | $params['workflow_id'] = $this->_workflow_id; |
| 315 | $params['is_active'] = TRUE; |
| 316 | } |
| 317 | |
| 318 | $messageTemplate = MessageTemplate::save()->setDefaults($params)->setRecords([['id' => $this->_id]])->execute()->first(); |
| 319 | |
| 320 | // set the id on save, so it can be used in a extension using the posProcess hook |
| 321 | $this->_id = $messageTemplate['id']; |
| 322 | $this->postProcessHook(); |
| 323 | |
| 324 | CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', [1 => $messageTemplate['msg_title']]), ts('Saved'), 'success'); |
| 325 | |
| 326 | if (isset($this->_submitValues['_qf_MessageTemplates_upload'])) { |
| 327 | // Save button was pressed |
| 328 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates/add', "action=update&id={$messageTemplate['id']}&reset=1")); |
| 329 | } |
| 330 | // Save and done button was pressed |
| 331 | if ($this->_workflow_id) { |
| 332 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); |
| 333 | } |
| 334 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1')); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | } |