pos_entity_review.module

PHOTO EMBED

Tue Oct 04 2022 20:05:53 GMT+0000 (Coordinated Universal Time)

Saved by @igor #drupal #mysql

<?php

/**
 * @file
 * Contains pos_entity_review.module.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\pos_entity_cfp\Entity\Cfp;
use Drupal\pos_entity_form_part\Form\EntityPartForm;
use Drupal\pos_entity_review\Entity\ReviewType;
use Drupal\pos_entity_review\Entity\ReviewTypeInterface;

/**
 * Implements hook_help().
 */
function pos_entity_review_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the pos_entity_review module.
    case 'help.page.pos_entity_review':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Pos Review Base Entity Used by CFP in some states') . '</p>';
      return $output;

    default:
  }
}

/**
 * Returns all execution as options.
 *
 * @return array
 *   Array of all review types with labels.
 *
 * @throws \ReflectionException
 */
function _get_review_types_as_options() {
  $reflection_class = new ReflectionClass(ReviewTypeInterface::class);
  $states = [];
  foreach ($reflection_class->getConstants() as $constant_name => $constant) {
    if (strpos($constant_name, 'REVIEW_TYPE') !== FALSE) {
      $states[$constant] = ucfirst(strtolower(str_replace([
        'REVIEW_TYPE_',
        '_',
      ], [
        '',
        ' ',
      ], $constant_name)));
    }
  }
  return $states;
}

/**
 * Implements hook_theme().
 */
