Releasenotes added
[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';
cde51233 6require_once 'CRM/Wci/DAO/ProgressBarFormula.php';
2bf0718d
J
7
8/**
9 * Form controller class
10 *
11 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
12 */
13class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
cde51233 14 private $_id;
2bf0718d 15 function preProcess() {
abcb1117 16 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'REQUEST');
c68d158b 17 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'addmore.js');
2bf0718d 18 parent::preProcess();
cde51233 19 }
10119db1 20 function fillData() {
abcb1117
J
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;
cde51233
J
25 $params = array();
26
e364fc74 27 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
cde51233
J
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
abcb1117 40 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $this->_id;
cde51233 41 $params = array();
abcb1117 42
e364fc74 43 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
cde51233 44
cde51233
J
45 while ($dao->fetch()) {
46 $for_page[$dao->id] = array();
47 CRM_Core_DAO::storeValues($dao, $for_page[$dao->id]);
48
abcb1117
J
49 $this->add(
50 'select', // field type
51 'contribution_page_'.$count, // field name
52 'Contribution page', // field label
53 getContributionPageOptions(), // list of options
54 true // is required
55 );
56 $this->add(
57 'text', // field type
58 'percentage_'.$count, // field name
59 'Percentage', // field label
60 true // is required
61 );
62 //save formula id
63 $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id]['id']);
64
cde51233
J
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']));
abcb1117 69
cde51233
J
70 $count++;
71 }
abcb1117 72 $count--; // because last iteration increments it to the next
10119db1
J
73 }
74 else {
abcb1117
J
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);
2bf0718d 92 }
cde51233 93
2bf0718d 94 function buildQuickForm() {
cde51233 95
2bf0718d
J
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 );
abcb1117 114/* $this->add(
2bf0718d
J
115 'select', // field type
116 'contribution_page_1', // field name
117 'Contribution page', // field label
118 getContributionPageOptions(), // list of options
119 true // is required
120 );
121 $this->add(
122 'text', // field type
123 'percentage_1', // field name
fe572ea7 124 'Percentage', // field label
2bf0718d 125 true // is required
abcb1117 126 );*/
2bf0718d 127
10119db1 128 $this->fillData();
cde51233 129
2bf0718d 130 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add more');
2bf0718d 131
2bf0718d 132 $this->addButtons(array(
2bf0718d
J
133 array(
134 'type' => 'submit',
135 'name' => ts('Save'),
136 'isDefault' => TRUE,
137 ),
138 ));
139
140 // export form elements
141 $this->assign('elementNames', $this->getRenderableElementNames());
142
143 parent::buildQuickForm();
144 }
145
146 function postProcess() {
abcb1117
J
147 $errorScope = CRM_Core_TemporaryErrorScope::useException();
148 if (isset($this->_id)) {
149 try {
150 $sql = "UPDATE civicrm_wci_progress_bar SET name = '". $_REQUEST['progressbar_name'] .
151 "', starting_amount = '" . $_REQUEST['starting_amount'] .
152 "', goal_amount = '" . $_REQUEST['goal_amount'] .
153 "' where id =".$this->_id;
154
155 $transaction = new CRM_Core_Transaction();
156 CRM_Core_DAO::executeQuery($sql);
157
158 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
159 $page = 'contribution_page_' . (string)$i;
160 $perc = 'percentage_' . (string)$i;
161 if (isset($_REQUEST['contrib_elem_'.$i])) {
162 $sql = "UPDATE civicrm_wci_progress_bar_formula SET contribution_page_id = '". $_REQUEST[$page] . "',
163 percentage = '". $_REQUEST[$perc] . "'
164 WHERE id = " . (int)$_REQUEST['contrib_elem_'.$i];
10119db1
J
165 }
166 else {
abcb1117
J
167 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
168 VALUES ('" . $_REQUEST[$page] . "','" . $this->_id . "','" . $_REQUEST[$perc] . "')";
169 }
170
171 CRM_Core_DAO::executeQuery($sql);
172 endfor;
173 $transaction->commit();
9c5af9ef 174 CRM_Utils_System::redirect('civicrm/wci/progress-bar?reset=1');
abcb1117
J
175 }
176 catch (Exception $e) {
177 //TODO
178 print_r($e->getMessage());
179 $transaction->rollback();
180 }
cde51233 181
10119db1
J
182 }
183 else {
cde51233
J
184 $sql = "INSERT INTO civicrm_wci_progress_bar (name, starting_amount, goal_amount)
185 VALUES ('" . $_REQUEST['progressbar_name'] . "','" . $_REQUEST['starting_amount'] . "','" . $_REQUEST['goal_amount'] . "')";
cde51233
J
186 try {
187 $transaction = new CRM_Core_Transaction();
e5812687 188 CRM_Core_DAO::executeQuery($sql);
cde51233
J
189 $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
190 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
191 $page = 'contribution_page_' . (string)$i;
192 $perc = 'percentage_' . (string)$i;
193
194 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
195 VALUES ('" . $_REQUEST[$page] . "','" . $progressbar_id . "','" . $_REQUEST[$perc] . "')";
196
197 CRM_Core_DAO::executeQuery($sql);
198 endfor;
abcb1117 199 $transaction->commit();
9c5af9ef 200 CRM_Utils_System::redirect('civicrm/wci/progress-bar?reset=1');
cde51233
J
201 }
202 catch (Exception $e) {
203 //TODO
204 print_r($e->getMessage());
205 $transaction->rollback();
206 }
207 $elem = $this->getElement('contrib_count');
208 $elem->setValue('1');
209 }
2bf0718d
J
210 parent::postProcess();
211 }
2bf0718d
J
212
213 /**
214 * Get the fields/elements defined in this form.
215 *
216 * @return array (string)
217 */
218 function getRenderableElementNames() {
219 // The _elements list includes some items which should not be
220 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
221 // items don't have labels. We'll identify renderable by filtering on
222 // the 'label'.
223 $elementNames = array();
224 foreach ($this->_elements as $element) {
225 $label = $element->getLabel();
226 if (!empty($label)) {
227 $elementNames[] = $element->getName();
228 }
229 }
230 return $elementNames;
231 }
232}
233