Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Location Type | |
38 | * | |
39 | */ | |
40 | class CRM_Mailing_Form_Component extends CRM_Core_Form { | |
41 | ||
42 | /** | |
43 | * The id of the object being edited / created | |
44 | * | |
45 | * @var int | |
46 | */ | |
47 | protected $_id; | |
48 | ||
49 | /** | |
fe482240 | 50 | * The name of the BAO object for this form. |
6a488035 TO |
51 | * |
52 | * @var string | |
53 | */ | |
54 | protected $_BAOName; | |
55 | ||
00be9182 | 56 | public function preProcess() { |
6a488035 TO |
57 | $this->_id = $this->get('id'); |
58 | $this->_BAOName = $this->get('BAOName'); | |
59 | } | |
60 | ||
61 | /** | |
fe482240 | 62 | * Build the form object. |
6a488035 | 63 | * |
355ba699 | 64 | * @return void |
6a488035 TO |
65 | */ |
66 | public function buildQuickForm() { | |
67 | $this->applyFilter('__ALL__', 'trim'); | |
68 | ||
69 | $this->add('text', 'name', ts('Name'), | |
70 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'name'), TRUE | |
71 | ); | |
353ffa53 TO |
72 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array( |
73 | 'CRM_Mailing_DAO_Component', | |
79d7553f | 74 | $this->_id, |
353ffa53 | 75 | )); |
6a488035 TO |
76 | |
77 | $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues::mailingComponents()); | |
78 | ||
79 | $this->add('text', 'subject', ts('Subject'), | |
80 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'subject'), | |
81 | TRUE | |
82 | ); | |
83 | $this->add('textarea', 'body_text', ts('Body - TEXT Format'), | |
84 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_text'), | |
85 | TRUE | |
86 | ); | |
87 | $this->add('textarea', 'body_html', ts('Body - HTML Format'), | |
88 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_html') | |
89 | ); | |
90 | ||
899a6337 TO |
91 | $this->addYesNo('is_default', ts('Default?')); |
92 | $this->addYesNo('is_active', ts('Enabled?')); | |
6a488035 TO |
93 | |
94 | $this->addFormRule(array('CRM_Mailing_Form_Component', 'dataRule')); | |
95 | ||
96 | $this->addButtons(array( | |
97 | array( | |
98 | 'type' => 'next', | |
99 | 'name' => ts('Save'), | |
100 | 'isDefault' => TRUE, | |
101 | ), | |
102 | array( | |
103 | 'type' => 'cancel', | |
104 | 'name' => ts('Cancel'), | |
105 | ), | |
106 | ) | |
107 | ); | |
108 | } | |
109 | ||
110 | /** | |
c490a46a | 111 | * Set default values for the form. |
6a488035 | 112 | * |
6a488035 | 113 | * |
355ba699 | 114 | * @return void |
6a488035 | 115 | */ |
00be9182 | 116 | public function setDefaultValues() { |
6a488035 TO |
117 | $defaults = array(); |
118 | $params = array(); | |
119 | ||
120 | if (isset($this->_id)) { | |
121 | $params = array('id' => $this->_id); | |
0e6e8724 DL |
122 | $baoName = $this->_BAOName; |
123 | $baoName::retrieve($params, $defaults); | |
6a488035 | 124 | } |
899a6337 TO |
125 | else { |
126 | $defaults['is_active'] = 1; | |
127 | } | |
6a488035 TO |
128 | |
129 | return $defaults; | |
130 | } | |
131 | ||
132 | /** | |
fe482240 | 133 | * Process the form submission. |
6a488035 | 134 | * |
6a488035 | 135 | * |
355ba699 | 136 | * @return void |
6a488035 TO |
137 | */ |
138 | public function postProcess() { | |
139 | // store the submitted values in an array | |
140 | $params = $this->controller->exportValues($this->_name); | |
141 | ||
6a488035 | 142 | if ($this->_action & CRM_Core_Action::UPDATE) { |
2d75534c | 143 | $params['id'] = $this->_id; |
6a488035 TO |
144 | } |
145 | ||
2d75534c EM |
146 | $component = CRM_Mailing_BAO_Component::add($params); |
147 | CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', array( | |
21dfd5f5 | 148 | 1 => $component->name, |
2d75534c EM |
149 | ) |
150 | ), ts('Saved'), 'success'); | |
151 | ||
6a488035 | 152 | } |
6a488035 TO |
153 | |
154 | /** | |
fe482240 | 155 | * Validation. |
6a488035 | 156 | * |
90c8230e TO |
157 | * @param array $params |
158 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 159 | * |
d9f93da9 EM |
160 | * @param $files |
161 | * @param $options | |
162 | * | |
72b3a70c CW |
163 | * @return bool|array |
164 | * mixed true or array of errors | |
6a488035 | 165 | */ |
00be9182 | 166 | public static function dataRule($params, $files, $options) { |
6a488035 TO |
167 | if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') { |
168 | $InvalidTokens = array(); | |
169 | } | |
170 | else { | |
171 | $InvalidTokens = array('action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")); | |
172 | } | |
173 | $errors = array(); | |
174 | foreach (array( | |
353ffa53 | 175 | 'text', |
79d7553f | 176 | 'html', |
353ffa53 | 177 | ) as $type) { |
6a488035 TO |
178 | $dataErrors = array(); |
179 | foreach ($InvalidTokens as $token => $desc) { | |
180 | if ($params['body_' . $type]) { | |
181 | if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) { | |
182 | $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', array( | |
353ffa53 | 183 | 1 => $token, |
79d7553f | 184 | 2 => $desc, |
353ffa53 | 185 | )) . '</li>'; |
6a488035 TO |
186 | } |
187 | } | |
188 | } | |
189 | if (!empty($dataErrors)) { | |
190 | $errors['body_' . $type] = ts('The following errors were detected in %1 message:', array( | |
79d7553f | 191 | 1 => $type, |
353ffa53 | 192 | )) . '<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 |
193 | } |
194 | } | |
195 | ||
196 | return empty($errors) ? TRUE : $errors; | |
197 | } | |
96025800 | 198 | |
6a488035 | 199 | } |