Merge pull request #15419 from eileenmcnaughton/event_filter
[civicrm-core.git] / CRM / Contribute / Form / ContributionCharts.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33class CRM_Contribute_Form_ContributionCharts extends CRM_Core_Form {
34
35 /**
fe482240 36 * Year of chart.
6a488035
TO
37 *
38 * @var int
39 */
40 protected $_year = NULL;
41
42 /**
fe482240 43 * The type of chart.
6a488035
TO
44 *
45 * @var string
46 */
430ae6dd
TO
47 protected $_chartType = NULL;
48
00be9182 49 public function preProcess() {
6a488035
TO
50 $this->_year = CRM_Utils_Request::retrieve('year', 'Int', $this);
51 $this->_chartType = CRM_Utils_Request::retrieve('type', 'String', $this);
52
53 $buildChart = FALSE;
54
55 if ($this->_year || $this->_chartType) {
56 $buildChart = TRUE;
57 }
58 $this->assign('buildChart', $buildChart);
59 $this->postProcess();
60 }
61
62 /**
fe482240 63 * Build the form object.
6a488035
TO
64 */
65 public function buildQuickForm() {
66 //p3 = Three dimensional pie chart.
67 //bvg = Vertical bar chart
be2fb01f 68 $this->addElement('select', 'chart_type', ts('Chart Style'), [
1330f57a
SL
69 'bvg' => ts('Bar'),
70 'p3' => ts('Pie'),
71 ]);
6a488035
TO
72 $defaultValues['chart_type'] = $this->_chartType;
73 $this->setDefaults($defaultValues);
74
75 //take available years from database to show in drop down
76 $currentYear = date('Y');
be2fb01f 77 $years = [];
6a488035
TO
78 if (!empty($this->_years)) {
79 if (!array_key_exists($currentYear, $this->_years)) {
80 $this->_years[$currentYear] = $currentYear;
81 krsort($this->_years);
82 }
83 foreach ($this->_years as $k => $v) {
874c9be7 84 $years[substr($k, 0, 4)] = substr($k, 0, 4);
6a488035
TO
85 }
86 }
87
4cbc3ca2 88 $this->addElement('select', 'select_year', ts('Select Year (for monthly breakdown)'), $years);
be2fb01f 89 $this->setDefaults([
6a488035 90 'select_year' => ($this->_year) ? $this->_year : $currentYear,
be2fb01f 91 ]);
6a488035
TO
92 }
93
94 /**
fe482240 95 * Process the form after the input has been submitted and validated.
6a488035
TO
96 */
97 public function postProcess() {
98 $config = CRM_Core_Config::singleton();
99 $chartType = 'bvg';
100 if ($this->_chartType) {
101 $chartType = $this->_chartType;
102 }
103 $selectedYear = date('Y');
104 if ($this->_year) {
105 $selectedYear = $this->_year;
106 }
107
108 //take contribution information monthly
109 $chartInfoMonthly = CRM_Contribute_BAO_Contribution_Utils::contributionChartMonthly($selectedYear);
110
be2fb01f 111 $chartData = $abbrMonthNames = [];
6a488035
TO
112 if (is_array($chartInfoMonthly)) {
113 for ($i = 1; $i <= 12; $i++) {
114 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
115 }
116
117 foreach ($abbrMonthNames as $monthKey => $monthName) {
118 $val = CRM_Utils_Array::value($monthKey, $chartInfoMonthly['By Month'], 0);
119
120 // don't include zero value month.
121 if (!$val && ($chartType != 'bvg')) {
122 continue;
123 }
124
125 //build the params for chart.
126 $chartData['by_month']['values'][$monthName] = $val;
127 }
128 $chartData['by_month']['legend'] = 'By Month' . ' - ' . $selectedYear;
129
130 // handle onclick event.
131 $chartData['by_month']['on_click_fun_name'] = 'byMonthOnClick';
132 $chartData['by_month']['yname'] = ts('Contribution');
133 }
134
135 //take contribution information by yearly
136 $chartInfoYearly = CRM_Contribute_BAO_Contribution_Utils::contributionChartYearly();
137
138 //get the years.
139 $this->_years = $chartInfoYearly['By Year'];
140 $hasContributions = FALSE;
141 if (is_array($chartInfoYearly)) {
142 $hasContributions = TRUE;
143 $chartData['by_year']['legend'] = 'By Year';
144 $chartData['by_year']['values'] = $chartInfoYearly['By Year'];
145
146 // handle onclick event.
147 $chartData['by_year']['on_click_fun_name'] = 'byYearOnClick';
148 $chartData['by_year']['yname'] = ts('Total Amount');
149 }
150 $this->assign('hasContributions', $hasContributions);
151
152 // process the data.
153 $chartCnt = 1;
154
155 $monthlyChart = $yearlyChart = FALSE;
156
157 foreach ($chartData as $chartKey => & $values) {
158 $chartValues = CRM_Utils_Array::value('values', $values);
159
160 if (!is_array($chartValues) || empty($chartValues)) {
161 continue;
162 }
163 if ($chartKey == 'by_year') {
164 $yearlyChart = TRUE;
165 if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] !== 1 || $config->fiscalYearStart['d'] !== 1)) {
874c9be7 166 $values['xLabelAngle'] = 45;
0db6c3e1
TO
167 }
168 else {
874c9be7 169 $values['xLabelAngle'] = 0;
6a488035
TO
170 }
171 }
172 if ($chartKey == 'by_month') {
173 $monthlyChart = TRUE;
174 }
175
176 $values['divName'] = "open_flash_chart_{$chartKey}";
177 $funName = ($chartType == 'bvg') ? 'barChart' : 'pieChart';
178
179 // build the chart objects.
0e6e8724 180 $values['object'] = CRM_Utils_OpenFlashChart::$funName($values);
6a488035
TO
181
182 //build the urls.
183 $urlCnt = 0;
184 foreach ($chartValues as $index => $val) {
185 $urlParams = NULL;
186 if ($chartKey == 'by_month') {
187 $monthPosition = array_search($index, $abbrMonthNames);
be2fb01f 188 $startDate = CRM_Utils_Date::format(['Y' => $selectedYear, 'M' => $monthPosition]);
353ffa53
TO
189 $endDate = date('Ymd', mktime(0, 0, 0, $monthPosition + 1, 0, $selectedYear));
190 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
6a488035
TO
191 }
192 elseif ($chartKey == 'by_year') {
a48278eb 193 if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] != 1 || $config->fiscalYearStart['d'] != 1)) {
874c9be7 194 $startDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], substr($index, 0, 4)));
353ffa53 195 $endDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], (substr($index, 0, 4)) + 1));
a48278eb
KJ
196 }
197 else {
be2fb01f 198 $startDate = CRM_Utils_Date::format(['Y' => substr($index, 0, 4)]);
353ffa53 199 $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index, 0, 4)));
a48278eb 200 }
6a488035
TO
201 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
202 }
203 if ($urlParams) {
204 $values['on_click_urls']["url_" . $urlCnt++] = CRM_Utils_System::url('civicrm/contribute/search',
205 $urlParams, TRUE, FALSE, FALSE
206 );
207 }
208 }
209
210 // calculate chart size.
211 $xSize = 400;
212 $ySize = 300;
213 if ($chartType == 'bvg') {
214 $ySize = 250;
215 $xSize = 60 * count($chartValues);
216
217 // reduce x size by 100 for by_month
218 if ($chartKey == 'by_month') {
219 $xSize -= 100;
220 }
221
222 //hack to show tooltip.
223 if ($xSize < 150) {
224 $xSize = 150;
225 }
226 }
be2fb01f 227 $values['size'] = ['xSize' => $xSize, 'ySize' => $ySize];
6a488035
TO
228 }
229
230 // finally assign this chart data to template.
231 $this->assign('hasYearlyChart', $yearlyChart);
232 $this->assign('hasByMonthChart', $monthlyChart);
233 $this->assign('hasOpenFlashChart', empty($chartData) ? FALSE : TRUE);
234 $this->assign('openFlashChartData', json_encode($chartData));
235 }
96025800 236
6a488035 237}