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. |
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. |
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. |
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 +--------------------------------------------------------------------+
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';
30 * Form controller class
32 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
34 class CRM_Wci_Form_ProgressBar
extends CRM_Core_Form
{
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');
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'));
50 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
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']));
63 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =%1";
64 $params = array(1 => array($this->_id
, 'Integer'));
66 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
68 while ($dao->fetch()) {
69 $for_page[$dao->id
] = array();
70 CRM_Core_DAO
::storeValues($dao, $for_page[$dao->id
]);
73 'select', // field type
74 'contribution_page_'.$count, // field name
75 'Contribution page', // field label
76 getContributionPageOptions(), // list of options
81 'percentage_'.$count, // field name
82 'Percentage of contribution taken', // field label
86 $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id
]['id']);
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']));
95 CRM_Utils_System
::setTitle(ts('Edit Progress Bar'));
96 $count--; // because last iteration increments it to the next
99 /** New progress bar*/
101 'select', // field type
102 'contribution_page_1', // field name
103 'Contribution page', // field label
104 getContributionPageOptions(), // list of options
108 'text', // field type
109 'percentage_1', // field name
110 'Percentage of contribution taken', // field label
113 CRM_Utils_System
::setTitle(ts('Create Progress Bar'));
116 $this->addElement('hidden', 'contrib_count', $count);
119 function buildQuickForm() {
122 'text', // field type
123 'progressbar_name', // field name
124 'Name', // field label
128 'text', // field type
129 'starting_amount', // field name
130 'Starting amount', // field label
134 'text', // field type
135 'goal_amount', // field name
136 'Goal amount', // field label
142 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add another contribution page');
144 $this->addButtons(array(
147 'name' => ts('Save'),
152 // export form elements
153 $this->assign('elementNames', $this->getRenderableElementNames());
155 parent
::buildQuickForm();
158 function postProcess() {
159 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
160 if (isset($this->_id
)) {
162 $transaction = new CRM_Core_Transaction();
164 $sql = "UPDATE civicrm_wci_progress_bar SET name = %1,
165 starting_amount = %2, goal_amount = %3 where id = %4";
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'),
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')));
177 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++
) {
178 $page = 'contribution_page_' . (string)$i;
179 $perc = 'percentage_' . (string)$i;
181 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
182 (contribution_page_id, progress_bar_id, percentage)
183 VALUES (%1, %2, %3)";
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'),
192 $transaction->commit();
193 CRM_Core_Session
::setStatus(ts('Progress bar created successfuly'), '', 'success');
194 CRM_Utils_System
::redirect('progress-bar?reset=1');
196 catch (Exception
$e) {
197 CRM_Core_Session
::setStatus(ts('Failed to create progress bar'), '', 'error');
198 $transaction->rollback();
203 $sql = "INSERT INTO civicrm_wci_progress_bar
204 (name, starting_amount, goal_amount) VALUES (%1, %2, %3)";
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'),
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;
217 $sql = "INSERT INTO civicrm_wci_progress_bar_formula
218 (contribution_page_id, progress_bar_id, percentage)
219 VALUES (%1, %2, %3)";
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'),
227 $transaction->commit();
228 CRM_Utils_System
::redirect('civicrm/wci/progress-bar?reset=1');
230 catch (Exception
$e) {
231 CRM_Core_Session
::setStatus(ts('Failed to create Progress bar. ') .
232 $e->getMessage(), '', 'error');
233 $transaction->rollback();
235 $elem = $this->getElement('contrib_count');
236 $elem->setValue('1');
238 parent
::postProcess();
242 * Get the fields/elements defined in this form.
244 * @return array (string)
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
251 $elementNames = array();
252 foreach ($this->_elements
as $element) {
253 $label = $element->getLabel();
254 if (!empty($label)) {
255 $elementNames[] = $element->getName();
258 return $elementNames;