various comment fixes
[civicrm-core.git] / CRM / Contribute / BAO / Widget.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035 32 * $Id$
6a488035
TO
33 */
34
35/**
36 * Class to retrieve information about a contribution page
37 */
38class CRM_Contribute_BAO_Widget extends CRM_Contribute_DAO_Widget {
39
40 /**
41 * Gets all campaign related data and returns it as a std class.
42 *
43 * @param int $contributionPageID
44 * @param string $widgetID
45 *
bed98343 46 * @return object
6a488035 47 */
4553998a 48 public static function getContributionPageData($contributionPageID, $widgetID) {
6a488035
TO
49 $config = CRM_Core_Config::singleton();
50
51 $data = array();
52 $data['currencySymbol'] = $config->defaultCurrencySymbol;
53
54 if (empty($contributionPageID) ||
55 CRM_Utils_Type::validate($contributionPageID, 'Integer') == NULL
56 ) {
57 $data['is_error'] = TRUE;
58 CRM_Core_Error::debug_log_message("$contributionPageID is not set");
59 return $data;
60 }
61
62 $widget = new CRM_Contribute_DAO_Widget();
63 $widget->contribution_page_id = $contributionPageID;
64 if (!$widget->find(TRUE)) {
65 $data['is_error'] = TRUE;
66 CRM_Core_Error::debug_log_message("$contributionPageID is not found");
67 return $data;
68 }
69
70 $data['is_error'] = FALSE;
71 if (!$widget->is_active) {
72 $data['is_active'] = FALSE;
73 }
74
75 $data['is_active'] = TRUE;
76 $data['title'] = $widget->title;
77 $data['logo'] = $widget->url_logo;
78 $data['button_title'] = $widget->button_title;
79 $data['about'] = $widget->about;
80
81 $query = "
82 SELECT count( id ) as count,
83 sum( total_amount) as amount
84 FROM civicrm_contribution
85 WHERE is_test = 0
86 AND contribution_status_id = 1
87 AND contribution_page_id = %1";
88 $params = array(1 => array($contributionPageID, 'Integer'));
89 $dao = CRM_Core_DAO::executeQuery($query, $params);
90 if ($dao->fetch()) {
353ffa53
TO
91 $data['num_donors'] = (int) $dao->count;
92 $data['money_raised'] = (int) $dao->amount;
6a488035
TO
93 }
94 else {
95 $data['num_donors'] = $data['money_raised'] = $data->money_raised = 0;
96 }
97
98 $query = "
99 SELECT goal_amount, start_date, end_date, is_active
100 FROM civicrm_contribution_page
101 WHERE id = %1";
102 $params = array(1 => array($contributionPageID, 'Integer'));
103 $dao = CRM_Core_DAO::executeQuery($query, $params);
104
105 $data['campaign_start'] = '';
106 $startDate = NULL;
107 if ($dao->fetch()) {
353ffa53 108 $data['money_target'] = (int) $dao->goal_amount;
6a488035
TO
109
110 // conditions that needs to be handled
111 // 1. Campaign is not active - no text
112 // 2. Campaign start date greater than today - show start date
113 // 3. Campaign end date is set and greater than today - show end date
114 // 4. If no start and end date or no end date and start date greater than today, then it's ongoing
115 if ($dao->is_active) {
116 $data['campaign_start'] = ts('Campaign is ongoing');
117
118 // check for time being between start and end date
119 $now = time();
120 if ($dao->start_date) {
121 $startDate = CRM_Utils_Date::unixTime($dao->start_date);
122 if ($startDate &&
123 $startDate >= $now
124 ) {
125 $data['is_active'] = FALSE;
126 $data['campaign_start'] = ts('Campaign starts on %1', array(
bed98343 127 1 => CRM_Utils_Date::customFormat($dao->start_date, $config->dateformatFull),
353ffa53 128 )
bed98343 129 );
6a488035
TO
130 }
131 }
132
133 if ($dao->end_date) {
134 $endDate = CRM_Utils_Date::unixTime($dao->end_date);
135 if ($endDate &&
136 $endDate < $now
137 ) {
138 $data['is_active'] = FALSE;
bed98343 139 $data['campaign_start'] = ts('Campaign ended on %1',
140 array(
141 1 => CRM_Utils_Date::customFormat($dao->end_date, $config->dateformatFull),
353ffa53 142 )
bed98343 143 );
6a488035
TO
144 }
145 elseif ($startDate >= $now) {
bed98343 146 $data['campaign_start'] = ts('Campaign starts on %1',
147 array(
148 1 => CRM_Utils_Date::customFormat($dao->start_date, $config->dateformatFull),
353ffa53 149 )
bed98343 150 );
6a488035
TO
151 }
152 else {
bed98343 153 $data['campaign_start'] = ts('Campaign ends on %1',
154 array(
155 1 => CRM_Utils_Date::customFormat($dao->end_date, $config->dateformatFull),
353ffa53 156 )
bed98343 157 );
6a488035
TO
158 }
159 }
160 }
161 else {
162 $data['is_active'] = FALSE;
163 }
164 }
165 else {
166 $data['is_active'] = FALSE;
167 }
168
169 $data['money_raised_percentage'] = 0;
170 if ($data['money_target'] > 0) {
171 $percent = $data['money_raised'] / $data['money_target'];
172 $data['money_raised_percentage'] = (round($percent, 2)) * 100 . "%";
173 $data['money_target_display'] = CRM_Utils_Money::format($data['money_target']);
353ffa53
TO
174 $data['money_raised'] = ts('Raised %1 of %2', array(
175 1 => CRM_Utils_Money::format($data['money_raised']),
bed98343 176 2 => $data['money_target_display'],
353ffa53 177 ));
6a488035
TO
178 }
179 else {
180 $data['money_raised'] = ts('Raised %1', array(1 => CRM_Utils_Money::format($data['money_raised'])));
181 }
182
353ffa53 183 $data['money_low'] = 0;
6a488035 184 $data['num_donors'] = $data['num_donors'] . " " . ts('Donors');
353ffa53 185 $data['home_url'] = "<a href='{$config->userFrameworkBaseURL}' class='crm-home-url' style='color:" . $widget->color_homepage_link . "'>" . ts('Learn more.') . "</a>";
6a488035
TO
186
187 // if is_active is false, show this link and hide the contribute button
188 $data['homepage_link'] = $widget->url_homepage;
189
190 $data['colors'] = array();
191
192 $data['colors']["title"] = $widget->color_title;
193 $data['colors']["button"] = $widget->color_button;
194 $data['colors']["bar"] = $widget->color_bar;
195 $data['colors']["main_text"] = $widget->color_main_text;
196 $data['colors']["main"] = $widget->color_main;
197 $data['colors']["main_bg"] = $widget->color_main_bg;
198 $data['colors']["bg"] = $widget->color_bg;
199 $data['colors']["about_link"] = $widget->color_about_link;
200
201 return $data;
202 }
96025800 203
6a488035 204}