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