moved .js files to js folder
[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');
74f2803d 17 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'js/addmore.js');
2bf0718d 18 parent::preProcess();
cde51233 19 }
10119db1 20 function fillData() {
abcb1117 21 $count = 1;
540f6530 22 if (isset($this->_id)) {
abcb1117 23 /** Updating existing progress bar*/
b3e303a2
J
24 $query = "SELECT * FROM civicrm_wci_progress_bar where id=%1";
25 $params = array(1 => array($this->_id, 'Integer'));
cde51233 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
b3e303a2
J
40 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =%1";
41 $params = array(1 => array($this->_id, 'Integer'));
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
a8c878ab 54 false // is required
abcb1117
J
55 );
56 $this->add(
57 'text', // field type
58 'percentage_'.$count, // field name
fbe9a7d4 59 'Percentage of contribution taken', // field label
a8c878ab 60 false // is required
abcb1117
J
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 }
540f6530 72 CRM_Utils_System::setTitle(ts('Edit Progress Bar'));
abcb1117 73 $count--; // because last iteration increments it to the next
10119db1
J
74 }
75 else {
abcb1117
J
76 /** New progress bar*/
77 $this->add(
78 'select', // field type
79 'contribution_page_1', // field name
80 'Contribution page', // field label
81 getContributionPageOptions(), // list of options
82 true // is required
83 );
84 $this->add(
85 'text', // field type
86 'percentage_1', // field name
fbe9a7d4 87 'Percentage of contribution taken', // field label
abcb1117
J
88 true // is required
89 );
540f6530 90 CRM_Utils_System::setTitle(ts('Create Progress Bar'));
abcb1117
J
91 }
92
93 $this->addElement('hidden', 'contrib_count', $count);
2bf0718d 94 }
cde51233 95
2bf0718d 96 function buildQuickForm() {
cde51233 97
2bf0718d
J
98 $this->add(
99 'text', // field type
100 'progressbar_name', // field name
101 'Name', // field label
102 true // is required
4d4d53e3 103 )->setSize(35);
2bf0718d
J
104 $this->add(
105 'text', // field type
106 'starting_amount', // field name
107 'Starting amount', // field label
108 true // is required
4d4d53e3 109 )->setSize(35);
2bf0718d
J
110 $this->add(
111 'text', // field type
112 'goal_amount', // field name
113 'Goal amount', // field label
114 true // is required
4d4d53e3 115 )->setSize(35);
2bf0718d 116
10119db1 117 $this->fillData();
cde51233 118
c1a7ee1f 119 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add another contribution page');
2bf0718d 120
2bf0718d 121 $this->addButtons(array(
2bf0718d
J
122 array(
123 'type' => 'submit',
124 'name' => ts('Save'),
125 'isDefault' => TRUE,
126 ),
127 ));
128
129 // export form elements
130 $this->assign('elementNames', $this->getRenderableElementNames());
540f6530 131
2bf0718d
J
132 parent::buildQuickForm();
133 }
134
135 function postProcess() {
abcb1117
J
136 $errorScope = CRM_Core_TemporaryErrorScope::useException();
137 if (isset($this->_id)) {
138 try {
a8c878ab
J
139 $transaction = new CRM_Core_Transaction();
140
b3e303a2
J
141 $sql = "UPDATE civicrm_wci_progress_bar SET name = %1,
142 starting_amount = %2, goal_amount = %3 where id = %4";
a8c878ab 143
b3e303a2
J
144 CRM_Core_DAO::executeQuery($sql,
145 array(1=>array($_REQUEST['progressbar_name'], 'String'),
146 2=>array($_REQUEST['starting_amount'], 'Float'),
147 3=>array($_REQUEST['goal_amount'], 'Float'),
148 4=>array($this->_id, 'Integer'),
149 ));
150 /** Delete existiing formula fields and add fields fresh*/
151 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_wci_progress_bar_formula
152 WHERE progress_bar_id=%1', array(1 => array($this->_id, 'Integer')));
153
a8c878ab 154 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++) {
abcb1117
J
155 $page = 'contribution_page_' . (string)$i;
156 $perc = 'percentage_' . (string)$i;
abcb1117 157
b3e303a2
J
158 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
159 (contribution_page_id, progress_bar_id, percentage)
160 VALUES (%1, %2, %3)";
161
162 CRM_Core_DAO::executeQuery($sql,
163 array(1 => array($_REQUEST[$page], 'Integer'),
164 2 => array($this->_id, 'Integer'),
165 3 => array($_REQUEST[$perc], 'Float'),
166 ));
a8c878ab
J
167 }
168
abcb1117 169 $transaction->commit();
8966f337 170 CRM_Core_Session::setStatus(ts('Progress bar created successfuly'), '', 'success');
29da8770 171 CRM_Utils_System::redirect('progress-bar?reset=1');
abcb1117
J
172 }
173 catch (Exception $e) {
8966f337 174 CRM_Core_Session::setStatus(ts('Failed to create progress bar'), '', 'error');
abcb1117
J
175 $transaction->rollback();
176 }
cde51233 177
10119db1
J
178 }
179 else {
b3e303a2
J
180 $sql = "INSERT INTO civicrm_wci_progress_bar
181 (name, starting_amount, goal_amount) VALUES (%1, %2, %3)";
cde51233
J
182 try {
183 $transaction = new CRM_Core_Transaction();
b3e303a2
J
184 CRM_Core_DAO::executeQuery($sql,
185 array(1=>array($_REQUEST['progressbar_name'], 'String'),
186 2=>array($_REQUEST['starting_amount'], 'Float'),
187 3=>array($_REQUEST['goal_amount'], 'Float'),
188 ));
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
b3e303a2
J
194 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
195 (contribution_page_id, progress_bar_id, percentage)
196 VALUES (%1, %2, %3)";
cde51233 197
b3e303a2
J
198 CRM_Core_DAO::executeQuery($sql,
199 array(1 => array($_REQUEST[$page], 'Integer'),
200 2 => array($progressbar_id, 'Integer'),
201 3 => array($_REQUEST[$perc], 'Float'),
202 ));
cde51233 203 endfor;
abcb1117 204 $transaction->commit();
9c5af9ef 205 CRM_Utils_System::redirect('civicrm/wci/progress-bar?reset=1');
cde51233
J
206 }
207 catch (Exception $e) {
b3e303a2
J
208 CRM_Core_Session::setStatus(ts('Failed to create Progress bar. ') .
209 $e->getMessage(), '', 'error');
cde51233
J
210 $transaction->rollback();
211 }
212 $elem = $this->getElement('contrib_count');
213 $elem->setValue('1');
214 }
2bf0718d
J
215 parent::postProcess();
216 }
2bf0718d
J
217
218 /**
219 * Get the fields/elements defined in this form.
220 *
221 * @return array (string)
222 */
223 function getRenderableElementNames() {
224 // The _elements list includes some items which should not be
225 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
226 // items don't have labels. We'll identify renderable by filtering on
227 // the 'label'.
228 $elementNames = array();
229 foreach ($this->_elements as $element) {
230 $label = $element->getLabel();
231 if (!empty($label)) {
232 $elementNames[] = $element->getName();
233 }
234 }
235 return $elementNames;
236 }
237}
238