Merge pull request #15745 from seamuslee001/master
[civicrm-core.git] / CRM / Mailing / Form / Component.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for Location Type.
36 */
37 class 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 /**
47 * The name of the BAO object for this form.
48 *
49 * @var string
50 */
51 protected $_BAOName;
52
53 public function preProcess() {
54 $this->_id = $this->get('id');
55 $this->_BAOName = $this->get('BAOName');
56 }
57
58 /**
59 * Build the form object.
60 */
61 public function buildQuickForm() {
62 $this->applyFilter(['name', 'subject', 'body_html'], 'trim');
63
64 $this->add('text', 'name', ts('Name'),
65 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'name'), TRUE
66 );
67 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
68 'CRM_Mailing_BAO_MailingComponent',
69 $this->_id,
70 ]);
71
72 $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues::mailingComponents());
73
74 $this->add('text', 'subject', ts('Subject'),
75 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'subject'),
76 TRUE
77 );
78 $this->add('textarea', 'body_text', ts('Body - TEXT Format'),
79 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_text')
80 );
81 $this->add('textarea', 'body_html', ts('Body - HTML Format'),
82 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_html')
83 );
84
85 $this->addYesNo('is_default', ts('Default?'));
86 $this->addYesNo('is_active', ts('Enabled?'));
87
88 $this->addFormRule(['CRM_Mailing_Form_Component', 'formRule']);
89 $this->addFormRule(['CRM_Mailing_Form_Component', 'dataRule']);
90
91 $this->addButtons([
92 [
93 'type' => 'next',
94 'name' => ts('Save'),
95 'isDefault' => TRUE,
96 ],
97 [
98 'type' => 'cancel',
99 'name' => ts('Cancel'),
100 ],
101 ]);
102 }
103
104 /**
105 * Set default values for the form.
106 */
107 public function setDefaultValues() {
108 $defaults = [];
109 $params = [];
110
111 if (isset($this->_id)) {
112 $params = ['id' => $this->_id];
113 $baoName = $this->_BAOName;
114 $baoName::retrieve($params, $defaults);
115 }
116 else {
117 $defaults['is_active'] = 1;
118 }
119
120 return $defaults;
121 }
122
123 /**
124 * Process the form submission.
125 */
126 public function postProcess() {
127 // store the submitted values in an array
128 $params = $this->controller->exportValues($this->_name);
129
130 if ($this->_action & CRM_Core_Action::UPDATE) {
131 $params['id'] = $this->_id;
132 }
133
134 $component = CRM_Mailing_BAO_MailingComponent::add($params);
135 CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', [
136 1 => $component->name,
137 ]), ts('Saved'), 'success');
138
139 }
140
141 /**
142 * Validation.
143 *
144 * @param array $params
145 * (ref.) an assoc array of name/value pairs.
146 *
147 * @param $files
148 * @param $options
149 *
150 * @return bool|array
151 * mixed true or array of errors
152 */
153 public static function dataRule($params, $files, $options) {
154 if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') {
155 $InvalidTokens = [];
156 }
157 else {
158 $InvalidTokens = ['action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")];
159 }
160 $errors = [];
161 foreach ([
162 'text',
163 'html',
164 ] as $type) {
165 $dataErrors = [];
166 foreach ($InvalidTokens as $token => $desc) {
167 if ($params['body_' . $type]) {
168 if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) {
169 $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', [
170 1 => $token,
171 2 => $desc,
172 ]) . '</li>';
173 }
174 }
175 }
176 if (!empty($dataErrors)) {
177 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', [
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>';
180 }
181 }
182 return empty($errors) ? TRUE : $errors;
183 }
184
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) {
197 $errors = [];
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 }
202 return empty($errors) ? TRUE : $errors;
203 }
204
205 }