Skip to content

Commit

Permalink
Merge pull request #567 from UN-OCHA/feature/RW-700
Browse files Browse the repository at this point in the history
[RW-700] Better distinction between rivers, sub rivers etc.
  • Loading branch information
orakili authored Feb 16, 2023
2 parents bd2adf7 + 28ddba3 commit ce8bf83
Show file tree
Hide file tree
Showing 21 changed files with 826 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function getEntityMetaFromField($field, $code = NULL, array $extra_fields
if ($code !== NULL) {
$url = RiverServiceBase::getRiverUrl($this->bundle(), [
'advanced-search' => '(' . $code . $entity->id() . ')',
]);
], $entity->label(), TRUE);
}
else {
$url = $entity->toUrl();
Expand Down
100 changes: 25 additions & 75 deletions html/modules/custom/reliefweb_entities/src/Entity/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Drupal\reliefweb_entities\SectionedContentTrait;
use Drupal\reliefweb_revisions\EntityRevisionedInterface;
use Drupal\reliefweb_revisions\EntityRevisionedTrait;
use Drupal\reliefweb_rivers\AdvancedSearch;
use Drupal\reliefweb_rivers\Parameters;
use Drupal\reliefweb_rivers\RiverServiceBase;
use Drupal\reliefweb_utility\Helpers\MediaHelper;

Expand Down Expand Up @@ -231,7 +229,8 @@ public function getIcon() {
* Convert a river URL to API payload.
*
* @param array $data
* Array with the river URL, title and optional override.
* Array with the river URL, title and optional override, limit, view
* and exclude properties.
* @param int $limit
* Number of resources to retrieve.
*
Expand All @@ -250,103 +249,54 @@ protected function riverUrlToApi(array $data, $limit = 3) {
$url = $data['url'];
$title = $data['title'];
$override = $data['override'] ?? NULL;
$limit = $data['limit'] ?? $limit;

$info = RiverServiceBase::getRiverServiceFromUrl($url);
if (empty($info)) {
$service = RiverServiceBase::getRiverServiceFromUrl($url);
if (empty($service)) {
return [];
}

// Extract the query part from the URL.
parse_str(parse_url($url, PHP_URL_QUERY), $query);

// Parse the query parameters.
$parameters = new Parameters($query);

// Service and river information.
$service = $info['service'];
$resource = $service->getResource();
$bundle = $service->getBundle();
$river = $service->getRiver();

// Get the river view.
$view = $data['view'] ?? $info['view'] ?? $parameters->getString('view');
$views = $service->getViews();
$view = isset($views[$view]) ? $view : $service->getDefaultView();
if ($view === $service->getDefaultView()) {
$parameters->remove('view');
}
else {
$parameters->set('view', $view);
// Update the river view.
if (isset($data['view'])) {
$service->setSelectedView($data['view']);
}

// Get the advanced search handler.
$advanced_search = new AdvancedSearch(
$bundle,
$river,
$parameters,
$service->getFilters(),
$service->getFilterSample()
);

// Get the sanitized search parameter.
$search = $parameters->getString('search');

// Get the API payload for the river and set the limit.
$payload = $service->getApiPayload($view);
$payload['limit'] = $data['limit'] ?? $limit;
// Get the API payload ready for the API request.
$payload = $service->prepareApiRequest($limit, FALSE);

// If an override is defined we add a search condition on the override ID
// with a boost and sort by score first. This will results in the override
// document to appear first if it exists and keep the rest of the documents
// in the proper order.
if (!empty($override) && is_numeric($override)) {
if (!empty($search)) {
$search = 'id:' . $override . ' OR (' . $search . ')';
if (!empty($payload['query']['value'])) {
$payload['query']['value'] = 'id:' . $override . ' OR (' . $payload['query']['value'] . ')';
}
else {
$search = 'id:' . $override . '^1000 OR id:>0';
$payload['query']['value'] = 'id:' . $override . '^1000 OR id:>0';
}
array_unshift($payload['sort'], 'score:desc');
}

// Set the full text search query or remove it if empty.
if (!empty($search)) {
$payload['query']['value'] = $search;
}
else {
unset($payload['query']);
}

// Generate the API filter with the facet and advanced search filters.
$filter = $advanced_search->getApiFilter();
if (!empty($filter)) {
// Update the payload filter.
if (!empty($payload['filter'])) {
$payload['filter'] = [
'conditions' => [
$payload['filter'],
$filter,
],
'operator' => 'AND',
];
}
else {
$payload['filter'] = $filter;
}
}

return [
'resource' => $resource,
'bundle' => $bundle,
'resource' => $service->getResource(),
'bundle' => $service->getBundle(),
'entity_type' => $service->getEntityTypeId(),
'river' => $river,
'river' => $service->getRiver(),
'title' => $title,
'view' => $view,
'view' => $service->getSelectedView(),
'payload' => $payload,
'exclude' => $data['exclude'] ?? [],
// Create a sanitized version of the given URL for the view more.
'more' => [
'url' => RiverServiceBase::getRiverUrl($bundle, $parameters->getAll()),
'url' => RiverServiceBase::getRiverUrl(
$service->getBundle(),
$service->getParameters()->getAllSorted(['list'])
// @todo after reviewing the impact of adding the list parameter to
// river URL we may want to revisit what to do with the ones used
// in topics. For now no title because it's too random.
// @see RW-700, RW-706
),
'label' => $this->t('View all @title', [
'@title' => $title,
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getEntityMeta() {
'name' => $this->field_cost->value,
'url' => RiverServiceBase::getRiverUrl($this->bundle(), [
'advanced-search' => '(CO' . $this->field_cost->value . ')',
]),
], $this->field_cost->value === 'free' ? $this->t('Free') : $this->t('Fee-based'), TRUE),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ public function getKeyContentApiQuery($code = 'PC', $limit = 3) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('report', [
'advanced-search' => '(' . $code . $this->id() . ')_(F10)',
]),
], $this->t('@label Situation Reports', [
'@label' => $this->label(),
])),
'label' => $this->t('View all @label Situation Reports', [
'@label' => $this->label(),
]),
Expand Down Expand Up @@ -260,7 +262,9 @@ public function getAppealsResponsePlansApiQuery($code = 'PC', $limit = 50) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('report', [
'advanced-search' => '(' . $code . $this->id() . ')_(F4)',
]),
], $this->t('@label Appeals and Reponse Plans', [
'@label' => $this->label(),
])),
'label' => $this->t('View all @label Appeals and Response Plans', [
'@label' => $this->label(),
]),
Expand Down Expand Up @@ -325,7 +329,7 @@ public function getMostReadApiQuery($code = 'PC', $limit = 5) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('report', [
'advanced-search' => '(' . $code . $entity_id . ')',
]),
], $this->label(), TRUE),
'label' => $this->t('View all @label Updates', [
'@label' => $this->label(),
]),
Expand Down Expand Up @@ -363,7 +367,7 @@ public function getLatestUpdatesApiQuery($code = 'PC', $limit = 3) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('report', [
'advanced-search' => '(' . $code . $entity_id . ')',
]),
], $this->label(), TRUE),
'label' => $this->t('View all @label Updates', [
'@label' => $this->label(),
]),
Expand Down Expand Up @@ -410,8 +414,11 @@ public function getLatestMapsInfographicsApiQuery($code = 'PC', $limit = 3) {
// Link to the updates river with the maps/infographics for the entity.
'more' => [
'url' => RiverServiceBase::getRiverUrl('report', [
'advanced-search' => '(' . $code . $entity_id . ')_(F12.F12570)',
]),
'view' => 'maps',
'advanced-search' => '(' . $code . $entity_id . ')',
], $this->t('@label Maps and Infographics', [
'@label' => $this->label(),
])),
'label' => $this->t('View all @label Maps and Infographics', [
'@label' => $this->label(),
]),
Expand Down Expand Up @@ -444,7 +451,7 @@ public function getLatestJobsApiQuery($code = 'C', $limit = 3) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('job', [
'advanced-search' => '(' . $code . $entity_id . ')',
]),
], $this->label(), TRUE),
'label' => $this->t('View all @label Jobs', [
'@label' => $this->label(),
]),
Expand Down Expand Up @@ -477,7 +484,7 @@ public function getLatestTrainingApiQuery($code = 'C', $limit = 3) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('training', [
'advanced-search' => '(' . $code . $entity_id . ')',
]),
], $this->label(), TRUE),
'label' => $this->t('View all @label Training Opportunities', [
'@label' => $this->label(),
]),
Expand Down Expand Up @@ -521,7 +528,7 @@ public function getLatestDisastersApiQuery($code = 'C', $limit = 100) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('disaster', [
'advanced-search' => '(' . $code . $entity_id . ')',
]),
], $this->label(), TRUE),
'label' => $this->t('View all @label Disasters', [
'@label' => $this->label(),
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ public static function parseLinkData(array &$item, array $settings = []) {
return t('Invalid configuration: no river allowed.');
}

$info = RiverServiceBase::getRiverServiceFromUrl($item['url']);
if (empty($info) || !isset($allowed_rivers[$info['bundle']])) {
$service = RiverServiceBase::getRiverServiceFromUrl($item['url']);
if (!isset($service) || !isset($allowed_rivers[$service->getBundle()])) {
$allowed_rivers = array_intersect_key(ReliefWebSectionLinksFieldType::getAllowedRivers(), $allowed_rivers);
return t('Invalid URL: use a link to one of the following rivers: @rivers.', [
'@rivers' => implode(', ', $allowed_rivers),
Expand All @@ -275,7 +275,7 @@ public static function parseLinkData(array &$item, array $settings = []) {
}

$entity_type = \Drupal::entityTypeManager()
->getStorage($info['service']->getEntityTypeId())
->getStorage($service->getEntityTypeId())
->getEntityType();

$table = $entity_type->getDataTable();
Expand All @@ -296,9 +296,9 @@ public static function parseLinkData(array &$item, array $settings = []) {
return t('Invalid override: the document was not found.');
}
// Check that the document's type matches the river bundle.
elseif ($result->{$bundle_key} !== $info['bundle']) {
elseif ($result->{$bundle_key} !== $service->getBundle()) {
return t('Invalid override: the document is not a @bundle.', [
'@bundle' => $info['bundle'],
'@bundle' => $service->getBundle(),
]);
}
// Only published documents are valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function getHeadlinesApiPayload($limit = 8) {
'more' => [
'url' => RiverServiceBase::getRiverUrl('report', [
'view' => 'headlines',
]),
], $this->t('Updates (Headlines)')),
'label' => $this->t('View all headlines'),
],
];
Expand Down
Loading

0 comments on commit ce8bf83

Please sign in to comment.