Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | /** | |
50 | * The name of the BAO object for this form | |
51 | * | |
52 | * @var string | |
53 | */ | |
54 | protected $_BAOName; | |
55 | ||
56 | function preProcess() { | |
57 | $this->_id = $this->get('id'); | |
58 | $this->_BAOName = $this->get('BAOName'); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Function to build the form | |
63 | * | |
355ba699 | 64 | * @return void |
6a488035 TO |
65 | * @access public |
66 | */ | |
67 | public function buildQuickForm() { | |
68 | $this->applyFilter('__ALL__', 'trim'); | |
69 | ||
70 | $this->add('text', 'name', ts('Name'), | |
71 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'name'), TRUE | |
72 | ); | |
73 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Mailing_DAO_Component', $this->_id)); | |
74 | ||
75 | $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues::mailingComponents()); | |
76 | ||
77 | $this->add('text', 'subject', ts('Subject'), | |
78 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'subject'), | |
79 | TRUE | |
80 | ); | |
81 | $this->add('textarea', 'body_text', ts('Body - TEXT Format'), | |
82 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_text'), | |
83 | TRUE | |
84 | ); | |
85 | $this->add('textarea', 'body_html', ts('Body - HTML Format'), | |
86 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_html') | |
87 | ); | |
88 | ||
89 | $this->add('checkbox', 'is_default', ts('Default?')); | |
90 | $this->add('checkbox', 'is_active', ts('Enabled?')); | |
91 | ||
92 | $this->addFormRule(array('CRM_Mailing_Form_Component', 'dataRule')); | |
93 | ||
94 | $this->addButtons(array( | |
95 | array( | |
96 | 'type' => 'next', | |
97 | 'name' => ts('Save'), | |
98 | 'isDefault' => TRUE, | |
99 | ), | |
100 | array( | |
101 | 'type' => 'cancel', | |
102 | 'name' => ts('Cancel'), | |
103 | ), | |
104 | ) | |
105 | ); | |
106 | } | |
107 | ||
108 | /** | |
109 | * This function sets the default values for the form. | |
110 | * | |
111 | * @access public | |
112 | * | |
355ba699 | 113 | * @return void |
6a488035 TO |
114 | */ |
115 | function setDefaultValues() { | |
116 | $defaults = array(); | |
117 | $params = array(); | |
118 | ||
119 | if (isset($this->_id)) { | |
120 | $params = array('id' => $this->_id); | |
0e6e8724 DL |
121 | $baoName = $this->_BAOName; |
122 | $baoName::retrieve($params, $defaults); | |
6a488035 TO |
123 | } |
124 | $defaults['is_active'] = 1; | |
125 | ||
126 | return $defaults; | |
127 | } | |
128 | ||
129 | /** | |
130 | * Function to process the form | |
131 | * | |
132 | * @access public | |
133 | * | |
355ba699 | 134 | * @return void |
6a488035 TO |
135 | */ |
136 | public function postProcess() { | |
137 | // store the submitted values in an array | |
138 | $params = $this->controller->exportValues($this->_name); | |
139 | ||
6a488035 | 140 | if ($this->_action & CRM_Core_Action::UPDATE) { |
2d75534c | 141 | $params['id'] = $this->_id; |
6a488035 TO |
142 | } |
143 | ||
2d75534c EM |
144 | $component = CRM_Mailing_BAO_Component::add($params); |
145 | CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', array( | |
146 | 1 => $component->name | |
147 | ) | |
148 | ), ts('Saved'), 'success'); | |
149 | ||
6a488035 TO |
150 | } |
151 | //end of function | |
152 | ||
153 | /** | |
154 | * Function for validation | |
155 | * | |
156 | * @param array $params (ref.) an assoc array of name/value pairs | |
157 | * | |
d9f93da9 EM |
158 | * @param $files |
159 | * @param $options | |
160 | * | |
6a488035 TO |
161 | * @return mixed true or array of errors |
162 | * @access public | |
163 | * @static | |
164 | */ | |
165 | static function dataRule($params, $files, $options) { | |
166 | if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') { | |
167 | $InvalidTokens = array(); | |
168 | } | |
169 | else { | |
170 | $InvalidTokens = array('action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")); | |
171 | } | |
172 | $errors = array(); | |
173 | foreach (array( | |
174 | 'text', 'html') as $type) { | |
175 | $dataErrors = array(); | |
176 | foreach ($InvalidTokens as $token => $desc) { | |
177 | if ($params['body_' . $type]) { | |
178 | if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) { | |
179 | $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', array( | |
180 | 1 => $token, 2 => $desc)) . '</li>'; | |
181 | } | |
182 | } | |
183 | } | |
184 | if (!empty($dataErrors)) { | |
185 | $errors['body_' . $type] = ts('The following errors were detected in %1 message:', array( | |
186 | 1 => $type)) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>'; | |
187 | } | |
188 | } | |
189 | ||
190 | return empty($errors) ? TRUE : $errors; | |
191 | } | |
192 | } | |
193 |