View source
<?php
namespace Drupal\auditfiles;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Database\Connection;
class ServiceAuditFilesUsedNotManaged {
use MessengerTrait;
protected $stringTranslation;
protected $configFactory;
protected $connection;
public function __construct(TranslationInterface $translation, ConfigFactory $config_factory, Connection $connection) {
$this->stringTranslation = $translation;
$this->configFactory = $config_factory;
$this->connection = $connection;
}
public function auditfilesUsedNotManagedGetFileList() {
$connection = $this->connection;
$config = $this->configFactory
->get('auditfiles.settings');
$fm_query = $connection
->select('file_managed', 'fm')
->fields('fm', [
'fid',
])
->execute()
->fetchCol();
$query = $connection
->select('file_usage', 'fu')
->fields('fu', [
'fid',
]);
if (!empty($fm_query)) {
$query
->condition('fu.fid', $fm_query, 'NOT IN');
}
$maximum_records = $config
->get('auditfiles_report_options_maximum_records');
if ($maximum_records > 0) {
$query
->range(0, $maximum_records);
}
return $query
->execute()
->fetchCol();
}
public function auditfilesUsedNotManagedGetFileData($file_id) {
$connection = $this->connection;
$query = 'SELECT * FROM {file_usage} WHERE fid = :file_id';
$file = $connection
->query($query, [
'file_id' => $file_id,
])
->fetchObject();
$url = Url::fromUri('internal:/' . $file->type . '/' . $file->id);
$result = Link::fromTextAndUrl($file->type . '/' . $file->id, $url)
->toString();
return [
'fid' => $file->fid,
'module' => $file->module . ' ' . $this->stringTranslation
->translate('module'),
'id' => $result,
'count' => $file->count,
];
}
public function auditfilesUsedNotManagedGetHeader() {
return [
'fid' => [
'data' => $this->stringTranslation
->translate('File ID'),
],
'module' => [
'data' => $this->stringTranslation
->translate('Used by'),
],
'id' => [
'data' => $this->stringTranslation
->translate('Used in'),
],
'count' => [
'data' => $this->stringTranslation
->translate('Count'),
],
];
}
public function auditfilesUsedNotManagedBatchDeleteCreateBatch(array $fileids) {
$batch['error_message'] = $this->stringTranslation
->translate('One or more errors were encountered processing the files.');
$batch['finished'] = '\\Drupal\\auditfiles\\Batch\\AuditFilesBatchProcess::finishBatch';
$batch['progress_message'] = $this->stringTranslation
->translate('Completed @current of @total operations.');
$batch['title'] = $this->stringTranslation
->translate('Deleting files from the file_usage table');
$operations = [];
foreach ($fileids as $file_id) {
if ($file_id != 0) {
$operations[] = [
'\\Drupal\\auditfiles\\Batch\\AuditFilesUsedNotManagedBatchProcess::auditfilesUsedNotManagedBatchDeleteProcessBatch',
[
$file_id,
],
];
}
}
$batch['operations'] = $operations;
return $batch;
}
public function auditfilesUsedNotManagedBatchDeleteProcessFile($file_id) {
$connection = $this->connection;
$num_rows = $connection
->delete('file_usage')
->condition('fid', $file_id)
->execute();
if (empty($num_rows)) {
$this
->messenger()
->addWarning($this->stringTranslation
->translate('There was a problem deleting the record with file ID %fid from the file_usage table. Check the logs for more information.', [
'%fid' => $file_id,
]));
}
else {
$this
->messenger()
->addStatus($this->stringTranslation
->translate('Sucessfully deleted File ID : %fid from the file_usages table.', [
'%fid' => $file_id,
]));
}
}
}