b2610aba3673ad764557867910b15446cf4396e4
[civicrm-core.git] / CRM / Contribute / Form / ContributionCharts.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | 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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Contribute_Form_ContributionCharts extends CRM_Core_Form {
36
37 /**
38 * Year of chart
39 *
40 * @var int
41 */
42 protected $_year = NULL;
43
44 /**
45 * The type of chart
46 *
47 * @var string
48 */
49 protected $_chartType = NULL;
50
51 function preProcess() {
52 $this->_year = CRM_Utils_Request::retrieve('year', 'Int', $this);
53 $this->_chartType = CRM_Utils_Request::retrieve('type', 'String', $this);
54
55 $buildChart = FALSE;
56
57 if ($this->_year || $this->_chartType) {
58 $buildChart = TRUE;
59 }
60 $this->assign('buildChart', $buildChart);
61 $this->postProcess();
62 }
63
64 /**
65 * Build the form
66 *
67 * @access public
68 *
69 * @return void
70 */
71 public function buildQuickForm() {
72 //p3 = Three dimensional pie chart.
73 //bvg = Vertical bar chart
74 $this->addElement('select', 'chart_type', ts('Chart Style'), array('bvg' => ts('Bar'),
75 'p3' => ts('Pie'),
76 ),
77 array('onchange' => "getChart();")
78 );
79 $defaultValues['chart_type'] = $this->_chartType;
80 $this->setDefaults($defaultValues);
81
82 //take available years from database to show in drop down
83 $currentYear = date('Y');
84 $years = array();
85 if (!empty($this->_years)) {
86 if (!array_key_exists($currentYear, $this->_years)) {
87 $this->_years[$currentYear] = $currentYear;
88 krsort($this->_years);
89 }
90 foreach ($this->_years as $k => $v) {
91 $years[substr($k,0,4)] = substr($k,0,4);
92 }
93 }
94
95 $this->addElement('select', 'select_year', ts('Select Year (for monthly breakdown)'),
96 $years, array('onchange' => "getChart();")
97 );
98 $this->setDefaults(array(
99 'select_year' => ($this->_year) ? $this->_year : $currentYear,
100 ));
101 }
102
103 /**
104 * process the form after the input has been submitted and validated
105 *
106 * @access public
107 *
108 * @return None
109 */
110 public function postProcess() {
111 $config = CRM_Core_Config::singleton();
112 $chartType = 'bvg';
113 if ($this->_chartType) {
114 $chartType = $this->_chartType;
115 }
116 $selectedYear = date('Y');
117 if ($this->_year) {
118 $selectedYear = $this->_year;
119 }
120
121 //take contribution information monthly
122 $chartInfoMonthly = CRM_Contribute_BAO_Contribution_Utils::contributionChartMonthly($selectedYear);
123
124 $chartData = $abbrMonthNames = array();
125 if (is_array($chartInfoMonthly)) {
126 for ($i = 1; $i <= 12; $i++) {
127 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
128 }
129
130 foreach ($abbrMonthNames as $monthKey => $monthName) {
131 $val = CRM_Utils_Array::value($monthKey, $chartInfoMonthly['By Month'], 0);
132
133 // don't include zero value month.
134 if (!$val && ($chartType != 'bvg')) {
135 continue;
136 }
137
138 //build the params for chart.
139 $chartData['by_month']['values'][$monthName] = $val;
140 }
141 $chartData['by_month']['legend'] = 'By Month' . ' - ' . $selectedYear;
142
143 // handle onclick event.
144 $chartData['by_month']['on_click_fun_name'] = 'byMonthOnClick';
145 $chartData['by_month']['yname'] = ts('Contribution');
146 }
147
148 //take contribution information by yearly
149 $chartInfoYearly = CRM_Contribute_BAO_Contribution_Utils::contributionChartYearly();
150
151 //get the years.
152 $this->_years = $chartInfoYearly['By Year'];
153 $hasContributions = FALSE;
154 if (is_array($chartInfoYearly)) {
155 $hasContributions = TRUE;
156 $chartData['by_year']['legend'] = 'By Year';
157 $chartData['by_year']['values'] = $chartInfoYearly['By Year'];
158
159 // handle onclick event.
160 $chartData['by_year']['on_click_fun_name'] = 'byYearOnClick';
161 $chartData['by_year']['yname'] = ts('Total Amount');
162 }
163 $this->assign('hasContributions', $hasContributions);
164
165 // process the data.
166 $chartCnt = 1;
167
168 $monthlyChart = $yearlyChart = FALSE;
169
170 foreach ($chartData as $chartKey => & $values) {
171 $chartValues = CRM_Utils_Array::value('values', $values);
172
173 if (!is_array($chartValues) || empty($chartValues)) {
174 continue;
175 }
176 if ($chartKey == 'by_year') {
177 $yearlyChart = TRUE;
178 if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] !== 1 || $config->fiscalYearStart['d'] !== 1)) {
179 $values['xLabelAngle'] = 45 ;
180 } else {
181 $values['xLabelAngle'] = 0 ;
182 }
183 }
184 if ($chartKey == 'by_month') {
185 $monthlyChart = TRUE;
186 }
187
188 $values['divName'] = "open_flash_chart_{$chartKey}";
189 $funName = ($chartType == 'bvg') ? 'barChart' : 'pieChart';
190
191 // build the chart objects.
192 $values['object'] = CRM_Utils_OpenFlashChart::$funName($values);
193
194 //build the urls.
195 $urlCnt = 0;
196 foreach ($chartValues as $index => $val) {
197 $urlParams = NULL;
198 if ($chartKey == 'by_month') {
199 $monthPosition = array_search($index, $abbrMonthNames);
200 $startDate = CRM_Utils_Date::format(array('Y' => $selectedYear, 'M' => $monthPosition));
201 $endDate = date('Ymd', mktime(0, 0, 0, $monthPosition + 1, 0, $selectedYear));
202 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
203 }
204 elseif ($chartKey == 'by_year') {
205 if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] != 1 || $config->fiscalYearStart['d'] != 1)) {
206 $startDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], substr($index,0,4)));
207 $endDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], substr($index,0,4)+1));
208 }
209 else {
210 $startDate = CRM_Utils_Date::format(array('Y' => substr($index,0,4)));
211 $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index,0,4)));
212 }
213 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
214 }
215 if ($urlParams) {
216 $values['on_click_urls']["url_" . $urlCnt++] = CRM_Utils_System::url('civicrm/contribute/search',
217 $urlParams, TRUE, FALSE, FALSE
218 );
219 }
220 }
221
222 // calculate chart size.
223 $xSize = 400;
224 $ySize = 300;
225 if ($chartType == 'bvg') {
226 $ySize = 250;
227 $xSize = 60 * count($chartValues);
228
229 // reduce x size by 100 for by_month
230 if ($chartKey == 'by_month') {
231 $xSize -= 100;
232 }
233
234 //hack to show tooltip.
235 if ($xSize < 150) {
236 $xSize = 150;
237 }
238 }
239 $values['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
240 }
241
242 // finally assign this chart data to template.
243 $this->assign('hasYearlyChart', $yearlyChart);
244 $this->assign('hasByMonthChart', $monthlyChart);
245 $this->assign('hasOpenFlashChart', empty($chartData) ? FALSE : TRUE);
246 $this->assign('openFlashChartData', json_encode($chartData));
247 }
248 }
249