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