e3a72753e4815b52a0a6d1a27939aea68e949e86
[com.zyxware.civiwci.git] / CRM / Wci / Form / ProgressBar.php
1 <?php
2
3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
6
7 /**
8 * Form controller class
9 *
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
11 */
12 class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
13
14 function preProcess() {
15
16 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'addmore.js');
17 parent::preProcess();
18 }
19 function buildQuickForm() {
20 $this->add(
21 'text', // field type
22 'progressbar_name', // field name
23 'Name', // field label
24 true // is required
25 );
26 $this->add(
27 'text', // field type
28 'starting_amount', // field name
29 'Starting amount', // field label
30 true // is required
31 );
32 $this->add(
33 'text', // field type
34 'goal_amount', // field name
35 'Goal amount', // field label
36 true // is required
37 );
38 $this->add(
39 'select', // field type
40 'contribution_page_1', // field name
41 'Contribution page', // field label
42 getContributionPageOptions(), // list of options
43 true // is required
44 );
45 $this->add(
46 'text', // field type
47 'percentage_1', // field name
48 'Percentage', // field label
49 true // is required
50 );
51
52 $this->addElement('link', 'addmore_link',' ', 'addmore', 'Add more');
53
54 $this->addElement('hidden', 'contrib_count', '1');
55
56 $this->addButtons(array(
57 array(
58 'type' => 'submit',
59 'name' => ts('Save'),
60 'isDefault' => TRUE,
61 ),
62 ));
63
64 // export form elements
65 $this->assign('elementNames', $this->getRenderableElementNames());
66
67 parent::buildQuickForm();
68 }
69
70 function postProcess() {
71
72 $sql = "INSERT INTO civicrm_wci_progress_bar (name, starting_amount, goal_amount)
73 VALUES ('" . $_REQUEST['progressbar_name'] . "','" . $_REQUEST['starting_amount'] . "','" . $_REQUEST['goal_amount'] . "')";
74
75 CRM_Core_DAO::executeQuery($sql);
76 try {
77 $progressbar_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
78 for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++):
79 $page = 'contribution_page_' . (string)$i;
80 $perc = 'percentage_' . (string)$i;
81
82 $sql = "INSERT INTO civicrm_wci_progress_bar_formula (contribution_page_id, progress_bar_id, percentage)
83 VALUES ('" . $_REQUEST[$page] . "','" . $progressbar_id . "','" . $_REQUEST[$perc] . "')";
84
85 CRM_Core_DAO::executeQuery($sql);
86 endfor;
87 }
88 catch (Exception $e) {
89 //TODO
90 print_r($e);
91 }
92 parent::postProcess();
93 $elem = $this->getElement('contrib_count');
94 $elem->setValue('1');
95 }
96
97 /**
98 * Get the fields/elements defined in this form.
99 *
100 * @return array (string)
101 */
102 function getRenderableElementNames() {
103 // The _elements list includes some items which should not be
104 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
105 // items don't have labels. We'll identify renderable by filtering on
106 // the 'label'.
107 $elementNames = array();
108 foreach ($this->_elements as $element) {
109 $label = $element->getLabel();
110 if (!empty($label)) {
111 $elementNames[] = $element->getName();
112 }
113 }
114 return $elementNames;
115 }
116 }
117