You are here

function uc_file_user in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_file/uc_file.module \uc_file_user()

Implementation of hook_user().

File

uc_file/uc_file.module, line 101
Allows products to be associated with downloadable files.

Code

function uc_file_user($op, &$edit, &$account, $category = NULL) {
  global $user;
  switch ($op) {
    case 'delete':
      _user_table_action('remove', NULL, $account->uid);
      break;
    case 'form':
      if (user_access('administer users') && (is_null($category) || $category == 'account')) {
        $file_downloads = db_query("SELECT * FROM {uc_file_users} as u INNER JOIN {uc_files} as f ON u.fid = f.fid WHERE uid = %d", $account->uid);
        $files = db_query("SELECT * FROM {uc_files} ORDER BY filename ASC");
        $available_downloads = array();
        $available_files = array();
        while ($file_download = db_fetch_object($file_downloads)) {
          $available_downloads[$file_download->file_key] = $file_download->filename;
        }
        while ($file = db_fetch_object($files)) {
          if (substr($file->filename, -1) != '/' && substr($file->filename, -1) != '\\') {
            $available_files[$file->fid] = $file->filename;
          }
        }
        $form['file'] = array(
          '#type' => 'fieldset',
          '#title' => t('File downloads'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#weight' => 10,
        );
        $form['file']['remove_file'] = array(
          '#type' => 'select',
          '#title' => t('Remove file'),
          '#multiple' => TRUE,
          '#description' => t('Select a file to remove as a download. Hold Ctrl to select or unselect multiple files.'),
          '#options' => $available_downloads,
        );
        $form['file']['add_file'] = array(
          '#type' => 'select',
          '#title' => t('Add file'),
          '#multiple' => TRUE,
          '#description' => t('Select a file to add as a download. Hold Ctrl to select or unselect multiple files.'),
          '#options' => $available_files,
        );
      }
      return $form;
      break;
    case 'submit':
      if (!empty($edit['remove_file'])) {
        foreach ($edit['remove_file'] as $hash_key) {
          if (!is_null($hash_key)) {
            _user_table_action('remove', NULL, $account->uid, $hash_key);
          }
        }
      }
      if (!empty($edit['add_file'])) {
        foreach ($edit['add_file'] as $fid) {
          $pfid = db_result(db_query("SELECT pfid FROM {uc_file_products} WHERE fid = %d", $fid));
          _user_table_action('allow', $fid, $account->uid, $pfid);
        }
      }
      break;
    case 'view':
      $existing_download = db_result(db_query("SELECT fid FROM {uc_file_users} WHERE uid = %d", $account->uid));
      if ((user_access('view all downloads') || $user->uid == $account->uid) && $user->uid && $existing_download) {
        $items['uc_file_download'] = array(
          'value' => l(t('Click here to view your file downloads.'), 'user/' . $account->uid . '/files'),
          'class' => 'member',
        );
        return array(
          t('File downloads') => $items,
        );
      }
      break;
    default:
      break;
  }
}