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