3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
8 <script type
="text/javascript">
11 if ($
('#override').is(':checked') == true) {
12 $
('#custom_template').attr("disabled",false);
15 $
('#custom_template').attr("disabled",true);
17 if( $
('#title').val() != "") {
18 $
('#embd_code').parents('.crm-section').show();
20 $
('#embd_code').parents('.crm-section').hide();
22 // $('#embd_code').attr("disabled",true);
24 $
(document
).ready(setState
)
25 $
('#override').click(setState
);
28 $("input[name='_qf_CreateWidget_savenprev']").on('click', function( e ) {
30 alert( document.title );
38 * Form controller class
40 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
42 class CRM_Wci_Form_CreateWidget
extends CRM_Core_Form
{
45 * the widget id saved to the session for an update
52 function preProcess() {
54 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this,
55 FALSE, NULL, 'REQUEST' );
57 $this->_colorFields
= array('color_title' => array(ts('Title Text Color'),
62 'color_title_bg' => array(ts('Widget title background color'),
67 'color_bar' => array(ts('Progress Bar Color'),
72 'color_widget_bg' => array(ts('Widget background color'),
77 'color_description' => array(ts('Widget description color'),
82 'color_border' => array(ts('Widget border color'),
87 'color_button' => array(ts('Widget button text color'),
92 'color_button_bg' => array(ts('Widget button background color'),
97 'color_button_bg' => array(ts('Widget button background color'),
105 function setDefaultValues() {
108 $defaults['size_variant'] = 'normal';
109 foreach ($this->_colorFields
as $name => $val) {
110 $defaults[$name] = $val[3];
116 function buildQuickForm() {
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) {
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>'));
139 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
140 $this->addButtons(array(
143 'name' => ts('Save'),
148 'name' => ts('Save & Preview'),
152 // $this->add('textarea', 'embd_code', ts('Code to embed:'));
154 // export form elements
155 $this->assign('elementNames', $this->getRenderableElementNames());
157 if (isset($this->_id
)) {
158 /** Updating existing widget*/
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
;
164 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
166 while ($dao->fetch()) {
167 $wid_page[$dao->id
] = array();
168 CRM_Core_DAO
::storeValues($dao, $wid_page[$dao->id
]);
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']));
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));
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);
220 // $widget_controller_path = getWciWidgetControllerPath();
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>
225 // $( document ).ready(function() {
226 // $('#widgetwci').html(wciwidgetcode);
229 // <div id='widgetwci'>
231 // $this->getElement('embd_code')->setValue($emb_code);
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);
239 parent
::buildQuickForm();
242 function postProcess() {
243 $values = $this->exportValues();
252 /** If override check is checked state then only save the custom_template to the
253 database. otherwise wci uses default tpl file.
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";
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
;
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
304 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
306 $transaction = new CRM_Core_Transaction();
307 CRM_Core_DAO
::executeQuery($sql);
308 $transaction->commit();
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);
315 CRM_Utils_System
::redirect('?reset=1');
318 catch (Exception
$e) {
320 print_r($e->getMessage());
321 $transaction->rollback();
324 parent
::postProcess();
327 function getProgressBars() {
329 '' => ts('- select -'),
331 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
332 foreach ($pbList as $pb) {
333 $options[$pb['id']] = $pb['name'];
338 function getContributionPageOptions() {
340 '' => ts('- select -'),
343 $result = civicrm_api3('contribution_page', 'get');
344 foreach ($result['values'] as $contribution_page) {
345 $options[$contribution_page['id']] = $contribution_page['title'];
351 function getGroupOptions() {
353 '' => ts('- select -'),
356 $result = civicrm_api3('group', 'get', array(
360 foreach ($result['values'] as $group) {
361 $options[$group['id']] = $group['title'];
367 function getSizeOptions() {
369 'thin' => ts('Thin'),
370 'normal' => ts('Normal'),
371 'wide' => ts('Wide'),
378 * Get the fields/elements defined in this form.
380 * @return array (string)
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
387 $elementNames = array();
388 foreach ($this->_elements
as $element) {
389 $label = $element->getLabel();
390 if (!empty($label)) {
391 $elementNames[] = $element->getName();
394 return $elementNames;