Merge pull request #13967 from eileenmcnaughton/activity_token
[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(array('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', array(
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(array('CRM_Mailing_Form_Component', 'formRule'));
89 $this->addFormRule(array('CRM_Mailing_Form_Component', 'dataRule'));
90
91 $this->addButtons(array(
92 array(
93 'type' => 'next',
94 'name' => ts('Save'),
95 'isDefault' => TRUE,
96 ),
97 array(
98 'type' => 'cancel',
99 'name' => ts('Cancel'),
100 ),
101 )
102 );
103 }
104
105 /**
106 * Set default values for the form.
107 */
108 public function setDefaultValues() {
109 $defaults = array();
110 $params = array();
111
112 if (isset($this->_id)) {
113 $params = array('id' => $this->_id);
114 $baoName = $this->_BAOName;
115 $baoName::retrieve($params, $defaults);
116 }
117 else {
118 $defaults['is_active'] = 1;
119 }
120
121 return $defaults;
122 }
123
124 /**
125 * Process the form submission.
126 */
127 public function postProcess() {
128 // store the submitted values in an array
129 $params = $this->controller->exportValues($this->_name);
130
131 if ($this->_action & CRM_Core_Action::UPDATE) {
132 $params['id'] = $this->_id;
133 }
134
135 $component = CRM_Mailing_BAO_MailingComponent::add($params);
136 CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', array(
137 1 => $component->name,
138 )
139 ), ts('Saved'), 'success');
140
141 }
142
143 /**
144 * Validation.
145 *
146 * @param array $params
147 * (ref.) an assoc array of name/value pairs.
148 *
149 * @param $files
150 * @param $options
151 *
152 * @return bool|array
153 * mixed true or array of errors
154 */
155 public static function dataRule($params, $files, $options) {
156 if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') {
157 $InvalidTokens = array();
158 }
159 else {
160 $InvalidTokens = array('action.forward' => ts("This token can only be used in send mailing context (body, header, footer).."));
161 }
162 $errors = array();
163 foreach (array(
164 'text',
165 'html',
166 ) as $type) {
167 $dataErrors = array();
168 foreach ($InvalidTokens as $token => $desc) {
169 if ($params['body_' . $type]) {
170 if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) {
171 $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', array(
172 1 => $token,
173 2 => $desc,
174 )) . '</li>';
175 }
176 }
177 }
178 if (!empty($dataErrors)) {
179 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', array(
180 1 => $type,
181 )) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>';
182 }
183 }
184 return empty($errors) ? TRUE : $errors;
185 }
186
187 /**
188 * Validates that either body text or body html is required.
189 * @param array $params
190 * (ref.) an assoc array of name/value pairs.
191 *
192 * @param $files
193 * @param $options
194 *
195 * @return bool|array
196 * mixed true or array of errors
197 */
198 public static function formRule($params, $files, $options) {
199 $errors = array();
200 if (empty($params['body_text']) && empty($params['body_html'])) {
201 $errors['body_text'] = ts("Please provide either HTML or TEXT format for the Body.");
202 $errors['body_html'] = ts("Please provide either HTML or TEXT format for the Body.");
203 }
204 return empty($errors) ? TRUE : $errors;
205 }
206
207 }