Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 stdClass | |
48 | */ | |
4553998a | 49 | public static function getContributionPageData($contributionPageID, $widgetID) { |
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 | ||
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, | |
129 | $config->dateformatFull | |
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', array( | |
141 | 1 => CRM_Utils_Date::customFormat($dao->end_date, | |
142 | $config->dateformatFull | |
143 | ))); | |
144 | } | |
145 | elseif ($startDate >= $now) { | |
146 | $data['campaign_start'] = ts('Campaign starts on %1', array( | |
147 | 1 => CRM_Utils_Date::customFormat($dao->start_date, | |
148 | $config->dateformatFull | |
149 | ))); | |
150 | } | |
151 | else { | |
152 | $data['campaign_start'] = ts('Campaign ends on %1', array( | |
153 | 1 => CRM_Utils_Date::customFormat($dao->end_date, | |
154 | $config->dateformatFull | |
155 | ))); | |
156 | } | |
157 | } | |
158 | } | |
159 | else { | |
160 | $data['is_active'] = FALSE; | |
161 | } | |
162 | } | |
163 | else { | |
164 | $data['is_active'] = FALSE; | |
165 | } | |
166 | ||
167 | $data['money_raised_percentage'] = 0; | |
168 | if ($data['money_target'] > 0) { | |
169 | $percent = $data['money_raised'] / $data['money_target']; | |
170 | $data['money_raised_percentage'] = (round($percent, 2)) * 100 . "%"; | |
171 | $data['money_target_display'] = CRM_Utils_Money::format($data['money_target']); | |
172 | $data['money_raised'] = ts('Raised %1 of %2', array(1 => CRM_Utils_Money::format($data['money_raised']), 2 => $data['money_target_display'])); | |
173 | } | |
174 | else { | |
175 | $data['money_raised'] = ts('Raised %1', array(1 => CRM_Utils_Money::format($data['money_raised']))); | |
176 | } | |
177 | ||
178 | $data['money_low'] = 0; | |
179 | $data['num_donors'] = $data['num_donors'] . " " . ts('Donors'); | |
180 | $data['home_url'] = "<a href='{$config->userFrameworkBaseURL}' class='crm-home-url' style='color:" . $widget->color_homepage_link . "'>" . ts('Learn more.') . "</a>"; | |
181 | ||
182 | // if is_active is false, show this link and hide the contribute button | |
183 | $data['homepage_link'] = $widget->url_homepage; | |
184 | ||
185 | $data['colors'] = array(); | |
186 | ||
187 | $data['colors']["title"] = $widget->color_title; | |
188 | $data['colors']["button"] = $widget->color_button; | |
189 | $data['colors']["bar"] = $widget->color_bar; | |
190 | $data['colors']["main_text"] = $widget->color_main_text; | |
191 | $data['colors']["main"] = $widget->color_main; | |
192 | $data['colors']["main_bg"] = $widget->color_main_bg; | |
193 | $data['colors']["bg"] = $widget->color_bg; | |
194 | $data['colors']["about_link"] = $widget->color_about_link; | |
195 | ||
196 | return $data; | |
197 | } | |
198 | } | |
199 |