pos_entity_applicant.module

PHOTO EMBED

Thu Sep 29 2022 17:34:26 GMT+0000 (UTC)

Saved by @igor #drupal #mysql

<?php

/**
 * @file
 * Contains pos_entity_applicant.module.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\pos_entity_applicant\Entity\ApplicantType;
use Drupal\pos_entity_applicant\Event\UserCancelEvent;

module_load_include('inc', 'pos_entity_applicant', 'pos_entity_applicant.tokens');
module_load_include('inc','pos_entity_applicant', 'pos_entity_applicant.views');
module_load_include('inc', 'pos_entity_applicant', 'pos_entity_applicant.validation');

/**
 * Implements hook_help().
 */
function pos_entity_applicant_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the pos_entity_applicant module.
    case 'help.page.pos_entity_applicant':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('General Applicant Entity') . '</p>';
      return $output;

    default:
  }
}

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

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

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

/**
 * Implements hook_entity_extra_field_info().
 */
function pos_entity_applicant_entity_extra_field_info() {
  $extra = [];
  foreach (ApplicantType::loadMultiple() as $bundle) {
    $extra['applicant'][$bundle->id()]['display']['edit_link'] = [
      'label' => t('Edit link'),
      'description' => 'Entity edit link',
      'weight' => 20,
      'visible' => TRUE,
    ];

    $extra['applicant'][$bundle->id()]['display']['view_link'] = [
      'label' => t('View link'),
      'description' => 'Entity view link',
      'weight' => 20,
      'visible' => TRUE,
    ];

    $extra['applicant'][$bundle->id()]['display']['state_description'] = [
      'label' => t('State description'),
      'description' => 'Shows state description defined in applicant bundle',
      'weight' => 20,
      'visible' => TRUE,
    ];

    $extra['applicant'][$bundle->id()]['display']['locked_button'] = [
      'label' => t('Locked button'),
      'description' => 'Locked',
      'weight' => 20,
      'visible' => TRUE,
    ];

    $extra['applicant'][$bundle->id()]['display']['representative'] = [
      'label' => t('Ovlašteno lice za zastupanje'),
      'description' => t('Ime i prezime predstavnika'),
      'weight' => 20,
      'visible' => FALSE,
    ];
  }
  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function pos_entity_applicant_applicant_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display->getComponent('edit_link') && $entity->access('update')) {
    $build['edit_link'] = [
      '#type' => 'link',
      '#title' => t('Edit'),
      '#url' => Url::fromRoute('entity.' . $entity->getEntityTypeId() . '.edit_form', [
        $entity->getEntityTypeId() => $entity->id(),
      ]),
      '#attributes' => [
        'class' => ['button'],
      ],
    ];
  }
  if ($display->getComponent('view_link') && $entity->access('view')) {
    $build['view_link'] = [
      '#type' => 'link',
      '#title' => t('View'),
      '#url' => Url::fromRoute('entity.' . $entity->getEntityTypeId() . '.canonical', [
        $entity->getEntityTypeId() => $entity->id(),
      ]),
      '#attributes' => [
        'class' => ['button'],
      ],
    ];
  }
  if ($display->getComponent('state_description')) {
    $applicant_type = ApplicantType::load($entity->bundle());
    if ($description = $applicant_type->getStateDescription($entity->getState())) {
      $build['state_description'] = [
        '#type' => 'inline_template',
        '#template' => '<div class="applicant__state-description">' . $description . '</div>',
      ];
    }
  }
  if ($display->getComponent('locked_button')) {
    if (!$entity->isLocked() && (bool) $entity->get('validation_passed')->value) {
      $build['locked_button'] = [
        '#type' => 'link',
        '#url' => new Url('pos_entity_applicant.applicant_lock_data_confirm', [
          'applicant' => $entity->id(),
        ]),
        '#title' => t('Lock'),
        '#attributes' => [
          'class' => [
            'button',
          ],
        ],
      ];
    }
  }
  if ($display->getComponent('representative') && $entity->get('company_representative')->target_id) {
    $representative = $entity->get('company_representative')->entity;
    $rep_name = $representative->get('field_firstname')->value ?? '';
    $rep_lastname = $representative->get('field_lastname')->value ?? '';
    if ($rep_name === '' && $rep_lastname === '') {

    }
    else {
      $build['representative'] = [
        '#type' => 'container',
        'zastupnik' => [
          '#type' => 'container',
          'label' => [
            '#type' => 'markup',
            '#markup' => t('Ovlašteno lice za zastupanje'),
          ],
          '#attributes' => [
            'class' => ['current_applicant_representative-label'],
          ]
        ],
        'representative' => [
          '#type' => 'markup',
          '#markup' => '<div class="current_applicant_representative-item">' . $rep_lastname . ' ' . $rep_name . '</div>',
        ]
      ];
    }
  }
}

/**
 * Implements hook_cron().
 */
function pos_entity_applicant_cron() {
  $query = \Drupal::database()->query('SELECT applicant.id FROM applicant WHERE applicant.id NOT IN (SELECT applicant FROM users_field_data) ');
  $storage = \Drupal::entityTypeManager()->getStorage('applicant');
  while ($row = $query->fetch()) {
    $company = $storage->load($row->id);
    $company->delete();
  }
}

