Merge pull request #14141 from seamuslee001/view_url_double_protocol_test
[civicrm-core.git] / CRM / Mailing / Form / Component.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
25606795 35 * This class generates form components for Location Type.
6a488035
TO
36 */
37class CRM_Mailing_Form_Component extends CRM_Core_Form {
38
39 /**
40 * The id of the object being edited / created
41 *
42 * @var int
43 */
44 protected $_id;
45
46 /**
fe482240 47 * The name of the BAO object for this form.
6a488035
TO
48 *
49 * @var string
50 */
51 protected $_BAOName;
52
00be9182 53 public function preProcess() {
6a488035
TO
54 $this->_id = $this->get('id');
55 $this->_BAOName = $this->get('BAOName');
56 }
57
58 /**
fe482240 59 * Build the form object.
6a488035
TO
60 */
61 public function buildQuickForm() {
be2fb01f 62 $this->applyFilter(['name', 'subject', 'body_html'], 'trim');
6a488035
TO
63
64 $this->add('text', 'name', ts('Name'),
4825de4a 65 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'name'), TRUE
6a488035 66 );
be2fb01f 67 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
7e8c8317
SL
68 'CRM_Mailing_BAO_MailingComponent',
69 $this->_id,
70 ]);
6a488035
TO
71
72 $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues::mailingComponents());
73
74 $this->add('text', 'subject', ts('Subject'),
4825de4a 75 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'subject'),
6a488035
TO
76 TRUE
77 );
78 $this->add('textarea', 'body_text', ts('Body - TEXT Format'),
4825de4a 79 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_text')
6a488035
TO
80 );
81 $this->add('textarea', 'body_html', ts('Body - HTML Format'),
4825de4a 82 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_html')
6a488035
TO
83 );
84
899a6337
TO
85 $this->addYesNo('is_default', ts('Default?'));
86 $this->addYesNo('is_active', ts('Enabled?'));
6a488035 87
be2fb01f
CW
88 $this->addFormRule(['CRM_Mailing_Form_Component', 'formRule']);
89 $this->addFormRule(['CRM_Mailing_Form_Component', 'dataRule']);
6a488035 90
be2fb01f 91 $this->addButtons([
7e8c8317
SL
92 [
93 'type' => 'next',
94 'name' => ts('Save'),
95 'isDefault' => TRUE,
96 ],
97 [
98 'type' => 'cancel',
99 'name' => ts('Cancel'),
100 ],
101 ]);
6a488035
TO
102 }
103
104 /**
c490a46a 105 * Set default values for the form.
6a488035 106 */
00be9182 107 public function setDefaultValues() {
be2fb01f
CW
108 $defaults = [];
109 $params = [];
6a488035
TO
110
111 if (isset($this->_id)) {
be2fb01f 112 $params = ['id' => $this->_id];
0e6e8724
DL
113 $baoName = $this->_BAOName;
114 $baoName::retrieve($params, $defaults);
6a488035 115 }
899a6337
TO
116 else {
117 $defaults['is_active'] = 1;
118 }
6a488035
TO
119
120 return $defaults;
121 }
122
123 /**
fe482240 124 * Process the form submission.
6a488035
TO
125 */
126 public function postProcess() {
127 // store the submitted values in an array
128 $params = $this->controller->exportValues($this->_name);
129
6a488035 130 if ($this->_action & CRM_Core_Action::UPDATE) {
2d75534c 131 $params['id'] = $this->_id;
6a488035
TO
132 }
133
4825de4a 134 $component = CRM_Mailing_BAO_MailingComponent::add($params);
be2fb01f 135 CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', [
7e8c8317
SL
136 1 => $component->name,
137 ]), ts('Saved'), 'success');
2d75534c 138
6a488035 139 }
6a488035
TO
140
141 /**
fe482240 142 * Validation.
6a488035 143 *
90c8230e
TO
144 * @param array $params
145 * (ref.) an assoc array of name/value pairs.
6a488035 146 *
d9f93da9
EM
147 * @param $files
148 * @param $options
149 *
72b3a70c
CW
150 * @return bool|array
151 * mixed true or array of errors
6a488035 152 */
00be9182 153 public static function dataRule($params, $files, $options) {
6a488035 154 if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') {
be2fb01f 155 $InvalidTokens = [];
6a488035
TO
156 }
157 else {
be2fb01f 158 $InvalidTokens = ['action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")];
6a488035 159 }
be2fb01f
CW
160 $errors = [];
161 foreach ([
7e8c8317
SL
162 'text',
163 'html',
164 ] as $type) {
be2fb01f 165 $dataErrors = [];
6a488035
TO
166 foreach ($InvalidTokens as $token => $desc) {
167 if ($params['body_' . $type]) {
168 if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) {
be2fb01f 169 $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', [
7e8c8317
SL
170 1 => $token,
171 2 => $desc,
172 ]) . '</li>';
6a488035
TO
173 }
174 }
175 }
176 if (!empty($dataErrors)) {
be2fb01f 177 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', [
7e8c8317
SL
178 1 => $type,
179 ]) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>';
6a488035
TO
180 }
181 }
d2016abd
AU
182 return empty($errors) ? TRUE : $errors;
183 }
6a488035 184
d2016abd
AU
185 /**
186 * Validates that either body text or body html is required.
187 * @param array $params
188 * (ref.) an assoc array of name/value pairs.
189 *
190 * @param $files
191 * @param $options
192 *
193 * @return bool|array
194 * mixed true or array of errors
195 */
196 public static function formRule($params, $files, $options) {
be2fb01f 197 $errors = [];
d2016abd
AU
198 if (empty($params['body_text']) && empty($params['body_html'])) {
199 $errors['body_text'] = ts("Please provide either HTML or TEXT format for the Body.");
200 $errors['body_html'] = ts("Please provide either HTML or TEXT format for the Body.");
201 }
6a488035
TO
202 return empty($errors) ? TRUE : $errors;
203 }
96025800 204
6a488035 205}