<?php

/**
 * @file
 * Install and update functions for the social_profile_organization_tag module.
 */

use Drupal\Core\Config\FileStorage;

/**
 * Implements hook_install().
 */
function social_profile_organization_tag_install() {
  // Grant the default permissions for this feature.
  user_role_grant_permissions(
    'contentmanager',
    [
      'delete terms in profile_organization_tag',
      'edit terms in profile_tag',
      'edit profile organization tags',
    ]
  );
  user_role_grant_permissions(
    'sitemanager',
    [
      'delete terms in profile_organization_tag',
      'edit terms in profile_tag',
      'edit profile organization tags',
    ]
  );
  // Set the weight of this module to 1 so it is loaded after
  // the social_profile module.
  module_set_weight('social_profile_organization_tag', 1);
}

/**
 * Make "Profile Organization Tag" vocabulary translatable.
 */
function social_profile_organization_tag_update_11201(): void {
  if (!\Drupal::moduleHandler()->moduleExists('social_content_translation')) {
    return;
  }

  $config_storage = \Drupal::service('config.storage');
  $config_path = \Drupal::service('extension.list.module')->getPath('social_profile_organization_tag') . '/config/static';
  $source = new FileStorage($config_path);

  $config_storage->write('language.content_settings.taxonomy_term.profile_organization_tag', (array) $source->read('language.content_settings.taxonomy_term.profile_organization_tag_11201'));
}

/**
 * Add new permissions for taxonomies.
 */
function social_profile_organization_tag_update_121001(): void {
  // Nothing to do if the module is not installed.
  if (!\Drupal::moduleHandler()->moduleExists("taxonomy_access_fix")) {
    return;
  }

  // Permission to grant.
  $taxonomy_permissions = [
    'view terms in profile_organization_tag',
    'view term names in profile_organization_tag',
    'select terms in profile_organization_tag',
  ];

  // Authenticated roles.
  $authenticated_roles = [
    'sitemanager',
    'verified',
    'contentmanager',
  ];

  // Load the permission.
  $entity_type_manager = \Drupal::entityTypeManager();
  foreach ($authenticated_roles as $role) {
    /** @var \Drupal\user\RoleInterface $role */
    $role = $entity_type_manager->getStorage('user_role')->load($role);

    // If the role is not have the permission, grant permission.
    foreach ($taxonomy_permissions as $taxonomy_permission) {
      if (!$role->hasPermission($taxonomy_permission)) {
        $role->grantPermission($taxonomy_permission);
        $role->save();
      }
    }
  }

  // For anonymous role, we need to check the if Registration Fields is enabled.
  if (!\Drupal::moduleHandler()->moduleExists("social_registration_fields")) {
    return;
  }

  // Load anonymous role.
  /** @var \Drupal\user\RoleInterface $role */
  $role = $entity_type_manager->getStorage('user_role')->load('anonymous');

  // If the role is not have the permission, grant permission.
  foreach ($taxonomy_permissions as $taxonomy_permission) {
    if (!$role->hasPermission($taxonomy_permission)) {
      $role->grantPermission($taxonomy_permission);
      $role->save();
    }
  }
}
