You are here

function ServiceAuditFilesReferencedNotUsed::_auditfiles_referenced_not_used_batch_add_process_file in Audit Files 8

Adds the specified file to the file_usage table.

Parameters

string $reference_id: The ID for keeping track of the reference.

File

src/ServiceAuditFilesReferencedNotUsed.php, line 186
providing the service that used in 'referenced not used' functionality.

Class

ServiceAuditFilesReferencedNotUsed

Namespace

Drupal\auditfiles

Code

function _auditfiles_referenced_not_used_batch_add_process_file($reference_id) {
  $reference_id_parts = explode('.', $reference_id);
  $connection = Database::getConnection();
  $data = [
    'fid' => $reference_id_parts[4],
    // @todo This is hard coded for now, but need to determine how to figure out
    // which module needs to be here.
    'module' => 'file',
    'type' => $reference_id_parts[3],
    'id' => $reference_id_parts[2],
    'count' => 1,
  ];

  // Make sure the file is not already in the database.
  $query = 'SELECT fid FROM file_usage
    WHERE fid = :fid AND module = :module AND type = :type AND id = :id';
  $existing_file = $connection
    ->query($query, [
    ':fid' => $data['fid'],
    ':module' => $data['module'],
    ':type' => $data['type'],
    ':id' => $data['id'],
  ])
    ->fetchAll();
  if (empty($existing_file)) {

    // The file is not already in the database, so add it.
    $connection
      ->insert('file_usage')
      ->fields($data)
      ->execute();
  }
  else {
    drupal_set_message(t('The file is already in the file_usage table (file id: "@fid", module: "@module", type: "@type", entity id: "@id").', [
      '@fid' => $data['fid'],
      '@module' => $data['module'],
      '@type' => $data['type'],
      '@id' => $data['id'],
    ]), 'error');
  }
}