#29750 progressbar databse save
[com.zyxware.civiwci.git] / CRM / Wci / Form / ProgressBar.php
CommitLineData
2bf0718d
J
1<?php
2
3require_once 'CRM/Core/Form.php';
4require_once 'wci-helper-functions.php';
82ba21f9 5require_once 'CRM/Wci/BAO/ProgressBar.php';
2bf0718d
J
6
7/**
8 * Form controller class
9 *
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
11 */
12class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
13
14 function preProcess() {
82ba21f9 15
2bf0718d
J
16 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'addmore.js');
17 parent::preProcess();
18 }
19 function buildQuickForm() {
20 $this->add(
21 'text', // field type
22 'progressbar_name', // field name
23 'Name', // field label
24 true // is required
25 );
26 $this->add(
27 'text', // field type
28 'starting_amount', // field name
29 'Starting amount', // field label
30 true // is required
31 );
32 $this->add(
33 'text', // field type
34 'goal_amount', // field name
35 'Goal amount', // field label
36 true // is required
37 );
38 $this->add(
39 'select', // field type
40 'contribution_page_1', // field name
41 'Contribution page', // field label
42 getContributionPageOptions(), // list of options
43 true // is required
44 );
45 $this->add(
46 'text', // field type
47 'percentage_1', // field name
fe572ea7 48 'Percentage', // field label
2bf0718d
J
49 true // is required
50 );
2bf0718d
J
51
52 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add more');
2bf0718d
J
53
54 $this->addElement('hidden', 'contrib_count', '1');
2bf0718d
J
55
56 $this->addButtons(array(
2bf0718d
J
57 array(
58 'type' => 'submit',
59 'name' => ts('Save'),
60 'isDefault' => TRUE,
61 ),
62 ));
63
64 // export form elements
65 $this->assign('elementNames', $this->getRenderableElementNames());
66
67 parent::buildQuickForm();
68 }
69
70 function postProcess() {
82ba21f9
J
71
72 $sql = "INSERT INTO civicrm_wci_progress_bar (name, starting_amount, goal_amount)
73 VALUES ('" . $_REQUEST['progressbar_name'] . "','" . $_REQUEST['starting_amount'] . "','" . $_REQUEST['goal_amount'] . "')";
ae309324 74 $errorScope = CRM_Core_TemporaryErrorScope::useException();
e5812687 75 try {
ae309324
J
76 $transaction = new CRM_Core_Transaction();
77 CRM_Core_DAO::executeQuery($sql);
e5812687
J
78 $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
79 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
80 $page = 'contribution_page_' . (string)$i;
81 $perc = 'percentage_' . (string)$i;
82
83 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
84 VALUES ('" . $_REQUEST[$page] . "','" . $progressbar_id . "','" . $_REQUEST[$perc] . "')";
85
86 CRM_Core_DAO::executeQuery($sql);
87 endfor;
88 }
89 catch (Exception $e) {
90 //TODO
ae309324
J
91 print_r($e->getMessage());
92 $transaction->rollback();
e5812687 93 }
2bf0718d 94 parent::postProcess();
e5812687
J
95 $elem = $this->getElement('contrib_count');
96 $elem->setValue('1');
2bf0718d 97 }
2bf0718d
J
98
99 /**
100 * Get the fields/elements defined in this form.
101 *
102 * @return array (string)
103 */
104 function getRenderableElementNames() {
105 // The _elements list includes some items which should not be
106 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
107 // items don't have labels. We'll identify renderable by filtering on
108 // the 'label'.
109 $elementNames = array();
110 foreach ($this->_elements as $element) {
111 $label = $element->getLabel();
112 if (!empty($label)) {
113 $elementNames[] = $element->getName();
114 }
115 }
116 return $elementNames;
117 }
118}
119