function pos_entity_review_theme() {
  $theme = [];
  $theme['review'] = [
    'render element' => 'elements',
    'file' => 'review.page.inc',
    'template' => 'review',
  ];
  $theme['review_content_add_list'] = [
    'render element' => 'content',
    'variables' => ['content' => NULL],
    'file' => 'review.page.inc',
  ];
  return $theme;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function pos_entity_review_theme_suggestions_review(array $variables) {
  $suggestions = [];
  $entity = $variables['elements']['#review'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  $suggestions[] = 'review__' . $sanitized_view_mode;
  $suggestions[] = 'review__' . $entity->bundle();
  $suggestions[] = 'review__' . $entity->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'review__' . $entity->id();
  $suggestions[] = 'review__' . $entity->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}

/**
 * Implements hook_entity_extra_field_info().
 */
function pos_entity_review_entity_extra_field_info() {
  $extra = [];
  foreach (ReviewType::loadMultiple() as $review_type) {
    $extra['review'][$review_type->id()]['display']['view_link'] = [
      'label' => t('View link'),
      'description' => 'Entity view link',
      'visible' => FALSE,
    ];
    $extra['review'][$review_type->id()]['display']['edit_link'] = [
      'label' => t('Edit link'),
      'weight' => 0,
      'visible' => FALSE,
    ];
    $extra['review'][$review_type->id()]['display']['entity_label'] = [
      'label' => t('Entity label'),
      'weight' => 0,
      'visible' => FALSE,
    ];
    if ($review_type->questionCanBeClarified()) {
      $extra['review'][$review_type->id()]['display']['clarify_question'] = [
        'label' => t('Clarify question button'),
        'weight' => 0,
        'visible' => FALSE,
      ];
    }
    $extra['review'][$review_type->id()]['display']['correction_data'] = [
      'label' => t('Correction data(correction data for given)'),
      'description' => t('If review is for correction state, corrections will be shown'),
      'weight' => 0,
      'visible' => FALSE,
    ];
    $extra['review'][$review_type->id()]['display']['reviewer_additional_files'] = [
      'label' => t('Shows file upload for additional files'),
      'description' => t('Reviewer need to attach document during work in some cases'),
      'weight' => 0,
      'visible' => FALSE,
    ];
    $extra['review'][$review_type->id()]['display']['raw_signature'] = [
      'label' => t('Signature'),
      'visible' => FALSE,
    ];
  }

  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function pos_entity_review_review_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  /** @var \Drupal\pos_entity_review\Entity\Review $entity */

  if ($display->getComponent('view_link') && $entity->access('view') && !$entity->access('update')) {
    $view_link = $entity->toUrl();
    $build['view_link'] = [
      '#type' => 'link',
      '#title' => t('View'),
      '#url' => $view_link,
      '#attributes' => [
        'class' => ['button'],
      ],
    ];
  }
  if ($display->getComponent('edit_link') && $entity->access('update')) {
    $edit_link = $entity->toUrl('edit-form');
    $edit_link->setRouteParameter('destination', Url::fromRoute('<current>')->toString());
    $build['edit_link'] = [
      '#type' => 'link',
      '#title' => t('Edit'),
      '#url' => $edit_link,
      '#attributes' => [
        'class' => ['button', 'button--primary'],
      ],
    ];
  }
  if ($display->getComponent('entity_label')) {
    $build['entity_label'] = [
      '#type' => 'markup',
      '#markup' => '<h3>' . $entity->label() . '</h3>',
    ];
  }
  if ($display->getComponent('clarify_question') && $entity->access('update')) {
    $build['clarify_question'] = [
      '#title' => t('Clarify question'),
      '#type' => 'link',
      '#url' => $entity->toUrl('question-form')->setRouteParameter('destination', Url::fromRoute('<current>')->toString()),
      '#attributes' => [
        'class' => ['button'],
      ],
    ];
  }
  if ($display->getComponent('correction_data')) {
    /** @var \Drupal\pos_entity_cfp\Entity\Cfp $cfp */
    $cfp = $entity->getCfpEntity();
    if ($cfp && $state_for_review = $entity->get('state_for_review')->value) {
      $correction_plugin = $cfp->getStateCorrectionPlugin($state_for_review);
      if ($correction_plugin) {
        $build['correction_data']['#type'] = 'container';
        $build['correction_data']['questions'] = $correction_plugin->getCorrectionQuestionElement();
        $build['correction_data']['answers'] = $correction_plugin->getCorrectionAnswersElement();
      }
    }
  }
  if ($display->getComponent('reviewer_additional_files')) {
    if ((\Drupal::currentUser()->id() == $entity->getAssigneeId()) || (in_array('administrator', \Drupal::currentUser()->getRoles()))) {
      $field = 'reviewer_files';

      /** @var \Drupal\pos_entity_form_part\Form\EntityPartForm $form_part */
      $form_part = \Drupal::classResolver()
        ->getInstanceFromDefinition(EntityPartForm::class);
      $form_state = new FormState();
      $form_part->setFields([$field]);
      $form_part->setEntity($entity);
      $form_part->setRedirectionRoute('<current>');
      $form_part->setCurrentTabRedirect(TRUE);
      $form = \Drupal::formBuilder()->buildForm($form_part, $form_state);
      $form['reviewer_files']['widget']['#disable_dragging'] = TRUE;
      $build['reviewer_additional_files'] = $form;
    }
  }
  if ($display->getComponent('raw_signature')) {
    /** @var \Drupal\pos_entity_cfp\Entity\Cfp $cfp */
    $cfp = $entity->getCfpEntity();
    $build['raw_signature'] = [
      '#type' => 'inline_template',
      '#template' => pos_entity_review_get_signature_part($cfp),
    ];
  }
}

/**
 * Get signature part.
 */
function pos_entity_review_get_signature_part(Cfp $cfp) {
  $representative = $cfp->getApplicantEntity()->getCompanyRepresentative();
  return '<div class="signatures">
        <div class="signature-date"><p>Mjesto i datum: ____________________________ 2022.</p></div>
        <div class="signature">
            <p>____________________________</p>
            <p>' . $cfp->getApplicantEntity()->get('applicant_name')->value . '</p>
            <p>' . $representative?->label() .'</p>
        </div>';
}

/**
 * Implements hook_cron().
 */
function pos_entity_review_cron() {
  $query = \Drupal::database()->query('SELECT id FROM `review` WHERE cfp NOT IN (SELECT id FROM cfp)');
  $review_storage = \Drupal::entityTypeManager()->getStorage('review');
  while ($row = $query->fetch()) {
    $review = $review_storage->load($row->id);
    if ($review) {
      $review->delete();
    }
  }
}

/**
 * Implements hook_views_data_alter().
 */
function pos_entity_review_views_data_alter(array &$data) {
  $data['review']['review_project_implementation_filter'] = [
    'title' => t('Járás" - Okres - Project Implementation Place (Filter)'),
    'filter' => [
      'title' => t('Járás - Okres - Project Implementation Place (Filter)'),
      'field' => 'cfp',
      'id' => 'pos_review_address_filter',
    ],
  ];
}

/**
 * Implements hook_review_passed_alter().
 */
function pos_entity_review_review_passed_alter(&$passed, $entity) {
  /** @var \Drupal\pos_entity_review\Entity\ReviewType $review_type */
  $review_type = ReviewType::load($entity->bundle());
  if (($key_field = $review_type->getKeyField()) && !empty($accept_values = $review_type->getAcceptValues())) {
    if (in_array($entity->get($key_field)->value, $accept_values)) {
      if (pos_printer_is_printable_entity($entity->getEntityType(), $entity->bundle())) {
        if ($entity->isPrintConfirmed()) {
          $passed = TRUE;
        }
      }
      else {
        $passed = TRUE;
      }
    }
  }
}

/**
 * Implements hook_review_rejected_alter().
 */
function pos_entity_review_review_rejected_alter(&$rejected, $entity) {
  /** @var \Drupal\pos_entity_review\Entity\ReviewType $review_type */
  $review_type = ReviewType::load($entity->bundle());
  if (($key_field = $review_type->getKeyField()) && !empty($reject_values = $review_type->getRejectValues())) {
    if (in_array($entity->get($key_field)->value, $reject_values)) {
      if (pos_printer_is_printable_entity($entity->getEntityType(), $entity->bundle())) {
        if ($entity->isPrintConfirmed()) {
          $rejected = TRUE;
        }
      }
      else {
        $rejected = TRUE;
      }
    }
  }
}

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function pos_entity_review_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() === 'review' && !empty($bundle)) {
    $review_type = ReviewType::load($bundle);
    if ($key_field = $review_type->getKeyField()) {
      if (isset($fields[$key_field])) {
        $fields[$key_field]->addConstraint('reviewKeyFieldCondition');
      }
    }
  }
  if ($entity_type->id() === 'review' && $bundle === 'acceptance_2022_01') {
    if ($fields['cf_comment']) {
      $fields['cf_comment']->setRequired(TRUE);
      $fields['cf_comment']->addPropertyConstraints('value', ['NamedLength' => ['min' => 20, 'max' => 500]]);
    }
  }
}

/**
 * Implements hook_mail().
 */
function pos_entity_review_mail($key, &$message, $params) {
  switch ($key) {
    case 'reviewer_clarify_question':
      $message['headers']['From'] = $params['from'];
      $message['headers']['Sender'] = $params['from'];
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['message'];
      $message['headers']['Content-Type'] = 'text/html';
      break;
  }
}

content_copyCOPY