31453 31498 code cleanup
[com.zyxware.civiwci.git] / CRM / Wci / Form / ProgressBar.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM Widget Creation Interface (WCI) Version 1.0 |
5 +--------------------------------------------------------------------+
6 | Copyright Zyxware Technologies (c) 2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM WCI. |
9 | |
10 | CiviCRM WCI is free software; you can copy, modify, and distribute |
11 | it under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
13 | |
14 | CiviCRM WCI is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact Zyxware |
21 | Technologies at info[AT]zyxware[DOT]com. |
22 +--------------------------------------------------------------------+
23 */
24 require_once 'CRM/Core/Form.php';
25 require_once 'wci-helper-functions.php';
26 require_once 'CRM/Wci/BAO/ProgressBar.php';
27 require_once 'CRM/Wci/DAO/ProgressBarFormula.php';
28
29 /**
30 * Form controller class
31 *
32 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
33 */
34 class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
35 private $_id;
36
37 function preProcess() {
38 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'REQUEST');
39 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'js/addmore.js');
40 parent::preProcess();
41 }
42
43 function fillData() {
44 $count = 1;
45 if (isset($this->_id)) {
46 /** Updating existing progress bar*/
47 $query = "SELECT * FROM civicrm_wci_progress_bar where id=%1";
48 $params = array(1 => array($this->_id, 'Integer'));
49
50 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
51
52 while ($dao->fetch()) {
53 $con_page[$dao->id] = array();
54 CRM_Core_DAO::storeValues($dao, $con_page[$dao->id]);
55 $this->setDefaults(array(
56 'progressbar_name' => $con_page[$dao->id]['name']));
57 $this->setDefaults(array(
58 'starting_amount' => $con_page[$dao->id]['starting_amount']));
59 $this->setDefaults(array(
60 'goal_amount' => $con_page[$dao->id]['goal_amount']));
61 }
62
63 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =%1";
64 $params = array(1 => array($this->_id, 'Integer'));
65
66 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
67
68 while ($dao->fetch()) {
69 $for_page[$dao->id] = array();
70 CRM_Core_DAO::storeValues($dao, $for_page[$dao->id]);
71
72 $this->add(
73 'select', // field type
74 'contribution_page_'.$count, // field name
75 'Contribution page', // field label
76 getContributionPageOptions(), // list of options
77 false // is required
78 );
79 $this->add(
80 'text', // field type
81 'percentage_'.$count, // field name
82 'Percentage of contribution taken', // field label
83 false // is required
84 );
85 //save formula id
86 $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id]['id']);
87
88 $this->setDefaults(array(
89 'contribution_page_'.$count => $for_page[$dao->id]['contribution_page_id']));
90 $this->setDefaults(array(
91 'percentage_'.$count => $for_page[$dao->id]['percentage']));
92
93 $count++;
94 }
95 CRM_Utils_System::setTitle(ts('Edit Progress Bar'));
96 $count--; // because last iteration increments it to the next
97 }
98 else {
99 /** New progress bar*/
100 $this->add(
101 'select', // field type
102 'contribution_page_1', // field name
103 'Contribution page', // field label
104 getContributionPageOptions(), // list of options
105 true // is required
106 );
107 $this->add(
108 'text', // field type
109 'percentage_1', // field name
110 'Percentage of contribution taken', // field label
111 true // is required
112 );
113 CRM_Utils_System::setTitle(ts('Create Progress Bar'));
114 }
115
116 $this->addElement('hidden', 'contrib_count', $count);
117 }
118
119 function buildQuickForm() {
120
121 $this->add(
122 'text', // field type
123 'progressbar_name', // field name
124 'Name', // field label
125 true // is required
126 )->setSize(35);
127 $this->add(
128 'text', // field type
129 'starting_amount', // field name
130 'Starting amount', // field label
131 true // is required
132 )->setSize(35);
133 $this->add(
134 'text', // field type
135 'goal_amount', // field name
136 'Goal amount', // field label
137 true // is required
138 )->setSize(35);
139
140 $this->fillData();
141
142 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add another contribution page');
143
144 $this->addButtons(array(
145 array(
146 'type' => 'submit',
147 'name' => ts('Save'),
148 'isDefault' => TRUE,
149 ),
150 ));
151
152 // export form elements
153 $this->assign('elementNames', $this->getRenderableElementNames());
154
155 parent::buildQuickForm();
156 }
157
158 function postProcess() {
159 $errorScope = CRM_Core_TemporaryErrorScope::useException();
160 if (isset($this->_id)) {
161 try {
162 $transaction = new CRM_Core_Transaction();
163
164 $sql = "UPDATE civicrm_wci_progress_bar SET name = %1,
165 starting_amount = %2, goal_amount = %3 where id = %4";
166
167 CRM_Core_DAO::executeQuery($sql,
168 array(1=>array($_REQUEST['progressbar_name'], 'String'),
169 2=>array($_REQUEST['starting_amount'], 'Float'),
170 3=>array($_REQUEST['goal_amount'], 'Float'),
171 4=>array($this->_id, 'Integer'),
172 ));
173 /** Delete existiing formula fields and add fields fresh*/
174 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_wci_progress_bar_formula
175 WHERE progress_bar_id=%1', array(1 => array($this->_id, 'Integer')));
176
177 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++) {
178 $page = 'contribution_page_' . (string)$i;
179 $perc = 'percentage_' . (string)$i;
180
181 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
182 (contribution_page_id, progress_bar_id, percentage)
183 VALUES (%1, %2, %3)";
184
185 CRM_Core_DAO::executeQuery($sql,
186 array(1 => array($_REQUEST[$page], 'Integer'),
187 2 => array($this->_id, 'Integer'),
188 3 => array($_REQUEST[$perc], 'Float'),
189 ));
190 }
191
192 $transaction->commit();
193 CRM_Core_Session::setStatus(ts('Progress bar created successfuly'), '', 'success');
194 CRM_Utils_System::redirect('progress-bar?reset=1');
195 }
196 catch (Exception $e) {
197 CRM_Core_Session::setStatus(ts('Failed to create progress bar'), '', 'error');
198 $transaction->rollback();
199 }
200
201 }
202 else {
203 $sql = "INSERT INTO civicrm_wci_progress_bar
204 (name, starting_amount, goal_amount) VALUES (%1, %2, %3)";
205 try {
206 $transaction = new CRM_Core_Transaction();
207 CRM_Core_DAO::executeQuery($sql,
208 array(1=>array($_REQUEST['progressbar_name'], 'String'),
209 2=>array($_REQUEST['starting_amount'], 'Float'),
210 3=>array($_REQUEST['goal_amount'], 'Float'),
211 ));
212 $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
213 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
214 $page = 'contribution_page_' . (string)$i;
215 $perc = 'percentage_' . (string)$i;
216
217 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
218 (contribution_page_id, progress_bar_id, percentage)
219 VALUES (%1, %2, %3)";
220
221 CRM_Core_DAO::executeQuery($sql,
222 array(1 => array($_REQUEST[$page], 'Integer'),
223 2 => array($progressbar_id, 'Integer'),
224 3 => array($_REQUEST[$perc], 'Float'),
225 ));
226 endfor;
227 $transaction->commit();
228 CRM_Utils_System::redirect('civicrm/wci/progress-bar?reset=1');
229 }
230 catch (Exception $e) {
231 CRM_Core_Session::setStatus(ts('Failed to create Progress bar. ') .
232 $e->getMessage(), '', 'error');
233 $transaction->rollback();
234 }
235 $elem = $this->getElement('contrib_count');
236 $elem->setValue('1');
237 }
238 parent::postProcess();
239 }
240
241 /**
242 * Get the fields/elements defined in this form.
243 *
244 * @return array (string)
245 */
246 function getRenderableElementNames() {
247 // The _elements list includes some items which should not be
248 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
249 // items don't have labels. We'll identify renderable by filtering on
250 // the 'label'.
251 $elementNames = array();
252 foreach ($this->_elements as $element) {
253 $label = $element->getLabel();
254 if (!empty($label)) {
255 $elementNames[] = $element->getName();
256 }
257 }
258 return $elementNames;
259 }
260 }