You are here

function uc_file_add_to_cart in Ubercart 6.2

Implements hook_add_to_cart().

If specified in the administration interface, notify a customer when downloading a duplicate file. Calculate and show the new limits.

File

uc_file/uc_file.module, line 509

Code

function uc_file_add_to_cart($nid, $qty, $data) {

  // Only warn if it's set in the product admin interface.
  if (!variable_get('uc_file_duplicate_warning', TRUE)) {
    return;
  }
  global $user;

  // Get all the files on this product
  $product_features = db_query("SELECT * FROM {uc_product_features} AS upf " . "INNER JOIN {uc_file_products} AS ufp ON upf.pfid = ufp.pfid " . "INNER JOIN {uc_files} AS uf ON ufp.fid = uf.fid " . "WHERE upf.nid = %d", $nid);
  while ($product_feature = db_fetch_object($product_features)) {

    // Match up models...
    if (!empty($product_feature->model) && $product_feature->model != $data['model']) {
      continue;
    }

    // Get the current limits, and calculate the new limits to show the user.
    if ($file_user = _uc_file_user_get($user, $product_feature->fid)) {
      $file_user = (array) $file_user;
      $old_limits = $file_user;

      // Get the limits from the product feature. (Or global if it says pass through)
      $file_modification = array(
        'download_limit' => uc_file_get_download_limit($product_feature),
        'address_limit' => uc_file_get_address_limit($product_feature),
        'expiration' => _uc_file_expiration_date(uc_file_get_time_limit($product_feature), max($file_user['expiration'], time())),
      );

      // Calculate the new limits
      _uc_file_accumulate_limits($file_user, $file_modification, FALSE);

      // Don't allow the product to be purchased if it won't increase the
      // download limit or expiration time.
      if ($old_limits['download_limit'] || $old_limits['expiration']) {

        // But still show the message if it does.
        drupal_set_message(t('You already have privileges to <a href="!url">download %file</a>. If you complete the purchase of this item, your new download limit will be %download_limit, your access location limit will be %address_limit, and your new expiration time will be %expiration.', array(
          '!url' => $user->uid ? url('user/' . $user->uid . '/purchased-files') : url('user/login'),
          '%file' => $product_feature->filename,
          '%download_limit' => $file_user['download_limit'] ? $file_user['download_limit'] : t('unlimited'),
          '%address_limit' => $file_user['address_limit'] ? $file_user['address_limit'] : t('unlimited'),
          '%expiration' => $file_user['expiration'] ? format_date($file_user['expiration'], 'short') : t('never'),
        )));
      }
      else {
        return array(
          array(
            'success' => FALSE,
            'message' => t('You already have privileges to <a href="!url">download %file</a>.', array(
              '!url' => $user->uid ? url('user/' . $user->uid . '/purchased-files') : url('user/login'),
              '%file' => $product_feature->filename,
            )),
          ),
        );
      }
    }
  }
}