3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
35 class CRM_Contribute_Form_ContributionCharts
extends CRM_Core_Form
{
42 protected $_year = NULL;
49 protected $_chartType = NULL;
51 public function preProcess() {
52 $this->_year
= CRM_Utils_Request
::retrieve('year', 'Int', $this);
53 $this->_chartType
= CRM_Utils_Request
::retrieve('type', 'String', $this);
57 if ($this->_year ||
$this->_chartType
) {
60 $this->assign('buildChart', $buildChart);
65 * Build the form object.
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(
78 $defaultValues['chart_type'] = $this->_chartType
;
79 $this->setDefaults($defaultValues);
81 //take available years from database to show in drop down
82 $currentYear = date('Y');
84 if (!empty($this->_years
)) {
85 if (!array_key_exists($currentYear, $this->_years
)) {
86 $this->_years
[$currentYear] = $currentYear;
87 krsort($this->_years
);
89 foreach ($this->_years
as $k => $v) {
90 $years[substr($k, 0, 4)] = substr($k, 0, 4);
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,
101 * Process the form after the input has been submitted and validated.
106 public function postProcess() {
107 $config = CRM_Core_Config
::singleton();
109 if ($this->_chartType
) {
110 $chartType = $this->_chartType
;
112 $selectedYear = date('Y');
114 $selectedYear = $this->_year
;
117 //take contribution information monthly
118 $chartInfoMonthly = CRM_Contribute_BAO_Contribution_Utils
::contributionChartMonthly($selectedYear);
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));
126 foreach ($abbrMonthNames as $monthKey => $monthName) {
127 $val = CRM_Utils_Array
::value($monthKey, $chartInfoMonthly['By Month'], 0);
129 // don't include zero value month.
130 if (!$val && ($chartType != 'bvg')) {
134 //build the params for chart.
135 $chartData['by_month']['values'][$monthName] = $val;
137 $chartData['by_month']['legend'] = 'By Month' . ' - ' . $selectedYear;
139 // handle onclick event.
140 $chartData['by_month']['on_click_fun_name'] = 'byMonthOnClick';
141 $chartData['by_month']['yname'] = ts('Contribution');
144 //take contribution information by yearly
145 $chartInfoYearly = CRM_Contribute_BAO_Contribution_Utils
::contributionChartYearly();
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'];
155 // handle onclick event.
156 $chartData['by_year']['on_click_fun_name'] = 'byYearOnClick';
157 $chartData['by_year']['yname'] = ts('Total Amount');
159 $this->assign('hasContributions', $hasContributions);
164 $monthlyChart = $yearlyChart = FALSE;
166 foreach ($chartData as $chartKey => & $values) {
167 $chartValues = CRM_Utils_Array
::value('values', $values);
169 if (!is_array($chartValues) ||
empty($chartValues)) {
172 if ($chartKey == 'by_year') {
174 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] !== 1 ||
$config->fiscalYearStart
['d'] !== 1)) {
175 $values['xLabelAngle'] = 45;
178 $values['xLabelAngle'] = 0;
181 if ($chartKey == 'by_month') {
182 $monthlyChart = TRUE;
185 $values['divName'] = "open_flash_chart_{$chartKey}";
186 $funName = ($chartType == 'bvg') ?
'barChart' : 'pieChart';
188 // build the chart objects.
189 $values['object'] = CRM_Utils_OpenFlashChart
::$funName($values);
193 foreach ($chartValues as $index => $val) {
195 if ($chartKey == 'by_month') {
196 $monthPosition = array_search($index, $abbrMonthNames);
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";
201 elseif ($chartKey == 'by_year') {
202 if (!empty($config->fiscalYearStart
) && ($config->fiscalYearStart
['M'] != 1 ||
$config->fiscalYearStart
['d'] != 1)) {
203 $startDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart
['M'], $config->fiscalYearStart
['d'], substr($index, 0, 4)));
204 $endDate = date('Ymd', mktime(0, 0, 0, $config->fiscalYearStart
['M'], $config->fiscalYearStart
['d'], (substr($index, 0, 4)) +
1));
207 $startDate = CRM_Utils_Date
::format(array('Y' => substr($index, 0, 4)));
208 $endDate = date('Ymd', mktime(0, 0, 0, 13, 0, substr($index, 0, 4)));
210 $urlParams = "reset=1&force=1&status=1&start={$startDate}&end={$endDate}&test=0";
213 $values['on_click_urls']["url_" . $urlCnt++
] = CRM_Utils_System
::url('civicrm/contribute/search',
214 $urlParams, TRUE, FALSE, FALSE
219 // calculate chart size.
222 if ($chartType == 'bvg') {
224 $xSize = 60 * count($chartValues);
226 // reduce x size by 100 for by_month
227 if ($chartKey == 'by_month') {
231 //hack to show tooltip.
236 $values['size'] = array('xSize' => $xSize, 'ySize' => $ySize);
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));