5517583b9da639b983764c1077032957eddedac4
[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 * Form controller class
9 *
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
11 */
12 class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
13
14 /**
15 * the widget id saved to the session for an update
16 *
17 * @var int
18 * @access protected
19 */
20 protected $_id;
21
22 function preProcess() {
23 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'createwidget.js');
24 parent::preProcess();
25 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
26 FALSE, NULL, 'REQUEST' );
27
28 $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
29 'text',
30 FALSE,
31 '#BF0F0F',
32 ),
33 'color_title_bg' => array(ts('Title background color'),
34 'text',
35 FALSE,
36 '#FFFFFF',
37 ),
38 'color_bar' => array(ts('Progress Bar Color'),
39 'text',
40 FALSE,
41 '#BF0F0F',
42 ),
43 'color_widget_bg' => array(ts('Background color'),
44 'text',
45 FALSE,
46 '#FFFFFF',
47 ),
48 'color_description' => array(ts('Description color'),
49 'text',
50 FALSE,
51 '#000000',
52 ),
53 'color_border' => array(ts('Border color'),
54 'text',
55 FALSE,
56 '#BF0F0F',
57 ),
58 'color_button' => array(ts('Button text color'),
59 'text',
60 FALSE,
61 '#FFFFFF',
62 ),
63 'color_button_bg' => array(ts('Button background color'),
64 'text',
65 FALSE,
66 '#BF0F0F',
67 ),
68 'color_btn_newsletter' => array(ts('Newsletter Button text color'),
69 'text',
70 FALSE,
71 '#FFFFFF',
72 ),
73 'color_btn_newsletter_bg' => array(ts('Newsletter Button color'),
74 'text',
75 FALSE,
76 '#BF0F0F',
77 ),
78 'newsletter_text' => array(ts('Newsletter text'),
79 'text',
80 FALSE,
81 'Get the monthly newsletter',
82 ),
83 'color_newsletter_text' => array(ts('Newsletter text color'),
84 'text',
85 FALSE,
86 '#BF0F0F',
87 ),
88 );
89 }
90
91 function setDefaultValues() {
92 $defaults = array();
93 if (!isset($this->_id)) {
94 $defaults['size_variant'] = 'normal';
95 foreach ($this->_colorFields as $name => $val) {
96 $defaults[$name] = $val[3];
97 }
98 }
99 return $defaults;
100 }
101
102 function buildQuickForm() {
103 // add form elements
104 $this->add('text', 'title', ts('Title'),true)->setSize(45);
105 $this->add('text', 'logo_image', ts('Logo image'))->setSize(45);
106 $this->add('text', 'image', ts('Image'))->setSize(45);
107 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
108 $this->add('text', 'button_title', ts('Contribution button title'))->setSize(45);
109 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
110 $this->addWysiwyg('description', ts('Description'), '');
111 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
112 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
113 // $fieldset = $this->addElement('fieldset')->setLabel('Advanced Settings');
114 $this->add('checkbox', 'hide_title', ts('Hide Title'));
115 $this->add('checkbox', 'hide_border', ts('Hide border'));
116 $this->add('checkbox', 'hide_pbcap', ts('Hide progress bar caption'));
117 foreach ($this->_colorFields as $name => $val) {
118 $this->addElement($val[1],
119 $name,
120 $val[0],
121 $name,
122 $val[2]
123 );
124 }
125 $this->add('textarea', 'style_rules', ts('Additional Style Rules'), 'cols="70"')->setRows(5);
126 $this->add('checkbox', 'override', ts('Override default template'));
127 $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>'), 'cols="70"')->setRows(10);
128
129 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
130 $this->addButtons(array(
131 array(
132 'type' => 'submit',
133 'name' => ts('Save'),
134 'isDefault' => TRUE,
135 ),
136 array(
137 'type' => 'next',
138 'name' => ts('Save & Preview'),
139 ),
140 ));
141
142 // export form elements
143 $this->assign('elementNames', $this->getRenderableElementNames());
144
145 if (isset($this->_id)) {
146 /** Updating existing widget*/
147
148 /*$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
149 where w.id=" . $this->_id;*/
150
151 $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id;
152 $params = array();
153
154 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
155
156 while ($dao->fetch()) {
157
158 $wid_page[$dao->id] = array();
159 CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
160
161 $this->setDefaults(array(
162 'title' => $wid_page[$dao->id]['title']));
163 $this->setDefaults(array(
164 'logo_image' => $wid_page[$dao->id]['logo_image']));
165 $this->setDefaults(array(
166 'image' => $wid_page[$dao->id]['image']));
167 $this->setDefaults(array(
168 'button_link_to' => $wid_page[$dao->id]['button_link_to']));
169 $this->setDefaults(array(
170 'button_title' => $wid_page[$dao->id]['button_title']));
171
172 $this->setDefaults(array(
173 'progress_bar' => $dao->progress_bar_id/*$dao->pbid*/));
174 $description = $wid_page[$dao->id]['description'];
175 $this->setDefaults(array(
176 'description' => $description));
177 $this->setDefaults(array(
178 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
179 $this->setDefaults(array(
180 'size_variant' => $dao->size_variant));
181 $this->setDefaults(array(
182 'color_title' => $wid_page[$dao->id]['color_title']));
183 $this->setDefaults(array(
184 'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
185 $this->setDefaults(array(
186 'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
187 $this->setDefaults(array(
188 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
189 $this->setDefaults(array(
190 'color_description' => $wid_page[$dao->id]['color_description']));
191 $this->setDefaults(array(
192 'color_border' => $wid_page[$dao->id]['color_border']));
193 $this->setDefaults(array(
194 'color_button' => $wid_page[$dao->id]['color_button']));
195 $this->setDefaults(array(
196 'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
197 $this->setDefaults(array(
198 'style_rules' => $wid_page[$dao->id]['style_rules']));
199 $this->setDefaults(array(
200 'override' => $wid_page[$dao->id]['override']));
201 $this->setDefaults(array(
202 'hide_title' => $wid_page[$dao->id]['hide_title']));
203 $this->setDefaults(array(
204 'hide_border' => $wid_page[$dao->id]['hide_border']));
205 $this->setDefaults(array(
206 'hide_pbcap' => $wid_page[$dao->id]['hide_pbcap']));
207
208 $this->setDefaults(array(
209 'color_btn_newsletter' => $wid_page[$dao->id]['color_btn_newsletter']));
210 $this->setDefaults(array(
211 'color_btn_newsletter_bg' => $wid_page[$dao->id]['color_btn_newsletter_bg']));
212 $this->setDefaults(array(
213 'newsletter_text' => $wid_page[$dao->id]['newsletter_text']));
214 $this->setDefaults(array(
215 'color_newsletter_text' => $wid_page[$dao->id]['color_newsletter_text']));
216
217 if(true == $wid_page[$dao->id]['override']) {
218 $cust_templ = html_entity_decode($wid_page[$dao->id]['custom_template']);
219 $this->setDefaults(array(
220 'custom_template' => $cust_templ));
221 } else {
222 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
223 $elem = $this->getElement('custom_template');
224 $elem->setValue($output);
225 }
226 }
227 CRM_Utils_System::setTitle(ts('Edit Widget'));
228 }
229 else {
230 CRM_Utils_System::setTitle(ts('Create Widget'));
231 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
232 $elem = $this->getElement('custom_template');
233 $elem->setValue($output);
234 }
235 parent::buildQuickForm();
236 }
237
238 function postProcess() {
239 $values = $this->exportValues();
240 $override = 0;
241 $hide_title = 0;
242 $hide_border = 0;
243 $hide_pbcap = 0;
244 $cust_tmpl = "";
245 $cust_tmpl_col = "";
246 $sql = "";
247 $coma = "";
248 $equals = "";
249 $quote = "";
250 /** If override check is checked state then only save the custom_template to the
251 database. otherwise wci uses default tpl file.
252 */
253 if(isset($values['override'])){
254 $override = $values['override'];
255 $cust_tmpl = str_replace("'", "''", $values['custom_template']);
256 $cust_tmpl_col = "custom_template";
257 $coma = ",";
258 $equals = " = ";
259 $quote = "'";
260 }
261 if(isset($values['hide_title'])){
262 $hide_title = $values['hide_title'];
263 }
264 if(isset($values['hide_border'])){
265 $hide_border = $values['hide_border'];
266 }
267 if(isset($values['hide_pbcap'])){
268 $hide_pbcap = $values['hide_pbcap'];
269 }
270 $title = str_replace("'", "''", $values['title']);
271
272 if (isset($this->_id)) {
273 $sql = "UPDATE civicrm_wci_widget SET title = '". $title
274 . "', logo_image = '" . $values['logo_image'] . "', image = '"
275 . $values['image'] . "', button_title = '" . $values['button_title']
276 . "', button_link_to = '" . $values['button_link_to']
277 . "', progress_bar_id = '" . $values['progress_bar']
278 . "', description = '" . str_replace("'", "''", $values['description'])
279 . "', email_signup_group_id = '" . $values['email_signup_group_id']
280 . "', size_variant = '" . $values['size_variant']
281 . "', color_title = '" . $values['color_title']
282 . "', color_title_bg = '" . $values['color_title_bg']
283 . "', color_progress_bar = '" . $values['color_bar']
284 . "', color_widget_bg = '" . $values['color_widget_bg']
285 . "', color_description = '" . $values['color_description']
286 . "', color_border = '" . $values['color_border']
287 . "', color_button = '" . $values['color_button']
288 . "', color_button_bg = '" . $values['color_button_bg']
289 . "', hide_title = '" . $hide_title
290 . "', hide_border = '" . $hide_border
291 . "', hide_pbcap = '" . $hide_pbcap
292 . "', color_btn_newsletter = '" . $values['color_btn_newsletter']
293 . "', color_btn_newsletter_bg = '" . $values['color_btn_newsletter_bg']
294 . "', newsletter_text = '" . $values['newsletter_text']
295 . "', color_newsletter_text = '" . $values['color_newsletter_text']
296 . "', style_rules = '" . str_replace("'", "''", $values['style_rules']) . "', override = '"
297 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
298 }
299 else {
300 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
301 button_title, button_link_to, progress_bar_id, description,
302 email_signup_group_id, size_variant, color_title, color_title_bg,
303 color_progress_bar, color_widget_bg, color_description, color_border,
304 color_button, color_button_bg, hide_title, hide_border, hide_pbcap,
305 color_btn_newsletter, color_btn_newsletter_bg, newsletter_text, color_newsletter_text,
306 style_rules, override" . $coma . $cust_tmpl_col ." )
307 VALUES ('" . $title . "','" . $values['logo_image'] . "','" .
308 $values['image'] . "','" . $values['button_title'] . "','" .
309 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
310 str_replace("'", "''", $values['description']) . "','" .
311 $values['email_signup_group_id'] . "','" .
312 $values['size_variant'] . "','" . $values['color_title'] . "','" .
313 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
314 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
315 $values['color_border'] . "','" . $values['color_button'] . "','" .
316 $values['color_button_bg'] . "','" . $hide_title . "','" .
317 $hide_border . "','" . $hide_pbcap . "','" .
318 $values['color_btn_newsletter'] . "','" . $values['color_btn_newsletter_bg'] . "','" .
319 $values['newsletter_text'] . "','" . $values['color_newsletter_text'] . "','" .
320 str_replace("'", "''", $values['style_rules']) . "','" .
321 $override . $quote . $coma . $quote . $cust_tmpl
322 . "')";
323 }
324
325 $errorScope = CRM_Core_TemporaryErrorScope::useException();
326 try {
327 $transaction = new CRM_Core_Transaction();
328 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;");
329 CRM_Core_DAO::executeQuery($sql);
330 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
331 $transaction->commit();
332 CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success');
333 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
334 (isset($this->_id)) ? $widget_id = $this->_id :
335 $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
336 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
337 } else {
338 CRM_Utils_System::redirect('widget?reset=1');
339 }
340 }
341 catch (Exception $e) {
342 CRM_Core_Session::setStatus(ts('Failed to create widget'), '', 'error');
343 $transaction->rollback();
344 }
345
346 parent::postProcess();
347 }
348
349 function getProgressBars() {
350 $options = array(
351 '' => ts('- select -'),
352 );
353 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
354 foreach ($pbList as $pb) {
355 $options[$pb['id']] = $pb['name'];
356 }
357
358 return $options;
359 }
360 function getContributionPageOptions() {
361 $options = array(
362 '' => ts('- select -'),
363 );
364
365 $result = civicrm_api3('contribution_page', 'get');
366 foreach ($result['values'] as $contribution_page) {
367 $options[$contribution_page['id']] = $contribution_page['title'];
368 }
369
370 return $options;
371 }
372
373 function getGroupOptions() {
374 $options = array(
375 '' => ts('- select -'),
376 );
377
378 $result = civicrm_api3('group', 'get', array(
379 'group_type' => '2'
380 ));
381
382 foreach ($result['values'] as $group) {
383 $options[$group['id']] = $group['title'];
384 }
385
386 return $options;
387 }
388
389 function getSizeOptions() {
390 $options = array(
391 'thin' => ts('Thin'),
392 'normal' => ts('Normal'),
393 'wide' => ts('Wide'),
394 );
395
396 return $options;
397 }
398
399 /**
400 * Get the fields/elements defined in this form.
401 *
402 * @return array (string)
403 */
404 function getRenderableElementNames() {
405 // The _elements list includes some items which should not be
406 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
407 // items don't have labels. We'll identify renderable by filtering on
408 // the 'label'.
409 $elementNames = array();
410 foreach ($this->_elements as $element) {
411 $label = $element->getLabel();
412 if (!empty($label)) {
413 $elementNames[] = $element->getName();
414 }
415 }
416 return $elementNames;
417 }
418 }