31603 progress bar update not clearing widget cache
[com.zyxware.civiwci.git] / CRM / Wci / Form / ProgressBar.php
CommitLineData
2bf0718d 1<?php
bccdda02
J
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*/
2bf0718d
J
24require_once 'CRM/Core/Form.php';
25require_once 'wci-helper-functions.php';
82ba21f9 26require_once 'CRM/Wci/BAO/ProgressBar.php';
cde51233 27require_once 'CRM/Wci/DAO/ProgressBarFormula.php';
2bf0718d
J
28
29/**
30 * Form controller class
31 *
32 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
33 */
34class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
bccdda02
J
35 private $_id;
36
2bf0718d 37 function preProcess() {
abcb1117 38 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'REQUEST');
74f2803d 39 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'js/addmore.js');
2bf0718d 40 parent::preProcess();
cde51233 41 }
bccdda02 42
10119db1 43 function fillData() {
abcb1117 44 $count = 1;
540f6530 45 if (isset($this->_id)) {
abcb1117 46 /** Updating existing progress bar*/
b3e303a2
J
47 $query = "SELECT * FROM civicrm_wci_progress_bar where id=%1";
48 $params = array(1 => array($this->_id, 'Integer'));
bccdda02 49
e364fc74 50 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
cde51233
J
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 }
bccdda02 62
b3e303a2
J
63 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =%1";
64 $params = array(1 => array($this->_id, 'Integer'));
abcb1117 65
e364fc74 66 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
cde51233 67
cde51233
J
68 while ($dao->fetch()) {
69 $for_page[$dao->id] = array();
70 CRM_Core_DAO::storeValues($dao, $for_page[$dao->id]);
71
abcb1117
J
72 $this->add(
73 'select', // field type
74 'contribution_page_'.$count, // field name
75 'Contribution page', // field label
76 getContributionPageOptions(), // list of options
a8c878ab 77 false // is required
abcb1117
J
78 );
79 $this->add(
80 'text', // field type
81 'percentage_'.$count, // field name
fbe9a7d4 82 'Percentage of contribution taken', // field label
a8c878ab 83 false // is required
abcb1117 84 );
bccdda02 85 //save formula id
abcb1117
J
86 $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id]['id']);
87
cde51233
J
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']));
abcb1117 92
cde51233
J
93 $count++;
94 }
540f6530 95 CRM_Utils_System::setTitle(ts('Edit Progress Bar'));
abcb1117 96 $count--; // because last iteration increments it to the next
bccdda02 97 }
10119db1 98 else {
abcb1117
J
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
fbe9a7d4 110 'Percentage of contribution taken', // field label
abcb1117
J
111 true // is required
112 );
540f6530 113 CRM_Utils_System::setTitle(ts('Create Progress Bar'));
abcb1117 114 }
bccdda02 115
abcb1117 116 $this->addElement('hidden', 'contrib_count', $count);
2bf0718d 117 }
bccdda02 118
2bf0718d 119 function buildQuickForm() {
bccdda02 120
2bf0718d
J
121 $this->add(
122 'text', // field type
123 'progressbar_name', // field name
124 'Name', // field label
125 true // is required
4d4d53e3 126 )->setSize(35);
2bf0718d
J
127 $this->add(
128 'text', // field type
129 'starting_amount', // field name
130 'Starting amount', // field label
131 true // is required
4d4d53e3 132 )->setSize(35);
2bf0718d
J
133 $this->add(
134 'text', // field type
135 'goal_amount', // field name
136 'Goal amount', // field label
137 true // is required
4d4d53e3 138 )->setSize(35);
bccdda02 139
10119db1 140 $this->fillData();
bccdda02 141
c1a7ee1f 142 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add another contribution page');
2bf0718d 143
2bf0718d 144 $this->addButtons(array(
2bf0718d
J
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());
bccdda02 154
2bf0718d
J
155 parent::buildQuickForm();
156 }
157
158 function postProcess() {
abcb1117
J
159 $errorScope = CRM_Core_TemporaryErrorScope::useException();
160 if (isset($this->_id)) {
161 try {
a8c878ab 162 $transaction = new CRM_Core_Transaction();
bccdda02
J
163
164 $sql = "UPDATE civicrm_wci_progress_bar SET name = %1,
b3e303a2 165 starting_amount = %2, goal_amount = %3 where id = %4";
bccdda02
J
166
167 CRM_Core_DAO::executeQuery($sql,
b3e303a2
J
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
a8c878ab 177 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++) {
abcb1117
J
178 $page = 'contribution_page_' . (string)$i;
179 $perc = 'percentage_' . (string)$i;
abcb1117 180
b3e303a2 181 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
bccdda02 182 (contribution_page_id, progress_bar_id, percentage)
b3e303a2
J
183 VALUES (%1, %2, %3)";
184
bccdda02 185 CRM_Core_DAO::executeQuery($sql,
b3e303a2
J
186 array(1 => array($_REQUEST[$page], 'Integer'),
187 2 => array($this->_id, 'Integer'),
188 3 => array($_REQUEST[$perc], 'Float'),
189 ));
a8c878ab 190 }
bccdda02 191
abcb1117 192 $transaction->commit();
e7d8928b 193 CRM_Wci_BAO_WidgetCache::clearCache($this->_id);
8966f337 194 CRM_Core_Session::setStatus(ts('Progress bar created successfuly'), '', 'success');
29da8770 195 CRM_Utils_System::redirect('progress-bar?reset=1');
abcb1117
J
196 }
197 catch (Exception $e) {
8966f337 198 CRM_Core_Session::setStatus(ts('Failed to create progress bar'), '', 'error');
abcb1117
J
199 $transaction->rollback();
200 }
bccdda02
J
201
202 }
10119db1 203 else {
bccdda02 204 $sql = "INSERT INTO civicrm_wci_progress_bar
b3e303a2 205 (name, starting_amount, goal_amount) VALUES (%1, %2, %3)";
cde51233
J
206 try {
207 $transaction = new CRM_Core_Transaction();
bccdda02 208 CRM_Core_DAO::executeQuery($sql,
b3e303a2
J
209 array(1=>array($_REQUEST['progressbar_name'], 'String'),
210 2=>array($_REQUEST['starting_amount'], 'Float'),
bccdda02 211 3=>array($_REQUEST['goal_amount'], 'Float'),
b3e303a2 212 ));
cde51233
J
213 $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
214 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
215 $page = 'contribution_page_' . (string)$i;
216 $perc = 'percentage_' . (string)$i;
217
bccdda02
J
218 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
219 (contribution_page_id, progress_bar_id, percentage)
b3e303a2 220 VALUES (%1, %2, %3)";
bccdda02
J
221
222 CRM_Core_DAO::executeQuery($sql,
b3e303a2
J
223 array(1 => array($_REQUEST[$page], 'Integer'),
224 2 => array($progressbar_id, 'Integer'),
225 3 => array($_REQUEST[$perc], 'Float'),
226 ));
cde51233 227 endfor;
abcb1117 228 $transaction->commit();
9c5af9ef 229 CRM_Utils_System::redirect('civicrm/wci/progress-bar?reset=1');
bccdda02 230 }
cde51233 231 catch (Exception $e) {
bccdda02 232 CRM_Core_Session::setStatus(ts('Failed to create Progress bar. ') .
b3e303a2 233 $e->getMessage(), '', 'error');
cde51233
J
234 $transaction->rollback();
235 }
236 $elem = $this->getElement('contrib_count');
bccdda02 237 $elem->setValue('1');
cde51233 238 }
2bf0718d
J
239 parent::postProcess();
240 }
2bf0718d
J
241
242 /**
243 * Get the fields/elements defined in this form.
244 *
245 * @return array (string)
246 */
247 function getRenderableElementNames() {
248 // The _elements list includes some items which should not be
249 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
250 // items don't have labels. We'll identify renderable by filtering on
251 // the 'label'.
252 $elementNames = array();
253 foreach ($this->_elements as $element) {
254 $label = $element->getLabel();
255 if (!empty($label)) {
256 $elementNames[] = $element->getName();
257 }
258 }
259 return $elementNames;
260 }
261}