Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contribute_Form_ContributionCharts extends CRM_Core_Form { | |
36 | ||
37 | /** | |
fe482240 | 38 | * Year of chart. |
6a488035 TO |
39 | * |
40 | * @var int | |
41 | */ | |
42 | protected $_year = NULL; | |
43 | ||
44 | /** | |
fe482240 | 45 | * The type of chart. |
6a488035 TO |
46 | * |
47 | * @var string | |
48 | */ | |
430ae6dd TO |
49 | protected $_chartType = NULL; |
50 | ||
00be9182 | 51 | public function preProcess() { |
6a488035 TO |
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 | /** | |
fe482240 | 65 | * Build the form object. |
6a488035 | 66 | * |
6a488035 TO |
67 | * |
68 | * @return void | |
69 | */ | |
70 | public function buildQuickForm() { | |
71 | //p3 = Three dimensional pie chart. | |
72 | //bvg = Vertical bar chart | |
4cbc3ca2 CW |
73 | $this->addElement('select', 'chart_type', ts('Chart Style'), array( |
74 | 'bvg' => ts('Bar'), | |
6a488035 | 75 | 'p3' => ts('Pie'), |
4cbc3ca2 | 76 | ) |
6a488035 TO |
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) { | |
874c9be7 | 90 | $years[substr($k, 0, 4)] = substr($k, 0, 4); |
6a488035 TO |
91 | } |
92 | } | |
93 | ||
4cbc3ca2 | 94 | $this->addElement('select', 'select_year', ts('Select Year (for monthly breakdown)'), $years); |
6a488035 TO |
95 | $this->setDefaults(array( |
96 | 'select_year' => ($this->_year) ? $this->_year : $currentYear, | |
353ffa53 | 97 | )); |
6a488035 TO |
98 | } |
99 | ||
100 | /** | |
fe482240 | 101 | * Process the form after the input has been submitted and validated. |
6a488035 | 102 | * |
6a488035 | 103 | * |
355ba699 | 104 | * @return void |
6a488035 TO |
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)) { | |
874c9be7 | 175 | $values['xLabelAngle'] = 45; |
0db6c3e1 TO |
176 | } |
177 | else { | |
874c9be7 | 178 | $values['xLabelAngle'] = 0; |
6a488035 TO |
179 | } |
180 | } | |
181 | if ($chartKey == 'by_month') { | |
182 | $monthlyChart = TRUE; | |
183 | } | |
184 | ||
185 | $values['divName'] = "open_flash_chart_{$chartKey}"; | |
186 | $funName = ($chartType == 'bvg') ? 'barChart' : 'pieChart'; | |
187 | ||
188 | // build the chart objects. | |
0e6e8724 | 189 | $values['object'] = CRM_Utils_OpenFlashChart::$funName($values); |
6a488035 TO |
190 | |
191 | //build the urls. | |
192 | $urlCnt = 0; | |
193 | foreach ($chartValues as $index => $val) { | |
194 | $urlParams = NULL; | |
195 | if ($chartKey == 'by_month') { | |
196 | $monthPosition = array_search($index, $abbrMonthNames); | |
353ffa53 TO |
197 | $startDate = CRM_Utils_Date::format(array('Y' => $selectedYear, 'M' => $monthPosition)); |
198 | $endDate = date('Ymd', mktime(0, 0, 0, $monthPosition + 1, 0, $selectedYear)); | |
199 | $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0"; | |
6a488035 TO |
200 | } |
201 | elseif ($chartKey == 'by_year') { | |
a48278eb | 202 | if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] != 1 || $config->fiscalYearStart['d'] != 1)) { |
874c9be7 | 203 | $startDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], substr($index, 0, 4))); |
353ffa53 | 204 | $endDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], (substr($index, 0, 4)) + 1)); |
a48278eb KJ |
205 | } |
206 | else { | |
874c9be7 | 207 | $startDate = CRM_Utils_Date::format(array('Y' => substr($index, 0, 4))); |
353ffa53 | 208 | $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index, 0, 4))); |
a48278eb | 209 | } |
6a488035 TO |
210 | $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0"; |
211 | } | |
212 | if ($urlParams) { | |
213 | $values['on_click_urls']["url_" . $urlCnt++] = CRM_Utils_System::url('civicrm/contribute/search', | |
214 | $urlParams, TRUE, FALSE, FALSE | |
215 | ); | |
216 | } | |
217 | } | |
218 | ||
219 | // calculate chart size. | |
220 | $xSize = 400; | |
221 | $ySize = 300; | |
222 | if ($chartType == 'bvg') { | |
223 | $ySize = 250; | |
224 | $xSize = 60 * count($chartValues); | |
225 | ||
226 | // reduce x size by 100 for by_month | |
227 | if ($chartKey == 'by_month') { | |
228 | $xSize -= 100; | |
229 | } | |
230 | ||
231 | //hack to show tooltip. | |
232 | if ($xSize < 150) { | |
233 | $xSize = 150; | |
234 | } | |
235 | } | |
236 | $values['size'] = array('xSize' => $xSize, 'ySize' => $ySize); | |
237 | } | |
238 | ||
239 | // finally assign this chart data to template. | |
240 | $this->assign('hasYearlyChart', $yearlyChart); | |
241 | $this->assign('hasByMonthChart', $monthlyChart); | |
242 | $this->assign('hasOpenFlashChart', empty($chartData) ? FALSE : TRUE); | |
243 | $this->assign('openFlashChartData', json_encode($chartData)); | |
244 | } | |
96025800 | 245 | |
6a488035 | 246 | } |