function uc_file_user_form in Ubercart 7.3
Same name and namespace in other branches
- 6.2 uc_file/uc_file.module \uc_file_user_form()
Form builder for per-user file download administration.
See also
1 string reference to 'uc_file_user_form'
- uc_file_user_downloads in uc_file/
uc_file.pages.inc - Table builder for user downloads.
File
- uc_file/
uc_file.module, line 129 - Allows products to be associated with downloadable files.
Code
function uc_file_user_form($form, &$form_state, $account) {
$form['file'] = array(
'#type' => 'fieldset',
'#title' => t('Administration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// Drop out early if we don't even have any files uploaded.
if (!db_query_range('SELECT 1 FROM {uc_files}', 0, 1)
->fetchField()) {
$form['file']['file_message'] = array(
'#markup' => '<p>' . t('You must add files at the <a href="!url">Ubercart file download administration page</a> in order to attach them to a user.', array(
'!url' => url('admin/store/products/files', array(
'query' => array(
'destination' => 'user/' . $account->uid . '/edit',
),
)),
)) . '</p>',
);
return $form;
}
// Table displaying current downloadable files and limits.
$form['file']['download']['#theme'] = 'uc_file_hook_user_file_downloads';
$form['file']['download']['file_download']['#tree'] = TRUE;
$downloadable_files = array();
$file_downloads = db_query("SELECT * FROM {uc_file_users} ufu INNER JOIN {uc_files} uf ON ufu.fid = uf.fid WHERE ufu.uid = :uid ORDER BY uf.filename ASC", array(
':uid' => $account->uid,
));
$behavior = 0;
foreach ($file_downloads as $file_download) {
// Store a flat array so we can array_diff the ones already allowed when
// building the list of which can be attached.
$downloadable_files[$file_download->fid] = $file_download->filename;
$form['file']['download']['file_download'][$file_download->fid] = array(
'fuid' => array(
'#type' => 'value',
'#value' => $file_download->fuid,
),
'expiration' => array(
'#type' => 'value',
'#value' => $file_download->expiration,
),
'remove' => array(
'#type' => 'checkbox',
),
'filename' => array(
'#markup' => $file_download->filename,
),
'expires' => array(
'#markup' => $file_download->expiration ? format_date($file_download->expiration, 'short') : t('Never'),
),
'time_polarity' => array(
'#type' => 'select',
'#default_value' => '+',
'#options' => array(
'+' => '+',
'-' => '-',
),
),
'time_quantity' => array(
'#type' => 'textfield',
'#size' => 2,
'#maxlength' => 2,
),
'time_granularity' => array(
'#type' => 'select',
'#default_value' => 'day',
'#options' => array(
'never' => t('never'),
'day' => t('day(s)'),
'week' => t('week(s)'),
'month' => t('month(s)'),
'year' => t('year(s)'),
),
),
'downloads_in' => array(
'#markup' => $file_download->accessed,
),
'download_limit' => array(
'#type' => 'textfield',
'#maxlength' => 3,
'#size' => 3,
'#default_value' => $file_download->download_limit ? $file_download->download_limit : NULL,
),
'addresses_in' => array(
'#markup' => count(unserialize($file_download->addresses)),
),
'address_limit' => array(
'#type' => 'textfield',
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $file_download->address_limit ? $file_download->address_limit : NULL,
),
);
// Incrementally add behaviors.
_uc_file_download_table_behavior($behavior++, $file_download->fid);
// Store old values for comparing to see if we actually made any changes.
$less_reading =& $form['file']['download']['file_download'][$file_download->fid];
$less_reading['download_limit_old'] = array(
'#type' => 'value',
'#value' => $less_reading['download_limit']['#default_value'],
);
$less_reading['address_limit_old'] = array(
'#type' => 'value',
'#value' => $less_reading['address_limit']['#default_value'],
);
$less_reading['expiration_old'] = array(
'#type' => 'value',
'#value' => $less_reading['expiration']['#value'],
);
}
// Create the list of files able to be attached to this user.
$available_files = array();
$files = db_query("SELECT * FROM {uc_files} ORDER BY filename ASC");
foreach ($files as $file) {
if (substr($file->filename, -1) != '/' && substr($file->filename, -1) != '\\') {
$available_files[$file->fid] = $file->filename;
}
}
// Dialog for uploading new files.
$available_files = array_diff($available_files, $downloadable_files);
if (count($available_files)) {
$form['file']['file_add'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 6,
'#title' => t('Add file'),
'#description' => t('Select a file to add as a download. Newly added files will inherit the settings at the !url.', array(
'!url' => l(t('Ubercart product settings page'), 'admin/store/settings/products'),
)),
'#options' => $available_files,
'#tree' => TRUE,
);
}
$form['file']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}