Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | */ | |
430ae6dd TO |
49 | protected $_chartType = NULL; |
50 | ||
51 | 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 | /** | |
65 | * Build the form | |
66 | * | |
67 | * @access public | |
68 | * | |
69 | * @return void | |
70 | */ | |
71 | public function buildQuickForm() { | |
72 | //p3 = Three dimensional pie chart. | |
73 | //bvg = Vertical bar chart | |
4cbc3ca2 CW |
74 | $this->addElement('select', 'chart_type', ts('Chart Style'), array( |
75 | 'bvg' => ts('Bar'), | |
6a488035 | 76 | 'p3' => ts('Pie'), |
4cbc3ca2 | 77 | ) |
6a488035 TO |
78 | ); |
79 | $defaultValues['chart_type'] = $this->_chartType; | |
80 | $this->setDefaults($defaultValues); | |
81 | ||
82 | //take available years from database to show in drop down | |
83 | $currentYear = date('Y'); | |
84 | $years = array(); | |
85 | if (!empty($this->_years)) { | |
86 | if (!array_key_exists($currentYear, $this->_years)) { | |
87 | $this->_years[$currentYear] = $currentYear; | |
88 | krsort($this->_years); | |
89 | } | |
90 | foreach ($this->_years as $k => $v) { | |
91 | $years[substr($k,0,4)] = substr($k,0,4); | |
92 | } | |
93 | } | |
94 | ||
4cbc3ca2 | 95 | $this->addElement('select', 'select_year', ts('Select Year (for monthly breakdown)'), $years); |
6a488035 TO |
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 | * | |
355ba699 | 106 | * @return void |
6a488035 TO |
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. | |
0e6e8724 | 190 | $values['object'] = CRM_Utils_OpenFlashChart::$funName($values); |
6a488035 TO |
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') { | |
a48278eb KJ |
203 | if (!empty($config->fiscalYearStart) && ($config->fiscalYearStart['M'] != 1 || $config->fiscalYearStart['d'] != 1)) { |
204 | $startDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], substr($index,0,4))); | |
205 | $endDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart['M'], $config->fiscalYearStart['d'], substr($index,0,4)+1)); | |
206 | } | |
207 | else { | |
208 | $startDate = CRM_Utils_Date::format(array('Y' => substr($index,0,4))); | |
209 | $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index,0,4))); | |
210 | } | |
6a488035 TO |
211 | $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0"; |
212 | } | |
213 | if ($urlParams) { | |
214 | $values['on_click_urls']["url_" . $urlCnt++] = CRM_Utils_System::url('civicrm/contribute/search', | |
215 | $urlParams, TRUE, FALSE, FALSE | |
216 | ); | |
217 | } | |
218 | } | |
219 | ||
220 | // calculate chart size. | |
221 | $xSize = 400; | |
222 | $ySize = 300; | |
223 | if ($chartType == 'bvg') { | |
224 | $ySize = 250; | |
225 | $xSize = 60 * count($chartValues); | |
226 | ||
227 | // reduce x size by 100 for by_month | |
228 | if ($chartKey == 'by_month') { | |
229 | $xSize -= 100; | |
230 | } | |
231 | ||
232 | //hack to show tooltip. | |
233 | if ($xSize < 150) { | |
234 | $xSize = 150; | |
235 | } | |
236 | } | |
237 | $values['size'] = array('xSize' => $xSize, 'ySize' => $ySize); | |
238 | } | |
239 | ||
240 | // finally assign this chart data to template. | |
241 | $this->assign('hasYearlyChart', $yearlyChart); | |
242 | $this->assign('hasByMonthChart', $monthlyChart); | |
243 | $this->assign('hasOpenFlashChart', empty($chartData) ? FALSE : TRUE); | |
244 | $this->assign('openFlashChartData', json_encode($chartData)); | |
245 | } | |
246 | } | |
247 |