2018-04-30 12:24:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Drupal\opencase\Plugin\Block;
|
|
|
|
|
2018-06-12 15:16:40 +00:00
|
|
|
use Drupal\opencase\EntityTypeRelations;
|
2018-04-30 12:24:01 +00:00
|
|
|
use Drupal\Core\Block\BlockBase;
|
|
|
|
use Drupal\Core\Link;
|
2018-04-30 13:45:34 +00:00
|
|
|
use Drupal\Core\Url;
|
2018-04-30 12:24:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a 'ContextualMenu' block.
|
|
|
|
*
|
2018-05-07 17:20:09 +00:00
|
|
|
* Displays contextual links on certain pages.
|
|
|
|
* The block is forbidden by hook_block_access on other pages, so if more are added they need adding there too.
|
|
|
|
*
|
2018-04-30 12:24:01 +00:00
|
|
|
* @Block(
|
2018-05-07 17:20:09 +00:00
|
|
|
* id = "opencase_contextual_menu",
|
2018-04-30 12:24:01 +00:00
|
|
|
* 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':
|
2018-05-08 11:21:12 +00:00
|
|
|
case 'entity.oc_actor.edit_form':
|
2018-04-30 12:24:01 +00:00
|
|
|
$markup = $this->actorPage();
|
|
|
|
break;
|
|
|
|
case 'view.cases.page_1':
|
2020-11-10 13:42:32 +00:00
|
|
|
$markup = $this->caseListPageForActor();
|
2018-04-30 12:24:01 +00:00
|
|
|
break;
|
2018-04-30 15:14:51 +00:00
|
|
|
case 'entity.oc_case.canonical':
|
2018-05-08 11:21:12 +00:00
|
|
|
case 'entity.oc_case.edit_form':
|
2018-04-30 15:14:51 +00:00
|
|
|
$markup = $this->casePage();
|
|
|
|
break;
|
2018-05-08 11:21:12 +00:00
|
|
|
case 'entity.oc_case.add_form':
|
|
|
|
$markup = $this->caseAddPage();
|
|
|
|
break;
|
2018-04-30 15:14:51 +00:00
|
|
|
case 'view.activities.page_1':
|
|
|
|
$markup = $this->activityListPage();
|
|
|
|
break;
|
2018-04-30 16:30:43 +00:00
|
|
|
case 'entity.oc_activity.canonical':
|
2018-05-08 11:21:12 +00:00
|
|
|
case 'entity.oc_activity.edit_form':
|
2018-04-30 16:30:43 +00:00
|
|
|
$markup = $this->activityPage();
|
|
|
|
break;
|
2018-05-08 11:21:12 +00:00
|
|
|
case 'entity.oc_activity.add_form':
|
|
|
|
$markup = $this->activityAddPage();
|
|
|
|
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() {
|
2020-11-10 13:42:32 +00:00
|
|
|
$actor_id = \Drupal::routeMatch()->getParameter('oc_actor')->id();
|
|
|
|
$link = $this->getCaseListLink('oc_actor', $actor_id);
|
2018-05-03 11:35:35 +00:00
|
|
|
return $this->asNavLinks([$link]);
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Case list page
|
2018-05-08 12:54:11 +00:00
|
|
|
* - Link to actor whose case list this is
|
2018-04-30 13:45:34 +00:00
|
|
|
* - Links to add cases of various types
|
2018-05-08 12:54:11 +00:00
|
|
|
* - Store the actor id in the session, so that the user experiences
|
|
|
|
* a hierachy actor->case->activities which they can navigate
|
2018-04-30 12:24:01 +00:00
|
|
|
*/
|
2020-11-10 13:42:32 +00:00
|
|
|
private function caseListPageForActor() {
|
2018-04-30 12:24:01 +00:00
|
|
|
$actor_id = \Drupal::routeMatch()->getParameter('actor_id');
|
2020-11-10 13:42:32 +00:00
|
|
|
\Drupal::service('user.private_tempstore')->get('opencase')->set('parent_type', 'oc_actor');
|
|
|
|
\Drupal::service('user.private_tempstore')->get('opencase')->set('parent_id', $actor_id);
|
2018-06-12 15:16:40 +00:00
|
|
|
$actor = \Drupal::entityTypeManager()->getStorage('oc_actor')->load($actor_id);
|
|
|
|
$link = $actor->toLink()->toString();
|
2018-05-03 12:06:01 +00:00
|
|
|
$markup = $this->asNavLinks([$link]);
|
2018-04-30 16:30:43 +00:00
|
|
|
$current_path = \Drupal::service('path.current')->getPath();
|
2018-06-12 15:16:40 +00:00
|
|
|
$title = "Add new case";
|
|
|
|
$query = ['actor_id' => $actor_id, 'destination' => $current_path];
|
2020-11-10 13:42:32 +00:00
|
|
|
$markup .= $this->generateLinksForAddingNewCasesForActor($actor, $title, $query);
|
2018-04-30 13:45:34 +00:00
|
|
|
return $markup;
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Case page
|
2018-07-18 19:06:26 +00:00
|
|
|
* - Link either the case list for the actor stored in the session (because their case list page was previously loaded)
|
|
|
|
* or the home page
|
2018-04-30 16:30:43 +00:00
|
|
|
* - 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() {
|
|
|
|
$case = \Drupal::routeMatch()->getParameter('oc_case');
|
2020-11-10 13:42:32 +00:00
|
|
|
$links = [$this->getCaseListLinkForParentEntity(), $this->getActivityListLink($case)];
|
2018-05-08 12:09:26 +00:00
|
|
|
return $this->asNavLinks($links);
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 11:21:12 +00:00
|
|
|
/**
|
|
|
|
* Contextual menu for Add-New-Case page
|
2020-11-10 13:42:32 +00:00
|
|
|
* - Link to Case list for the parent entity (actor by default but plugins can change the type)
|
|
|
|
* that is stored in the session
|
2018-05-08 11:21:12 +00:00
|
|
|
*/
|
|
|
|
private function caseAddPage() {
|
2020-11-10 13:42:32 +00:00
|
|
|
$link = $this->getCaseListLinkForParentEntity();
|
2018-05-08 11:21:12 +00:00
|
|
|
return $this->asNavLinks([$link]);
|
|
|
|
}
|
|
|
|
|
2020-11-10 13:42:32 +00:00
|
|
|
private function getCaseListLinkForParentEntity() {
|
|
|
|
$parent_type = \Drupal::service('user.private_tempstore')->get('opencase')->get('parent_type');
|
|
|
|
$parent_id = \Drupal::service('user.private_tempstore')->get('opencase')->get('parent_id');
|
|
|
|
return $this->getCaseListLink($parent_type, $parent_id);
|
|
|
|
}
|
|
|
|
private function getCaseListLink($entity_type, $entity_id) {
|
|
|
|
$url = "/opencase/$entity_type/$entity_id/case_list";
|
|
|
|
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id);
|
|
|
|
$link_text = $entity->getName(). ": Cases";
|
|
|
|
return "<a href=$url>$link_text</a>";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a link to the list of all cases
|
|
|
|
*/
|
|
|
|
private function getCaseListLinkAll() {
|
|
|
|
$url = Url::fromRoute('view.cases.page_2');
|
|
|
|
return Link::fromTextAndUrl(t("All cases"), $url)->toString();
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:24:01 +00:00
|
|
|
/**
|
|
|
|
* Contextual menu for Activity list page
|
2018-05-03 12:06:01 +00:00
|
|
|
* - Link to the case that the activity list is for
|
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-05-03 12:06:01 +00:00
|
|
|
$case = \Drupal::entityTypeManager()->getStorage('oc_case')->load($case_id);
|
2018-05-08 12:54:11 +00:00
|
|
|
$url = $case->toUrl();
|
2018-05-08 12:09:26 +00:00
|
|
|
$link = Link::fromTextAndUrl(t($case->getName() .": Case Details and Files"), $url)->toString();
|
2018-05-03 12:06:01 +00:00
|
|
|
$markup = $this->asNavLinks([$link]);
|
2018-05-03 11:35:35 +00:00
|
|
|
$current_path = \Drupal::service('path.current')->getPath();
|
2018-06-12 16:12:30 +00:00
|
|
|
return $markup . $this->generateLinksForAddingNewActivities($case, "Add activity", ['case_id' => $case_id, 'destination' => $current_path]);
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|
2018-04-30 16:30:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contextual menu for Activity page
|
2018-05-08 12:09:26 +00:00
|
|
|
* - Link to the activity list for the case
|
2018-04-30 16:30:43 +00:00
|
|
|
*/
|
|
|
|
private function activityPage() {
|
|
|
|
$activity = \Drupal::routeMatch()->getParameter('oc_activity');
|
|
|
|
$case = $activity->oc_case->entity;
|
|
|
|
$link = $this->getActivityListLink($case);
|
2018-05-03 11:35:35 +00:00
|
|
|
return $this->asNavLinks([$link]);
|
2018-04-30 16:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-08 11:21:12 +00:00
|
|
|
/**
|
|
|
|
* Contextual menu for Add-New-Activity page
|
2018-05-08 12:09:26 +00:00
|
|
|
* - Link to the activity list for the case
|
2018-05-08 11:21:12 +00:00
|
|
|
*/
|
|
|
|
private function activityAddPage() {
|
|
|
|
$case_id = \Drupal::request()->query->get('case_id');
|
|
|
|
$case = \Drupal::entityTypeManager()->getStorage('oc_case')->load($case_id);
|
|
|
|
$link = $this->getActivityListLink($case);
|
|
|
|
return $this->asNavLinks([$link]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-30 16:30:43 +00:00
|
|
|
/**
|
|
|
|
* Given an case entity, returns a link to the activity list
|
|
|
|
*/
|
|
|
|
private function getActivityListLink($case) {
|
2018-05-08 12:54:11 +00:00
|
|
|
$url = Url::fromRoute('view.activities.page_1', ['case_id' => $case->id()]);
|
2018-05-03 12:06:01 +00:00
|
|
|
return Link::fromTextAndUrl(t($case->getName() .": Activities"), $url)->toString();
|
2018-04-30 16:30:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 11:35:35 +00:00
|
|
|
/**
|
|
|
|
* Render given links as nav links div with heading
|
|
|
|
*/
|
|
|
|
private function asNavLinks(array $links) {
|
|
|
|
$markup = '';
|
|
|
|
foreach($links as $link) {
|
|
|
|
$markup .= "<p>$link</p>";
|
|
|
|
}
|
|
|
|
$title = t("Go to:");
|
2018-05-03 12:06:01 +00:00
|
|
|
return "<div class='opencase_nav_links'><h1>$title</h1>$markup</div>";
|
2018-05-03 11:35:35 +00:00
|
|
|
}
|
2018-05-08 12:09:26 +00:00
|
|
|
|
2018-06-12 14:11:28 +00:00
|
|
|
/**
|
|
|
|
* returns html markup.
|
|
|
|
*/
|
2020-11-10 13:42:32 +00:00
|
|
|
private function generateLinksForAddingNewCasesForActor($actor, $title, $query = []) {
|
2018-06-12 15:16:40 +00:00
|
|
|
$actor_type = $actor->bundle();
|
2018-06-13 15:51:14 +00:00
|
|
|
$allCaseTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_case');
|
|
|
|
// $allCaseTypes is array where the key is the machine name and the value is array containing label
|
|
|
|
// Now we pick just the allowed ones and produced an array of labels keyed by machine name
|
|
|
|
$allowedCaseTypes = array();
|
|
|
|
foreach(array_keys($allCaseTypes) as $caseType) {
|
|
|
|
if (in_array($actor_type, EntityTypeRelations::getAllowedActorTypesForCaseType($caseType))) {
|
|
|
|
$allowedCaseTypes[$caseType] = $allCaseTypes[$caseType]['label'];
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 14:11:28 +00:00
|
|
|
$title = t($title);
|
|
|
|
$markup = "<h1>$title: </h1>";
|
2018-06-13 15:51:14 +00:00
|
|
|
foreach($allowedCaseTypes as $machine_name => $label) {
|
2018-06-12 15:16:40 +00:00
|
|
|
$url = \Drupal\Core\Url::fromRoute("entity.oc_case.add_form", ['oc_case_type' => $machine_name]);
|
2018-06-12 14:11:28 +00:00
|
|
|
$url->setOption('query', $query);
|
|
|
|
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
|
|
|
|
$markup .= "<p>$link</p>";
|
|
|
|
}
|
|
|
|
return "<div class='opencase_add_links'>$markup</div>";
|
|
|
|
}
|
2018-06-12 16:12:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns html markup.
|
|
|
|
*/
|
|
|
|
private function generateLinksForAddingNewActivities($case, $title, $query = []) {
|
|
|
|
$title = t($title);
|
|
|
|
$markup = "<h1>$title: </h1>";
|
2018-06-13 15:51:14 +00:00
|
|
|
$caseType = $case->bundle();
|
|
|
|
$allActivityTypes = \Drupal::service('entity_type.bundle.info')->getBundleInfo('oc_activity');
|
|
|
|
// $allActivityTypes is array where the key is the machine name and the value is array containing label
|
|
|
|
// Now we pick just the allowed ones and produced an array of labels keyed by machine name
|
|
|
|
$allowedActivityTypes = EntityTypeRelations::getAllowedActivityTypesForCaseType($caseType);
|
|
|
|
foreach($allowedActivityTypes as $machine_name => $is_allowed) {
|
|
|
|
if ($is_allowed) {
|
|
|
|
$label = $allActivityTypes[$machine_name]['label'];
|
|
|
|
$url = \Drupal\Core\Url::fromRoute("entity.oc_activity.add_form", ['oc_activity_type' => $machine_name]);
|
|
|
|
$url->setOption('query', $query);
|
|
|
|
$link = \Drupal\Core\Link::fromTextAndUrl($label, $url)->toString();
|
|
|
|
$markup .= "<p>$link</p>";
|
|
|
|
}
|
2018-06-12 16:12:30 +00:00
|
|
|
}
|
|
|
|
return "<div class='opencase_add_links'>$markup</div>";
|
|
|
|
}
|
2018-04-30 12:24:01 +00:00
|
|
|
}
|