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