/**
 * Get applicant bundles.
 *
 * @return array
 *   Applicant bundles.
 */
function get_applicant_bundles() {
  $applicant_types = ApplicantType::loadMultiple();
  $applicant_list = [];
  foreach ($applicant_types as $applicant_type) {
    $applicant_list[$applicant_type->id()] = $applicant_type->label();
  }
  return $applicant_list;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function pos_entity_applicant_form_applicant_company_edit_form_alter(&$form, FormStateInterface $form_state) {
  $form['vat_number_entitled_true']['#states'] = [
    'visible' => [
      ':input[name="entitled_to_vat_refund"]' => ['value' => 'yes']
    ],
  ];
  $form['annual_income']['widget']['#description'] = 'Molimo vas da unesete najmanje podatke za 2021.godinu, ukoliko je vaše pravno lice osnovano prije 31. decembra 2021.';
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function pos_entity_applicant_form_applicant_individual_edit_form_alter(&$form, FormStateInterface $form_state) {
  $form['vat_number_entitled_true']['#states'] = [
    'visible' => [
      ':input[name="entitled_to_vat_refund"]' => ['value' => 'yes']
    ],
  ];
  $form['annual_income']['widget']['#description'] = 'Molimo vas da unesete najmanje podatke za 2021.godinu, ukoliko je vaše gazdinstvo osnovano prije 31. decembra 2021.';
}

/**
 * Implements hook_inline_entity_form_entity_form_alter().
 */
function pos_entity_applicant_inline_entity_form_entity_form_alter(array &$entity_form, FormStateInterface &$form_state) {
  if ($entity_form['#entity']->getEntityTypeId() === 'basic' && $entity_form['#entity']->bundle() === 'owned_other_companies') {
    if (isset($entity_form['cf_vat_number_entitled_true'])) {
      $entity_form['cf_vat_number_entitled_true']['#states'] = [
        'visible' => [
          [
            ':input[name="cf_owners_companies[form][inline_entity_form][entities][0][form][cf_entitled_to_vat_refund]"]' => ['value' => 1],
          ],
          'or',
          [
            ':input[name="cf_owners_companies[form][0][cf_entitled_to_vat_refund]"]' => ['value' => 1],
          ],
        ],
      ];
    }
  }
}

/**
 * Implements hook_views_data().
 */
function pos_entity_applicant_views_data() {
  $data = [];

  $data['views']['applicant_has_cfp'] = [
    'title' => t('Did the applicant start cfp - Custom filter'),
    'filter' => [
      'title' => t('Did the applicant start cfp - Custom filter'),
      'field' => 'id',
      'id' => 'applicant_has_cfp',
    ],
  ];
  $data['views']['applicant_cfp'] = [
    'title' => t('Did the applicant start cfp - NEW Custom filter'),
    'filter' => [
      'title' => t('Did the applicant start cfp - NEW Custom filter'),
      'field' => 'id',
      'id' => 'applicant_cfp'
    ],
  ];

  return $data;
}

/**
 * Implements hook_ENTITY_TYPE_predelete().
 */
function pos_entity_applicant_user_predelete(\Drupal\Core\Entity\EntityInterface $entity) {
  $event = new UserCancelEvent($entity);

  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, UserCancelEvent::EVENT_NAME);
}

/**
 * Implements hook_mail().
 */
function pos_entity_applicant_mail($key, &$message, $params) {
  switch($key) {
    case 'pos_entity_applicant_mail_send':
      $mail_sender = \Drupal::config('system.site')->get('mail');
      $message['headers']['From'] = $mail_sender;
      $message['headers']['Sender'] = $mail_sender;
      $message['headers']['Return-Path'] = $mail_sender;
      $message['headers']['Content-Type'] = 'text/html';
      $message['from'] = $mail_sender;
      $message['subject'] = $params['title'];
      /** @var \Drupal\Core\Utility\Token $token */
      $token = \Drupal::service('token');
      /** @var \Drupal\pos_entity_applicant\Entity\Applicant $applicant */
      $applicant = $params['entity']->get('applicant')->entity ?? NULL;
      // Not using this tokens currently, but maybe we will some day in the future.
      $token_data = [
        'applicant' => $applicant,
        'user' => $params['entity'],
      ];
      $message['subject'] = $token->replace($params['title'], $token_data, ['clear' => TRUE]);
      $message['body'][] = $token->replace($params['message'], $token_data, ['clear' => TRUE]);

      $files = [];
      if (isset($params['attachments'])) {
        $message['params']['files'] = $params['attachments'];
        foreach ($params['attachments'] as $file) {
          $files[] = $file['filename'];
        }
      }
      $email_log = \Drupal::entityTypeManager()
        ->getStorage('pos_entity_email_logs')
        ->create([
          'title' => $message['subject'],
          'body' => isset($message['body'][0]) ? $message['body'][0] : '',
        ]);
      $email_log->save();
      break;
  }
}
content_copyCOPY