3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Contribute_Form_ContributionCharts
extends CRM_Core_Form
{
40 protected $_year = NULL;
47 protected $_chartType = NULL;
49 public function preProcess() {
50 $this->_year
= CRM_Utils_Request
::retrieve('year', 'Int', $this);
51 $this->_chartType
= CRM_Utils_Request
::retrieve('type', 'String', $this);
55 if ($this->_year ||
$this->_chartType
) {
58 $this->assign('buildChart', $buildChart);
63 * Build the form object.
65 public function buildQuickForm() {
66 //p3 = Three dimensional pie chart.
67 //bvg = Vertical bar chart
68 $this->addElement('select', 'chart_type', ts('Chart Style'), array(
73 $defaultValues['chart_type'] = $this->_chartType
;
74 $this->setDefaults($defaultValues);
76 //take available years from database to show in drop down
77 $currentYear = date('Y');
79 if (!empty($this->_years
)) {
80 if (!array_key_exists($currentYear, $this->_years
)) {
81 $this->_years
[$currentYear] = $currentYear;
82 krsort($this->_years
);
84 foreach ($this->_years
as $k => $v) {
85 $years[substr($k, 0, 4)] = substr($k, 0, 4);
89 $this->addElement('select', 'select_year', ts('Select Year (for monthly breakdown)'), $years);
90 $this->setDefaults(array(
91 'select_year' => ($this->_year
) ?
$this->_year
: $currentYear,
96 * Process the form after the input has been submitted and validated.
98 public function postProcess() {
99 $config = CRM_Core_Config
::singleton();
101 if ($this->_chartType
) {
102 $chartType = $this->_chartType
;
104 $selectedYear = date('Y');
106 $selectedYear = $this->_year
;
109 //take contribution information monthly
110 $chartInfoMonthly = CRM_Contribute_BAO_Contribution_Utils
::contributionChartMonthly($selectedYear);
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));
118 foreach ($abbrMonthNames as $monthKey => $monthName) {
119 $val = CRM_Utils_Array
::value($monthKey, $chartInfoMonthly['By Month'], 0);
121 // don't include zero value month.
122 if (!$val && ($chartType != 'bvg')) {
126 //build the params for chart.
127 $chartData['by_month']['values'][$monthName] = $val;
129 $chartData['by_month']['legend'] = 'By Month' . ' - ' . $selectedYear;
131 // handle onclick event.
132 $chartData['by_month']['on_click_fun_name'] = 'byMonthOnClick';
133 $chartData['by_month']['yname'] = ts('Contribution');
136 //take contribution information by yearly
137 $chartInfoYearly = CRM_Contribute_BAO_Contribution_Utils
::contributionChartYearly();
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'];
147 // handle onclick event.
148 $chartData['by_year']['on_click_fun_name'] = 'byYearOnClick';
149 $chartData['by_year']['yname'] = ts('Total Amount');
151 $this->assign('hasContributions', $hasContributions);
156 $monthlyChart = $yearlyChart = FALSE;
158 foreach ($chartData as $chartKey => & $values) {
159 $chartValues = CRM_Utils_Array
::value('values', $values);
161 if (!is_array($chartValues) ||
empty($chartValues)) {
164 if ($chartKey == 'by_year') {
166 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] !== 1 ||
$config->fiscalYearStart
['d'] !== 1)) {
167 $values['xLabelAngle'] = 45;
170 $values['xLabelAngle'] = 0;
173 if ($chartKey == 'by_month') {
174 $monthlyChart = TRUE;
177 $values['divName'] = "open_flash_chart_{$chartKey}";
178 $funName = ($chartType == 'bvg') ?
'barChart' : 'pieChart';
180 // build the chart objects.
181 $values['object'] = CRM_Utils_OpenFlashChart
::$funName($values);
185 foreach ($chartValues as $index => $val) {
187 if ($chartKey == 'by_month') {
188 $monthPosition = array_search($index, $abbrMonthNames);
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";
193 elseif ($chartKey == 'by_year') {
194 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] != 1 ||
$config->fiscalYearStart
['d'] != 1)) {
195 $startDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart
['M'], $config->fiscalYearStart
['d'], substr($index, 0, 4)));
196 $endDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart
['M'], $config->fiscalYearStart
['d'], (substr($index, 0, 4)) +
1));
199 $startDate = CRM_Utils_Date
::format(array('Y' => substr($index, 0, 4)));
200 $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index, 0, 4)));
202 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
205 $values['on_click_urls']["url_" . $urlCnt++
] = CRM_Utils_System
::url('civicrm/contribute/search',
206 $urlParams, TRUE, FALSE, FALSE
211 // calculate chart size.
214 if ($chartType == 'bvg') {
216 $xSize = 60 * count($chartValues);
218 // reduce x size by 100 for by_month
219 if ($chartKey == 'by_month') {
223 //hack to show tooltip.
228 $values['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
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));