bug fixes
[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 if(true == $wid_page[$dao->id]['override']) {
193 $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);
194 $this->setDefaults(array(
195 'custom_template' => $cust_templ));
196 }
197 }
198 }
199 else {
200 /** Keep template in civicrm-wci/templates folder*/
201 $output = file_get_contents('templates/CRM/Wci/Page/Widget.tpl',FILE_USE_INCLUDE_PATH);
202 $elem = $this->getElement('custom_template');
203 $elem->setValue($output);
204 }
205 parent::buildQuickForm();
206 }
207
208 function postProcess() {
209 $values = $this->exportValues();
210
211 $override = 0;
212 $cust_tmpl = "";
213 $cust_tmpl_col = "";
214 $sql = "";
215 $coma = "";
216 $equals = "";
217 $quote = "";
218 /** If override check is checked state then only save the custom_template to the
219 database. otherwise wci uses default tpl file.
220 */
221 if(isset($values['override'])){
222 $override = $values['override'];
223 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
224 $cust_tmpl_col = "custom_template";
225 $coma = ",";
226 $equals = " = ";
227 $quote = "'";
228 }
229 if (isset($this->_id)) {
230 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
231 . "', logo_image = '" . $values['logo_image'] . "', image = '"
232 . $values['image'] . "', button_title = '" . $values['button_title']
233 . "', button_link_to = '" . $values['button_link_to']
234 . "', progress_bar_id = '" . $values['progress_bar']
235 . "', description = '" . base64_encode($values['description'])
236 . "', email_signup_group_id = '" . $values['email_signup_group_id']
237 . "', size_variant = '" . $values['size_variant']
238 . "', color_title = '" . $values['color_title']
239 . "', color_title_bg = '" . $values['color_title_bg']
240 . "', color_progress_bar = '" . $values['color_bar']
241 . "', color_widget_bg = '" . $values['color_widget_bg']
242 . "', color_description = '" . $values['color_description']
243 . "', color_border = '" . $values['color_border']
244 . "', color_button = '" . $values['color_button']
245 . "', color_button_bg = '" . $values['color_button_bg']
246 . "', style_rules = '" . $values['style_rules'] . "', override = '"
247 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
248 }
249 else {
250 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
251 button_title, button_link_to, progress_bar_id, description,
252 email_signup_group_id, size_variant, color_title, color_title_bg,
253 color_progress_bar, color_widget_bg, color_description, color_border,
254 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
255 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
256 $values['image'] . "','" . $values['button_title'] . "','" .
257 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
258 base64_encode($values['description']) . "','" .
259 $values['email_signup_group_id'] . "','" .
260 $values['size_variant'] . "','" . $values['color_title'] . "','" .
261 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
262 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
263 $values['color_border'] . "','" . $values['color_button'] . "','" .
264 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
265 $override . $quote . $coma . $quote . $cust_tmpl
266 . "')";
267 }
268
269 $errorScope = CRM_Core_TemporaryErrorScope::useException();
270 try {
271 $transaction = new CRM_Core_Transaction();
272 CRM_Core_DAO::executeQuery($sql);
273
274 $transaction->commit();
275 }
276 catch (Exception $e) {
277 //TODO
278 print_r($e->getMessage());
279 $transaction->rollback();
280 }
281
282 parent::postProcess();
283 }
284
285 function getProgressBars() {
286 $options = array(
287 '' => ts('- select -'),
288 );
289 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
290 foreach ($pbList as $pb) {
291 $options[$pb['id']] = $pb['name'];
292 }
293
294 return $options;
295 }
296 function getContributionPageOptions() {
297 $options = array(
298 '' => ts('- select -'),
299 );
300
301 $result = civicrm_api3('contribution_page', 'get');
302 foreach ($result['values'] as $contribution_page) {
303 $options[$contribution_page['id']] = $contribution_page['title'];
304 }
305
306 return $options;
307 }
308
309 function getGroupOptions() {
310 $options = array(
311 '' => ts('- select -'),
312 );
313
314 $result = civicrm_api3('group', 'get');
315 foreach ($result['values'] as $group) {
316 $options[$group['id']] = $group['title'];
317 }
318
319 return $options;
320 }
321
322 function getSizeOptions() {
323 $options = array(
324 'thin' => ts('Thin'),
325 'normal' => ts('Normal'),
326 'wide' => ts('Wide'),
327 );
328
329 return $options;
330 }
331
332 /**
333 * Get the fields/elements defined in this form.
334 *
335 * @return array (string)
336 */
337 function getRenderableElementNames() {
338 // The _elements list includes some items which should not be
339 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
340 // items don't have labels. We'll identify renderable by filtering on
341 // the 'label'.
342 $elementNames = array();
343 foreach ($this->_elements as $element) {
344 $label = $element->getLabel();
345 if (!empty($label)) {
346 $elementNames[] = $element->getName();
347 }
348 }
349 return $elementNames;
350 }
351 }