province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Mailing / Form / Component.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
25606795 19 * This class generates form components for Location Type.
6a488035
TO
20 */
21class 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 /**
fe482240 31 * The name of the BAO object for this form.
6a488035
TO
32 *
33 * @var string
34 */
35 protected $_BAOName;
36
00be9182 37 public function preProcess() {
6a488035
TO
38 $this->_id = $this->get('id');
39 $this->_BAOName = $this->get('BAOName');
40 }
41
42 /**
fe482240 43 * Build the form object.
6a488035
TO
44 */
45 public function buildQuickForm() {
be2fb01f 46 $this->applyFilter(['name', 'subject', 'body_html'], 'trim');
6a488035
TO
47
48 $this->add('text', 'name', ts('Name'),
4825de4a 49 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'name'), TRUE
6a488035 50 );
be2fb01f 51 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
7e8c8317
SL
52 'CRM_Mailing_BAO_MailingComponent',
53 $this->_id,
54 ]);
6a488035
TO
55
56 $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues::mailingComponents());
57
58 $this->add('text', 'subject', ts('Subject'),
4825de4a 59 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'subject'),
6a488035
TO
60 TRUE
61 );
62 $this->add('textarea', 'body_text', ts('Body - TEXT Format'),
4825de4a 63 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_text')
6a488035
TO
64 );
65 $this->add('textarea', 'body_html', ts('Body - HTML Format'),
4825de4a 66 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_html')
6a488035
TO
67 );
68
899a6337
TO
69 $this->addYesNo('is_default', ts('Default?'));
70 $this->addYesNo('is_active', ts('Enabled?'));
6a488035 71
be2fb01f
CW
72 $this->addFormRule(['CRM_Mailing_Form_Component', 'formRule']);
73 $this->addFormRule(['CRM_Mailing_Form_Component', 'dataRule']);
6a488035 74
be2fb01f 75 $this->addButtons([
7e8c8317
SL
76 [
77 'type' => 'next',
78 'name' => ts('Save'),
79 'isDefault' => TRUE,
80 ],
81 [
82 'type' => 'cancel',
83 'name' => ts('Cancel'),
84 ],
85 ]);
6a488035
TO
86 }
87
88 /**
c490a46a 89 * Set default values for the form.
6a488035 90 */
00be9182 91 public function setDefaultValues() {
be2fb01f
CW
92 $defaults = [];
93 $params = [];
6a488035
TO
94
95 if (isset($this->_id)) {
be2fb01f 96 $params = ['id' => $this->_id];
0e6e8724
DL
97 $baoName = $this->_BAOName;
98 $baoName::retrieve($params, $defaults);
6a488035 99 }
899a6337
TO
100 else {
101 $defaults['is_active'] = 1;
102 }
6a488035
TO
103
104 return $defaults;
105 }
106
107 /**
fe482240 108 * Process the form submission.
6a488035
TO
109 */
110 public function postProcess() {
111 // store the submitted values in an array
112 $params = $this->controller->exportValues($this->_name);
113
6a488035 114 if ($this->_action & CRM_Core_Action::UPDATE) {
2d75534c 115 $params['id'] = $this->_id;
6a488035
TO
116 }
117
4825de4a 118 $component = CRM_Mailing_BAO_MailingComponent::add($params);
f7a67541
D
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
be2fb01f 123 CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', [
7e8c8317
SL
124 1 => $component->name,
125 ]), ts('Saved'), 'success');
2d75534c 126
6a488035 127 }
6a488035
TO
128
129 /**
fe482240 130 * Validation.
6a488035 131 *
90c8230e
TO
132 * @param array $params
133 * (ref.) an assoc array of name/value pairs.
6a488035 134 *
d9f93da9
EM
135 * @param $files
136 * @param $options
137 *
72b3a70c
CW
138 * @return bool|array
139 * mixed true or array of errors
6a488035 140 */
00be9182 141 public static function dataRule($params, $files, $options) {
6a488035 142 if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') {
be2fb01f 143 $InvalidTokens = [];
6a488035
TO
144 }
145 else {
be2fb01f 146 $InvalidTokens = ['action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")];
6a488035 147 }
be2fb01f
CW
148 $errors = [];
149 foreach ([
7e8c8317
SL
150 'text',
151 'html',
152 ] as $type) {
be2fb01f 153 $dataErrors = [];
6a488035
TO
154 foreach ($InvalidTokens as $token => $desc) {
155 if ($params['body_' . $type]) {
156 if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) {
be2fb01f 157 $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', [
7e8c8317
SL
158 1 => $token,
159 2 => $desc,
160 ]) . '</li>';
6a488035
TO
161 }
162 }
163 }
164 if (!empty($dataErrors)) {
be2fb01f 165 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', [
7e8c8317
SL
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>';
6a488035
TO
168 }
169 }
d2016abd
AU
170 return empty($errors) ? TRUE : $errors;
171 }
6a488035 172
d2016abd
AU
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) {
be2fb01f 185 $errors = [];
d2016abd
AU
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 }
6a488035
TO
190 return empty($errors) ? TRUE : $errors;
191 }
96025800 192
6a488035 193}