Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $file_ids = array_filter($form_state->getValue('file_select'));
     $form['file_ids'] = array('#type' => 'value', '#value' => $file_ids);
     $form['action'] = array('#type' => 'value', '#value' => $form_state->getValue(['uc_file_action', 'action']));
     $file_ids = _uc_file_sort_names(_uc_file_get_dir_file_ids($file_ids, FALSE));
     $affected_list = $this->buildJsFileDisplay($file_ids);
     $has_directory = FALSE;
     foreach ($file_ids as $file_id) {
         // Gather a list of user-selected filenames.
         $file = uc_file_get_by_id($file_id);
         $filename = $file->filename;
         $file_list[] = substr($filename, -1) == "/" ? $filename . ' (' . $this->t('directory') . ')' : $filename;
         // Determine if there are any directories in this list.
         $path = uc_file_qualify_file($filename);
         if (is_dir($path)) {
             $has_directory = TRUE;
         }
     }
     // Base files/dirs the user selected.
     $form['selected_files'] = array('#theme' => 'item_list', '#items' => $file_list, '#attributes' => array('class' => array('selected-file-name')));
     // Don't even show the recursion checkbox unless we have any directories.
     if ($has_directory && $affected_list[TRUE] !== FALSE) {
         $form['recurse_directories'] = array('#type' => 'checkbox', '#title' => $this->t('Delete selected directories and their sub directories'));
         // Default to FALSE. Although we have the JS behavior to update with the
         // state of the checkbox on load, this should improve the experience of
         // users who don't have JS enabled over not defaulting to any info.
         $form['affected_files'] = array('#theme' => 'item_list', '#items' => $affected_list[FALSE], '#title' => $this->t('Affected files'), '#attributes' => array('class' => array('affected-file-name')));
     }
     return parent::buildForm($form, $form_state);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $form['#tree'] = TRUE;
     $form['#attached']['library'][] = 'uc_file/uc_file.styles';
     $header = array('filename' => array('data' => $this->t('File'), 'field' => 'f.filename', 'sort' => 'asc'), 'title' => array('data' => $this->t('Product'), 'field' => 'n.title'), 'model' => array('data' => $this->t('SKU'), 'field' => 'fp.model'));
     // Create pager.
     $query = db_select('uc_files', 'f')->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')->orderByHeader($header)->limit(UC_FILE_PAGER_SIZE);
     $query->leftJoin('uc_file_products', 'fp', 'f.fid = fp.fid');
     $query->leftJoin('uc_product_features', 'pf', 'fp.pfid = pf.pfid');
     $query->leftJoin('node_field_data', 'n', 'pf.nid = n.nid');
     $query->addField('n', 'nid');
     $query->addField('f', 'filename');
     $query->addField('n', 'title');
     $query->addField('fp', 'model');
     $query->addField('f', 'fid');
     $query->addField('pf', 'pfid');
     $count_query = db_select('uc_files');
     $count_query->addExpression('COUNT(*)');
     $query->setCountQuery($count_query);
     $result = $query->execute();
     $options = array();
     foreach ($result as $file) {
         // All files are shown here, including files which are not attached to products.
         if (isset($file->nid)) {
             $options[$file->fid] = array('filename' => array('data' => SafeMarkup::checkPlain($file->filename), 'class' => is_dir(uc_file_qualify_file($file->filename)) ? array('uc-file-directory-view') : array()), 'title' => array('#type' => 'link', '#title' => $file->title, '#url' => Url::fromRoute('entity.node.canonical', ['node' => $file->nid])), 'model' => SafeMarkup::checkPlain($file->model));
         } else {
             $options[$file->fid] = array('filename' => array('data' => SafeMarkup::checkPlain($file->filename), 'class' => is_dir(uc_file_qualify_file($file->filename)) ? array('uc-file-directory-view') : array()), 'title' => '', 'model' => '');
         }
     }
     // Create checkboxes for each file.
     $form['file_select'] = array('#type' => 'tableselect', '#header' => $header, '#options' => $options, '#empty' => $this->t('No file downloads available.'));
     $form['uc_file_action'] = array('#type' => 'details', '#title' => $this->t('File options'), '#open' => TRUE);
     // Set our default actions.
     $file_actions = array('uc_file_upload' => $this->t('Upload file'), 'uc_file_delete' => $this->t('Delete file(s)'));
     // Check if any hook_uc_file_action('info', $args) are implemented
     $module_handler = \Drupal::moduleHandler();
     foreach ($module_handler->getImplementations('uc_file_action') as $module) {
         $name = $module . '_uc_file_action';
         $result = $name('info', NULL);
         if (is_array($result)) {
             foreach ($result as $key => $action) {
                 if ($key != 'uc_file_delete' && $key != 'uc_file_upload') {
                     $file_actions[$key] = $action;
                 }
             }
         }
     }
     $form['uc_file_action']['action'] = array('#type' => 'select', '#title' => $this->t('Action'), '#prefix' => '<div class="duration">', '#options' => $file_actions, '#suffix' => '</div>');
     $form['uc_file_actions']['actions'] = array('#type' => 'actions');
     $form['uc_file_action']['actions']['submit'] = array('#type' => 'submit', '#prefix' => '<div class="duration">', '#value' => $this->t('Perform action'), '#suffix' => '</div>');
     return $form;
 }
