2018-04-30 12:24:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Drupal\opencase\Plugin\Block;
|
|
|
|
|
|
|
|
use Drupal\Core\Block\BlockBase;
|
|
|
|
use Drupal\Core\Link;
|
2018-04-30 13:45:34 +00:00
|
|
|
use Drupal\Core\Url;
|
2018-04-30 14:49:44 +00:00
|
|
|
use Drupal\opencase\Utils;
|
2018-04-30 12:24:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a 'ContextualMenu' block.
|
|
|
|
*
|
|
|
|
* @Block(
|
|
|
|
* id = "contextual_menu",
|
|
|
|
* admin_label = @Translation("OpenCase Contextual Menu"),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
class ContextualMenu extends BlockBase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function build() {
|
|
|
|
|
|
|
|
$route_name = \Drupal::routeMatch()->getRouteName();
|
|
|
|
switch ($route_name) {
|
|
|
|
case 'entity.oc_actor.canonical':
|
|
|
|
$markup = $this->actorPage();
|
|
|
|
break;
|
|
|
|
case 'view.cases.page_1':
|
|
|
|
$markup = $this->caseListPage();
|
|
|
|
break;
|
2018-04-30 15:14:51 +00:00
|
|
|
case 'entity.oc_case.canonical':
|
|
|
|
$markup = $this->casePage();
|
|
|
|
break;
|
|
|
|
case 'view.activities.page_1':
|
|
|
|
$markup = $this->activityListPage();
|
|
|
|
break;
|
2018-04-30 16:30:43 +00:00
|
|
|
case 'entity.oc_activity.canonical':
|
|
|
|
$markup = $this->activityPage();
|
|
|
|
break;
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$build = [];
|
|
|
|
$build['contextual_menu'] = [
|
2018-04-30 13:45:34 +00:00
|
|
|
'#markup' => "<div id='opencase_contextual_menu'>$markup</div",
|
2018-04-30 12:24:01 +00:00
|
|
|
'#cache' => ['max-age' => 0]
|
|
|
|
];
|
|
|
|
return $build;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Actor page
|
2018-04-30 13:45:34 +00:00
|
|
|
* - Link to case list for that actor
|
2018-04-30 12:24:01 +00:00
|
|
|
*/
|
|
|
|
private function actorPage() {
|
|
|
|
$actor = \Drupal::routeMatch()->getParameter('oc_actor');
|
2018-04-30 16:30:43 +00:00
|
|
|
$link = $this->getCaseListLink($actor);
|
2018-04-30 14:49:44 +00:00
|
|
|
return "<div class='opencase_nav_links'><p>$link</p></div>";
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Case list page
|
2018-04-30 13:45:34 +00:00
|
|
|
* - Links to add cases of various types
|
2018-04-30 12:24:01 +00:00
|
|
|
*/
|
|
|
|
private function caseListPage() {
|
|
|
|
$actor_id = \Drupal::routeMatch()->getParameter('actor_id');
|
2018-04-30 16:30:43 +00:00
|
|
|
$current_path = \Drupal::service('path.current')->getPath();
|
|
|
|
$markup .= Utils::generateAddLinks('oc_case', ['actor_id' => $actor_id, 'destination' => $current_path]);
|
2018-04-30 13:45:34 +00:00
|
|
|
return $markup;
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Case page
|
2018-04-30 16:30:43 +00:00
|
|
|
* - Link to case list if user has just come from there
|
|
|
|
* - Link to Activity list for that case
|
2018-04-30 12:24:01 +00:00
|
|
|
*/
|
2018-04-30 15:14:51 +00:00
|
|
|
private function casePage() {
|
2018-04-30 16:30:43 +00:00
|
|
|
|
|
|
|
$links = '';
|
|
|
|
|
|
|
|
// Ascertain if user has come from a case list and if so link back
|
|
|
|
// This is fragle code, it needs doing better.
|
|
|
|
$referer = \Drupal::request()->headers->get('referer');
|
|
|
|
$parts = parse_url($referer);
|
|
|
|
$path_parts= explode('/', $parts[path]);
|
|
|
|
if ($path_parts[4] == 'case_list') {
|
|
|
|
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($path_parts[3]);
|
|
|
|
$link = $this->getCaseListLink($actor);
|
|
|
|
$links .= "<p>$link</p>";
|
|
|
|
}
|
|
|
|
|
2018-04-30 15:14:51 +00:00
|
|
|
$case = \Drupal::routeMatch()->getParameter('oc_case');
|
2018-04-30 16:30:43 +00:00
|
|
|
$link = $this->getActivityListLink($case);
|
|
|
|
$links .= "<p>$link</p>";
|
|
|
|
return "<div class='opencase_nav_links'>$links</div>";
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Activity list page
|
2018-04-30 15:14:51 +00:00
|
|
|
* - Links to add activities of various types
|
2018-04-30 12:24:01 +00:00
|
|
|
*/
|
2018-04-30 15:14:51 +00:00
|
|
|
private function activityListPage() {
|
|
|
|
$case_id = \Drupal::routeMatch()->getParameter('case_id');
|
2018-04-30 16:30:43 +00:00
|
|
|
$current_path = \Drupal::service('path.current')->getPath();
|
|
|
|
$markup .= Utils::generateAddLinks('oc_activity', ['case_id' => $case_id, 'destination' => $current_path]);
|
2018-04-30 15:14:51 +00:00
|
|
|
return $markup;
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
2018-04-30 16:30:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Activity page
|
|
|
|
* - Links to the activity list for the case
|
|
|
|
*/
|
|
|
|
private function activityPage() {
|
|
|
|
$activity = \Drupal::routeMatch()->getParameter('oc_activity');
|
|
|
|
$case = $activity->oc_case->entity;
|
|
|
|
$link = $this->getActivityListLink($case);
|
|
|
|
return "<div class='opencase_nav_links'><p>$link</p></div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an case entity, returns a link to the activity list
|
|
|
|
*/
|
|
|
|
private function getActivityListLink($case) {
|
|
|
|
$url = Url::fromRoute('view.activities.page_1', array('case_id' => $case->id()));
|
|
|
|
return Link::fromTextAndUrl(t("Activity List for " . $case->getName()), $url)->toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an case entity, returns a link to the activity list
|
|
|
|
*/
|
|
|
|
private function getCaseListLink($actor) {
|
|
|
|
$url = Url::fromRoute('view.cases.page_1', array('actor_id' => $actor->id()));
|
|
|
|
return Link::fromTextAndUrl(t("Case List for " . $actor->getName()), $url)->toString();
|
|
|
|
}
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|