a87d940aae60f7f57cc34bbcb874b60fe2b7d8f3
[com.zyxware.civiwci.git] / CRM / Wci / Form / CreateWidget.php
1 <?php
2
3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
6 ?>
7
8 <script type="text/javascript">
9 cj(function ( $ ) {
10 function setState() {
11 if ($('#override').is(':checked') == true) {
12 $('#custom_template').attr("disabled",false);
13 }
14 else {
15 $('#custom_template').attr("disabled",true);
16 }
17 }
18 $(document).ready(setState)
19 $('#override').click(setState);
20
21 });
22 </script>
23
24 <?php
25 /**
26 * Form controller class
27 *
28 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
29 */
30 class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
31
32 /**
33 * the widget id saved to the session for an update
34 *
35 * @var int
36 * @access protected
37 */
38 protected $_id;
39
40 function preProcess() {
41 parent::preProcess();
42 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
43 FALSE, NULL, 'REQUEST' );
44
45 $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
46 'text',
47 FALSE,
48 '#2786C2',
49 ),
50 'color_title_bg' => array(ts('Widget title background color'),
51 'text',
52 FALSE,
53 '#FFFFFF',
54 ),
55 'color_bar' => array(ts('Progress Bar Color'),
56 'text',
57 FALSE,
58 '#FFFFFF',
59 ),
60 'color_widget_bg' => array(ts('Widget background color'),
61 'text',
62 FALSE,
63 '#96C0E7',
64 ),
65 'color_description' => array(ts('Widget description color'),
66 'text',
67 FALSE,
68 '#96C0E7',
69 ),
70 'color_border' => array(ts('Widget border color'),
71 'text',
72 FALSE,
73 '#96C0E7',
74 ),
75 'color_button' => array(ts('Widget button text color'),
76 'text',
77 FALSE,
78 '#96C0E7',
79 ),
80 'color_button_bg' => array(ts('Widget button background color'),
81 'text',
82 FALSE,
83 '#96C0E7',
84 ),
85 'color_button_bg' => array(ts('Widget button background color'),
86 'text',
87 FALSE,
88 '#96C0E7',
89 ),
90 );
91 }
92
93 function setDefaultValues() {
94 $defaults = array();
95
96 $defaults['size_variant'] = 'normal';
97 foreach ($this->_colorFields as $name => $val) {
98 $defaults[$name] = $val[3];
99 }
100
101 return $defaults;
102 }
103
104 function buildQuickForm() {
105 // add form elements
106 $this->add('text', 'title', ts('Title'),true);
107 $this->add('text', 'logo_image', ts('Logo image'));
108 $this->add('text', 'image', ts('Image'));
109 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
110 $this->add('text', 'button_title', ts('Contribution button title'));
111 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
112 $this->addWysiwyg('description', ts('Description'), '');
113 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
114 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
115 foreach ($this->_colorFields as $name => $val) {
116 $this->add($val[1],
117 $name,
118 $val[0],
119 $name,
120 $val[2]
121 );
122 }
123 $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
124 $this->add('checkbox', 'override', ts('Override default template'));
125 $this->add('textarea', 'custom_template', ts('Custom template:<br><SMALL><font color="red">Please customize the smarty v2 template only if you know what you are doing</font></SMALL>'));
126
127 $this->addButtons(array(
128 array(
129 'type' => 'submit',
130 'name' => ts('Save'),
131 'isDefault' => TRUE,
132 ),
133 ));
134
135 // export form elements
136 $this->assign('elementNames', $this->getRenderableElementNames());
137
138 $smarty = CRM_Core_Smarty::singleton();
139 if (isset($this->_id)) {
140 /** Updating existing widget*/
141
142 $query = "SELECT pb.id as pbid, w.* FROM civicrm_wci_widget w INNER JOIN civicrm_wci_progress_bar pb on pb.id = w.progress_bar_id
143 where w.id=" . $this->_id;
144 $params = array();
145
146 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
147
148 while ($dao->fetch()) {
149 $wid_page[$dao->id] = array();
150 CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
151
152 $this->setDefaults(array(
153 'title' => $wid_page[$dao->id]['title']));
154 $this->setDefaults(array(
155 'logo_image' => $wid_page[$dao->id]['logo_image']));
156 $this->setDefaults(array(
157 'image' => $wid_page[$dao->id]['image']));
158 $this->setDefaults(array(
159 'button_link_to' => $wid_page[$dao->id]['button_link_to']));
160 $this->setDefaults(array(
161 'button_title' => $wid_page[$dao->id]['button_title']));
162
163 $this->setDefaults(array(
164 'progress_bar' => $dao->pbid));
165 $description = base64_decode($wid_page[$dao->id]['description']);
166 $this->setDefaults(array(
167 'description' => $description));
168 $this->setDefaults(array(
169 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
170 $this->setDefaults(array(
171 'size_variant' => $wid_page[$dao->id]['size_variant']));
172 $this->setDefaults(array(
173 'color_title' => $wid_page[$dao->id]['color_title']));
174 $this->setDefaults(array(
175 'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
176 $this->setDefaults(array(
177 'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
178 $this->setDefaults(array(
179 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
180 $this->setDefaults(array(
181 'color_description' => $wid_page[$dao->id]['color_description']));
182 $this->setDefaults(array(
183 'color_border' => $wid_page[$dao->id]['color_border']));
184 $this->setDefaults(array(
185 'color_button' => $wid_page[$dao->id]['color_button']));
186 $this->setDefaults(array(
187 'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
188 $this->setDefaults(array(
189 'style_rules' => $wid_page[$dao->id]['style_rules']));
190 $this->setDefaults(array(
191 'override' => $wid_page[$dao->id]['override']));
192 $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);
193
194 $this->setDefaults(array(
195 'custom_template' => $cust_templ));
196 }
197 }
198 else {
199 /** Keep template in civicrm-wci/templates folder*/
200 $output = file_get_contents('templates/CRM/Wci/Page/Widget.tpl',FILE_USE_INCLUDE_PATH);
201 $elem = $this->getElement('custom_template');
202 $elem->setValue($output);
203 }
204 parent::buildQuickForm();
205 }
206
207 function postProcess() {
208 $values = $this->exportValues();
209
210 $override = 0;
211 $cust_tmpl = "";
212 $cust_tmpl_col = "";
213 $sql = "";
214 /** If override check is checked state then only save the custom_template to the
215 database. otherwise wci uses default tpl file.
216 */
217 if(isset($values['override'])){
218 $override = $values['override'];
219 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
220 $cust_tmpl_col = ", custom_template";
221 }
222 if (isset($this->_id)) {
223 $cust_tmpl_col = "'" . $cust_tmpl_col . "='";
224 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
225 . "', logo_image = '" . $values['logo_image'] . "', image = '"
226 . $values['image'] . "', button_title = '" . $values['button_title']
227 . "', button_link_to = '" . $values['button_link_to']
228 . "', progress_bar_id = '" . $values['progress_bar']
229 . "', description = '" . base64_encode($values['description'])
230 . "', email_signup_group_id = '" . $values['email_signup_group_id']
231 . "', size_variant = '" . $values['size_variant']
232 . "', color_title = '" . $values['color_title']
233 . "', color_title_bg = '" . $values['color_title_bg']
234 . "', color_progress_bar = '" . $values['color_bar']
235 . "', color_widget_bg = '" . $values['color_widget_bg']
236 . "', color_description = '" . $values['color_description']
237 . "', color_border = '" . $values['color_border']
238 . "', color_button = '" . $values['color_button']
239 . "', color_button_bg = '" . $values['color_button_bg']
240 . "', style_rules = '" . $values['style_rules'] . "', override = '"
241 . $override . $cust_tmpl_col . $cust_tmpl . "'";
242 }
243 else {
244 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
245 button_title, button_link_to, progress_bar_id, description,
246 email_signup_group_id, size_variant, color_title, color_title_bg,
247 color_progress_bar, color_widget_bg, color_description, color_border,
248 color_button, color_button_bg, style_rules, override" . $cust_tmpl_col ." )
249 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
250 $values['image'] . "','" . $values['button_title'] . "','" .
251 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
252 base64_encode($values['description']) . "','" .
253 $values['email_signup_group_id'] . "','" .
254 $values['size_variant'] . "','" . $values['color_title'] . "','" .
255 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
256 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
257 $values['color_border'] . "','" . $values['color_button'] . "','" .
258 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
259 $override . "','" . $cust_tmpl
260 . "')";
261 }
262 $errorScope = CRM_Core_TemporaryErrorScope::useException();
263 try {
264 $transaction = new CRM_Core_Transaction();
265 CRM_Core_DAO::executeQuery($sql);
266
267 $transaction->commit();
268 }
269 catch (Exception $e) {
270 //TODO
271 print_r($e->getMessage());
272 $transaction->rollback();
273 }
274
275 parent::postProcess();
276 }
277
278 function getProgressBars() {
279 $options = array(
280 '' => ts('- select -'),
281 );
282 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
283 foreach ($pbList as $pb) {
284 $options[$pb['id']] = $pb['name'];
285 }
286
287 return $options;
288 }
289 function getContributionPageOptions() {
290 $options = array(
291 '' => ts('- select -'),
292 );
293
294 $result = civicrm_api3('contribution_page', 'get');
295 foreach ($result['values'] as $contribution_page) {
296 $options[$contribution_page['id']] = $contribution_page['title'];
297 }
298
299 return $options;
300 }
301
302 function getGroupOptions() {
303 $options = array(
304 '' => ts('- select -'),
305 );
306
307 $result = civicrm_api3('group', 'get');
308 foreach ($result['values'] as $group) {
309 $options[$group['id']] = $group['title'];
310 }
311
312 return $options;
313 }
314
315 function getSizeOptions() {
316 $options = array(
317 'thin' => ts('Thin'),
318 'normal' => ts('Normal'),
319 'wide' => ts('Wide'),
320 );
321
322 return $options;
323 }
324
325 /**
326 * Get the fields/elements defined in this form.
327 *
328 * @return array (string)
329 */
330 function getRenderableElementNames() {
331 // The _elements list includes some items which should not be
332 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
333 // items don't have labels. We'll identify renderable by filtering on
334 // the 'label'.
335 $elementNames = array();
336 foreach ($this->_elements as $element) {
337 $label = $element->getLabel();
338 if (!empty($label)) {
339 $elementNames[] = $element->getName();
340 }
341 }
342 return $elementNames;
343 }
344 }