Ejemplo n.º 3
0
 /**
  *
  *
  * @return array
  *   A render array.
  */
 public function show()
 {
     $build['#tree'] = TRUE;
     // Form that provides operations.
     $build['file_action_form'] = $this->formBuilder->getForm('Drupal\\uc_file\\Form\\FileActionForm');
     // Table of files to operate on.
     $header = array('filename' => array('data' => $this->t('File'), 'field' => 'f.filename', 'sort' => 'asc'), 'title' => array('data' => $this->t('Product'), 'field' => 'n.title'), 'model' => array('data' => $this->t('SKU'), 'field' => 'fp.model', 'class' => array(RESPONSIVE_PRIORITY_LOW)));
     // Create pager.
     $query = $this->database->select('uc_files', 'f')->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')->orderByHeader($header)->limit(UC_FILE_PAGER_SIZE);
     $query->leftJoin('uc_file_products', 'fp', 'f.fid = fp.fid');
     $query->leftJoin('uc_product_features', 'pf', 'fp.pfid = pf.pfid');
     $query->leftJoin('node_field_data', 'n', 'pf.nid = n.nid');
     $query->addField('n', 'nid');
     $query->addField('f', 'filename');
     $query->addField('n', 'title');
     $query->addField('fp', 'model');
     $query->addField('f', 'fid');
     $query->addField('pf', 'pfid');
     $count_query = $this->database->select('uc_files');
     $count_query->addExpression('COUNT(*)');
     $query->setCountQuery($count_query);
     $result = $query->execute();
     $options = array();
     foreach ($result as $file) {
         // All files are shown here, including files which are not attached to products.
         if (isset($file->nid)) {
             $options[$file->fid] = array('filename' => array('data' => array('#plain_text' => $file->filename), 'class' => is_dir(uc_file_qualify_file($file->filename)) ? array('uc-file-directory-view') : array()), 'title' => array('data' => array('#type' => 'link', '#title' => $file->title, '#url' => Url::fromRoute('entity.node.canonical', ['node' => $file->nid]))), 'model' => array('data' => array('#plain_text' => $file->model)));
         } else {
             $options[$file->fid] = array('filename' => array('data' => array('#plain_text' => $file->filename), 'class' => is_dir(uc_file_qualify_file($file->filename)) ? array('uc-file-directory-view') : array()), 'title' => '', 'model' => '');
         }
     }
     // Create checkboxes for each file.
     $build['file_select'] = array('#type' => 'tableselect', '#header' => $header, '#options' => $options, '#empty' => $this->t('No file downloads available.'));
     $build['file_select_pager'] = array('#type' => 'pager');
     return $build;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $file_ids = array_filter($form_state->getValue('file_select'));
     $form['file_ids'] = array('#type' => 'value', '#value' => $file_ids);
     $form['action'] = array('#type' => 'value', '#value' => $form_state->getValue(['uc_file_action', 'action']));
     $file_ids = _uc_file_sort_names(_uc_file_get_dir_file_ids($file_ids, FALSE));
     switch ($form_state->getValue(['uc_file_action', 'action'])) {
         case 'uc_file_delete':
             $affected_list = _uc_file_build_js_file_display($file_ids);
             $has_directory = FALSE;
             foreach ($file_ids as $file_id) {
                 // Gather a list of user-selected filenames.
                 $file = uc_file_get_by_id($file_id);
                 $filename = $file->filename;
                 $file_list[] = substr($filename, -1) == "/" ? $filename . ' (' . t('directory') . ')' : $filename;
                 // Determine if there are any directories in this list.
                 $path = uc_file_qualify_file($filename);
                 if (is_dir($path)) {
                     $has_directory = TRUE;
                 }
             }
             // Base files/dirs the user selected.
             $form['selected_files'] = array('#theme' => 'item_list', '#items' => $file_list, '#attributes' => array('class' => array('selected-file-name')));
             $form = confirm_form($form, t('Delete file(s)'), 'admin/store/products/files', t('Deleting a file will remove all its associated file downloads and product features. Removing a directory will remove any files it contains and their associated file downloads and product features.'), t('Delete affected files'), t('Cancel'));
             // Don't even show the recursion checkbox unless we have any directories.
             if ($has_directory && $affected_list[TRUE] !== FALSE) {
                 $form['recurse_directories'] = array('#type' => 'checkbox', '#title' => t('Delete selected directories and their sub directories'));
                 // Default to FALSE. Although we have the JS behavior to update with the
                 // state of the checkbox on load, this should improve the experience of
                 // users who don't have JS enabled over not defaulting to any info.
                 $form['affected_files'] = array('#theme' => 'item_list', '#items' => $affected_list[FALSE], '#title' => t('Affected files'), '#attributes' => array('class' => array('affected-file-name')));
             }
             break;
         case 'uc_file_upload':
             // Calculate the max size of uploaded files, in bytes.
             $max_bytes = trim(ini_get('post_max_size'));
             switch (strtolower($max_bytes[strlen($max_bytes) - 1])) {
                 case 'g':
                     $max_bytes *= 1024;
                 case 'm':
                     $max_bytes *= 1024;
                 case 'k':
                     $max_bytes *= 1024;
             }
             // Gather list of directories under the selected one(s).
             // '/' is always available.
             $directories = array('' => '/');
             $files = db_query("SELECT * FROM {uc_files}");
             foreach ($files as $file) {
                 if (is_dir(\Drupal::config('uc_file.settings')->get('base_dir') . "/" . $file->filename)) {
                     $directories[$file->filename] = $file->filename;
                 }
             }
             $form['upload_dir'] = array('#type' => 'select', '#title' => t('Directory'), '#description' => t('The directory on the server where the file should be put. The default directory is the root of the file downloads directory.'), '#options' => $directories);
             $form['upload'] = array('#type' => 'file', '#title' => t('File'), '#description' => t('The maximum file size that can be uploaded is %size bytes. You will need to use a different method to upload the file to the directory (e.g. (S)FTP, SCP) if your file exceeds this size. Files you upload using one of these alternate methods will be automatically detected.', ['%size' => number_format($max_bytes)]));
             $form['#attributes']['class'][] = 'foo';
             $form = confirm_form($form, t('Upload file'), 'admin/store/products/files', '', t('Upload file'), t('Cancel'));
             // Must add this after confirm_form, as it runs over $form['#attributes'].
             // Issue logged at d#319723
             $form['#attributes']['enctype'] = 'multipart/form-data';
             break;
         default:
             // This action isn't handled by us, so check if any
             // hook_uc_file_action('form', $args) are implemented.
             $module_handler = \Drupal::moduleHandler();
             foreach ($module_handler->getImplementations('uc_file_action') as $module) {
                 $name = $module . '_uc_file_action';
                 $result = $name('form', array('action' => $form_state->getValue(['uc_file_action', 'action']), 'file_ids' => $file_ids));
                 $form = is_array($result) ? array_merge($form, $result) : $form;
             }
             break;
     }
     return $form;
 }
 /**
  * Handles file downloading and error states.
  *
  * @param $fid
  *   The fid of the file specified to download.
  * @param $key
  *   The hash key of a user's download.
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request of the page.
  */
 public function download($fid, Request $request)
 {
     $user = \Drupal::currentUser();
     // Error messages for various failed download states.
     $admin_message = $this->t('Please contact the site administrator if this message has been received in error.');
     $error_messages = array(UC_FILE_ERROR_NOT_A_FILE => $this->t('The file you requested does not exist.'), UC_FILE_ERROR_TOO_MANY_BOGUS_REQUESTS => $this->t('You have attempted to download an incorrect file URL too many times.'), UC_FILE_ERROR_INVALID_DOWNLOAD => $this->t('The following URL is not a valid download link.') . ' ', UC_FILE_ERROR_TOO_MANY_LOCATIONS => $this->t('You have downloaded this file from too many different locations.'), UC_FILE_ERROR_TOO_MANY_DOWNLOADS => $this->t('You have reached the download limit for this file.'), UC_FILE_ERROR_EXPIRED => $this->t('This file download has expired.') . ' ', UC_FILE_ERROR_HOOK_ERROR => $this->t('A hook denied your access to this file.') . ' ');
     $ip = $request->getClientIp();
     if ($user->hasPermission('view all downloads')) {
         $file_download = uc_file_get_by_id($fid);
     } else {
         $file_download = uc_file_get_by_uid($user->id(), $fid);
     }
     if (isset($file_download->filename)) {
         $file_download->full_path = uc_file_qualify_file($file_download->filename);
     } else {
         throw new AccessDeniedHttpException();
     }
     // If it's ok, we push the file to the user.
     $status = UC_FILE_ERROR_OK;
     if (!$user->hasPermission('view all downloads')) {
         $status = $this->validateDownload($file_download, $user, $ip);
     }
     if ($status == UC_FILE_ERROR_OK) {
         $this->transferDownload($file_download, $ip);
     } else {
         drupal_set_message($error_messages[$status] . $admin_message, 'error');
         // Kick 'em to the curb. >:)
         $this->redirectDownload($user->id());
     }
     drupal_exit();
 }