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