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