263 lines
9.2 KiB
Plaintext
263 lines
9.2 KiB
Plaintext
<?php
|
|
declare(strict_types = 1);
|
|
|
|
/**
|
|
* @file
|
|
* Contains opencase_cases.module.
|
|
*/
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface;
|
|
|
|
/**
|
|
* Implements hook_help().
|
|
*/
|
|
function opencase_cases_help($route_name, RouteMatchInterface $route_match) {
|
|
switch ($route_name) {
|
|
// Main module help for the opencase_cases module.
|
|
case 'help.page.opencase_cases':
|
|
$output = '';
|
|
$output .= '<h3>' . t('About') . '</h3>';
|
|
$output .= '<p>' . t('Provides case entities and a required field on activities linking them to a case, plus case fee and case provision entities.') . '</p>';
|
|
return $output;
|
|
|
|
default:
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme().
|
|
*/
|
|
function opencase_cases_theme() {
|
|
return [
|
|
'opencase_cases' => [
|
|
'render element' => 'children',
|
|
],
|
|
'oc_case' => [
|
|
'render element' => 'elements',
|
|
'file' => 'oc_case.page.inc',
|
|
'template' => 'oc_case',
|
|
],
|
|
'oc_case_content_add_list' => [
|
|
'render element' => 'content',
|
|
'variables' => ['content' => NULL],
|
|
'file' => 'oc_case.page.inc',
|
|
],
|
|
'oc_case_provision' => [
|
|
'render element' => 'elements',
|
|
'file' => 'oc_case_provision.page.inc',
|
|
'template' => 'oc_case_provision',
|
|
],
|
|
'oc_case_provision_content_add_list' => [
|
|
'render element' => 'content',
|
|
'variables' => ['content' => NULL],
|
|
'file' => 'oc_case_provision.page.inc',
|
|
],
|
|
'oc_case_fee' => [
|
|
'render element' => 'elements',
|
|
'file' => 'oc_case_fee.page.inc',
|
|
'template' => 'oc_case_fee',
|
|
],
|
|
'oc_case_fee_content_add_list' => [
|
|
'render element' => 'content',
|
|
'variables' => ['content' => NULL],
|
|
'file' => 'oc_case_fee.page.inc',
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return Drupal\opencase_cases\entity\OCCaseFee[]
|
|
*/
|
|
function opencase_cases_get_case_fees(Drupal\opencase_cases\entity\OCCase $case): array {
|
|
$query = \Drupal::entityQuery('oc_case_fee')
|
|
->condition('oc_case.target_id', $case->id());
|
|
$case_fee_ids = $query->execute();
|
|
$case_fees = [];
|
|
foreach($case_fee_ids as $id) {
|
|
$case_fees[] = Drupal\opencase_cases\entity\OCCaseFee::load($id);
|
|
}
|
|
return $case_fees;
|
|
}
|
|
|
|
/**
|
|
* @return Drupal\opencase_cases\entity\OCActivity[]
|
|
*/
|
|
function opencase_cases_get_activities(Drupal\opencase_cases\entity\OCCase $case): array {
|
|
$query = \Drupal::entityQuery('oc_activity')
|
|
->condition('oc_case.target_id', $case->id());
|
|
$activity_ids = $query->execute();
|
|
$activities = [];
|
|
foreach($activity_ids as $id) {
|
|
$activities[] = Drupal\opencase_entities\Entity\OCActivity::load($id);
|
|
}
|
|
return $activities;
|
|
}
|
|
|
|
function opencase_cases_get_amount(Drupal\opencase_cases\entity\OCCaseFee $case_fee): string {
|
|
return $case_fee->amount->first()->value;
|
|
}
|
|
|
|
function opencase_cases_get_case(Drupal\opencase_cases\entity\OCCaseFee $case_fee): Drupal\opencase_cases\entity\OCCase {
|
|
return $case_fee->oc_case->referencedEntities()[0];
|
|
}
|
|
|
|
function opencase_cases_update_total_fee_for_case_belonging_to_case_fee(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
|
|
$total = 0;
|
|
$case = opencase_cases_get_case($case_fee);
|
|
$case_fees = opencase_cases_get_case_fees($case);
|
|
foreach($case_fees as $case_fee) {
|
|
$total += opencase_cases_get_amount($case_fee);
|
|
}
|
|
$case->set('total_fee', $total);
|
|
$case->save();
|
|
}
|
|
|
|
function opencase_cases_oc_case_fee_update(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
|
|
opencase_cases_update_total_fee_for_case_belonging_to_case_fee($case_fee);
|
|
}
|
|
|
|
function opencase_cases_oc_case_fee_insert(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
|
|
opencase_cases_update_total_fee_for_case_belonging_to_case_fee($case_fee);
|
|
}
|
|
|
|
function opencase_cases_oc_case_fee_delete(Drupal\opencase_cases\entity\OCCaseFee $case_fee): void {
|
|
opencase_cases_update_total_fee_for_case_belonging_to_case_fee($case_fee);
|
|
}
|
|
|
|
function opencase_cases_entity_base_field_info($entity_type) {
|
|
$fields = array();
|
|
|
|
// Add Case field to Activity
|
|
if ($entity_type->id() === 'oc_activity') {
|
|
$fields['oc_case'] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
|
|
->setLabel(t('Case'))
|
|
->setDescription(t('The case this activity belongs to.'))
|
|
->setSetting('target_type', 'oc_case')
|
|
->setSetting('handler', 'default')
|
|
->setTranslatable(TRUE)
|
|
->setCardinality(1)
|
|
->setDefaultValueCallback('opencase_cases_default_case_id')
|
|
->setDisplayOptions('view', [
|
|
'type' => 'string',
|
|
'weight' => -3,
|
|
])
|
|
->setRequired(TRUE);
|
|
}
|
|
return $fields;
|
|
}
|
|
|
|
// When deleting a case, delete the activities associated with it
|
|
function opencase_cases_oc_case_delete(Drupal\opencase_cases\Entity\OCCase $case): void {
|
|
$activities = opencase_cases_get_activities($case);
|
|
foreach ($activities as $activity) {
|
|
$activity->delete();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* When creating an activity, it sets the case id from the URL.
|
|
*/
|
|
function opencase_cases_default_case_id() {
|
|
return array(\Drupal::request()->query->get('case_id'));
|
|
}
|
|
|
|
/**
|
|
* Implementation of hook_form_alter()
|
|
* Changes what page is redirected to after deleting things
|
|
*/
|
|
function opencase_cases_form_alter(&$form, &$form_state, $form_id) {
|
|
if (preg_match('/oc_actor_.*_delete_form/', $form_id) or (preg_match('/oc_case_.*_delete_form/', $form_id)) or (preg_match('/oc_organisation_.*_delete_form/', $form_id))) {
|
|
$form['actions']['submit']['#submit'][] = '_opencase_cases_redirect_to_home';
|
|
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
|
|
}
|
|
if (preg_match('/oc_activity_.*_delete_form/', $form_id)) {
|
|
$form['actions']['submit']['#submit'][] = '_opencase_cases_delete_activity_redirect';
|
|
$form['actions']['cancel']['#url'] = $form_state->getFormObject()->getEntity()->toUrl();
|
|
}
|
|
}
|
|
|
|
function _opencase_cases_redirect_to_home($form, &$form_state) {
|
|
$form_state->setRedirect('<front>');
|
|
}
|
|
function _opencase_cases_delete_activity_redirect($form, &$form_state) {
|
|
$case_id = $form_state->getFormObject()->getEntity()->oc_case->target_id;
|
|
$form_state->setRedirect('entity.oc_case.canonical', ['oc_case' => $case_id]);
|
|
}
|
|
/**
|
|
* Implements hook_theme_suggestions_HOOK().
|
|
*/
|
|
function opencase_cases_theme_suggestions_oc_case(array $variables) {
|
|
$suggestions = [];
|
|
$entity = $variables['elements']['#oc_case'];
|
|
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
|
|
|
|
$suggestions[] = 'oc_case__' . $sanitized_view_mode;
|
|
$suggestions[] = 'oc_case__' . $entity->bundle();
|
|
$suggestions[] = 'oc_case__' . $entity->bundle() . '__' . $sanitized_view_mode;
|
|
$suggestions[] = 'oc_case__' . $entity->id();
|
|
$suggestions[] = 'oc_case__' . $entity->id() . '__' . $sanitized_view_mode;
|
|
return $suggestions;
|
|
}
|
|
|
|
//function opencase_views_query_alter(Drupal\views\ViewExecutable $view, $query) {
|
|
// if ($view->getBaseEntityType() && $view->getBaseEntityType()->id() == 'oc_case') {
|
|
// $query->addTag('oc_case_access');
|
|
// }
|
|
// if ($view->getBaseEntityType() && $view->getBaseEntityType()->id() == 'oc_activity') {
|
|
// $query->addTag('oc_activity_access');
|
|
// }
|
|
//}
|
|
//
|
|
//function opencase_query_oc_case_access_alter($query) {
|
|
// if (\Drupal::currentUser()->hasPermission('view published case entities')) {
|
|
// return;
|
|
// }
|
|
// $linked_actor_id = CaseInvolvement::getLinkedActorId(\Drupal::currentUser());
|
|
// $query->addJoin('INNER', 'oc_case__actors_involved', 'access_filter', 'access_filter.entity_id = oc_case_field_data.id');
|
|
// $query->condition('access_filter.actors_involved_target_id', $linked_actor_id);
|
|
//}
|
|
//
|
|
//function opencase_query_oc_activity_access_alter($query) {
|
|
// if (\Drupal::currentUser()->hasPermission('view published case entities')) {
|
|
// return;
|
|
// }
|
|
// $linked_actor_id = CaseInvolvement::getLinkedActorId(\Drupal::currentUser());
|
|
// $query->addJoin('INNER', 'oc_case__actors_involved', 'access_filter', 'access_filter.entity_id = oc_activity_field_data.oc_case');
|
|
// $query->condition('access_filter.actors_involved_target_id', $linked_actor_id);
|
|
//}
|
|
|
|
|
|
|
|
/**
|
|
* Implements hook_theme_suggestions_HOOK().
|
|
*/
|
|
function opencase_cases_theme_suggestions_oc_case_provision(array $variables) {
|
|
$suggestions = [];
|
|
$entity = $variables['elements']['#oc_case_provision'];
|
|
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
|
|
|
|
$suggestions[] = 'oc_case_provision__' . $sanitized_view_mode;
|
|
$suggestions[] = 'oc_case_provision__' . $entity->bundle();
|
|
$suggestions[] = 'oc_case_provision__' . $entity->bundle() . '__' . $sanitized_view_mode;
|
|
$suggestions[] = 'oc_case_provision__' . $entity->id();
|
|
$suggestions[] = 'oc_case_provision__' . $entity->id() . '__' . $sanitized_view_mode;
|
|
return $suggestions;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme_suggestions_HOOK().
|
|
*/
|
|
function opencase_cases_theme_suggestions_oc_case_fee(array $variables) {
|
|
$suggestions = [];
|
|
$entity = $variables['elements']['#oc_case_fee'];
|
|
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
|
|
|
|
$suggestions[] = 'oc_case_fee__' . $sanitized_view_mode;
|
|
$suggestions[] = 'oc_case_fee__' . $entity->bundle();
|
|
$suggestions[] = 'oc_case_fee__' . $entity->bundle() . '__' . $sanitized_view_mode;
|
|
$suggestions[] = 'oc_case_fee__' . $entity->id();
|
|
$suggestions[] = 'oc_case_fee__' . $entity->id() . '__' . $sanitized_view_mode;
|
|
return $suggestions;
|
|
}
|