3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
6 require_once 'CRM/Wci/DAO/ProgressBarFormula.php';
9 * Form controller class
11 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
13 class CRM_Wci_Form_ProgressBar
extends CRM_Core_Form
{
15 function preProcess() {
16 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, NULL, 'REQUEST');
17 CRM_Core_Resources
::singleton()->addScriptFile('org.civicrm.wci', 'addmore.js');
22 if (isset($this->_id
)) {
23 /** Updating existing progress bar*/
24 $query = "SELECT * FROM civicrm_wci_progress_bar where id=" . $this->_id
;
27 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
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']));
40 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $this->_id
;
43 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
45 while ($dao->fetch()) {
46 $for_page[$dao->id
] = array();
47 CRM_Core_DAO
::storeValues($dao, $for_page[$dao->id
]);
50 'select', // field type
51 'contribution_page_'.$count, // field name
52 'Contribution page', // field label
53 getContributionPageOptions(), // list of options
58 'percentage_'.$count, // field name
59 'Percentage of contribution taken', // field label
63 $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id
]['id']);
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']));
72 CRM_Utils_System
::setTitle(ts('Edit Progress Bar'));
73 $count--; // because last iteration increments it to the next
76 /** New progress bar*/
78 'select', // field type
79 'contribution_page_1', // field name
80 'Contribution page', // field label
81 getContributionPageOptions(), // list of options
86 'percentage_1', // field name
87 'Percentage of contribution taken', // field label
90 CRM_Utils_System
::setTitle(ts('Create Progress Bar'));
93 $this->addElement('hidden', 'contrib_count', $count);
96 function buildQuickForm() {
100 'progressbar_name', // field name
101 'Name', // field label
105 'text', // field type
106 'starting_amount', // field name
107 'Starting amount', // field label
111 'text', // field type
112 'goal_amount', // field name
113 'Goal amount', // field label
119 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add another contribution page');
121 $this->addButtons(array(
124 'name' => ts('Save'),
129 // export form elements
130 $this->assign('elementNames', $this->getRenderableElementNames());
132 parent
::buildQuickForm();
135 function postProcess() {
136 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
137 if (isset($this->_id
)) {
139 $transaction = new CRM_Core_Transaction();
141 $sql = "UPDATE civicrm_wci_progress_bar SET name = '". $_REQUEST['progressbar_name'] .
142 "', starting_amount = '" . $_REQUEST['starting_amount'] .
143 "', goal_amount = '" . $_REQUEST['goal_amount'] .
144 "' where id =".$this->_id
;
146 CRM_Core_DAO
::executeQuery($sql);
147 /** Delete existiing formula fields and add fields fresh*/
148 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id=' . $this->_id
);
150 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++
) {
151 $page = 'contribution_page_' . (string)$i;
152 $perc = 'percentage_' . (string)$i;
153 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
154 VALUES ('" . $_REQUEST[$page] . "','" . $this->_id
. "','" . $_REQUEST[$perc] . "')";
156 CRM_Core_DAO
::executeQuery($sql);
159 $transaction->commit();
160 CRM_Core_Session
::setStatus(ts('Progress bar created successfuly'), '', 'success');
161 CRM_Utils_System
::redirect('progress-bar?reset=1');
163 catch (Exception
$e) {
164 CRM_Core_Session
::setStatus(ts('Failed to create progress bar'), '', 'error');
165 $transaction->rollback();
170 $sql = "INSERT INTO civicrm_wci_progress_bar (name, starting_amount, goal_amount)
171 VALUES ('" . $_REQUEST['progressbar_name'] . "','" . $_REQUEST['starting_amount'] . "','" . $_REQUEST['goal_amount'] . "')";
173 $transaction = new CRM_Core_Transaction();
174 CRM_Core_DAO
::executeQuery($sql);
175 $progressbar_id = CRM_Core_DAO
::singleValueQuery('SELECT LAST_INSERT_ID()');
176 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++
):
177 $page = 'contribution_page_' . (string)$i;
178 $perc = 'percentage_' . (string)$i;
180 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
181 VALUES ('" . $_REQUEST[$page] . "','" . $progressbar_id . "','" . $_REQUEST[$perc] . "')";
183 CRM_Core_DAO
::executeQuery($sql);
185 $transaction->commit();
186 CRM_Utils_System
::redirect('civicrm/wci/progress-bar?reset=1');
188 catch (Exception
$e) {
190 print_r($e->getMessage());
191 $transaction->rollback();
193 $elem = $this->getElement('contrib_count');
194 $elem->setValue('1');
196 parent
::postProcess();
200 * Get the fields/elements defined in this form.
202 * @return array (string)
204 function getRenderableElementNames() {
205 // The _elements list includes some items which should not be
206 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
207 // items don't have labels. We'll identify renderable by filtering on
209 $elementNames = array();
210 foreach ($this->_elements
as $element) {
211 $label = $element->getLabel();
212 if (!empty($label)) {
213 $elementNames[] = $element->getName();
216 return $elementNames;