You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
5.1 KiB
173 lines
5.1 KiB
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace Drupal\islandora_collection_harvest\Form; |
|
|
|
use Drupal\Core\Form\FormBase; |
|
use Drupal\Core\Form\FormStateInterface; |
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
use Drupal\Core\File\FileSystemInterface; |
|
use Drupal\flysystem\FlysystemFactory; |
|
use Drupal\islandora\IslandoraUtils; |
|
use Drupal\Core\TempStore\PrivateTempStoreFactory; |
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
/** |
|
* Form for selecting Collection and Media Use and preparing ZIP download. |
|
*/ |
|
final class CollectionHarvestForm extends FormBase { |
|
|
|
protected EntityTypeManagerInterface $entityTypeManager; |
|
|
|
protected IslandoraUtils $utils; |
|
|
|
protected FileSystemInterface $fileSystem; |
|
|
|
protected FlysystemFactory $flysystemFactory; |
|
|
|
protected PrivateTempStoreFactory $tempStoreFactory; |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function __construct( |
|
EntityTypeManagerInterface $entity_type_manager, |
|
IslandoraUtils $utils, |
|
FileSystemInterface $file_system, |
|
FlysystemFactory $flysystem_factory, |
|
PrivateTempStoreFactory $temp_store_factory |
|
) { |
|
$this->entityTypeManager = $entity_type_manager; |
|
$this->utils = $utils; |
|
$this->fileSystem = $file_system; |
|
$this->flysystemFactory = $flysystem_factory; |
|
$this->tempStoreFactory = $temp_store_factory; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public static function create(ContainerInterface $container): self { |
|
return new self( |
|
$container->get('entity_type.manager'), |
|
$container->get('islandora.utils'), |
|
$container->get('file_system'), |
|
$container->get('flysystem_factory'), |
|
$container->get('tempstore.private') |
|
); |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function getFormId(): string { |
|
return 'islandora_collection_harvest_form'; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function buildForm(array $form, FormStateInterface $form_state): array { |
|
$term = $this->utils->getTermForUri('http://purl.org/dc/dcmitype/Collection'); |
|
$collections = []; |
|
if ($term) { |
|
$query = $this->entityTypeManager->getStorage('node')->getQuery(); |
|
$query->condition('type', 'islandora_object'); |
|
$query->condition('field_model', $term->id(), 'IN'); |
|
$query->accessCheck(FALSE); |
|
$nids = $query->execute(); |
|
$nodes = $this->entityTypeManager->getStorage('node') |
|
->loadMultiple($nids); |
|
foreach ($nodes as $node) { |
|
$collections[$node->id()] = $node->label(); |
|
} |
|
} |
|
|
|
$vid = 'islandora_media_use'; |
|
$terms = $this->entityTypeManager->getStorage('taxonomy_term') |
|
->loadTree($vid); |
|
$media_use_options = []; |
|
foreach ($terms as $term) { |
|
$media_use_options[$term->tid] = $term->name; |
|
} |
|
|
|
$form['#attributes']['id'] = 'collection-harvest-form'; |
|
$form['collection'] = [ |
|
'#type' => 'select', |
|
'#title' => $this->t('Collection'), |
|
'#options' => $collections, |
|
'#required' => TRUE, |
|
]; |
|
|
|
$form['media_use'] = [ |
|
'#type' => 'select', |
|
'#title' => $this->t('Media Use'), |
|
'#options' => $media_use_options, |
|
'#required' => TRUE, |
|
]; |
|
|
|
$form['actions'] = [ |
|
'#type' => 'actions', |
|
'submit' => [ |
|
'#type' => 'submit', |
|
'#value' => $this->t('Build Download'), |
|
'#button_type' => 'primary', |
|
], |
|
]; |
|
return $form; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function submitForm(array &$form, FormStateInterface $form_state): void { |
|
$collection_id = $form_state->getValue('collection'); |
|
$media_use_tid = $form_state->getValue('media_use'); |
|
|
|
$term = $this->entityTypeManager->getStorage('taxonomy_term') |
|
->load($media_use_tid); |
|
if (!$term) { |
|
$this->messenger()->addError($this->t('Media use term not found.')); |
|
return; |
|
} |
|
|
|
// Load nodes in collection |
|
$query = $this->entityTypeManager->getStorage('node')->getQuery(); |
|
$query->condition('type', 'islandora_object'); |
|
$query->condition('field_member_of', $collection_id); |
|
$query->accessCheck(FALSE); |
|
$nids = $query->execute(); |
|
|
|
$media_entities = []; |
|
foreach ($nids as $nid) { |
|
$node = $this->entityTypeManager->getStorage('node')->load($nid); |
|
$media = $this->utils->getMediaWithTerm($node, $term); |
|
if ($media) { |
|
$source_field = $media->getSource() |
|
->getConfiguration()['source_field'] ?? NULL; |
|
if ($source_field && $media->hasField($source_field) && !$media->get($source_field) |
|
->isEmpty()) { |
|
$media_entities[] = $media; |
|
} |
|
} |
|
} |
|
|
|
if (empty($media_entities)) { |
|
$this->messenger() |
|
->addError($this->t('No media found for this collection.')); |
|
return; |
|
} |
|
|
|
// Store in tempstore |
|
$temp_store = $this->tempStoreFactory->get('islandora_collection_harvest'); |
|
$zip_filename = 'collection_' . time() . '.zip'; |
|
$temp_store->set($zip_filename, $media_entities); |
|
$this->messenger() |
|
->addStatus($this->t('Your ZIP download has been initiated. Please wait for the download to start.')); |
|
|
|
// Redirect to download route |
|
$form_state->setRedirect('islandora_collection_harvest.download_zip', ['filename' => $zip_filename]); |
|
} |
|
|
|
}
|
|
|