Skip to content

Commit

Permalink
Merge pull request #256 from UN-OCHA/develop
Browse files Browse the repository at this point in the history
Deploy 18/07/2022
  • Loading branch information
lazysoundsystem authored Jul 18, 2022
2 parents 192ab3d + 6f27ef2 commit fc54a74
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 25 deletions.
2 changes: 1 addition & 1 deletion html/modules/custom/hr_paragraphs/drush.services.yml
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -698,17 +717,22 @@ 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)) {
$sync_domain = str_replace('https://', 'https://' . $sync_credentials . '@', $sync_domain);
}
$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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
119 changes: 111 additions & 8 deletions html/modules/custom/hr_paragraphs/src/Controller/HdxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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'],
Expand All @@ -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),
Expand Down Expand Up @@ -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',
Expand All @@ -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) {
Expand Down Expand Up @@ -263,6 +332,10 @@ public function getHdxFilters($key = NULL) {
'organization' => $this->t('Organizations'),
'vocab_Topics' => $this->t('Tags'),
'license_id' => $this->t('Licenses'),
'cod' => $this->t('CODs'),
'ext_cod' => $this->t('CODs'),
'subnational' => $this->t('Sub-national'),
'has_geodata' => $this->t('Geodata'),
];

if ($key) {
Expand All @@ -278,6 +351,36 @@ public function getHdxFilters($key = NULL) {
}
}

/**
* Yes/No filters.
*
* @return array<string>
* All yes/no filters.
*/
protected function getHdxYesNoFilters() {
$filters = [
'cod',
'subnational',
'has_geodata',
];

return $filters;
}

/**
* Query filters.
*
* @return array<string>
* All query filters.
*/
public function getHdxQueryFilters() {
$filters = [
'cod' => 'ext_cod',
];

return $filters;
}

/**
* Execute Hdx query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -123,19 +124,21 @@ 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,
];
}
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,
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '"';
}
}
}

Expand Down

0 comments on commit fc54a74

Please sign in to comment.