From b4c3c982eb7db338cc7eea5a1eb56a222e1023e2 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 12 Jul 2022 10:09:56 +0200 Subject: [PATCH 1/4] bug: image migration Refs: #RWR-167 --- .../custom/hr_paragraphs/drush.services.yml | 2 +- .../src/Commands/HrParagraphsCommands.php | 32 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/html/modules/custom/hr_paragraphs/drush.services.yml b/html/modules/custom/hr_paragraphs/drush.services.yml index 064c04a7..de4fafab 100644 --- a/html/modules/custom/hr_paragraphs/drush.services.yml +++ b/html/modules/custom/hr_paragraphs/drush.services.yml @@ -1,6 +1,6 @@ services: hr_paragraphs.commands: class: \Drupal\hr_paragraphs\Commands\HrParagraphsCommands - arguments: ['@config.factory', '@entity_type.manager', '@http_client', '@hr_paragraphs.reliefweb_controller'] + arguments: ['@config.factory', '@entity_type.manager', '@http_client', '@file.repository', '@file_system', '@hr_paragraphs.reliefweb_controller'] tags: - { name: drush.command } diff --git a/html/modules/custom/hr_paragraphs/src/Commands/HrParagraphsCommands.php b/html/modules/custom/hr_paragraphs/src/Commands/HrParagraphsCommands.php index 4d3f9d80..b738a7d6 100644 --- a/html/modules/custom/hr_paragraphs/src/Commands/HrParagraphsCommands.php +++ b/html/modules/custom/hr_paragraphs/src/Commands/HrParagraphsCommands.php @@ -4,6 +4,9 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\File\FileSystem; +use Drupal\Core\File\FileSystemInterface; +use Drupal\file\FileRepositoryInterface; use Drupal\hr_paragraphs\Controller\ReliefwebController; use Drupal\paragraphs\Entity\Paragraph; use Drupal\user\Entity\User; @@ -38,6 +41,20 @@ class HrParagraphsCommands extends DrushCommands { */ protected $httpClient; + /** + * File repository. + * + * @var \Drupal\Core\file\FileRepositoryInterface + */ + protected $fileRepository; + + /** + * File system. + * + * @var \Drupal\Core\file\FileSystemInterface + */ + protected $fileSystem; + /** * Reliefweb controller. * @@ -48,10 +65,12 @@ class HrParagraphsCommands extends DrushCommands { /** * {@inheritdoc} */ - public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, ClientInterface $http_client, ReliefwebController $reliefweb_controller) { + public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, ClientInterface $http_client, FileRepositoryInterface $file_repository, FileSystem $file_system, ReliefwebController $reliefweb_controller) { $this->configFactory = $config_factory; $this->entityTypeManager = $entity_type_manager; $this->httpClient = $http_client; + $this->fileRepository = $file_repository; + $this->fileSystem = $file_system; $this->reliefwebController = $reliefweb_controller; } @@ -698,6 +717,12 @@ protected function fixInlineImagesAndUrls($html) { foreach ($tags as $tag) { $src = $tag->getAttribute('src'); + // Set destination. + $file_name = basename($src); + $destination = 'public://images/' . date('Y-m-d'); + $this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY); + $destination .= '/' . $file_name; + $sync_domain = $this->configFactory->get('hr_paragraphs.settings')->get('sync_domain', 'http://hrinfo.docksal.site'); $sync_credentials = $this->configFactory->get('hr_paragraphs.settings')->get('sync_credentials', ''); if (!empty($sync_credentials)) { @@ -705,10 +730,9 @@ protected function fixInlineImagesAndUrls($html) { } $src = str_replace($this->configFactory->get('hr_paragraphs.settings')->get('sync_domain', 'http://hrinfo.docksal.site'), $sync_domain, $src); + // Get and save file. $image = file_get_contents($src); - - /** @var \Drupal\file\Entity\File $file */ - $file = file_save_data($image); + $file = $this->fileRepository->writeData($image, $destination); $tag->setAttribute('src', $file->createFileUrl()); } From e88764cb3ddee50472c44a48f673bd186a8969eb Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 12 Jul 2022 11:54:21 +0200 Subject: [PATCH 2/4] feat: Expose feature filter for HDX Refs: #RWR-166 --- .../src/Controller/HdxController.php | 125 ++++++++++++++++-- .../src/Controller/ParagraphController.php | 12 +- 2 files changed, 126 insertions(+), 11 deletions(-) diff --git a/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php b/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php index 49d17f23..516d953a 100644 --- a/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php +++ b/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php @@ -43,11 +43,20 @@ public function __construct(ClientInterface $http_client) { */ public function buildHdxActiveFacets(string $base_url, array $filters, array $all_facets) : array { $active_facets = []; + $yes_no_filters = $this->getHdxYesNoFilters(); + $hdx_query_filters = $this->getHdxQueryFilters(); foreach ($filters as $key => $keywords) { if (is_string($keywords)) { $name = $filters[$key]; - if (isset($all_facets[$key])) { + + if (in_array($key, $yes_no_filters) || in_array($key, $hdx_query_filters)) { + $name = $this->getHdxFilters($key) . ': ' . $this->t('No'); + if ($keywords == '1' || $keywords == 'true') { + $name = $this->getHdxFilters($key) . ': ' . $this->t('Yes'); + } + } + elseif (isset($all_facets[$key])) { foreach ($all_facets[$key]['items'] as $item) { if ($item['name'] == $name) { $name = $item['display_name']; @@ -117,20 +126,45 @@ public function buildHdxFacets(string $base_url, array $embedded_facets, array $ $facets = []; $facet_blocks = []; + $yes_no_filters = $this->getHdxYesNoFilters(); + $hdx_query_filters = $this->getHdxQueryFilters(); + $allowed_filters = $this->getHdxFilters(); foreach (array_keys($allowed_filters) as $key) { - $facets[$key] = $embedded_facets[$key]; + if (isset($embedded_facets[$key])) { + $facets[$key] = $embedded_facets[$key]; + } + } + + foreach (array_keys($hdx_query_filters) as $key) { + foreach ($embedded_facets['queries'] as $query_facet) { + if ($key == $query_facet['name']) { + $facets[$hdx_query_filters[$key]] = [ + 'items' => [ + [ + 'display_name' => '1', + 'name' => '1', + 'count' => $query_facet['count'], + ], + [ + 'display_name' => '0', + 'name' => '0', + ], + ], + ]; + } + } } foreach ($facets as $name => $facet) { $links = []; - // Sort facets. - uasort($facet['items'], function ($a, $b) { - return strcmp($a['display_name'], $b['display_name']); - }); - if (isset($facet['items']) && count($facet['items']) > 1) { + // Sort facets. + uasort($facet['items'], function ($a, $b) { + return strcmp($a['display_name'], $b['display_name']); + }); + foreach ($facet['items'] as $term) { $filter = [ $name => $term['name'], @@ -156,8 +190,20 @@ public function buildHdxFacets(string $base_url, array $embedded_facets, array $ } } + $title = $term['display_name']; + if (in_array($name, $yes_no_filters) || in_array($name, $hdx_query_filters)) { + $title = $this->t('No'); + if ($term['display_name'] == '1' || $term['display_name'] == 'true') { + $title = $this->t('Yes'); + } + } + + if (isset($term['count'])) { + $title = $title . ' (' . $term['count'] . ')'; + } + $links[] = [ - 'title' => $term['display_name'] . ' (' . $term['count'] . ')', + 'title' => $title, 'url' => Url::fromUserInput($base_url, [ 'query' => [ 'filters' => array_merge_recursive($filters, $filter), @@ -192,10 +238,25 @@ public function buildHdxFacets(string $base_url, array $embedded_facets, array $ * Search parameters. */ public function buildHdxParameters(int $offset, int $limit, array $query_filters) : array { + $filter_to_facet = [ + 'ext_subnational' => 'subnational', + 'ext_geodata' => 'has_geodata', + 'ext_requestdata' => 'extras_is_requestdata_type', + 'ext_quickcharts' => 'has_quickcharts', + 'ext_showcases' => 'has_showcases', + ]; + + $filter_to_query = [ + 'ext_hxl' => '{!key=hxl} vocab_Topics:hxl', + 'ext_sadd' => '{!key=sadd} vocab_Topics:"sex and age disaggregated data - sadd"', + 'ext_cod' => '{!key=cod} vocab_Topics:"common operational dataset - cod"', + ]; + $parameters = [ 'q' => '', 'fq' => '+dataset_type:dataset -extras_archived:true', 'fq_list' => [], + 'facet.query' => [], 'facet.field' => [ 'groups', 'res_format', @@ -209,6 +270,14 @@ public function buildHdxParameters(int $offset, int $limit, array $query_filters 'rows' => $limit, ]; + foreach ($filter_to_facet as $facet) { + $parameters['facet.field'][] = $facet; + } + + foreach ($filter_to_query as $facet) { + $parameters['facet.query'][] = $facet; + } + // Pasted filters from URL are using OR. foreach ($query_filters as $key => $values) { switch ($key) { @@ -263,6 +332,13 @@ public function getHdxFilters($key = NULL) { 'organization' => $this->t('Organizations'), 'vocab_Topics' => $this->t('Tags'), 'license_id' => $this->t('Licenses'), + 'subnational' => $this->t('Sub-national'), + 'has_geodata' => $this->t('Geodata'), + 'extras_is_requestdata_type' => $this->t('HDX connect'), + 'has_quickcharts' => $this->t('Quickcharts'), + 'has_showcases' => $this->t('Showcases'), + 'cod' => $this->t('CODs'), + 'ext_cod' => $this->t('CODs'), ]; if ($key) { @@ -278,6 +354,39 @@ public function getHdxFilters($key = NULL) { } } + /** + * Yes/No filters. + * + * @return array + * All yes/no filters. + */ + protected function getHdxYesNoFilters() { + $filters = [ + 'subnational', + 'has_geodata', + 'extras_is_requestdata_type', + 'has_quickcharts', + 'has_showcases', + 'cod', + ]; + + return $filters; + } + + /** + * Query filters. + * + * @return array + * All query filters. + */ + public function getHdxQueryFilters() { + $filters = [ + 'cod' => 'ext_cod', + ]; + + return $filters; + } + /** * Execute Hdx query. * diff --git a/html/modules/custom/hr_paragraphs/src/Controller/ParagraphController.php b/html/modules/custom/hr_paragraphs/src/Controller/ParagraphController.php index fef1a2cf..653f674e 100644 --- a/html/modules/custom/hr_paragraphs/src/Controller/ParagraphController.php +++ b/html/modules/custom/hr_paragraphs/src/Controller/ParagraphController.php @@ -403,12 +403,18 @@ public function getDatasets(Group $group, Request $request) : array|TrustedRedir $parameters = $this->hdxController->buildHdxParameters($offset, $limit, $query_filters); // Add filters. + $hdx_query_filters = $this->hdxController->getHdxQueryFilters(); foreach ($filters as $key => $filter) { - if (is_array($filter)) { - $parameters['fq_list'][] = $key . ':"' . implode('" AND "', $filter) . '"'; + if (in_array($key, $hdx_query_filters)) { + $parameters[$key] = $filter; } else { - $parameters['fq_list'][] = $key . ':"' . $filter . '"'; + if (is_array($filter)) { + $parameters['fq_list'][] = $key . ':"' . implode('" AND "', $filter) . '"'; + } + else { + $parameters['fq_list'][] = $key . ':"' . $filter . '"'; + } } } From da01d15325b56a6955578a43591fc6e5e9425c8b Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 12 Jul 2022 13:22:29 +0200 Subject: [PATCH 3/4] feat: limit feature filters for HDX Refs: #RWR-166 --- .../hr_paragraphs/src/Controller/HdxController.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php b/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php index 516d953a..28877d1c 100644 --- a/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php +++ b/html/modules/custom/hr_paragraphs/src/Controller/HdxController.php @@ -332,13 +332,10 @@ public function getHdxFilters($key = NULL) { 'organization' => $this->t('Organizations'), 'vocab_Topics' => $this->t('Tags'), 'license_id' => $this->t('Licenses'), - 'subnational' => $this->t('Sub-national'), - 'has_geodata' => $this->t('Geodata'), - 'extras_is_requestdata_type' => $this->t('HDX connect'), - 'has_quickcharts' => $this->t('Quickcharts'), - 'has_showcases' => $this->t('Showcases'), 'cod' => $this->t('CODs'), 'ext_cod' => $this->t('CODs'), + 'subnational' => $this->t('Sub-national'), + 'has_geodata' => $this->t('Geodata'), ]; if ($key) { @@ -362,12 +359,9 @@ public function getHdxFilters($key = NULL) { */ protected function getHdxYesNoFilters() { $filters = [ + 'cod', 'subnational', 'has_geodata', - 'extras_is_requestdata_type', - 'has_quickcharts', - 'has_showcases', - 'cod', ]; return $filters; From 2a8a506d46cc8edd5f93ced55081a455b5b09abc Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 12 Jul 2022 17:19:11 +0200 Subject: [PATCH 4/4] bug: ical feed without timezone Refs: #RWR-168 --- .../src/Controller/CalFileParser.php | 6 ++++ .../src/Controller/IcalController.php | 32 +++++++++++++------ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/html/modules/custom/hr_paragraphs/src/Controller/CalFileParser.php b/html/modules/custom/hr_paragraphs/src/Controller/CalFileParser.php index 56ab2889..bedd3c35 100644 --- a/html/modules/custom/hr_paragraphs/src/Controller/CalFileParser.php +++ b/html/modules/custom/hr_paragraphs/src/Controller/CalFileParser.php @@ -184,6 +184,12 @@ public function parse($file = '', $output = '') { $this->_user_timezone = $this->_file_timezone; } } + elseif (preg_match('/TZID:(.+)/i', $file_contents, $timezone) === 1) { + $this->_file_timezone = trim($timezone[1]); + if ($this->_user_timezone == NULL) { + $this->_user_timezone = $this->_file_timezone; + } + } else { $this->_file_timezone = $this->_user_timezone; } diff --git a/html/modules/custom/hr_paragraphs/src/Controller/IcalController.php b/html/modules/custom/hr_paragraphs/src/Controller/IcalController.php index e7206f23..d8324e55 100644 --- a/html/modules/custom/hr_paragraphs/src/Controller/IcalController.php +++ b/html/modules/custom/hr_paragraphs/src/Controller/IcalController.php @@ -99,9 +99,10 @@ public function getIcalEvents(Group $group, string $range_start = NULL, string $ foreach ($generator as $occurrence) { $output[] = [ - 'title' => $event['SUMMARY'], - 'description' => $event['DESCRIPTION'], - 'location' => $event['LOCATION'], + 'title' => $event['SUMMARY'] ?? '', + 'description' => $event['DESCRIPTION'] ?? '', + 'location' => $event['LOCATION'] ?? '', + 'backgroundColor' => $this->setBackgroundColor($event['CATEGORIES'] ?? ''), 'start' => $occurrence->getStart()->format(\DateTimeInterface::W3C), 'end' => $occurrence->getEnd()->format(\DateTimeInterface::W3C), 'attachments' => $attachments, @@ -123,9 +124,10 @@ public function getIcalEvents(Group $group, string $range_start = NULL, string $ } $output[] = [ - 'title' => $event['SUMMARY'], - 'description' => $event['DESCRIPTION'], - 'location' => $event['LOCATION'], + 'title' => $event['SUMMARY'] ?? '', + 'description' => $event['DESCRIPTION'] ?? '', + 'location' => $event['LOCATION'] ?? '', + 'backgroundColor' => $this->setBackgroundColor($event['CATEGORIES'] ?? ''), 'start' => $event['DTSTART']->format(\DateTimeInterface::W3C), 'end' => $event['DTEND']->format(\DateTimeInterface::W3C), 'attachments' => $attachments, @@ -133,9 +135,10 @@ public function getIcalEvents(Group $group, string $range_start = NULL, string $ } else { $output[] = [ - 'title' => $event['SUMMARY'], - 'description' => $event['DESCRIPTION'], - 'location' => $event['LOCATION'], + 'title' => $event['SUMMARY'] ?? '', + 'description' => $event['DESCRIPTION'] ?? '', + 'location' => $event['LOCATION'] ?? '', + 'backgroundColor' => $this->setBackgroundColor($event['CATEGORIES'] ?? ''), 'start' => $event['DTSTART']->format(\DateTimeInterface::W3C), 'end' => $event['DTEND']->format(\DateTimeInterface::W3C), 'attachments' => $attachments, @@ -147,4 +150,15 @@ public function getIcalEvents(Group $group, string $range_start = NULL, string $ return $output; } + /** + * Calculate color value from string. + */ + protected function setBackgroundColor($category) { + if (empty($category)) { + return ''; + } + + return '#' . substr(md5($category), 0, 6); + } + }