You are here

function uc_coupon_view in Ubercart Discount Coupons 6

Same name and namespace in other branches
  1. 7.3 uc_coupon.admin.inc \uc_coupon_view()
  2. 7.2 uc_coupon.admin.inc \uc_coupon_view()

View coupon details.

Parameters

$coupon object: Coupon to view.

1 string reference to 'uc_coupon_view'
uc_coupon_menu in ./uc_coupon.module
Implementation of hook_menu().

File

./uc_coupon.admin.inc, line 146

Code

function uc_coupon_view($coupon) {
  $rows = array();
  $rows[] = array(
    t('Name'),
    check_plain($coupon->name),
  );
  if (!$coupon->status) {
    $status = t('Inactive');
  }
  else {
    if (!$coupon->valid_until) {
      $status = t('Active');
    }
    else {
      if (time() < $coupon->valid_from) {
        $status = t('Not valid until @date', array(
          '@date' => _uc_coupon_format_date($coupon->valid_from, variable_get('uc_date_format_default', 'm/d/Y H:iT')),
        ));
      }
      else {
        if (time() > $coupon->valid_until) {
          $status = t('Expired on @date', array(
            '@date' => _uc_coupon_format_date($coupon->valid_until, variable_get('uc_date_format_default', 'm/d/Y H:iT')),
          ));
        }
        else {
          $status = t('Active until @date', array(
            '@date' => _uc_coupon_format_date($coupon->valid_until, variable_get('uc_date_format_default', 'm/d/Y H:iT')),
          ));
        }
      }
    }
  }
  $rows[] = array(
    t('Status'),
    $status,
  );
  if (!$coupon->bulk) {
    $rows[] = array(
      t('Code'),
      check_plain($coupon->code) . ' (' . l(t('Print'), 'admin/store/coupons/' . $coupon->cid . '/print') . ')',
    );
    if ($coupon->max_uses) {
      $rows[] = array(
        t('Maximum uses'),
        $coupon->max_uses,
      );
    }
  }
  else {
    $codes = '<strong>' . check_plain($coupon->code) . ' &times; ' . check_plain($coupon->data['bulk_number']) . '</strong>';
    $codes .= ' (' . l(t('Download codes'), 'admin/store/coupons/' . $coupon->cid . '/codes') . ')';
    $codes .= ' (' . l(t('Print all'), 'admin/store/coupons/' . $coupon->cid . '/print') . ')';
    for ($id = 0; $id < $coupon->data['bulk_number']; $id++) {
      $code = uc_coupon_get_bulk_code($coupon, $id);
      $codes .= '<br />' . check_plain($code) . ' (' . l(t('Print'), 'admin/store/coupons/' . $coupon->cid . '/print/' . $code) . ')';
    }
    $rows[] = array(
      t('Codes'),
      $codes,
    );

    //$rows[] = array(t('Bulk seed'), check_plain($coupon->bulk_seed));
    if ($coupon->max_uses) {
      $rows[] = array(
        t('Maximum uses per code'),
        $coupon->max_uses,
      );
    }
  }
  $rows[] = array(
    t('Discount value'),
    uc_coupon_format_discount($coupon),
  );
  switch ($coupon->data['apply_to']) {
    case 'subtotal':
      $applies = t('Order subtotal');
      break;
    case 'products_total':
      $applies = t('Total of matching products');
      break;
    case 'products':
      $applies = t('Matching products');
      break;
    case 'cheapest':
      $applies = format_plural($coupon->data['apply_count'], 'Cheapest product', '@count cheapest products');
      break;
    case 'expensive':
      $applies = format_plural($coupon->data['apply_count'], 'Most expensive product', '@count most expensive products');
      break;
  }
  $rows[] = array(
    t('Applied to'),
    $applies,
  );
  if ($coupon->data['apply_to'] != 'subtotal') {
    $restrict = array();
    if (isset($coupon->data['product_types'])) {
      $key = format_plural(count($coupon->data['product_types']), 'All products in class', 'All products in classes');
      $restrict[$key] = $coupon->data['product_types'];
    }
    if (isset($coupon->data['products'])) {
      $products = array();
      foreach ($coupon->data['products'] as $nid) {
        $products[] = check_plain(db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid)));
      }
      if (isset($coupon->data['negate_products'])) {
        $restrict[t('All products except')] = $products;
      }
      else {
        $restrict[format_plural(count($products), 'Product', 'Products')] = $products;
      }
    }
    if (isset($coupon->data['skus'])) {
      $restrict[format_plural(count($coupon->data['skus']), 'SKU', 'SKUs')] = $coupon->data['skus'];
    }
    if (isset($coupon->data['terms'])) {
      $terms = array();
      foreach ($coupon->data['terms'] as $tid) {
        $terms[] = check_plain(db_result(db_query("SELECT name FROM {term_data} WHERE tid = %d", $tid)));
      }
      if (isset($coupon->data['negate_terms'])) {
        $restrict[t('All taxonomy terms except')] = $terms;
      }
      else {
        $restrict[format_plural(count($terms), 'Taxonomy term', 'Taxonomy terms')] = $terms;
      }
    }
    if ($restrict) {
      $or = FALSE;
      foreach ($restrict as $title => &$restriction) {
        if ($or) {
          $title = t('or') . ' ' . $title;
        }
        $restriction = $title . ': <em>' . implode('</em>, <em>', $restriction) . '</em>';
        $or = TRUE;
      }
      $rows[] = array(
        t('Product restrictions'),
        implode('<br />', $restrict),
      );
    }
  }
  $restrict = array();
  if (isset($coupon->data['users'])) {
    $users = array();
    foreach ($coupon->data['users'] as $uid) {
      $users[] = check_plain(db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $uid)));
    }
    if (isset($coupon->data['negate_users'])) {
      $restrict[t('All users except')] = $users;
    }
    else {
      $restrict[format_plural(count($users), 'User', 'Users')] = $users;
    }
  }
  if (isset($coupon->data['max_uses_per_user'])) {
    $restrict['Maximum uses per user'] = array(
      $coupon->data['max_uses_per_user'],
    );
  }
  if (isset($coupon->data['roles'])) {
    if (isset($coupon->data['negate_roles'])) {
      $restrict[t('All roles except')] = $coupon->data['roles'];
    }
    else {
      $restrict[format_plural(count($users), 'Role', 'Roles')] = $coupon->data['roles'];
    }
  }
  if ($coupon->data['wholesale'] == 2) {
    $restrict['Has permission'] = array(
      'coupon wholesale pricing',
    );
  }
  else {
    if ($coupon->data['wholesale'] == 3) {
      $restrict['Does not have permission'] = array(
        'coupon wholesale pricing',
      );
    }
  }
  if ($restrict) {
    foreach ($restrict as $title => &$restriction) {
      $restriction = $title . ': <em>' . implode('</em>, <em>', $restriction) . '</em>';
    }
    $rows[] = array(
      t('User restrictions'),
      implode('<br />', $restrict),
    );
  }
  if ($coupon->minimum_order > 0) {
    $rows[] = array(
      t('Order restrictions'),
      t('Minimum subtotal') . ': <em>' . uc_currency_format($coupon->minimum_order) . '</em>',
    );
  }
  foreach ($rows as &$row) {
    $row[0] = array(
      'header' => TRUE,
      'data' => $row[0],
    );
  }
  $output = theme('table', array(), $rows);
  return $output;
}