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