<?php

/**
 * @file
 * Install, update and uninstall functions for the social_event_managers module.
 */

use Drupal\user\Entity\Role;
use Symfony\Component\Yaml\Yaml;

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_event_managers.
 */
function social_event_managers_install() {
  // Fetch site manager role.
  $role = Role::load('sitemanager');
  // Set permission for site manager.
  if ($role instanceof Role) {
    // Set permission.
    $role->grantPermission('set social event managers settings');
    $role->trustData()->save();
  }

  // Add enrollments permissions to CM and SM.
  $roles = [
    'sitemanager',
    'contentmanager',
  ];

  foreach ($roles as $rolename) {
    $role = Role::load($rolename);
    // Set permission for site manager.
    if ($role instanceof Role) {
      // Set permission.
      $role->grantPermission('manage everything enrollments');
      $role->trustData()->save();
    }
  }
}

/**
 * Implements hook_update_dependencies().
 */
function social_event_managers_update_dependencies(): array {
  // New config changes should run after the VBO module changes
  // the structure of views.
  $dependencies['social_event_managers'][11001] = [
    'views_bulk_operations' => 8034,
  ];

  return $dependencies;
}

/**
 * Perform actions related to the installation of social_event_managers.
 */
function social_event_managers_update_8001() {
  social_event_managers_install();
}

/**
 * Add manage enrollments permission to SM and CM.
 */
function social_event_managers_update_8003() {
  $roles = [
    'sitemanager',
    'contentmanager',
  ];

  foreach ($roles as $rolename) {
    $role = Role::load($rolename);
    // Set permission for site manager.
    if ($role instanceof Role) {
      // Set permission.
      $role->grantPermission('manage everything enrollments');
      $role->trustData()->save();
    }
  }
}

/**
 * Add new field for rendered profile entity so we can sort it.
 *
 * Load in a config file from an specific update hook that will never change.
 * Update helper did not update all fields correctly.
 */
function social_event_managers_update_8004() {
  $config_file = \Drupal::service('extension.list.module')->getPath('social_event_managers') . '/config/update/social_event_managers_update_8004.yml';

  if (is_file($config_file)) {
    $settings = Yaml::parse((string) file_get_contents($config_file));
    if (is_array($settings)) {
      $config = \Drupal::configFactory()
        ->getEditable('views.view.event_manage_enrollments');

      $config->setData($settings)->save(TRUE);
    }
  }
}

/**
 * Update labels for event organiser field.
 */
function social_event_managers_update_8005() {
  // Load the existing configuration.
  $config_name = 'field.field.node.event.field_event_managers';
  $config = \Drupal::configFactory()->getEditable($config_name);
  $config_data = $config->getRawData();

  if (!empty($config_data['label'])) {
    // This to ensure any custom added values are not affected.
    if (strpos($config_data['label'], 'Event organisers') !== FALSE) {
      $config_data['label'] = 'Organizers';
    }

    $config->setData($config_data)->save();
    // Make sure we clear cached definitions for the fields.
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  }
}

/**
 * Add translation compatibility for Event fields and views.
 */
function social_event_managers_update_8006() {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper->executeUpdate('social_event_managers', 'social_event_managers_update_8006');

  // Output logged messages to related channel of update execution.
  return $updateHelper->logger()->output();
}

/**
 * Remove translation filter for the Manage All Enrollments view.
 */
function social_event_managers_update_10301() {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper->executeUpdate('social_event_managers', 'social_event_managers_update_10301');

  // Output logged messages to related channel of update execution.
  return $updateHelper->logger()->output();
}

/**
 * Update message template "message.template.activity_on_events_im_organizing".
 *
 * Added new 'preview' and 'cta_button' tokens.
 */
function social_event_managers_update_10302() {
  $config_file = \Drupal::service('extension.list.module')->getPath('social_event_managers') . '/config/static/message.template.member_added_by_event_organiser_10302.yml';
  if (is_file($config_file)) {
    $settings = Yaml::parse(file_get_contents($config_file));
    if (is_array($settings)) {
      $config = \Drupal::configFactory()
        ->getEditable('message.template.member_added_by_event_organiser');
      $config->setData($settings)->save(TRUE);
    }
  }
}

/**
 * Update 'views.view.event_manage_enrollments' after VBO module updates.
 */
function social_event_managers_update_11001(array &$sandbox): void {
  $config_file = \Drupal::service('extension.list.module')->getPath('social_event_managers') . '/config/static/views.view.event_manage_enrollments_11001.yml';

  if (is_file($config_file)) {
    $settings = Yaml::parse((string) file_get_contents($config_file));
    if (is_array($settings)) {
      $config = \Drupal::configFactory()
        ->getEditable('views.view.event_manage_enrollments');

      $config->setData($settings)->save(TRUE);
    }
  }
}

/**
 * Update label for message template.
 */
function social_event_managers_update_11101(): string {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper->executeUpdate('social_event_managers', __FUNCTION__);

  // Output logged messages to related channel of update execution.
  return $updateHelper->logger()->output();
}

/**
 * Update exposed filter button name.
 */
function social_event_managers_update_11401(): string {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper->executeUpdate('social_event_managers', __FUNCTION__);

  // Output logged messages to related channel of update execution.
  return $updateHelper->logger()->output();
}

/**
 * Enable pager limit filter on the event's page "Manage enrollments".
 */
function social_event_managers_update_12201(array &$sandbox): string {
  /** @var \Drupal\update_helper\UpdaterInterface $update_helper */
  $update_helper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $update_helper->executeUpdate('social_event_managers', __FUNCTION__);

  // Output logged messages to related channel of update execution.
  return $update_helper->logger()->output();
}
