| 1 |
<?php
|
| 2 |
// $Id: ad_ubercart.module,v 1.18 2009/06/08 18:29:01 neochief Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enhances the ad module to support ads selling.
|
| 7 |
*/
|
| 8 |
|
| 9 |
define('AD_CONTENT_TYPE', variable_get('ad_ubercart_ad_content_type', 'ad_adbard'));
|
| 10 |
|
| 11 |
// Forms here:
|
| 12 |
require_once('ad_ubercart.forms.inc');
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_theme().
|
| 16 |
*/
|
| 17 |
function ad_ubercart_theme($existing, $type, $theme, $path) {
|
| 18 |
return array(
|
| 19 |
'date_picker' => array(
|
| 20 |
'arguments' => array('element' => NULL),
|
| 21 |
),
|
| 22 |
'cart_item_description' => array(
|
| 23 |
'arguments' => array('data' => NULL),
|
| 24 |
),
|
| 25 |
'ad_in_cart' => array(
|
| 26 |
'arguments' => array('node' => NULL),
|
| 27 |
),
|
| 28 |
);
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implementation of hook_menu().
|
| 33 |
*/
|
| 34 |
function ad_ubercart_menu() {
|
| 35 |
$items['admin/settings/ad_ubercart'] = array(
|
| 36 |
'title' => 'Ad Ubercart settings',
|
| 37 |
'page callback' => 'ad_ubercart_quota',
|
| 38 |
'access arguments' => array('administer store'),
|
| 39 |
'file' => 'ad_ubercart.admin.inc',
|
| 40 |
'type' => MENU_NORMAL_ITEM,
|
| 41 |
);
|
| 42 |
$items['admin/settings/ad_ubercart/quota'] = array(
|
| 43 |
'title' => 'Advertisements quota',
|
| 44 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 45 |
'weight' => -10,
|
| 46 |
);
|
| 47 |
$items['admin/settings/ad_ubercart/add_quota'] = array(
|
| 48 |
'title' => 'Add quota',
|
| 49 |
'page callback' => 'drupal_get_form',
|
| 50 |
'page arguments' => array('ad_ubercart_quota_form'),
|
| 51 |
'access arguments' => array('administer store'),
|
| 52 |
'file' => 'ad_ubercart.admin.inc',
|
| 53 |
'type' => MENU_LOCAL_TASK,
|
| 54 |
);
|
| 55 |
$items['admin/settings/ad_ubercart/quota/%'] = array(
|
| 56 |
'title' => 'Edit quota',
|
| 57 |
'page callback' => 'drupal_get_form',
|
| 58 |
'page arguments' => array('ad_ubercart_quota_form', 4),
|
| 59 |
'access arguments' => array('administer store'),
|
| 60 |
'file' => 'ad_ubercart.admin.inc',
|
| 61 |
'type' => MENU_CALLBACK,
|
| 62 |
);
|
| 63 |
$items['cart/ad_ubercart/ahah'] = array(
|
| 64 |
'page callback' => 'ad_ubercart_add_to_cart_ahah',
|
| 65 |
'access arguments' => array('access content'),
|
| 66 |
'file' => 'ad_ubercart.forms.inc',
|
| 67 |
'type' => MENU_NORMAL_ITEM,
|
| 68 |
);
|
| 69 |
$items['ad/autocomplete'] = array(
|
| 70 |
'page callback' => 'ad_autocomplete',
|
| 71 |
'access callback' => 'user_access',
|
| 72 |
'access arguments' => array('administer order workflow'),
|
| 73 |
'type' => MENU_CALLBACK,
|
| 74 |
'file' => 'ad_ubercart.forms.inc',
|
| 75 |
);
|
| 76 |
|
| 77 |
return $items;
|
| 78 |
}
|
| 79 |
|
| 80 |
/**
|
| 81 |
* Implementation of hook_menu_alter().
|
| 82 |
*/
|
| 83 |
function ad_ubercart_menu_alter(&$items) {
|
| 84 |
// Alternative FileField AHAH callback which supports sub-form elements.
|
| 85 |
if (isset($items['filefield/ahah/%/%/%'])) {
|
| 86 |
$items['filefield/ahah/%/%/%']['page callback'] = 'ad_ubercart_filefield_js';
|
| 87 |
}
|
| 88 |
}
|
| 89 |
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Implementation of hook_cron().
|
| 93 |
*/
|
| 94 |
function ad_ubercart_cron() {
|
| 95 |
// Clean up temporary Ad nodes.
|
| 96 |
$time = strtotime(variable_get('uc_cart_anon_duration', '4') .' '. variable_get('uc_cart_anon_unit', 'hours') .' ago');
|
| 97 |
$result = db_query("SELECT nid FROM {node} WHERE uid = 0 AND changed <= %d", $time);
|
| 98 |
while ($nid = db_result($result)) {
|
| 99 |
node_delete($nid);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Implementation of hook_elements().
|
| 106 |
*/
|
| 107 |
function ad_ubercart_elements() {
|
| 108 |
$types = array();
|
| 109 |
|
| 110 |
$types['date_picker'] = array(
|
| 111 |
'#input' => TRUE,
|
| 112 |
);
|
| 113 |
|
| 114 |
return $types;
|
| 115 |
}
|
| 116 |
|
| 117 |
function ad_ubercart_preprocess_node(&$variables) {
|
| 118 |
$node = $variables['node'];
|
| 119 |
if (strpos($node->type, 'ad_product') === 0) {
|
| 120 |
unset($variables['submitted']);
|
| 121 |
}
|
| 122 |
}
|
| 123 |
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Theming date_picker element.
|
| 127 |
*/
|
| 128 |
function theme_date_picker($element) {
|
| 129 |
// Target for calendar
|
| 130 |
$calendar = '<div class="calendar"></div><div style="clear:both"></div>';
|
| 131 |
|
| 132 |
// ...and a hidden field which will store period
|
| 133 |
$hidden = theme('hidden', $element);
|
| 134 |
|
| 135 |
// Get everything mixed and return
|
| 136 |
$output = theme('form_element', $element, $hidden . $calendar);
|
| 137 |
|
| 138 |
// Make sure that JQuery is added.
|
| 139 |
drupal_add_js('misc/jquery.js');
|
| 140 |
|
| 141 |
// Inserting calendar JS and other stuff. Doing in this way, because this
|
| 142 |
// field may be loaded via AJAX, so we need to get all scripts and styles.
|
| 143 |
$path = drupal_get_path('module', 'ad_ubercart');
|
| 144 |
$output .= '<script defer="defer" type="text/javascript">
|
| 145 |
var calendars = new Array();
|
| 146 |
Drupal.settings.calendar = '. drupal_to_js($element['#calendar_attributes']) .';
|
| 147 |
</script>';
|
| 148 |
$output .= '<script defer="defer" type="text/javascript" src="'. url($path .'/date_picker/dateMethods.js') .'"></script>';
|
| 149 |
$output .= '<script defer="defer" type="text/javascript" src="'. url($path .'/date_picker/datePicker.js') .'"></script>';
|
| 150 |
$output .= '<script defer="defer" type="text/javascript" src="'. url($path .'/ad_ubercart_js.js') .'"></script>';
|
| 151 |
$output .= '<style>'. drupal_load_stylesheet($path .'/date_picker/datePicker.css')
|
| 152 |
.'table.jCalendar td.used { background-image: url('. url($path .'/images/dash.png') .'); font-weight:bold; }'
|
| 153 |
.'</style>';
|
| 154 |
return $output;
|
| 155 |
}
|
| 156 |
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Implementation of Ubercart hook_add_to_cart_data().
|
| 160 |
*
|
| 161 |
* Adding information about Advertisement to our cart.
|
| 162 |
*/
|
| 163 |
function ad_ubercart_add_to_cart_data($form_values) {
|
| 164 |
return array(
|
| 165 |
'period' => $form_values['period'],
|
| 166 |
'ad_plan' => $form_values['ad_plan'],
|
| 167 |
'ad' => $form_values['ad'],
|
| 168 |
);
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Implementation of Ubercart hook_cart_item_description().
|
| 173 |
*
|
| 174 |
* Displaying Ad's details in a cart.
|
| 175 |
*/
|
| 176 |
function ad_ubercart_cart_item_description($item) {
|
| 177 |
if (isset($item->data['ad_plan'])) {
|
| 178 |
$output = theme('cart_item_description', $item->data);
|
| 179 |
return $output;
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
/**
|
| 184 |
* Theme cart item description.
|
| 185 |
*/
|
| 186 |
function theme_cart_item_description($data) {
|
| 187 |
if ($data['ad_plan'] == 'ad_product_daily') {
|
| 188 |
$title = t('Days included');
|
| 189 |
}
|
| 190 |
if ($data['ad_plan'] == 'ad_product_monthly') {
|
| 191 |
$title = t('Months included');
|
| 192 |
}
|
| 193 |
foreach ($data['period'] as $month) {
|
| 194 |
list($y, $m) = explode('-', $month);
|
| 195 |
$data['period'][$month] = date('F, Y', mktime(0, 0, 0, $m, 1, $y));
|
| 196 |
}
|
| 197 |
|
| 198 |
$output = '<dl>';
|
| 199 |
$output .= '<dt><strong>'. $title .':</strong></dt><dd>'. theme('item_list', $data['period']) .'</dd>';
|
| 200 |
if ($data['ad']) {
|
| 201 |
$ad = node_load($data['ad']);
|
| 202 |
$output .= '<dt><strong>'. t('Linked Advertisement') .':</strong></dt><dd>'. theme('ad_in_cart', $ad) .'</dd>';
|
| 203 |
}
|
| 204 |
$output .= '</dl>';
|
| 205 |
return $output;
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
| 209 |
* Implementation of Ubercart hook_order().
|
| 210 |
*/
|
| 211 |
function ad_ubercart_order($op, &$arg1, $arg2) {
|
| 212 |
$order = $arg1;
|
| 213 |
switch ($op) {
|
| 214 |
case 'new':
|
| 215 |
case 'update':
|
| 216 |
if ($op == 'update') {
|
| 217 |
$status = $arg2;
|
| 218 |
}
|
| 219 |
else {
|
| 220 |
$status = $order->order_status;
|
| 221 |
}
|
| 222 |
|
| 223 |
switch ($status) {
|
| 224 |
case 'completed':
|
| 225 |
// We should cleanup old usage data before saving
|
| 226 |
db_query('DELETE FROM {ad_used_quota} WHERE order_id = %d', $order->order_id);
|
| 227 |
db_query('DELETE FROM {ad_orders} WHERE order_id = %d', $order->order_id);
|
| 228 |
|
| 229 |
foreach ($order->products as $product) {
|
| 230 |
// Save used quota
|
| 231 |
foreach ($product->data['period'] as $date) {
|
| 232 |
$type = str_replace('ad_product_', '', $product->data['ad_plan']);
|
| 233 |
$record = array(
|
| 234 |
'nid' => $product->nid,
|
| 235 |
'order_id' => $order->order_id,
|
| 236 |
'type' => $type,
|
| 237 |
'date' => $date,
|
| 238 |
);
|
| 239 |
drupal_write_record('ad_used_quota', $record);
|
| 240 |
}
|
| 241 |
|
| 242 |
// Insert info about used Ad
|
| 243 |
db_query('INSERT INTO {ad_orders} VALUES (%d, %d, %d, 1)', $order->order_id, $product->order_product_id, $product->data['ad']);
|
| 244 |
|
| 245 |
// Approve ordered ad
|
| 246 |
if ($product->data['ad']) {
|
| 247 |
db_query('UPDATE {ads} SET adstatus = "approved" WHERE aid = %d AND adstatus IN ("pending", "denied")', $product->data['ad']);
|
| 248 |
}
|
| 249 |
}
|
| 250 |
|
| 251 |
/*********************************************
|
| 252 |
*** Here order can be additionally processed
|
| 253 |
***/
|
| 254 |
|
| 255 |
break;
|
| 256 |
case 'canceled':
|
| 257 |
// Decline canceled ad
|
| 258 |
foreach ($order->products as $product) {
|
| 259 |
if ($product->data['ad']) {
|
| 260 |
db_query('UPDATE {ads} SET adstatus = "denied" WHERE aid = %d', $product->data['ad']);
|
| 261 |
}
|
| 262 |
}
|
| 263 |
|
| 264 |
/**************************************************************
|
| 265 |
*** Here order can be additionally processed declined orders
|
| 266 |
***/
|
| 267 |
|
| 268 |
break;
|
| 269 |
}
|
| 270 |
break;
|
| 271 |
}
|
| 272 |
}
|
| 273 |
|
| 274 |
/**
|
| 275 |
* Implementation of hook_nodeapi().
|
| 276 |
*/
|
| 277 |
function ad_ubercart_nodeapi($node, $op, $a3, $a4) {
|
| 278 |
static $pre_changed_statuses = array();
|
| 279 |
|
| 280 |
if ($node->type == AD_CONTENT_TYPE) {
|
| 281 |
switch ($op) {
|
| 282 |
case 'save':
|
| 283 |
// Save statuses before save to be able to revert theme on insert/update
|
| 284 |
$status = db_result(db_query('SELECT adstatus FROM {ads} WHERE aid = %d', $node->nid));
|
| 285 |
$pre_changed_statuses[$node->nid] = $status;
|
| 286 |
break;
|
| 287 |
case 'insert':
|
| 288 |
case 'update':
|
| 289 |
$status_changed = FALSE;
|
| 290 |
if ($node->adstatus == 'approved' || $node->adstatus == 'active') {
|
| 291 |
$result = db_query('SELECT * FROM {ad_orders} WHERE aid = %d AND paid = 0', $node->nid);
|
| 292 |
while ($item_info = db_fetch_object($result)) {
|
| 293 |
$new_errors = $old_errors = 0;
|
| 294 |
$order = uc_order_load($item_info->order_id);
|
| 295 |
// Ugly way to get needed product, as Ubercart sores products not in keyed array
|
| 296 |
$product = NULL;
|
| 297 |
foreach ($order->products as $p) {
|
| 298 |
if ($p->order_product_id == $item_info->order_product_id) {
|
| 299 |
$product = $p;
|
| 300 |
break;
|
| 301 |
}
|
| 302 |
}
|
| 303 |
if (!isset($product)) {
|
| 304 |
continue;
|
| 305 |
}
|
| 306 |
$item_price = $product->qty * $product->price;
|
| 307 |
$order_balance = uc_payment_balance($order);
|
| 308 |
|
| 309 |
// We need to check charge amount to not get above the order total.
|
| 310 |
$to_pay = min($item_price, $order_balance);
|
| 311 |
|
| 312 |
// We should not charge user if paid amount less or equals zero.
|
| 313 |
if ($to_pay > 0) {
|
| 314 |
// Submit payment terminal
|
| 315 |
$form_state['values'] = array(
|
| 316 |
'order_id' => $item_info->order_id,
|
| 317 |
'cc_data' => $order->payment_details,
|
| 318 |
'amount' => $to_pay,
|
| 319 |
'op' => t('Charge amount'),
|
| 320 |
);
|
| 321 |
$old_errors = count(drupal_get_messages('error'));
|
| 322 |
|
| 323 |
module_load_include('admin.inc', 'uc_credit');
|
| 324 |
uc_credit_terminal_form_submit(NULL, $form_state);
|
| 325 |
$new_errors = count(drupal_get_messages('error'));
|
| 326 |
}
|
| 327 |
|
| 328 |
// Freakin' ugly way to ckeck errors, but there's no other way
|
| 329 |
if ($new_errors > $old_errors) {
|
| 330 |
// If errors, revert the ad status
|
| 331 |
drupal_set_message(t('Ad status wasn\'t set due to errors durring payment charge attempt.'));
|
| 332 |
db_query('UPDATE {ads} SET adstatus = "%s" WHERE aid = %d', $pre_changed_statuses[$node->nid], $node->nid);
|
| 333 |
}
|
| 334 |
else {
|
| 335 |
// Update paid info if success
|
| 336 |
db_query('UPDATE {ad_orders} SET paid = 1 WHERE aid = %s and order_id = %d', $item_info->aid, $item_info->order_id);
|
| 337 |
$status_changed = TRUE;
|
| 338 |
}
|
| 339 |
}
|
| 340 |
}
|
| 341 |
elseif ($node->adstatus == 'declined') {
|
| 342 |
// Mark order item as declined (qty == 0)
|
| 343 |
$result = db_query("UPDATE {uc_order_products} SET qty = 0 WHERE order_id = %d AND order_product_id = %d", $item_info->order_id, $item_info->order_product_id);
|
| 344 |
$status_changed = TRUE;
|
| 345 |
}
|
| 346 |
|
| 347 |
// Final checks to order itself if status was changed.
|
| 348 |
if ($status_changed) {
|
| 349 |
$order = uc_order_load($item_info->order_id);
|
| 350 |
// Check all order items. If all of them was declined, cancel whole order.
|
| 351 |
$all_declined = TRUE;
|
| 352 |
foreach ($order->products as $product) {
|
| 353 |
if ($product->qty > 0) {
|
| 354 |
$all_declined = FALSE;
|
| 355 |
}
|
| 356 |
}
|
| 357 |
if ($all_declined) {
|
| 358 |
uc_order_update_status($order->order_id, 'canceled');
|
| 359 |
}
|
| 360 |
else {
|
| 361 |
// If everything has been paid, we can complete a whole order.
|
| 362 |
if (uc_payment_balance($order) == 0) {
|
| 363 |
uc_order_update_status($order->order_id, 'completed');
|
| 364 |
}
|
| 365 |
}
|
| 366 |
}
|
| 367 |
// Clean up status cache.
|
| 368 |
unset($pre_changed_statuses[$node->nid]);
|
| 369 |
break;
|
| 370 |
}
|
| 371 |
}
|
| 372 |
}
|
| 373 |
|
| 374 |
function theme_ad_in_cart($node) {
|
| 375 |
return module_invoke("ad_$node->adtype", 'display_ad', $node);
|
| 376 |
}
|
| 377 |
|
| 378 |
/**
|
| 379 |
* Returns textual representaion of YYYY-MM dates.
|
| 380 |
*/
|
| 381 |
function ad_ubercart_format_month($y_m) {
|
| 382 |
list($y, $m) = explode('-', $y_m);
|
| 383 |
$result = date('F, Y', mktime(0, 0, 0, $m, 1, $y));
|
| 384 |
return $result;
|
| 385 |
}
|