You are here

function uc_file_uc_add_to_cart in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 uc_file/uc_file.module \uc_file_uc_add_to_cart()

Implements hook_uc_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 99
Allows products to be associated with downloadable files.

Code

function uc_file_uc_add_to_cart($nid, $qty, $data) {
  $file_config = \Drupal::config('uc_file.settings');

  // Only warn if it's set in the product admin interface.
  if (!$file_config
    ->get('duplicate_warning')) {
    return;
  }
  $user = \Drupal::currentUser();
  $connection = \Drupal::database();

  // Get all the files on this product.
  $product_features = $connection
    ->query("SELECT * FROM {uc_product_features} upf " . "INNER JOIN {uc_file_products} ufp ON upf.pfid = ufp.pfid " . "INNER JOIN {uc_files} uf ON ufp.fid = uf.fid " . "WHERE upf.nid = :nid", [
    ':nid' => $nid,
  ]);
  foreach ($product_features as $product_feature) {

    // Match up models...
    if (!empty($product_feature->model) && isset($data['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 = [
        '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'], \Drupal::time()
          ->getRequestTime())),
      ];

      // 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::messenger()
          ->addWarning(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.', [
          ':url' => $user
            ->id() ? Url::fromRoute('uc_file.user_downloads', [
            'user' => $user
              ->id(),
          ])
            ->toString() : Url::fromRoute('user.login')
            ->toString(),
          '%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'] ? \Drupal::service('date.formatter')
            ->format($file_user['expiration'], 'small') : t('never'),
        ]));
      }
      else {
        return [
          [
            'success' => FALSE,
            'message' => t('You already have privileges to <a href=":url">download %file</a>.', [
              ':url' => $user
                ->id() ? Url::fromRoute('uc_file.user_downloads', [
                'user' => $user
                  ->id(),
              ])
                ->toString() : Url::fromRoute('user.login')
                ->toString(),
              '%file' => $product_feature->filename,
            ]),
          ],
        ];
      }
    }
  }
}