This repository has been archived on 2022-07-12. You can view files and clone it, but cannot push or open issues or pull requests.
opencase/opencase.module
2022-07-07 17:20:10 +01:00

232 lines
6.7 KiB
Plaintext

<?php
/**
* @file
* Contains opencase.module.
*/
use Drupal\Core\Render\Element;
use Drupal\Core\Access\AccessResult;
use Drupal\opencase_cases\Entity\OCCase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\opencase_cases\Entity\OCCaseProvision;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements hook_form_FORM_ID_alter().
*/
function opencase_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#submit'][] = 'opencase_user_login_form_submit';
}
/**
* If the user does not have permissions to see all cases, then when the login they are
* redirected to a view of just their own cases.
*/
function opencase_user_login_form_submit($form, FormStateInterface $form_state) {
if (!\Drupal::currentUser()->hasPermission('view published case entities')) {
$form_state->setRedirectUrl(Url::fromRoute('view.cases_by_provider.page_1'));
}
}
/**
* Implements hook_element_info_alter().
*/
function opencase_element_info_alter(array &$types) {
$types['datetime']['#process'][] = 'opencase_process_element';
}
function _template_preprocess_entity(&$variables) {
foreach (Element::children($variables['elements']) as $key) {
if (is_extra_field($variables['elements'][$key])) {
$variables['extra_fields'][$key] = $variables['elements'][$key];
} else {
$variables['normal_fields'][$key] = $variables['elements'][$key];
}
}
}
function is_extra_field($element){
return array_key_exists('#field', $element);
}
/**
* Element process callback for datetime fields. Removes the seconds part.
*/
function opencase_process_element($element) {
if ($element['#date_time_element'] !== 'none') {
$element['#date_time_format'] = 'H:i';
}
if (!empty($element['time']['#value'])) {
$parts = explode(':', $element['time']['#value']);
$parts = array_splice($parts, 0, 2);
$element['time']['#value'] = implode(':', $parts);
}
// Remove seconds in browsers that support HTML5 type=date.
$element['time']['#attributes']['step'] = 60;
return $element;
}
function opencase_oc_case_provision_insert(OCCaseProvision $case_provision): void {
$case_provision->oc_provider->entity->calculateTotalCases();
}
function opencase_oc_case_provision_delete(OCCaseProvision $case_provision): void {
$case_provision->oc_provider->entity->calculateTotalCases();
}
function opencase_oc_case_provision_update(OCCaseProvision $case_provision): void {
$case_provision->oc_provider->entity->calculateTotalCases();
$case_provision->original->oc_provider->entity->calculateTotalCases();
}
function opencase_oc_case_delete(OCCase $case): void {
$case->deleteCaseProvisions();
$case->deleteActivities();
}
/**
* Implements hook_page_attachments
*
* Add the opencase library to every page
*/
function opencase_page_attachments(array &$page) {
$page['#attached']['library'][] = 'opencase/opencase-lib';
}
/**
* Implements hook_link_alter
*
* Makes menu items that are external links open in new tab.
*/
function opencase_link_alter(&$variables) {
if ($variables['url']->isExternal()) {
$variables['options']['attributes'] = ['target' => '_blank'];
}
}
/**
* Implements hook_help().
*/
function opencase_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the opencase module.
case 'help.page.opencase':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Simple Case Management') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function opencase_theme() {
return [
'opencase' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_uninstall().
* Removes configs.
*/
function opencase_uninstall() {
$configs = [
'block.block.opencase',
'system.menu.opencase',
'views.view.contact_details_changes',
'views.view.equal_opps_records',
'block.block.exposedformequal_opps_recordspage_1',
];
foreach($configs as $config) {
Drupal::configFactory()->getEditable($config)->delete();
}
}
function opencase_views_pre_render($view) {
if (!empty($view->result)) {
foreach ($view->result as $key => $result) {
if (empty($result->_entity)) {
continue;
}
$access = \Drupal::entityTypeManager()
->getAccessControlHandler($result->_entity->getEntityTypeId())
->access($result->_entity, 'view', NULL, TRUE);
if (!$access->isAllowed()) {
unset($view->result[$key]);
}
}
}
if (empty($view->result) && empty($view->exposed_input)) {
$view->exposed_widgets = NULL;
}
}
function opencase_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $account, $items = NULL) {
if ($field_definition->getName() == 'field_linked_opencase_actor'
&& $operation == 'edit'
&& !$account->hasPermission('administer users')) {
return AccessResult::forbidden();
}
return AccessResult::neutral();
}
/*
Custom hook
*/
function opencase_relevant_case_type_ids(string $bundle):array {
switch ($bundle) {
case 'client':
return ['accommodation', 'asylum_support', 'employability', 'health', 'immigration', 'welfare_rights'];
case 'volunteer':
return ['volunteer_engagement'];
}
return [];
}
/*
Custom hook
*/
function opencase_relevant_activity_type_ids(string $entityType, string $bundle):array {
if ($entityType == 'oc_actor') {
switch ($bundle) {
case 'volunteer':
return ['email', 'phone_call', 'supervision', 'application', 'interview'];
case 'client':
return ['email', 'lete', 'phone_call', 'case_note', 'destitution_funds_provided', 'research', 'application'];
case 'staff_member':
return ['application', 'interview'];
}
}
if ($entityType == 'oc_case') {
switch ($bundle) {
case 'volunteer_engagement':
return ['email', 'phone_call', 'supervision', 'application', 'interview'];
default:
return ['email', 'lete', 'phone_call', 'case_note', 'destitution_funds_provided', 'research', 'application'];
}
}
return [];
}
/**
* Implementation of hook_form_alter()
* Changes what page is redirected to after adding or deleting linked organisation
*/
function opencase_form_alter(&$form, &$form_state, $form_id) {
if (preg_match('/oc_organisation_relation_.*_delete_form/', $form_id) or (preg_match('/oc_organisation_relation_.*_add_form/', $form_id))) {
$form['actions']['submit']['#submit'][] = '_opencase_organisation_relation_redirect';
}
}
function _opencase_organisation_relation_redirect($form, &$form_state) {
$organisation_id = \Drupal::request()->query->get('organisation_id');
$form_state->setRedirect('entity.oc_organisation.canonical', ['oc_organisation' => $organisation_id]);
}