Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 | 32 | * $Id$ |
6a488035 TO |
33 | */ |
34 | ||
35 | /** | |
36 | * Class to retrieve information about a contribution page | |
37 | */ | |
38 | class 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 | |
c6a8e7a3 | 44 | * @param int $widgetID |
8bf434e2 | 45 | * @param bool $includePending |
6a488035 | 46 | * |
bed98343 | 47 | * @return object |
6a488035 | 48 | */ |
8bf434e2 | 49 | public static function getContributionPageData($contributionPageID, $widgetID, $includePending = FALSE) { |
6a488035 TO |
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 | ||
c6a8e7a3 | 82 | //check if pending status needs to be included |
83 | $status = '1'; | |
84 | if ($includePending) { | |
85 | $status = '1,2'; | |
86 | } | |
87 | ||
6a488035 TO |
88 | $query = " |
89 | SELECT count( id ) as count, | |
90 | sum( total_amount) as amount | |
91 | FROM civicrm_contribution | |
92 | WHERE is_test = 0 | |
c6a8e7a3 | 93 | AND contribution_status_id IN ({$status}) |
6a488035 TO |
94 | AND contribution_page_id = %1"; |
95 | $params = array(1 => array($contributionPageID, 'Integer')); | |
96 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
97 | if ($dao->fetch()) { | |
353ffa53 TO |
98 | $data['num_donors'] = (int) $dao->count; |
99 | $data['money_raised'] = (int) $dao->amount; | |
6a488035 TO |
100 | } |
101 | else { | |
102 | $data['num_donors'] = $data['money_raised'] = $data->money_raised = 0; | |
103 | } | |
104 | ||
105 | $query = " | |
106 | SELECT goal_amount, start_date, end_date, is_active | |
107 | FROM civicrm_contribution_page | |
108 | WHERE id = %1"; | |
109 | $params = array(1 => array($contributionPageID, 'Integer')); | |
110 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
111 | ||
112 | $data['campaign_start'] = ''; | |
113 | $startDate = NULL; | |
114 | if ($dao->fetch()) { | |
353ffa53 | 115 | $data['money_target'] = (int) $dao->goal_amount; |
6a488035 TO |
116 | |
117 | // conditions that needs to be handled | |
118 | // 1. Campaign is not active - no text | |
119 | // 2. Campaign start date greater than today - show start date | |
120 | // 3. Campaign end date is set and greater than today - show end date | |
121 | // 4. If no start and end date or no end date and start date greater than today, then it's ongoing | |
122 | if ($dao->is_active) { | |
123 | $data['campaign_start'] = ts('Campaign is ongoing'); | |
124 | ||
125 | // check for time being between start and end date | |
126 | $now = time(); | |
127 | if ($dao->start_date) { | |
128 | $startDate = CRM_Utils_Date::unixTime($dao->start_date); | |
129 | if ($startDate && | |
130 | $startDate >= $now | |
131 | ) { | |
132 | $data['is_active'] = FALSE; | |
133 | $data['campaign_start'] = ts('Campaign starts on %1', array( | |
bed98343 | 134 | 1 => CRM_Utils_Date::customFormat($dao->start_date, $config->dateformatFull), |
353ffa53 | 135 | ) |
bed98343 | 136 | ); |
6a488035 TO |
137 | } |
138 | } | |
139 | ||
140 | if ($dao->end_date) { | |
141 | $endDate = CRM_Utils_Date::unixTime($dao->end_date); | |
142 | if ($endDate && | |
143 | $endDate < $now | |
144 | ) { | |
145 | $data['is_active'] = FALSE; | |
bed98343 | 146 | $data['campaign_start'] = ts('Campaign ended on %1', |
147 | array( | |
148 | 1 => CRM_Utils_Date::customFormat($dao->end_date, $config->dateformatFull), | |
353ffa53 | 149 | ) |
bed98343 | 150 | ); |
6a488035 TO |
151 | } |
152 | elseif ($startDate >= $now) { | |
bed98343 | 153 | $data['campaign_start'] = ts('Campaign starts on %1', |
154 | array( | |
155 | 1 => CRM_Utils_Date::customFormat($dao->start_date, $config->dateformatFull), | |
353ffa53 | 156 | ) |
bed98343 | 157 | ); |
6a488035 TO |
158 | } |
159 | else { | |
bed98343 | 160 | $data['campaign_start'] = ts('Campaign ends on %1', |
161 | array( | |
162 | 1 => CRM_Utils_Date::customFormat($dao->end_date, $config->dateformatFull), | |
353ffa53 | 163 | ) |
bed98343 | 164 | ); |
6a488035 TO |
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']); | |
353ffa53 TO |
181 | $data['money_raised'] = ts('Raised %1 of %2', array( |
182 | 1 => CRM_Utils_Money::format($data['money_raised']), | |
bed98343 | 183 | 2 => $data['money_target_display'], |
353ffa53 | 184 | )); |
6a488035 TO |
185 | } |
186 | else { | |
187 | $data['money_raised'] = ts('Raised %1', array(1 => CRM_Utils_Money::format($data['money_raised']))); | |
188 | } | |
189 | ||
353ffa53 | 190 | $data['money_low'] = 0; |
6a488035 | 191 | $data['num_donors'] = $data['num_donors'] . " " . ts('Donors'); |
353ffa53 | 192 | $data['home_url'] = "<a href='{$config->userFrameworkBaseURL}' class='crm-home-url' style='color:" . $widget->color_homepage_link . "'>" . ts('Learn more.') . "</a>"; |
6a488035 TO |
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'] = array(); | |
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 | } | |
96025800 | 210 | |
6a488035 | 211 | } |