3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC https://civicrm.org/licensing
33 class CRM_Contribute_Form_ContributionCharts
extends CRM_Core_Form
{
40 protected $_year = NULL;
47 protected $_chartType = NULL;
49 public function preProcess() {
50 $this->_year
= CRM_Utils_Request
::retrieve('year', 'Int', $this);
51 $this->_chartType
= CRM_Utils_Request
::retrieve('type', 'String', $this);
55 if ($this->_year ||
$this->_chartType
) {
58 $this->assign('buildChart', $buildChart);
63 * Build the form object.
65 public function buildQuickForm() {
66 //p3 = Three dimensional pie chart.
67 //bvg = Vertical bar chart
68 $this->addElement('select', 'chart_type', ts('Chart Style'), [
72 $defaultValues['chart_type'] = $this->_chartType
;
73 $this->setDefaults($defaultValues);
75 //take available years from database to show in drop down
76 $currentYear = date('Y');
78 if (!empty($this->_years
)) {
79 if (!array_key_exists($currentYear, $this->_years
)) {
80 $this->_years
[$currentYear] = $currentYear;
81 krsort($this->_years
);
83 foreach ($this->_years
as $k => $v) {
84 $years[substr($k, 0, 4)] = substr($k, 0, 4);
88 $this->addElement('select', 'select_year', ts('Select Year (for monthly breakdown)'), $years);
90 'select_year' => ($this->_year
) ?
$this->_year
: $currentYear,
95 * Process the form after the input has been submitted and validated.
97 public function postProcess() {
98 $config = CRM_Core_Config
::singleton();
100 if ($this->_chartType
) {
101 $chartType = $this->_chartType
;
103 $selectedYear = date('Y');
105 $selectedYear = $this->_year
;
108 //take contribution information monthly
109 $chartInfoMonthly = CRM_Contribute_BAO_Contribution_Utils
::contributionChartMonthly($selectedYear);
111 $chartData = $abbrMonthNames = [];
112 if (is_array($chartInfoMonthly)) {
113 for ($i = 1; $i <= 12; $i++
) {
114 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
117 foreach ($abbrMonthNames as $monthKey => $monthName) {
118 $val = CRM_Utils_Array
::value($monthKey, $chartInfoMonthly['By Month'], 0);
120 // don't include zero value month.
121 if (!$val && ($chartType != 'bvg')) {
125 //build the params for chart.
126 $chartData['by_month']['values'][$monthName] = $val;
128 $chartData['by_month']['legend'] = 'By Month' . ' - ' . $selectedYear;
130 // handle onclick event.
131 $chartData['by_month']['on_click_fun_name'] = 'byMonthOnClick';
132 $chartData['by_month']['yname'] = ts('Contribution');
135 //take contribution information by yearly
136 $chartInfoYearly = CRM_Contribute_BAO_Contribution_Utils
::contributionChartYearly();
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'];
146 // handle onclick event.
147 $chartData['by_year']['on_click_fun_name'] = 'byYearOnClick';
148 $chartData['by_year']['yname'] = ts('Total Amount');
150 $this->assign('hasContributions', $hasContributions);
155 $monthlyChart = $yearlyChart = FALSE;
157 foreach ($chartData as $chartKey => & $values) {
158 $chartValues = CRM_Utils_Array
::value('values', $values);
160 if (!is_array($chartValues) ||
empty($chartValues)) {
163 if ($chartKey == 'by_year') {
165 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] !== 1 ||
$config->fiscalYearStart
['d'] !== 1)) {
166 $values['xLabelAngle'] = 45;
169 $values['xLabelAngle'] = 0;
172 if ($chartKey == 'by_month') {
173 $monthlyChart = TRUE;
176 $values['divName'] = "chart_{$chartKey}";
177 $funName = ($chartType == 'bvg') ?
'barChart' : 'pieChart';
179 // build the chart objects.
180 $values['object'] = CRM_Utils_Chart
::$funName($values);
184 foreach ($chartValues as $index => $val) {
186 if ($chartKey == 'by_month') {
187 $monthPosition = array_search($index, $abbrMonthNames);
188 $startDate = CRM_Utils_Date
::format(['Y' => $selectedYear, 'M' => $monthPosition]);
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";
192 elseif ($chartKey == 'by_year') {
193 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] != 1 ||
$config->fiscalYearStart
['d'] != 1)) {
194 $startDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart
['M'], $config->fiscalYearStart
['d'], substr($index, 0, 4)));
195 $endDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart
['M'], $config->fiscalYearStart
['d'], (substr($index, 0, 4)) +
1));
198 $startDate = CRM_Utils_Date
::format(['Y' => substr($index, 0, 4)]);
199 $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index, 0, 4)));
201 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
204 $values['on_click_urls']["url_" . $urlCnt++
] = CRM_Utils_System
::url('civicrm/contribute/search',
205 $urlParams, TRUE, FALSE, FALSE
210 // calculate chart size.
213 if ($chartType == 'bvg') {
215 $xSize = 60 * count($chartValues);
217 // reduce x size by 100 for by_month
218 if ($chartKey == 'by_month') {
222 //hack to show tooltip.
227 $values['size'] = ['xSize' => $xSize, 'ySize' => $ySize];
230 // finally assign this chart data to template.
231 $this->assign('hasYearlyChart', $yearlyChart);
232 $this->assign('hasByMonthChart', $monthlyChart);
233 $this->assign('hasChart', empty($chartData) ?
FALSE : TRUE);
234 $this->assign('chartData', json_encode($chartData ??
[]));