示例#1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     module_load_include('inc', 'uc_role', 'uc_role.admin');
     if ($form_state->get('step') == UC_FILE_FORM_ACTION) {
         return array('#validate' => array('uc_file_admin_files_form_action_validate'), '#submit' => array('uc_file_admin_files_form_action_submit')) + $form + uc_file_admin_files_form_action($form, $form_state);
     } else {
         // Refresh our file list before display.
         uc_file_refresh();
         return array('#theme' => 'uc_file_admin_files_form_show', '#validate' => array('uc_file_admin_files_form_show_validate'), '#submit' => array('uc_file_admin_files_form_show_submit')) + $form + uc_file_admin_files_form_show_files($form, $form_state);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // No directory now; truncate the file list.
     if ($form_state->isValueEmpty('base_dir')) {
         uc_file_empty();
     } else {
         uc_file_refresh();
     }
     $file_config = $this->config('uc_file.settings');
     $file_config->set('base_dir', $form_state->getValue('base_dir'))->set('duplicate_warning', $form_state->getValue('duplicate_warning'))->set('download_limit_number', $form_state->getValue('download_limit_number'))->set('download_limit_addresses', $form_state->getValue('download_limit_addresses'))->set('download_limit_duration_qty', $form_state->getValue('download_limit_duration_qty'))->set('download_limit_duration_granularity', $form_state->getValue('download_limit_duration_granularity'))->save();
     parent::submitForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $module_handler = \Drupal::moduleHandler();
     //    $module_handler->loadInclude('uc_file', 'inc', 'uc_file.admin');
     //if ($form_state->get('step') == UC_FILE_FORM_ACTION) {
     //  return $form + \Drupal::formBuilder()->buildForm('Drupal\uc_file\Form\ActionForm', $form, $form_state);
     //}
     //else {
     // Refresh our file list before display.
     uc_file_refresh();
     // Rebuilds uc_file table from directory contents! I sure hope it's smart about it...
     // Render everything.
     //  return $form + \Drupal::formBuilder()->buildForm('Drupal\uc_file\Form\ShowForm', $form, $form_state);
     //}
     $form['#attached']['library'][] = 'uc_file/uc_file.styles';
     $form['help'] = array('#prefix' => '<p>', '#markup' => $this->t('File downloads can be attached to any Ubercart product as a product feature. For security reasons the <a href=":download_url">file downloads directory</a> is separated from the Drupal <a href=":file_url">file system</a>. Below is the list of files (and their associated Ubercart products, if any) that can be used for file downloads.', [':download_url' => Url::fromRoute('uc_product.settings', [], ['query' => ['destination' => 'admin/store/products/files']])->toString(), ':file_url' => Url::fromRoute('system.file_system_settings')->toString()]), '#suffix' => '<p>');
     $form['uc_file_action'] = array('#type' => 'fieldset', '#title' => $this->t('File options'));
     // Set our default actions.
     $file_actions = array('uc_file_upload' => $this->t('Upload file(s)'), 'uc_file_delete' => $this->t('Delete file(s)'));
     // Check if any hook_uc_file_action('info', $args) are implemented.
     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;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL, $feature = NULL)
 {
     $file_config = $this->config('uc_file.settings');
     if (!is_dir($file_config->get('base_dir'))) {
         drupal_set_message($this->t('A file directory needs to be configured in <a href=":url">product settings</a> under the file download settings tab before a file can be selected.', [':url' => Url::fromRoute('uc_product.settings')->toString()]), 'warning');
         return $form;
     }
     // Rescan the file directory to populate {uc_files} with the current list
     // because files uploaded via any method other than the Upload button
     // (e.g. by FTP) won'b be in {uc_files} yet.
     uc_file_refresh();
     if (!db_query_range('SELECT 1 FROM {uc_files}', 0, 1)->fetchField()) {
         $form['file']['file_message'] = array('#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 model.', [':url' => Url::fromRoute('uc_file.downloads', [], ['query' => ['destination' => 'node/' . $node->id() . '/edit/features/file/add']])->toString()]));
         return $form;
     }
     // Grab all the models on this product.
     $models = uc_product_get_models($node->id());
     // Use the feature's values to fill the form, if they exist.
     if (!empty($feature)) {
         $file_product = db_query("SELECT * FROM {uc_file_products} p LEFT JOIN {uc_files} f ON p.fid = f.fid WHERE pfid = :pfid", [':pfid' => $feature['pfid']])->fetchObject();
         $default_feature = $feature['pfid'];
         $default_model = $file_product->model;
         $default_filename = $file_product->filename;
         $default_description = $file_product->description;
         $default_shippable = $file_product->shippable;
         $download_status = $file_product->download_limit != UC_FILE_LIMIT_SENTINEL;
         $download_value = $download_status ? $file_product->download_limit : NULL;
         $address_status = $file_product->address_limit != UC_FILE_LIMIT_SENTINEL;
         $address_value = $address_status ? $file_product->address_limit : NULL;
         $time_status = $file_product->time_granularity != UC_FILE_LIMIT_SENTINEL;
         $quantity_value = $time_status ? $file_product->time_quantity : NULL;
         $granularity_value = $time_status ? $file_product->time_granularity : 'never';
     } else {
         $default_feature = NULL;
         $default_model = '';
         $default_filename = '';
         $default_description = '';
         $default_shippable = $node->shippable->value;
         $download_status = FALSE;
         $download_value = NULL;
         $address_status = FALSE;
         $address_value = NULL;
         $time_status = FALSE;
         $quantity_value = NULL;
         $granularity_value = 'never';
     }
     $form['#attached']['library'][] = 'uc_file/uc_file.styles';
     $form['nid'] = array('#type' => 'value', '#value' => $node->id());
     $form['pfid'] = array('#type' => 'value', '#value' => $default_feature);
     $form['uc_file_model'] = array('#type' => 'select', '#title' => $this->t('SKU'), '#default_value' => $default_model, '#description' => $this->t('This is the SKU that will need to be purchased to obtain the file download.'), '#options' => $models);
     $form['uc_file_filename'] = array('#type' => 'textfield', '#title' => $this->t('File download'), '#default_value' => $default_filename, '#autocomplete_route_name' => 'uc_file.autocomplete_filename', '#description' => $this->t('The file that can be downloaded when product is purchased (enter a path relative to the %dir directory).', ['%dir' => $file_config->get('base_dir')]), '#maxlength' => 255, '#required' => TRUE);
     $form['uc_file_description'] = array('#type' => 'textfield', '#title' => $this->t('Description'), '#default_value' => $default_description, '#maxlength' => 255, '#description' => $this->t('A description of the download associated with the product.'));
     $form['uc_file_shippable'] = array('#type' => 'checkbox', '#title' => $this->t('Shippable product'), '#default_value' => $default_shippable, '#description' => $this->t('Check if this product model/SKU file download is also associated with a shippable product.'));
     $form['uc_file_limits'] = array('#type' => 'fieldset', '#description' => $this->t('Use these options to override any global download limits set at the :url.', [':url' => Link::createFromRoute($this->t('Ubercart product settings page'), 'uc_product.settings', [], ['query' => ['destination' => 'node/' . $node->id() . '/edit/features/file/add']])->toString()]), '#title' => $this->t('File limitations'));
     $form['uc_file_limits']['download_override'] = array('#type' => 'checkbox', '#title' => $this->t('Override download limit'), '#default_value' => $download_status);
     $form['uc_file_limits']['download_limit_number'] = array('#type' => 'textfield', '#title' => $this->t('Downloads'), '#default_value' => $download_value, '#description' => $this->t("The number of times this file can be downloaded."), '#maxlength' => 4, '#size' => 4, '#states' => array('visible' => array('input[name="download_override"]' => array('checked' => TRUE))));
     $form['uc_file_limits']['location_override'] = array('#type' => 'checkbox', '#title' => $this->t('Override IP address limit'), '#default_value' => $address_status);
     $form['uc_file_limits']['download_limit_addresses'] = array('#type' => 'textfield', '#title' => $this->t('IP addresses'), '#default_value' => $address_value, '#description' => $this->t("The number of unique IPs that a file can be downloaded from."), '#maxlength' => 4, '#size' => 4, '#states' => array('visible' => array('input[name="location_override"]' => array('checked' => TRUE))));
     $form['uc_file_limits']['time_override'] = array('#type' => 'checkbox', '#title' => $this->t('Override time limit'), '#default_value' => $time_status);
     $form['uc_file_limits']['download_limit_duration_qty'] = array('#type' => 'textfield', '#title' => $this->t('Time'), '#default_value' => $quantity_value, '#size' => 4, '#maxlength' => 4, '#prefix' => '<div class="duration">', '#suffix' => '</div>', '#states' => array('disabled' => array('select[name="download_limit_duration_granularity"]' => array('value' => 'never')), 'visible' => array('input[name="time_override"]' => array('checked' => TRUE))));
     $form['uc_file_limits']['download_limit_duration_granularity'] = array('#type' => 'select', '#default_value' => $granularity_value, '#options' => array('never' => $this->t('never'), 'day' => $this->t('day(s)'), 'week' => $this->t('week(s)'), 'month' => $this->t('month(s)'), 'year' => $this->t('year(s)')), '#description' => $this->t('How long after this product has been purchased until this file download expires.'), '#prefix' => '<div class="duration">', '#suffix' => '</div>', '#states' => array('visible' => array('input[name="time_override"]' => array('checked' => TRUE))));
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save feature'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Build the destination location. We start with the base directory,
     // then add any directory which was explicitly selected.
     $dir = $this->config('uc_file.settings')->get('base_dir') . '/' . $form_state->getValue('upload_dir');
     if (is_dir($dir)) {
         // Retrieve our uploaded files.
         $file_objects = $form_state->get('temp_files');
         foreach ($file_objects as $file_object) {
             // Copy the file to its final location.
             if (copy($file_object->getFileUri(), $dir . '/' . $file_object->getFilename())) {
                 // Check if any hook_uc_file_action('upload', $args) are implemented
                 $module_handler = \Drupal::moduleHandler();
                 foreach ($module_handler->getImplementations('uc_file_action') as $module) {
                     $name = $module . '_uc_file_action';
                     $name('upload', array('file_object' => $file_object, 'form_id' => $form_id, 'form_state' => $form_state));
                 }
                 // Update the file list
                 uc_file_refresh();
                 drupal_set_message($this->t('The file %file has been uploaded to %dir', ['%file' => $file_object->getFilename(), '%dir' => $dir]));
             } else {
                 drupal_set_message($this->t('An error occurred while copying the file to %dir', ['%dir' => $dir]), 'error');
             }
         }
     } else {
         drupal_set_message($this->t('Can not move file to %dir', ['%dir' => $dir]), 'error');
     }
     $form_state->setRedirect('uc_file.downloads');
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     switch ($form_state->getValue('action')) {
         case 'uc_file_delete':
             // File deletion status.
             $status = TRUE;
             // Delete the selected file(s), with recursion if selected.
             $status = uc_file_remove_by_id($form_state->getValue('file_ids'), !$form_state->isValueEmpty('recurse_directories')) && $status;
             if ($status) {
                 drupal_set_message(t('The selected file(s) have been deleted.'));
             } else {
                 drupal_set_message(t('One or more files could not be deleted.'));
             }
             break;
         case 'uc_file_upload':
             // Build the destination location. We start with the base directory,
             // then add any directory which was explicitly selected.
             $dir = \Drupal::config('uc_file.settings')->get('base_dir') . '/' . $form_state->getValue('upload_dir');
             if (is_dir($dir)) {
                 // Retrieve our uploaded file.
                 $file_object = $form_state->get('temp_file');
                 // Copy the file to its final location.
                 if (copy($file_object->uri, $dir . '/' . $file_object->filename)) {
                     // Check if any hook_uc_file_action('upload', $args) are implemented
                     $module_handler = \Drupal::moduleHandler();
                     foreach ($module_handler->getImplementations('uc_file_action') as $module) {
                         $name = $module . '_uc_file_action';
                         $name('upload', array('file_object' => $file_object, 'form_id' => $form_id, 'form_state' => $form_state));
                     }
                     // Update the file list
                     uc_file_refresh();
                     drupal_set_message(t('The file %file has been uploaded to %dir', array('%file' => $file_object->filename, '%dir' => $dir)));
                 } else {
                     drupal_set_message(t('An error occurred while copying the file to %dir', array('%dir' => $dir)));
                 }
             } else {
                 drupal_set_message(t('Can not move file to %dir', array('%dir' => $dir)));
             }
             break;
         default:
             // This action isn't handled by us, so check if any
             // hook_uc_file_action('submit', $args) are implemented
             $module_handler = \Drupal::moduleHandler();
             foreach ($module_handler->getImplementations('uc_file_action') as $module) {
                 $name = $module . '_uc_file_action';
                 $name('submit', array('form_id' => $form_id, 'form_state' => $form_state));
             }
             break;
     }
     // Return to the original form state.
     $form_state->setRebuild(FALSE);
     drupal_goto('admin/store/products/files');
 }