Merge pull request #585 from agh1/fintyperel-relfirst
[civicrm-core.git] / CRM / Contribute / Form / ContributionCharts.php
CommitLineData
6a488035
TO
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 */
35class 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; function preProcess() {
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 /**
63 * Build the form
64 *
65 * @access public
66 *
67 * @return void
68 */
69 public function buildQuickForm() {
70 //p3 = Three dimensional pie chart.
71 //bvg = Vertical bar chart
72 $this->addElement('select', 'chart_type', ts('Chart Style'), array('bvg' => ts('Bar'),
73 'p3' => ts('Pie'),
74 ),
75 array('onchange' => "getChart();")
76 );
77 $defaultValues['chart_type'] = $this->_chartType;
78 $this->setDefaults($defaultValues);
79
80 //take available years from database to show in drop down
81 $currentYear = date('Y');
82 $years = array();
83 if (!empty($this->_years)) {
84 if (!array_key_exists($currentYear, $this->_years)) {
85 $this->_years[$currentYear] = $currentYear;
86 krsort($this->_years);
87 }
88 foreach ($this->_years as $k => $v) {
89 $years[substr($k,0,4)] = substr($k,0,4);
90 }
91 }
92
93 $this->addElement('select', 'select_year', ts('Select Year (for monthly breakdown)'),
94 $years, array('onchange' => "getChart();")
95 );
96 $this->setDefaults(array(
97 'select_year' => ($this->_year) ? $this->_year : $currentYear,
98 ));
99 }
100
101 /**
102 * process the form after the input has been submitted and validated
103 *
104 * @access public
105 *
106 * @return None
107 */
108 public function postProcess() {
109 $config = CRM_Core_Config::singleton();
110 $chartType = 'bvg';
111 if ($this->_chartType) {
112 $chartType = $this->_chartType;
113 }
114 $selectedYear = date('Y');
115 if ($this->_year) {
116 $selectedYear = $this->_year;
117 }
118
119 //take contribution information monthly
120 $chartInfoMonthly = CRM_Contribute_BAO_Contribution_Utils::contributionChartMonthly($selectedYear);
121
122 $chartData = $abbrMonthNames = array();
123 if (is_array($chartInfoMonthly)) {
124 for ($i = 1; $i <= 12; $i++) {
125 $abbrMonthNames[$i] = strftime('%b', mktime(0, 0, 0, $i, 10, 1970));
126 }
127
128 foreach ($abbrMonthNames as $monthKey => $monthName) {
129 $val = CRM_Utils_Array::value($monthKey, $chartInfoMonthly['By Month'], 0);
130
131 // don't include zero value month.
132 if (!$val && ($chartType != 'bvg')) {
133 continue;
134 }
135
136 //build the params for chart.
137 $chartData['by_month']['values'][$monthName] = $val;
138 }
139 $chartData['by_month']['legend'] = 'By Month' . ' - ' . $selectedYear;
140
141 // handle onclick event.
142 $chartData['by_month']['on_click_fun_name'] = 'byMonthOnClick';
143 $chartData['by_month']['yname'] = ts('Contribution');
144 }
145
146 //take contribution information by yearly
147 $chartInfoYearly = CRM_Contribute_BAO_Contribution_Utils::contributionChartYearly();
148
149 //get the years.
150 $this->_years = $chartInfoYearly['By Year'];
151 $hasContributions = FALSE;
152 if (is_array($chartInfoYearly)) {
153 $hasContributions = TRUE;
154 $chartData['by_year']['legend'] = 'By Year';
155 $chartData['by_year']['values'] = $chartInfoYearly['By Year'];
156
157 // handle onclick event.
158 $chartData['by_year']['on_click_fun_name'] = 'byYearOnClick';
159 $chartData['by_year']['yname'] = ts('Total Amount');
160 }
161 $this->assign('hasContributions', $hasContributions);
162
163 // process the data.
164 $chartCnt = 1;
165
166 $monthlyChart = $yearlyChart = FALSE;
167
168 foreach ($chartData as $chartKey => & $values) {
169 $chartValues = CRM_Utils_Array::value('values', $values);
170
171 if (!is_array($chartValues) || empty($chartValues)) {
172 continue;
173 }
174 if ($chartKey == 'by_year') {
175 $yearlyChart = TRUE;
176 if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] !== 1 || $config->fiscalYearStart['d'] !== 1)) {
177 $values['xLabelAngle'] = 45 ;
178 } else {
179 $values['xLabelAngle'] = 0 ;
180 }
181 }
182 if ($chartKey == 'by_month') {
183 $monthlyChart = TRUE;
184 }
185
186 $values['divName'] = "open_flash_chart_{$chartKey}";
187 $funName = ($chartType == 'bvg') ? 'barChart' : 'pieChart';
188
189 // build the chart objects.
190 eval("\$values['object'] = CRM_Utils_OpenFlashChart::" . $funName . '( $values );');
191
192 //build the urls.
193 $urlCnt = 0;
194 foreach ($chartValues as $index => $val) {
195 $urlParams = NULL;
196 if ($chartKey == 'by_month') {
197 $monthPosition = array_search($index, $abbrMonthNames);
198 $startDate = CRM_Utils_Date::format(array('Y' => $selectedYear, 'M' => $monthPosition));
199 $endDate = date('Ymd', mktime(0, 0, 0, $monthPosition + 1, 0, $selectedYear));
200 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
201 }
202 elseif ($chartKey == 'by_year') {
203 $startDate = CRM_Utils_Date::format(array('Y' => substr($index,0,4)));
204 $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index,0,4)));
205 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
206 }
207 if ($urlParams) {
208 $values['on_click_urls']["url_" . $urlCnt++] = CRM_Utils_System::url('civicrm/contribute/search',
209 $urlParams, TRUE, FALSE, FALSE
210 );
211 }
212 }
213
214 // calculate chart size.
215 $xSize = 400;
216 $ySize = 300;
217 if ($chartType == 'bvg') {
218 $ySize = 250;
219 $xSize = 60 * count($chartValues);
220
221 // reduce x size by 100 for by_month
222 if ($chartKey == 'by_month') {
223 $xSize -= 100;
224 }
225
226 //hack to show tooltip.
227 if ($xSize < 150) {
228 $xSize = 150;
229 }
230 }
231 $values['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
232 }
233
234 // finally assign this chart data to template.
235 $this->assign('hasYearlyChart', $yearlyChart);
236 $this->assign('hasByMonthChart', $monthlyChart);
237 $this->assign('hasOpenFlashChart', empty($chartData) ? FALSE : TRUE);
238 $this->assign('openFlashChartData', json_encode($chartData));
239 }
240}
241