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