Merge pull request #13967 from eileenmcnaughton/activity_token
[civicrm-core.git] / CRM / Mailing / Form / Component.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
25606795 35 * This class generates form components for Location Type.
6a488035
TO
36 */
37class 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 /**
fe482240 47 * The name of the BAO object for this form.
6a488035
TO
48 *
49 * @var string
50 */
51 protected $_BAOName;
52
00be9182 53 public function preProcess() {
6a488035
TO
54 $this->_id = $this->get('id');
55 $this->_BAOName = $this->get('BAOName');
56 }
57
58 /**
fe482240 59 * Build the form object.
6a488035
TO
60 */
61 public function buildQuickForm() {
d8944e84 62 $this->applyFilter(array('name', 'subject', 'body_html'), 'trim');
6a488035
TO
63
64 $this->add('text', 'name', ts('Name'),
4825de4a 65 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'name'), TRUE
6a488035 66 );
353ffa53 67 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
4825de4a 68 'CRM_Mailing_BAO_MailingComponent',
79d7553f 69 $this->_id,
353ffa53 70 ));
6a488035
TO
71
72 $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues::mailingComponents());
73
74 $this->add('text', 'subject', ts('Subject'),
4825de4a 75 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'subject'),
6a488035
TO
76 TRUE
77 );
78 $this->add('textarea', 'body_text', ts('Body - TEXT Format'),
4825de4a 79 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_text')
6a488035
TO
80 );
81 $this->add('textarea', 'body_html', ts('Body - HTML Format'),
4825de4a 82 CRM_Core_DAO::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_html')
6a488035
TO
83 );
84
899a6337
TO
85 $this->addYesNo('is_default', ts('Default?'));
86 $this->addYesNo('is_active', ts('Enabled?'));
6a488035 87
d2016abd 88 $this->addFormRule(array('CRM_Mailing_Form_Component', 'formRule'));
6a488035
TO
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 /**
c490a46a 106 * Set default values for the form.
6a488035 107 */
00be9182 108 public function setDefaultValues() {
6a488035
TO
109 $defaults = array();
110 $params = array();
111
112 if (isset($this->_id)) {
113 $params = array('id' => $this->_id);
0e6e8724
DL
114 $baoName = $this->_BAOName;
115 $baoName::retrieve($params, $defaults);
6a488035 116 }
899a6337
TO
117 else {
118 $defaults['is_active'] = 1;
119 }
6a488035
TO
120
121 return $defaults;
122 }
123
124 /**
fe482240 125 * Process the form submission.
6a488035
TO
126 */
127 public function postProcess() {
128 // store the submitted values in an array
129 $params = $this->controller->exportValues($this->_name);
130
6a488035 131 if ($this->_action & CRM_Core_Action::UPDATE) {
2d75534c 132 $params['id'] = $this->_id;
6a488035
TO
133 }
134
4825de4a 135 $component = CRM_Mailing_BAO_MailingComponent::add($params);
2d75534c 136 CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', array(
21dfd5f5 137 1 => $component->name,
2d75534c
EM
138 )
139 ), ts('Saved'), 'success');
140
6a488035 141 }
6a488035
TO
142
143 /**
fe482240 144 * Validation.
6a488035 145 *
90c8230e
TO
146 * @param array $params
147 * (ref.) an assoc array of name/value pairs.
6a488035 148 *
d9f93da9
EM
149 * @param $files
150 * @param $options
151 *
72b3a70c
CW
152 * @return bool|array
153 * mixed true or array of errors
6a488035 154 */
00be9182 155 public static function dataRule($params, $files, $options) {
6a488035
TO
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(
353ffa53 164 'text',
79d7553f 165 'html',
353ffa53 166 ) as $type) {
6a488035
TO
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(
353ffa53 172 1 => $token,
79d7553f 173 2 => $desc,
353ffa53 174 )) . '</li>';
6a488035
TO
175 }
176 }
177 }
178 if (!empty($dataErrors)) {
179 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', array(
79d7553f 180 1 => $type,
353ffa53 181 )) . '<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
182 }
183 }
d2016abd
AU
184 return empty($errors) ? TRUE : $errors;
185 }
6a488035 186
d2016abd
AU
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 }
6a488035
TO
204 return empty($errors) ? TRUE : $errors;
205 }
96025800 206
6a488035 207}