09217e0bad9048c16d1dc288fb4c2d19db092b97
[com.zyxware.civiwci.git] / CRM / Wci / Form / ProgressBar.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 require_once 'CRM/Wci/DAO/ProgressBarFormula.php';
7
8 /**
9 * Form controller class
10 *
11 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
12 */
13 class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
14 private $_id;
15 function preProcess() {
16 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'REQUEST');
17 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'addmore.js');
18 parent::preProcess();
19 }
20 function fillData() {
21 $count = 1;
22 if (isset($this->_id)) {
23 /** Updating existing progress bar*/
24 $query = "SELECT * FROM civicrm_wci_progress_bar where id=" . $this->_id;
25 $params = array();
26
27 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
28
29 while ($dao->fetch()) {
30 $con_page[$dao->id] = array();
31 CRM_Core_DAO::storeValues($dao, $con_page[$dao->id]);
32 $this->setDefaults(array(
33 'progressbar_name' => $con_page[$dao->id]['name']));
34 $this->setDefaults(array(
35 'starting_amount' => $con_page[$dao->id]['starting_amount']));
36 $this->setDefaults(array(
37 'goal_amount' => $con_page[$dao->id]['goal_amount']));
38 }
39
40 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $this->_id;
41 $params = array();
42
43 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
44
45 while ($dao->fetch()) {
46 $for_page[$dao->id] = array();
47 CRM_Core_DAO::storeValues($dao, $for_page[$dao->id]);
48
49 $this->add(
50 'select', // field type
51 'contribution_page_'.$count, // field name
52 'Contribution page', // field label
53 getContributionPageOptions(), // list of options
54 false // is required
55 );
56 $this->add(
57 'text', // field type
58 'percentage_'.$count, // field name
59 'Percentage', // field label
60 false // is required
61 );
62 //save formula id
63 $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id]['id']);
64
65 $this->setDefaults(array(
66 'contribution_page_'.$count => $for_page[$dao->id]['contribution_page_id']));
67 $this->setDefaults(array(
68 'percentage_'.$count => $for_page[$dao->id]['percentage']));
69
70 $count++;
71 }
72 $count--; // because last iteration increments it to the next
73 }
74 else {
75 /** New progress bar*/
76 $this->add(
77 'select', // field type
78 'contribution_page_1', // field name
79 'Contribution page', // field label
80 getContributionPageOptions(), // list of options
81 true // is required
82 );
83 $this->add(
84 'text', // field type
85 'percentage_1', // field name
86 'Percentage', // field label
87 true // is required
88 );
89 }
90
91 $this->addElement('hidden', 'contrib_count', $count);
92 }
93
94 function buildQuickForm() {
95
96 $this->add(
97 'text', // field type
98 'progressbar_name', // field name
99 'Name', // field label
100 true // is required
101 );
102 $this->add(
103 'text', // field type
104 'starting_amount', // field name
105 'Starting amount', // field label
106 true // is required
107 );
108 $this->add(
109 'text', // field type
110 'goal_amount', // field name
111 'Goal amount', // field label
112 true // is required
113 );
114
115 $this->fillData();
116
117 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add more');
118
119 $this->addButtons(array(
120 array(
121 'type' => 'submit',
122 'name' => ts('Save'),
123 'isDefault' => TRUE,
124 ),
125 ));
126
127 // export form elements
128 $this->assign('elementNames', $this->getRenderableElementNames());
129
130 parent::buildQuickForm();
131 }
132
133 function postProcess() {
134 $errorScope = CRM_Core_TemporaryErrorScope::useException();
135 if (isset($this->_id)) {
136 try {
137 $transaction = new CRM_Core_Transaction();
138
139 $sql = "UPDATE civicrm_wci_progress_bar SET name = '". $_REQUEST['progressbar_name'] .
140 "', starting_amount = '" . $_REQUEST['starting_amount'] .
141 "', goal_amount = '" . $_REQUEST['goal_amount'] .
142 "' where id =".$this->_id;
143
144 CRM_Core_DAO::executeQuery($sql);
145 /** Delete existiing formula fields and add fields fresh*/
146 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id=' . $this->_id);
147
148 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++) {
149 $page = 'contribution_page_' . (string)$i;
150 $perc = 'percentage_' . (string)$i;
151 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
152 VALUES ('" . $_REQUEST[$page] . "','" . $this->_id . "','" . $_REQUEST[$perc] . "')";
153
154 CRM_Core_DAO::executeQuery($sql);
155 }
156
157 $transaction->commit();
158
159 CRM_Utils_System::redirect('progress-bar?reset=1');
160 }
161 catch (Exception $e) {
162 //TODO
163 print_r($e->getMessage());
164 $transaction->rollback();
165 }
166
167 }
168 else {
169 $sql = "INSERT INTO civicrm_wci_progress_bar (name, starting_amount, goal_amount)
170 VALUES ('" . $_REQUEST['progressbar_name'] . "','" . $_REQUEST['starting_amount'] . "','" . $_REQUEST['goal_amount'] . "')";
171 try {
172 $transaction = new CRM_Core_Transaction();
173 CRM_Core_DAO::executeQuery($sql);
174 $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
175 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
176 $page = 'contribution_page_' . (string)$i;
177 $perc = 'percentage_' . (string)$i;
178
179 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
180 VALUES ('" . $_REQUEST[$page] . "','" . $progressbar_id . "','" . $_REQUEST[$perc] . "')";
181
182 CRM_Core_DAO::executeQuery($sql);
183 endfor;
184 $transaction->commit();
185 CRM_Utils_System::redirect('civicrm/wci/progress-bar?reset=1');
186 }
187 catch (Exception $e) {
188 //TODO
189 print_r($e->getMessage());
190 $transaction->rollback();
191 }
192 $elem = $this->getElement('contrib_count');
193 $elem->setValue('1');
194 }
195 parent::postProcess();
196 }
197
198 /**
199 * Get the fields/elements defined in this form.
200 *
201 * @return array (string)
202 */
203 function getRenderableElementNames() {
204 // The _elements list includes some items which should not be
205 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
206 // items don't have labels. We'll identify renderable by filtering on
207 // the 'label'.
208 $elementNames = array();
209 foreach ($this->_elements as $element) {
210 $label = $element->getLabel();
211 if (!empty($label)) {
212 $elementNames[] = $element->getName();
213 }
214 }
215 return $elementNames;
216 }
217 }
218