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