View source
<?php
namespace Drupal\uc_file\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserForm extends FormBase {
protected $account;
protected $time;
protected $dateFormatter;
public function __construct(TimeInterface $time, DateFormatterInterface $date_formatter) {
$this->time = $time;
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('datetime.time'), $container
->get('date.formatter'));
}
public function getFormId() {
return 'uc_file_user_form';
}
public function buildForm(array $form, FormStateInterface $form_state, AccountInterface $account = NULL) {
$this->account = $account;
$form['file'] = [
'#type' => 'details',
'#title' => $this
->t('Administration'),
];
if (!\Drupal::database()
->queryRange('SELECT 1 FROM {uc_files}', 0, 1)
->fetchField()) {
$form['file']['file_message'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('You must add files at the <a href=":url">Ubercart file download administration page</a> in order to attach them to a user.', [
':url' => Url::fromRoute('uc_file.downloads', [], [
'query' => [
'destination' => 'user/' . $account
->id() . '/edit',
],
])
->toString(),
]),
'#suffix' => '</p>',
];
return $form;
}
$form['file']['download']['#theme'] = 'uc_file_hook_user_file_downloads';
$form['file']['download']['file_download']['#tree'] = TRUE;
$form['#attached']['library'][] = 'uc_file/uc_file.scripts';
$form['#attached']['library'][] = 'uc_file/uc_file.styles';
$downloadable_files = [];
$file_downloads = \Drupal::database()
->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', [
':uid' => $account
->id(),
]);
$behavior = 0;
foreach ($file_downloads as $file_download) {
$downloadable_files[$file_download->fid] = $file_download->filename;
$form['file']['download']['file_download'][$file_download->fid] = [
'fuid' => [
'#type' => 'value',
'#value' => $file_download->fuid,
],
'expiration' => [
'#type' => 'value',
'#value' => $file_download->expiration,
],
'remove' => [
'#type' => 'checkbox',
],
'filename' => [
'#markup' => $file_download->filename,
],
'expires' => [
'#markup' => $file_download->expiration ? $this->dateFormatter
->format($file_download->expiration, 'short') : $this
->t('Never'),
],
'time_polarity' => [
'#type' => 'select',
'#default_value' => '+',
'#options' => [
'+' => '+',
'-' => '-',
],
],
'time_quantity' => [
'#type' => 'textfield',
'#size' => 2,
'#maxlength' => 2,
],
'time_granularity' => [
'#type' => 'select',
'#default_value' => 'day',
'#options' => [
'never' => $this
->t('never'),
'day' => $this
->t('day(s)'),
'week' => $this
->t('week(s)'),
'month' => $this
->t('month(s)'),
'year' => $this
->t('year(s)'),
],
],
'downloads_in' => [
'#markup' => $file_download->accessed,
],
'download_limit' => [
'#type' => 'textfield',
'#maxlength' => 3,
'#size' => 3,
'#default_value' => $file_download->download_limit ? $file_download->download_limit : NULL,
],
'addresses_in' => [
'#markup' => count(unserialize($file_download->addresses)),
],
'address_limit' => [
'#type' => 'textfield',
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $file_download->address_limit ? $file_download->address_limit : NULL,
],
];
$form['#attached']['drupalSettings']['behavior'][$behavior++] = $file_download->fid;
$less_reading =& $form['file']['download']['file_download'][$file_download->fid];
$less_reading['download_limit_old'] = [
'#type' => 'value',
'#value' => $less_reading['download_limit']['#default_value'],
];
$less_reading['address_limit_old'] = [
'#type' => 'value',
'#value' => $less_reading['address_limit']['#default_value'],
];
$less_reading['expiration_old'] = [
'#type' => 'value',
'#value' => $less_reading['expiration']['#value'],
];
}
$available_files = [];
$files = \Drupal::database()
->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;
}
}
$available_files = array_diff($available_files, $downloadable_files);
if (count($available_files)) {
$form['file']['file_add'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 6,
'#title' => $this
->t('Add file'),
'#description' => [
'#markup' => $this
->t('Select a file to add as a download. Newly added files will inherit the settings at the <a href=":url">Ubercart products settings page</a>.', [
':url' => Url::fromRoute('uc_product.settings')
->toString(),
]),
],
'#options' => $available_files,
'#tree' => TRUE,
];
}
$form['file']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$edit = $form_state
->getValues();
if (isset($edit['file_download'])) {
foreach ((array) $edit['file_download'] as $key => $download_modification) {
if ($download_modification['remove']) {
continue;
}
if ($download_modification['download_limit'] < 0) {
$form_state
->setErrorByName('file_download][' . $key . '][download_limit', $this
->t('A negative download limit does not make sense. Please enter a positive integer, or leave empty for no limit.'));
}
if ($download_modification['address_limit'] < 0) {
$form_state
->setErrorByName('file_download][' . $key . '][address_limit', $this
->t('A negative address limit does not make sense. Please enter a positive integer, or leave empty for no limit.'));
}
if ($download_modification['time_granularity'] == 'never' || !$download_modification['time_quantity']) {
continue;
}
$new_expiration = _uc_file_expiration_date($download_modification, $download_modification['expiration']);
if ($new_expiration <= $this->time
->getRequestTime()) {
$form_state
->setErrorByName('file_download][' . $key . '][time_quantity', $this
->t('The date %date has already occurred.', [
'%date' => $this->dateFormatter
->format($new_expiration, 'short'),
]));
}
if ($download_modification['time_quantity'] < 0) {
$form_state
->setErrorByName('file_download][' . $key . '][time_quantity', $this
->t('A negative expiration quantity does not make sense. Use the polarity control to determine if the time should be added or subtracted.'));
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$edit = $form_state
->getValues();
if (isset($edit['file_download'])) {
foreach ((array) $edit['file_download'] as $fid => $download_modification) {
if ($download_modification['remove']) {
uc_file_remove_user_file_by_id($this->account, $fid);
}
else {
$download_modification['expiration'] = _uc_file_expiration_date($download_modification, $download_modification['expiration']);
if ($download_modification['download_limit'] == $download_modification['download_limit_old'] && $download_modification['address_limit'] == $download_modification['address_limit_old'] && $download_modification['expiration'] == $download_modification['expiration_old']) {
continue;
}
uc_file_user_renew($fid, $this->account, NULL, $download_modification, TRUE);
}
}
}
if (isset($edit['file_add'])) {
$file_config = $this
->config('uc_file.settings');
foreach ((array) $edit['file_add'] as $fid => $data) {
$download_modification['download_limit'] = $file_config
->get('download_limit_number');
$download_modification['address_limit'] = $file_config
->get('download_limit_addresses');
$download_modification['expiration'] = _uc_file_expiration_date([
'time_polarity' => '+',
'time_quantity' => $file_config
->get('duration_qty'),
'time_granularity' => $file_config
->get('duration_granularity'),
], $this->time
->getRequestTime());
uc_file_user_renew($fid, $this->account, NULL, $download_modification, TRUE);
}
}
}
}