fixed beta 1 bugs, 31062, 31063, 31101, 31067, 31065
[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<br><SMALL>Fixed width. Height depends on what contents selected.</SMALL>'), $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 // export form elements
142 $this->assign('elementNames', $this->getRenderableElementNames());
143 if (isset($this->_id)) {
144 /** Updating existing widget*/
145
146 /*$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
147 where w.id=" . $this->_id;*/
148
149 $query = "SELECT * FROM civicrm_wci_widget WHERE id=" . $this->_id;
150 $params = array();
151
152 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
153
154 while ($dao->fetch()) {
155 $wid_page[$dao->id] = array();
156 CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
157 $this->setDefaults(array(
158 'title' => $wid_page[$dao->id]['title']));
159 $this->setDefaults(array(
160 'logo_image' => $wid_page[$dao->id]['logo_image']));
161 $this->setDefaults(array(
162 'image' => $wid_page[$dao->id]['image']));
163 $this->setDefaults(array(
164 'button_link_to' => $wid_page[$dao->id]['button_link_to']));
165 $this->setDefaults(array(
166 'button_title' => $wid_page[$dao->id]['button_title']));
167 $this->setDefaults(array(
168 'progress_bar' => $dao->progress_bar_id/*$dao->pbid*/));
169 $description = $wid_page[$dao->id]['description'];
170 $this->setDefaults(array(
171 'description' => $description));
172 $this->setDefaults(array(
173 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
174 $this->setDefaults(array(
175 'size_variant' => $dao->size_variant));
176 $this->setDefaults(array(
177 'color_title' => $wid_page[$dao->id]['color_title']));
178 $this->setDefaults(array(
179 'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
180 $this->setDefaults(array(
181 'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
182 $this->setDefaults(array(
183 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
184 $this->setDefaults(array(
185 'color_description' => $wid_page[$dao->id]['color_description']));
186 $this->setDefaults(array(
187 'color_border' => $wid_page[$dao->id]['color_border']));
188 $this->setDefaults(array(
189 'color_button' => $wid_page[$dao->id]['color_button']));
190 $this->setDefaults(array(
191 'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
192 $this->setDefaults(array(
193 'style_rules' => $wid_page[$dao->id]['style_rules']));
194 $this->setDefaults(array(
195 'override' => $wid_page[$dao->id]['override']));
196 $this->setDefaults(array(
197 'hide_title' => $wid_page[$dao->id]['hide_title']));
198 $this->setDefaults(array(
199 'hide_border' => $wid_page[$dao->id]['hide_border']));
200 $this->setDefaults(array(
201 'hide_pbcap' => $wid_page[$dao->id]['hide_pbcap']));
202
203 $this->setDefaults(array(
204 'color_btn_newsletter' => $wid_page[$dao->id]['color_btn_newsletter']));
205 $this->setDefaults(array(
206 'color_btn_newsletter_bg' => $wid_page[$dao->id]['color_btn_newsletter_bg']));
207 $this->setDefaults(array(
208 'newsletter_text' => $wid_page[$dao->id]['newsletter_text']));
209 $this->setDefaults(array(
210 'color_newsletter_text' => $wid_page[$dao->id]['color_newsletter_text']));
211
212 if(true == $wid_page[$dao->id]['override']) {
213 $cust_templ = html_entity_decode($wid_page[$dao->id]['custom_template']);
214 $this->setDefaults(array(
215 'custom_template' => $cust_templ));
216 } else {
217 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
218 $elem = $this->getElement('custom_template');
219 $elem->setValue($output);
220 }
221 }
222 CRM_Utils_System::setTitle(ts('Edit Widget'));
223 }
224 else {
225 CRM_Utils_System::setTitle(ts('Create Widget'));
226 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
227 $elem = $this->getElement('custom_template');
228 $elem->setValue($output);
229 }
230 parent::buildQuickForm();
231 }
232
233 function postProcess() {
234 $values = $this->exportValues();
235 $override = 0;
236 $hide_title = 0;
237 $hide_border = 0;
238 $hide_pbcap = 0;
239 $cust_tmpl = "";
240 $cust_tmpl_col = "";
241 $sql = "";
242 $coma = "";
243 $equals = "";
244 $quote = "";
245 /** If override check is checked state then only save the custom_template to the
246 database. otherwise wci uses default tpl file.
247 */
248 if(isset($values['override'])){
249 $override = $values['override'];
250 $cust_tmpl = str_replace("'", "''", $values['custom_template']);
251 $cust_tmpl_col = "custom_template";
252 $coma = ",";
253 $equals = " = ";
254 $quote = "'";
255 }
256 if(isset($values['hide_title'])){
257 $hide_title = $values['hide_title'];
258 }
259 if(isset($values['hide_border'])){
260 $hide_border = $values['hide_border'];
261 }
262 if(isset($values['hide_pbcap'])){
263 $hide_pbcap = $values['hide_pbcap'];
264 }
265 $title = str_replace("'", "''", $values['title']);
266
267 if (isset($this->_id)) {
268 $sql = "UPDATE civicrm_wci_widget SET title = '". $title
269 . "', logo_image = '" . $values['logo_image'] . "', image = '"
270 . $values['image'] . "', button_title = '" . $values['button_title']
271 . "', button_link_to = '" . $values['button_link_to']
272 . "', progress_bar_id = '" . $values['progress_bar']
273 . "', description = '" . str_replace("'", "''", $values['description'])
274 . "', email_signup_group_id = '" . $values['email_signup_group_id']
275 . "', size_variant = '" . $values['size_variant']
276 . "', color_title = '" . $values['color_title']
277 . "', color_title_bg = '" . $values['color_title_bg']
278 . "', color_progress_bar = '" . $values['color_bar']
279 . "', color_widget_bg = '" . $values['color_widget_bg']
280 . "', color_description = '" . $values['color_description']
281 . "', color_border = '" . $values['color_border']
282 . "', color_button = '" . $values['color_button']
283 . "', color_button_bg = '" . $values['color_button_bg']
284 . "', hide_title = '" . $hide_title
285 . "', hide_border = '" . $hide_border
286 . "', hide_pbcap = '" . $hide_pbcap
287 . "', color_btn_newsletter = '" . $values['color_btn_newsletter']
288 . "', color_btn_newsletter_bg = '" . $values['color_btn_newsletter_bg']
289 . "', newsletter_text = '" . $values['newsletter_text']
290 . "', color_newsletter_text = '" . $values['color_newsletter_text']
291 . "', style_rules = '" . str_replace("'", "''", $values['style_rules']) . "', override = '"
292 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
293 }
294 else {
295 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
296 button_title, button_link_to, progress_bar_id, description,
297 email_signup_group_id, size_variant, color_title, color_title_bg,
298 color_progress_bar, color_widget_bg, color_description, color_border,
299 color_button, color_button_bg, hide_title, hide_border, hide_pbcap,
300 color_btn_newsletter, color_btn_newsletter_bg, newsletter_text, color_newsletter_text,
301 style_rules, override" . $coma . $cust_tmpl_col ." )
302 VALUES ('" . $title . "','" . $values['logo_image'] . "','" .
303 $values['image'] . "','" . $values['button_title'] . "','" .
304 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
305 str_replace("'", "''", $values['description']) . "','" .
306 $values['email_signup_group_id'] . "','" .
307 $values['size_variant'] . "','" . $values['color_title'] . "','" .
308 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
309 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
310 $values['color_border'] . "','" . $values['color_button'] . "','" .
311 $values['color_button_bg'] . "','" . $hide_title . "','" .
312 $hide_border . "','" . $hide_pbcap . "','" .
313 $values['color_btn_newsletter'] . "','" . $values['color_btn_newsletter_bg'] . "','" .
314 $values['newsletter_text'] . "','" . $values['color_newsletter_text'] . "','" .
315 str_replace("'", "''", $values['style_rules']) . "','" .
316 $override . $quote . $coma . $quote . $cust_tmpl
317 . "')";
318 }
319
320 $errorScope = CRM_Core_TemporaryErrorScope::useException();
321 try {
322 $transaction = new CRM_Core_Transaction();
323 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;");
324 CRM_Core_DAO::executeQuery($sql);
325 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
326 $transaction->commit();
327 CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success');
328 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
329 (isset($this->_id)) ? $widget_id = $this->_id :
330 $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
331 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
332 } else {
333 CRM_Utils_System::redirect('widget?reset=1');
334 }
335 }
336 catch (Exception $e) {
337 CRM_Core_Session::setStatus(ts('Failed to create widget'), '', 'error');
338 $transaction->rollback();
339 }
340
341 parent::postProcess();
342 }
343
344 function getProgressBars() {
345 $options = array(
346 '' => ts('- select -'),
347 );
348 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
349 foreach ($pbList as $pb) {
350 $options[$pb['id']] = $pb['name'];
351 }
352
353 return $options;
354 }
355 function getContributionPageOptions() {
356 $options = array(
357 '' => ts('- select -'),
358 );
359
360 $result = civicrm_api3('contribution_page', 'get');
361 foreach ($result['values'] as $contribution_page) {
362 $options[$contribution_page['id']] = $contribution_page['title'];
363 }
364
365 return $options;
366 }
367
368 function getGroupOptions() {
369 $options = array(
370 '' => ts('- select -'),
371 );
372
373 $result = civicrm_api3('group', 'get', array(
374 'group_type' => '2'
375 ));
376
377 foreach ($result['values'] as $group) {
378 $options[$group['id']] = $group['title'];
379 }
380
381 return $options;
382 }
383
384 function getSizeOptions() {
385 $options = array(
386 'thin' => ts('Thin (150px)'),
387 'normal' => ts('Normal (200px)'),
388 'wide' => ts('Wide (250px)'),
389 );
390
391 return $options;
392 }
393
394 /**
395 * Get the fields/elements defined in this form.
396 *
397 * @return array (string)
398 */
399 function getRenderableElementNames() {
400 // The _elements list includes some items which should not be
401 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
402 // items don't have labels. We'll identify renderable by filtering on
403 // the 'label'.
404 $elementNames = array();
405 foreach ($this->_elements as $element) {
406 $label = $element->getLabel();
407 if (!empty($label)) {
408 $elementNames[] = $element->getName();
409 }
410 }
411 return $elementNames;
412 }
413 }