province abbreviation patch - issue 724
[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
120 // set the id after save, so it can be used in a extension using the postProcess hook
121 $this->_id = $component->id;
122
123 CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', [
124 1 => $component->name,
125 ]), ts('Saved'), 'success');
126
127 }
128
129 /**
130 * Validation.
131 *
132 * @param array $params
133 * (ref.) an assoc array of name/value pairs.
134 *
135 * @param $files
136 * @param $options
137 *
138 * @return bool|array
139 * mixed true or array of errors
140 */
141 public static function dataRule($params, $files, $options) {
142 if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') {
143 $InvalidTokens = [];
144 }
145 else {
146 $InvalidTokens = ['action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")];
147 }
148 $errors = [];
149 foreach ([
150 'text',
151 'html',
152 ] as $type) {
153 $dataErrors = [];
154 foreach ($InvalidTokens as $token => $desc) {
155 if ($params['body_' . $type]) {
156 if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) {
157 $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', [
158 1 => $token,
159 2 => $desc,
160 ]) . '</li>';
161 }
162 }
163 }
164 if (!empty($dataErrors)) {
165 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', [
166 1 => $type,
167 ]) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>';
168 }
169 }
170 return empty($errors) ? TRUE : $errors;
171 }
172
173 /**
174 * Validates that either body text or body html is required.
175 * @param array $params
176 * (ref.) an assoc array of name/value pairs.
177 *
178 * @param $files
179 * @param $options
180 *
181 * @return bool|array
182 * mixed true or array of errors
183 */
184 public static function formRule($params, $files, $options) {
185 $errors = [];
186 if (empty($params['body_text']) && empty($params['body_html'])) {
187 $errors['body_text'] = ts("Please provide either HTML or TEXT format for the Body.");
188 $errors['body_html'] = ts("Please provide either HTML or TEXT format for the Body.");
189 }
190 return empty($errors) ? TRUE : $errors;
191 }
192
193 }