You are here

class AuditFilesMergeFileReferences in Audit Files 8

Same name and namespace in other branches
  1. 8.3 src/Form/AuditFilesMergeFileReferences.php \Drupal\auditfiles\Form\AuditFilesMergeFileReferences
  2. 8.2 src/Form/AuditFilesMergeFileReferences.php \Drupal\auditfiles\Form\AuditFilesMergeFileReferences
  3. 4.x src/Form/AuditFilesMergeFileReferences.php \Drupal\auditfiles\Form\AuditFilesMergeFileReferences

Hierarchy

Expanded class hierarchy of AuditFilesMergeFileReferences

1 string reference to 'AuditFilesMergeFileReferences'
auditfiles.routing.yml in ./auditfiles.routing.yml
auditfiles.routing.yml

File

src/Form/AuditFilesMergeFileReferences.php, line 15

Namespace

Drupal\auditfiles\Form
View source
class AuditFilesMergeFileReferences extends FormBase implements ConfirmFormInterface {

  /**
   * Widget Id.
   */
  public function getFormId() {
    return 'mergefilereferences';
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this
      ->t('This action cannot be undone.');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Confirm');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelText() {
    return $this
      ->t('Cancel');
  }

  /**
   * {@inheritdoc}
   */
  public function getFormName() {
    return 'mergeFileReferences';
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('auditfiles.audit_files_mergefilereferences');
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t("Do you wan't to merge following record");
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = \Drupal::config('auditfiles_config.settings');
    $connection = Database::getConnection();
    $storage = $form_state
      ->getStorage();
    $date_format = $config
      ->get('auditfiles_report_options_date_format') ? $config
      ->get('auditfiles_report_options_date_format') : 'long';
    if (isset($storage['confirm'])) {
      if ($storage['stage'] == 'confirm') {
        $header = [
          'origname' => [
            'data' => $this
              ->t('Filename'),
          ],
          'fileid' => [
            'data' => $this
              ->t('File ID'),
          ],
          'fileuri' => [
            'data' => $this
              ->t('URI'),
          ],
          'filesize' => [
            'data' => $this
              ->t('Size'),
          ],
          'timestamp' => [
            'data' => $this
              ->t('Time uploaded'),
          ],
        ];
        $files = [];
        $values = $form_state
          ->getValue('files_being_merged');
        foreach ($values as $file_id) {
          if (!empty($file_id)) {
            $query = $connection
              ->select('file_managed', 'fm');
            $query
              ->condition('fm.fid', $file_id);
            $query
              ->fields('fm', [
              'fid',
              'uid',
              'filename',
              'uri',
              'filemime',
              'filesize',
              'created',
              'status',
            ]);
            $results = $query
              ->execute()
              ->fetchAll();
            $file = $results[0];
            if (!empty($file)) {
              $files[$file_id] = [
                'origname' => $file->filename,
                'fileid' => $file_id,
                'fileuri' => $file->uri,
                'filesize' => number_format($file->filesize),
                'timestamp' => format_date($file->created, $date_format),
              ];
            }
            else {
              drupal_set_message(t('A file object was not found for file ID @fid.', [
                '@fid' => $file_id,
              ]));
            }
          }
          else {
            unset($form_state
              ->getValue('files_being_merged')[$file_id]);
          }
        }

        // Save the selected items for later use.
        $storage['files_being_merged'] = $files;
        $form_state
          ->setStorage($storage);
        $form['file_being_kept'] = [
          '#type' => 'tableselect',
          '#header' => $header,
          '#options' => $files,
          '#default_value' => key($files),
          '#empty' => $this
            ->t('No items found.'),
          '#multiple' => FALSE,
        ];
        $form['#title'] = $this
          ->t('Which file will be the one the others are merged into?');
        $form['#attributes']['class'][] = 'confirmation';
        $form['actions'] = [
          '#type' => 'actions',
        ];
        $form['actions']['submit'] = [
          '#type' => 'submit',
          '#value' => $this
            ->getConfirmText(),
          '#button_type' => 'primary',
          '#submit' => [
            '::confirmSubmissionHandlerFileMerge',
          ],
        ];
        $form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this
          ->getRequest());
        if (!isset($form['#theme'])) {
          $form['#theme'] = 'confirm_form';
        }
        return $form;
      }
      else {
        if ($storage['stage'] == 'preconfirm') {
          $header = [
            'filename' => [
              'data' => $this
                ->t('Filename'),
            ],
            'fileid' => [
              'data' => $this
                ->t('File ID'),
            ],
            'fileuri' => [
              'data' => $this
                ->t('URI'),
            ],
            'filesize' => [
              'data' => $this
                ->t('Size'),
            ],
            'timestamp' => [
              'data' => $this
                ->t('Time uploaded'),
            ],
          ];
          $files = [];
          $values = $form_state
            ->getValue('files');
          foreach ($values as $file_name) {
            if (!empty($file_name)) {
              $query = 'SELECT fid FROM {file_managed} WHERE filename = :file_name ORDER BY uri ASC';
              $results = $connection
                ->query($query, [
                ':file_name' => $file_name,
              ])
                ->fetchAll();
              if (!empty($results)) {
                foreach ($results as $result) {
                  $query = $connection
                    ->select('file_managed', 'fm');
                  $query
                    ->condition('fm.fid', $result->fid);
                  $query
                    ->fields('fm', [
                    'fid',
                    'uid',
                    'filename',
                    'uri',
                    'filemime',
                    'filesize',
                    'created',
                    'status',
                  ]);
                  $results = $query
                    ->execute()
                    ->fetchAll();
                  $file = $results[0];
                  if (!empty($file)) {
                    $files[$result->fid] = [
                      'filename' => $file->filename,
                      'fileid' => $file->fid,
                      'fileuri' => $file->uri,
                      'filesize' => number_format($file->filesize),
                      'timestamp' => format_date($file->created, $date_format),
                    ];
                  }
                  else {
                    drupal_set_message(t('A file object was not found for file ID @fid.', [
                      '@fid' => $result->fid,
                    ]));
                  }
                }
              }
            }
            else {
              unset($form_state
                ->getValue('files')[$file_name]);
            }
          }
          $form['files_being_merged'] = [
            '#type' => 'tableselect',
            '#header' => $header,
            '#options' => $files,
            '#empty' => $this
              ->t('No items found.'),
          ];
          if (!empty($files)) {
            $form['actions'] = [
              '#type' => 'actions',
              'submit' => [
                '#type' => 'submit',
                '#value' => $this
                  ->t('Next step'),
                '#submit' => [
                  '::submissionHandlerMergeRecordPreConfirm',
                ],
              ],
            ];
          }
          return $form;
        }
      }
    }
    $file_names = \Drupal::service('auditfiles.merge_file_references')
      ->_auditfiles_merge_file_references_get_file_list();
    if (!empty($file_names)) {
      $date_format = $config
        ->get('auditfiles_report_options_date_format') ? $config
        ->get('auditfiles_report_options_date_format') : 'long';
      foreach ($file_names as $file_name) {
        $rows[$file_name] = \Drupal::service('auditfiles.merge_file_references')
          ->_auditfiles_merge_file_references_get_file_data($file_name, $date_format);
      }
    }

