commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Widget / Widget.php
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 */
8
9 /**
10 * Class CRM_Widget_Widget
11 */
12 class CRM_Widget_Widget {
13
14 static $_methodTable;
15
16 public function initialize() {
17 if (!self::$_methodTable) {
18 self::$_methodTable = array(
19 'getContributionPageData' => array(
20 'description' => 'Gets all campaign related data and returns it as a std class.',
21 'access' => 'remote',
22 'arguments' => array(
23 'contributionPageID',
24 'widgetID',
25 ),
26 ),
27 'getEmbedCode' => array(
28 'description' => 'Gets embed code. Perhaps overkill, but we can track dropoffs in this case. by # of people reqeusting emebed code / number of unique instances.',
29 'access' => 'remote',
30 'arguments' => array(
31 'contributionPageID',
32 'widgetID',
33 'format',
34 ),
35 ),
36 );
37 }
38 }
39
40 public function &methodTable() {
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 */
55 public function registerRequest($contributionPageID, $widgetID, $action) {
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 *
65 * @return object
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
95 $data->is_active = TRUE;
96 $data->title = $widget->title;
97 $data->logo = $widget->url_logo;
98 $data->button_title = $widget->button_title;
99 $data->button_url = CRM_Utils_System::url('civicrm/contribute/transact',
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";
112 $params = array(1 => array($contributionPageID, 'Integer'));
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";
126 $params = array(1 => array($contributionPageID, 'Integer'));
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
161 $data->colors = array();
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
186 * @param string $format
187 * Either myspace or normal.
188 *
189 * @return string
190 */
191 public function getEmbedCode($contributionPageID, $widgetID, $format = "normal") {
192 self::registerRequest($contributionPageID, $widgetID, __FUNCTION__);
193 return "<embed>.......................</embed>" .
194 print_r(func_get_args(), 1);
195 }
196
197 }