Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | * Copyright (C) 2007 Jacob Singh, Sam Lerner | |
4 | * Licensed to CiviCRM under the Academic Free License version 3.0. | |
5 | * | |
6 | * Modified and improved upon by CiviCRM LLC (c) 2007 | |
7 | */ | |
bb3a214a EM |
8 | |
9 | /** | |
10 | * Class CRM_Widget_Widget | |
11 | */ | |
6a488035 TO |
12 | class CRM_Widget_Widget { |
13 | ||
683bf891 | 14 | public static $_methodTable; |
9d72cede | 15 | |
00be9182 | 16 | public function initialize() { |
6a488035 | 17 | if (!self::$_methodTable) { |
be2fb01f CW |
18 | self::$_methodTable = [ |
19 | 'getContributionPageData' => [ | |
6a488035 TO |
20 | 'description' => 'Gets all campaign related data and returns it as a std class.', |
21 | 'access' => 'remote', | |
be2fb01f | 22 | 'arguments' => [ |
6a488035 TO |
23 | 'contributionPageID', |
24 | 'widgetID', | |
be2fb01f CW |
25 | ], |
26 | ], | |
27 | 'getEmbedCode' => [ | |
674507b6 | 28 | 'description' => 'Gets embed code. Perhaps overkill, but we can track dropoffs in this case. by # of people requesting embed code / number of unique instances.', |
6a488035 | 29 | 'access' => 'remote', |
be2fb01f | 30 | 'arguments' => [ |
6a488035 TO |
31 | 'contributionPageID', |
32 | 'widgetID', | |
33 | 'format', | |
be2fb01f CW |
34 | ], |
35 | ], | |
36 | ]; | |
6a488035 TO |
37 | } |
38 | } | |
39 | ||
00be9182 | 40 | public function &methodTable() { |
6a488035 TO |
41 | self::initialize(); |
42 | ||
43 | return self::$_methodTable; | |
44 | } | |
45 | ||
46 | /** | |
47 | * Not implemented - registers an action and unique widget ID. Useful for stats and debugging | |
48 | * | |
49 | * @param int $contributionPageID | |
50 | * @param string $widgetID | |
51 | * @param string $action | |
52 | * | |
53 | * @return string | |
54 | */ | |
00be9182 | 55 | public function registerRequest($contributionPageID, $widgetID, $action) { |
6a488035 TO |
56 | return "I registered a request to $action on $contributionPageID from $widgetID"; |
57 | } | |
58 | ||
59 | /** | |
60 | * Gets all campaign related data and returns it as a std class. | |
61 | * | |
62 | * @param int $contributionPageID | |
63 | * @param string $widgetID | |
64 | * | |
c301f76e | 65 | * @return object |
6a488035 TO |
66 | */ |
67 | public function getContributionPageData($contributionPageID, $widgetID) { | |
68 | $config = CRM_Core_Config::singleton(); | |
69 | ||
70 | self::registerRequest($contributionPageID, $widgetID, __FUNCTION__); | |
71 | ||
72 | $data = new stdClass(); | |
73 | ||
74 | if (empty($contributionPageID) || | |
75 | CRM_Utils_Type::validate($contributionPageID, 'Integer') == NULL | |
76 | ) { | |
77 | $data->is_error = TRUE; | |
78 | CRM_Core_Error::debug_log_message("$contributionPageID is not set"); | |
79 | return $data; | |
80 | } | |
81 | ||
82 | $widget = new CRM_Contribute_DAO_Widget(); | |
83 | $widget->contribution_page_id = $contributionPageID; | |
84 | if (!$widget->find(TRUE)) { | |
85 | $data->is_error = TRUE; | |
86 | CRM_Core_Error::debug_log_message("$contributionPageID is not found"); | |
87 | return $data; | |
88 | } | |
89 | ||
90 | $data->is_error = FALSE; | |
91 | if (!$widget->is_active) { | |
92 | $data->is_active = FALSE; | |
93 | } | |
94 | ||
9d72cede EM |
95 | $data->is_active = TRUE; |
96 | $data->title = $widget->title; | |
97 | $data->logo = $widget->url_logo; | |
6a488035 | 98 | $data->button_title = $widget->button_title; |
9d72cede | 99 | $data->button_url = CRM_Utils_System::url('civicrm/contribute/transact', |
6a488035 TO |
100 | "reset=1&id=$contributionPageID", |
101 | TRUE, NULL, FALSE, TRUE | |
102 | ); | |
103 | $data->about = $widget->about; | |
104 | ||
105 | $query = " | |
106 | SELECT count( id ) as count, | |
107 | sum( total_amount) as amount | |
108 | FROM civicrm_contribution | |
109 | WHERE is_test = 0 | |
110 | AND contribution_status_id = 1 | |
111 | AND contribution_page_id = %1"; | |
be2fb01f | 112 | $params = [1 => [$contributionPageID, 'Integer']]; |
6a488035 TO |
113 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
114 | if ($dao->fetch()) { | |
115 | $data->num_donors = $dao->count; | |
116 | $data->money_raised = $dao->amount; | |
117 | } | |
118 | else { | |
119 | $data->num_donors = $data->money_raised = 0; | |
120 | } | |
121 | ||
122 | $query = " | |
123 | SELECT goal_amount, start_date, end_date, is_active | |
124 | FROM civicrm_contribution_page | |
125 | WHERE id = %1"; | |
be2fb01f | 126 | $params = [1 => [$contributionPageID, 'Integer']]; |
6a488035 TO |
127 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
128 | if ($dao->fetch()) { | |
129 | $data->money_target = $dao->goal_amount; | |
130 | $data->campaign_start = CRM_Utils_Date::customFormat($dao->start_date, $config->dateformatFull); | |
131 | $data->campaign_end = CRM_Utils_Date::customFormat($dao->end_date, $config->dateformatFull); | |
132 | ||
133 | // check for time being between start and end date | |
134 | $now = time(); | |
135 | if ($dao->start_date) { | |
136 | $startDate = CRM_Utils_Date::unixTime($dao->start_date); | |
137 | if ($startDate && | |
138 | $startDate >= $now | |
139 | ) { | |
140 | $data->is_active = FALSE; | |
141 | } | |
142 | } | |
143 | ||
144 | if ($dao->end_date) { | |
145 | $endDate = CRM_Utils_Date::unixTime($dao->end_date); | |
146 | if ($endDate && | |
147 | $endDate < $now | |
148 | ) { | |
149 | $data->is_active = FALSE; | |
150 | } | |
151 | } | |
152 | } | |
153 | else { | |
154 | $data->is_active = FALSE; | |
155 | } | |
156 | ||
157 | // if is_active is false, show this link and hide the contribute button | |
158 | $data->homepage_link = $widget->url_homepage; | |
159 | ||
160 | // movie clip colors, must be in '0xRRGGBB' format | |
be2fb01f | 161 | $data->colors = []; |
6a488035 TO |
162 | |
163 | $hexPrefix = '0x'; | |
164 | $data->colors["title"] = str_replace('#', $hexPrefix, $widget->color_title); | |
165 | $data->colors["button"] = str_replace('#', $hexPrefix, $widget->color_button); | |
166 | $data->colors["bar"] = str_replace('#', $hexPrefix, $widget->color_bar); | |
167 | $data->colors["main_text"] = str_replace('#', $hexPrefix, $widget->color_main_text); | |
168 | $data->colors["main"] = str_replace('#', $hexPrefix, $widget->color_main); | |
169 | $data->colors["main_bg"] = str_replace('#', $hexPrefix, $widget->color_main_bg); | |
170 | $data->colors["bg"] = str_replace('#', $hexPrefix, $widget->color_bg); | |
171 | ||
172 | // these two have colors as normal hex format | |
173 | // because they're being used in a CSS object | |
174 | $data->colors["about_link"] = str_replace('#', $hexPrefix, $widget->color_about_link); | |
175 | $data->colors["homepage_link"] = str_replace('#', $hexPrefix, $widget->color_homepage_link); | |
176 | ||
177 | return $data; | |
178 | } | |
179 | ||
180 | /** | |
181 | * Gets embed code. Perhaps overkill, but we can track dropoffs in this case. | |
182 | * by # of people reqeusting emebed code / number of unique instances. | |
183 | * | |
184 | * @param int $contributionPageID | |
185 | * @param string $widgetID | |
2c0c2857 TO |
186 | * @param string $format |
187 | * Either myspace or normal. | |
6a488035 TO |
188 | * |
189 | * @return string | |
190 | */ | |
191 | public function getEmbedCode($contributionPageID, $widgetID, $format = "normal") { | |
192 | self::registerRequest($contributionPageID, $widgetID, __FUNCTION__); | |
9d72cede EM |
193 | return "<embed>.......................</embed>" . |
194 | print_r(func_get_args(), 1); | |
6a488035 | 195 | } |
96025800 | 196 | |
6a488035 | 197 | } |