    // Set up the pager.
    if (!empty($rows)) {
      $items_per_page = $config
        ->get('auditfiles_report_options_items_per_page') ? $config
        ->get('auditfiles_report_options_items_per_page') : 50;
      if (!empty($items_per_page)) {
        $current_page = pager_default_initialize(count($rows), $items_per_page);

        // Break the total data set into page sized chunks.
        $pages = array_chunk($rows, $items_per_page, TRUE);
      }
    }

    // Setup the record count and related messages.
    $maximum_records = $config
      ->get('auditfiles_report_options_maximum_records') ? $config
      ->get('auditfiles_report_options_maximum_records') : 250;
    if (!empty($rows)) {
      if ($maximum_records > 0) {
        $file_count_message = 'Found at least @count files in the file_managed table with duplicate file names.';
      }
      else {
        $file_count_message = 'Found @count files in the file_managed table with duplicate file names.';
      }
      $form_count = $this
        ->formatPlural(count($rows), 'Found 1 file in the file_managed table with a duplicate file name.', $file_count_message);
    }
    else {
      $form_count = $this
        ->t('Found no files in the file_managed table with duplicate file names.');
    }
    $form['filter']['single_file_names']['auditfiles_merge_file_references_show_single_file_names'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show files without duplicate names'),
      '#default_value' => $config
        ->get('auditfiles_merge_file_references_show_single_file_names') ? $config
        ->get('auditfiles_merge_file_references_show_single_file_names') : 0,
      '#suffix' => '<div>' . $this
        ->t("Use this button to reset this report's variables and load the page anew.") . '</div>',
    ];
    $form['files'] = [
      '#type' => 'tableselect',
      '#header' => \Drupal::service('auditfiles.merge_file_references')
        ->_auditfiles_merge_file_references_get_header(),
      '#empty' => $this
        ->t('No items found.'),
      '#prefix' => '<div><em>' . $form_count . '</em></div>',
    ];
    if (!empty($rows) && !empty($pages)) {
      $form['files']['#options'] = $pages[$current_page];
    }
    elseif (!empty($rows)) {
      $form['files']['#options'] = $rows;
    }
    else {
      $form['files']['#options'] = [];
    }
    if (!empty($rows)) {
      $form['actions'] = [
        '#type' => 'actions',
      ];
      $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Merge selected items'),
        '#submit' => [
          '::submissionHandlerMergeRecord',
        ],
      ];
      $form['pager'] = [
        '#type' => 'pager',
      ];
    }
    return $form;
  }

  /**
   * Form validation.
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $storage = $form_state
      ->getStorage();
    if (isset($storage['confirm'])) {
      if ($storage['stage'] == 'preconfirm') {
        $counter = 0;
        foreach ($form_state
          ->getValue('files_being_merged') as $file) {
          if (!empty($file)) {
            $counter++;
          }
        }
        if ($counter < 2) {
          $form_state
            ->setErrorByName('files_being_merged', $this
            ->t('At least two file IDs must be selected in order to merge them.'));
        }
      }
    }
  }

  /**
   * Submit form.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * Submit form.
   */
  public function submissionHandlerMergeRecord(array &$form, FormStateInterface $form_state) {
    \Drupal::configFactory()
      ->getEditable('auditfiles_config.settings')
      ->set('auditfiles_merge_file_references_show_single_file_names', $form_state
      ->getValue('auditfiles_merge_file_references_show_single_file_names'))
      ->save();
    if (!empty($form_state
      ->getValue('files'))) {
      foreach ($form_state
        ->getValue('files') as $file_id) {
        if (!empty($file_id)) {
          $storage = [
            'files' => $form_state
              ->getValue('files'),
            'confirm' => TRUE,
            'stage' => 'preconfirm',
          ];
          $form_state
            ->setStorage($storage);
          $form_state
            ->setRebuild();
        }
      }
      if (!isset($storage)) {
        drupal_set_message($this
          ->t('At least one file name must be selected in order to merge the file IDs. No changes were made.'), 'error');
      }
    }
  }

  /**
   * Preconfirm form submission.
   */
  public function submissionHandlerMergeRecordPreConfirm(array &$form, FormStateInterface $form_state) {
    if (!empty($form_state
      ->getValue('files_being_merged'))) {
      foreach ($form_state
        ->getValue('files_being_merged') as $file_id) {
        if (!empty($file_id)) {
          $storage = [
            'files_being_merged' => $form_state
              ->getValue('files_being_merged'),
            'confirm' => TRUE,
            'stage' => 'confirm',
          ];
          $form_state
            ->setStorage($storage);
          $form_state
            ->setRebuild();
        }
      }
    }
  }

  /**
   * Confirm form submission.
   */
  public function confirmSubmissionHandlerFileMerge(array &$form, FormStateInterface $form_state) {
    $storage = $form_state
      ->getStorage();
    batch_set(\Drupal::service('auditfiles.merge_file_references')
      ->_auditfiles_merge_file_references_batch_merge_create_batch($form_state
      ->getValue('file_being_kept'), $storage['files_being_merged']));
    unset($storage['stage']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuditFilesMergeFileReferences::buildForm public function Form constructor. Overrides FormInterface::buildForm
AuditFilesMergeFileReferences::confirmSubmissionHandlerFileMerge public function Confirm form submission.
AuditFilesMergeFileReferences::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText
AuditFilesMergeFileReferences::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
AuditFilesMergeFileReferences::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormInterface::getConfirmText
AuditFilesMergeFileReferences::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormInterface::getDescription
AuditFilesMergeFileReferences::getFormId public function Widget Id. Overrides FormInterface::getFormId
AuditFilesMergeFileReferences::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
AuditFilesMergeFileReferences::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
AuditFilesMergeFileReferences::submissionHandlerMergeRecord public function Submit form.
AuditFilesMergeFileReferences::submissionHandlerMergeRecordPreConfirm public function Preconfirm form submission.
AuditFilesMergeFileReferences::submitForm public function Submit form. Overrides FormInterface::submitForm
AuditFilesMergeFileReferences::validateForm public function Form validation. Overrides FormBase::validateForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.