Resolved merge conflict.
[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 if( $('#title').val() != "") {
18 $('#embd_code').parents('.crm-section').show();
19 } else {
20 $('#embd_code').parents('.crm-section').hide();
21 }
22 // $('#embd_code').attr("disabled",true);
23 }
24 $(document).ready(setState)
25 $('#override').click(setState);
26
27 /*
28 $("input[name='_qf_CreateWidget_savenprev']").on('click', function( e ) {
29 e.preventDefault();
30 alert( document.title );
31
32 });*/
33 });
34 </script>
35
36 <?php
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 {
43
44 /**
45 * the widget id saved to the session for an update
46 *
47 * @var int
48 * @access protected
49 */
50 protected $_id;
51
52 function preProcess() {
53 parent::preProcess();
54 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
55 FALSE, NULL, 'REQUEST' );
56
57 $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
58 'text',
59 FALSE,
60 '#2786C2',
61 ),
62 'color_title_bg' => array(ts('Widget title background color'),
63 'text',
64 FALSE,
65 '#FFFFFF',
66 ),
67 'color_bar' => array(ts('Progress Bar Color'),
68 'text',
69 FALSE,
70 '#FFFFFF',
71 ),
72 'color_widget_bg' => array(ts('Widget background color'),
73 'text',
74 FALSE,
75 '#96C0E7',
76 ),
77 'color_description' => array(ts('Widget description color'),
78 'text',
79 FALSE,
80 '#96C0E7',
81 ),
82 'color_border' => array(ts('Widget border color'),
83 'text',
84 FALSE,
85 '#96C0E7',
86 ),
87 'color_button' => array(ts('Widget button text color'),
88 'text',
89 FALSE,
90 '#000000',
91 ),
92 'color_button_bg' => array(ts('Widget button background color'),
93 'text',
94 FALSE,
95 '#96C0E7',
96 ),
97 'color_button_bg' => array(ts('Widget button background color'),
98 'text',
99 FALSE,
100 '#96C0E7',
101 ),
102 );
103 }
104
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
116 function buildQuickForm() {
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'));
121 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
122 $this->add('text', 'button_title', ts('Contribution button title'));
123 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
124 $this->addWysiwyg('description', ts('Description'), '');
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'));
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
139 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
140 $this->addButtons(array(
141 array(
142 'type' => 'submit',
143 'name' => ts('Save'),
144 'isDefault' => TRUE,
145 ),
146 array(
147 'type' => 'next',
148 'name' => ts('Save & Preview'),
149 ),
150 ));
151
152 // $this->add('textarea', 'embd_code', ts('Code to embed:'));
153
154 // export form elements
155 $this->assign('elementNames', $this->getRenderableElementNames());
156
157 if (isset($this->_id)) {
158 /** Updating existing widget*/
159
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
164 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
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']));
210 if(true == $wid_page[$dao->id]['override']) {
211 $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);
212 $this->setDefaults(array(
213 'custom_template' => $cust_templ));
214 } else {
215 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
216 $elem = $this->getElement('custom_template');
217 $elem->setValue($output);
218 }
219 }
220 // $widget_controller_path = getWciWidgetControllerPath();
221 //
222 // $emb_code = "<script src=\"http://code.jquery.com/jquery-1.9.1.min.js\"></script>
223 // <script type=\"text/javascript\" src=\"" . $widget_controller_path . "?widgetId=" . $this->_id . "\"></script>
224 // <script>
225 // $( document ).ready(function() {
226 // $('#widgetwci').html(wciwidgetcode);
227 // });
228 // </script>
229 // <div id='widgetwci'>
230 // </div>";
231 // $this->getElement('embd_code')->setValue($emb_code);
232 }
233 else {
234 /** Keep template in civicrm-wci/templates folder*/
235 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
236 $elem = $this->getElement('custom_template');
237 $elem->setValue($output);
238 }
239 parent::buildQuickForm();
240 }
241
242 function postProcess() {
243 $values = $this->exportValues();
244
245 $override = 0;
246 $cust_tmpl = "";
247 $cust_tmpl_col = "";
248 $sql = "";
249 $coma = "";
250 $equals = "";
251 $quote = "";
252 /** If override check is checked state then only save the custom_template to the
253 database. otherwise wci uses default tpl file.
254 */
255 if(isset($values['override'])){
256 $override = $values['override'];
257 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
258 $cust_tmpl_col = "custom_template";
259 $coma = ",";
260 $equals = " = ";
261 $quote = "'";
262 }
263
264 if (isset($this->_id)) {
265 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
266 . "', logo_image = '" . $values['logo_image'] . "', image = '"
267 . $values['image'] . "', button_title = '" . $values['button_title']
268 . "', button_link_to = '" . $values['button_link_to']
269 . "', progress_bar_id = '" . $values['progress_bar']
270 . "', description = '" . base64_encode($values['description'])
271 . "', email_signup_group_id = '" . $values['email_signup_group_id']
272 . "', size_variant = '" . $values['size_variant']
273 . "', color_title = '" . $values['color_title']
274 . "', color_title_bg = '" . $values['color_title_bg']
275 . "', color_progress_bar = '" . $values['color_bar']
276 . "', color_widget_bg = '" . $values['color_widget_bg']
277 . "', color_description = '" . $values['color_description']
278 . "', color_border = '" . $values['color_border']
279 . "', color_button = '" . $values['color_button']
280 . "', color_button_bg = '" . $values['color_button_bg']
281 . "', style_rules = '" . $values['style_rules'] . "', override = '"
282 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
283 }
284 else {
285 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
286 button_title, button_link_to, progress_bar_id, description,
287 email_signup_group_id, size_variant, color_title, color_title_bg,
288 color_progress_bar, color_widget_bg, color_description, color_border,
289 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
290 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
291 $values['image'] . "','" . $values['button_title'] . "','" .
292 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
293 base64_encode($values['description']) . "','" .
294 $values['email_signup_group_id'] . "','" .
295 $values['size_variant'] . "','" . $values['color_title'] . "','" .
296 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
297 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
298 $values['color_border'] . "','" . $values['color_button'] . "','" .
299 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
300 $override . $quote . $coma . $quote . $cust_tmpl
301 . "')";
302 }
303
304 $errorScope = CRM_Core_TemporaryErrorScope::useException();
305 try {
306 $transaction = new CRM_Core_Transaction();
307 CRM_Core_DAO::executeQuery($sql);
308 $transaction->commit();
309
310 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
311 (isset($this->_id)) ? $widget_id = $this->_id :
312 $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
313 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
314 } else {
315 CRM_Utils_System::redirect('?reset=1');
316 }
317 }
318 catch (Exception $e) {
319 //TODO
320 print_r($e->getMessage());
321 $transaction->rollback();
322 }
323
324 parent::postProcess();
325 }
326
327 function getProgressBars() {
328 $options = array(
329 '' => ts('- select -'),
330 );
331 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
332 foreach ($pbList as $pb) {
333 $options[$pb['id']] = $pb['name'];
334 }
335
336 return $options;
337 }
338 function getContributionPageOptions() {
339 $options = array(
340 '' => ts('- select -'),
341 );
342
343 $result = civicrm_api3('contribution_page', 'get');
344 foreach ($result['values'] as $contribution_page) {
345 $options[$contribution_page['id']] = $contribution_page['title'];
346 }
347
348 return $options;
349 }
350
351 function getGroupOptions() {
352 $options = array(
353 '' => ts('- select -'),
354 );
355
356 $result = civicrm_api3('group', 'get', array(
357 'group_type' => '2'
358 ));
359
360 foreach ($result['values'] as $group) {
361 $options[$group['id']] = $group['title'];
362 }
363
364 return $options;
365 }
366
367 function getSizeOptions() {
368 $options = array(
369 'thin' => ts('Thin'),
370 'normal' => ts('Normal'),
371 'wide' => ts('Wide'),
372 );
373
374 return $options;
375 }
376
377 /**
378 * Get the fields/elements defined in this form.
379 *
380 * @return array (string)
381 */
382 function getRenderableElementNames() {
383 // The _elements list includes some items which should not be
384 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
385 // items don't have labels. We'll identify renderable by filtering on
386 // the 'label'.
387 $elementNames = array();
388 foreach ($this->_elements as $element) {
389 $label = $element->getLabel();
390 if (!empty($label)) {
391 $elementNames[] = $element->getName();
392 }
393 }
394 return $elementNames;
395 }
396 }