You are here

class moduleHelper in Rocket.Chat 8.2

Hierarchy

Expanded class hierarchy of moduleHelper

3 files declare their use of moduleHelper
RocketChatChannelBlock.php in modules/rocket_chat_group/src/Plugin/Block/RocketChatChannelBlock.php
RocketChatGroupHelper.php in src/Form/RocketChatGroupHelper.php
rocket_chat_group.module in modules/rocket_chat_group/rocket_chat_group.module

File

modules/rocket_chat_group/src/RocketChat/moduleHelper.php, line 18

Namespace

Drupal\rocket_chat_group\RocketChat
View source
class moduleHelper {
  function test() {
  }
  public static function updateChannelType(string $groupType, Channel &$Channel) {
    if ($groupType === "closed_group") {
      $Channel
        ->setChannelType(Channel::READ | Channel::WRITE | Channel::PRIVATE_CHANNEL);
    }
    else {
      $Channel
        ->setChannelType(Channel::READ | Channel::WRITE | Channel::PUBLIC_CHANNEL);
    }
  }

  //group_content

  /**
   * @param \Drupal\group\Entity\GroupContent $entity
   * @param string $action
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public static function ProcessGroupContentUpdate(GroupContent $entity, $action = "Unknown") {
    $groupSelected = $entity
      ->getGroup();
    $groupOwner = $entity
      ->getOwner();
    $targetUserFields = $entity
      ->getFields(FALSE);
    $targetGroupFields = $groupSelected
      ->getFields(FALSE);
    $userRef = $targetUserFields['entity_id']
      ->getValue();
    $userRefEntity = Drupal::entityTypeManager()
      ->getStorage('user')
      ->load($userRef[0]['target_id']);
    $groupEntity = $entity
      ->getGroup();
    $groupType = $groupEntity
      ->getGroupType();
    $groupEntitytypeId = $groupType
      ->id();
    $channel = new Channel();
    $rcState = new Drupal8State(Drupal::service('state'));
    $chatGroupOwner = new RocketchatUser($rcState, $groupOwner
      ->getAccountName(), $groupOwner
      ->getEmail(), $groupOwner
      ->getDisplayName());
    $channel
      ->setOwner($chatGroupOwner);
    $fieldMachineName = self::extractChannelMachineName($groupSelected);
    $channelName = NULL;
    if (!empty($fieldMachineName) && isset($targetGroupFields[$fieldMachineName])) {
      $test[] = $targetGroupFields[$fieldMachineName]
        ->getValue();
      foreach ($targetGroupFields[$fieldMachineName]
        ->getValue() as $channelDetails) {
        foreach ($channelDetails as $channelName) {
          $channel
            ->setChannelName($channelName);
          break;
        }
        break;
      }
      self::updateChannelType($groupEntitytypeId, $channel);
    }
    $apiConfig = new Drupal8Config(Drupal::configFactory(), Drupal::moduleHandler(), Drupal::state(), Drupal::messenger());
    $apiClient = new ApiClient($apiConfig);
    switch ($action) {
      default:
        self::addLogEntry("no action taken on [{$action}] for Group [" . $groupSelected
          ->id() . "]");
        break;
      case "insert":

        // Inserting something into a group content.
        $channel
          ->getChannelProxy($apiClient);
        $channel
          ->getChannelMembers($apiClient);

        /** @var \Drupal\user\Entity\User[] $GroupMembers */
        $GroupMembers = [];
        foreach ($userRef as $target) {
          $targetUser = Drupal::entityTypeManager()
            ->getStorage('user')
            ->load($target['target_id']);
          if (!empty($targetUser)) {
            $GroupMembers[] = $targetUser;
          }
          unset($targetUser);
        }
        foreach ($GroupMembers as $groupMember) {
          $rcUser = new RocketchatUser($rcState, $groupMember
            ->getAccountName(), $groupMember
            ->getEmail(), $groupMember
            ->getDisplayName());
          $rcUser
            ->getUserProxy($apiClient);
          $channel
            ->addMember($apiClient, $rcUser);
        }
        $channels = new Channels($rcState, $apiClient);
        $channels
          ->refreshCache(TRUE);
        return;
        break;
      case "delete":

        // Inserting something into a group content.
        $channel
          ->getChannelProxy($apiClient);
        $channel
          ->getChannelMembers($apiClient);

        /** @var \Drupal\user\Entity\User[] $GroupMembers */
        $GroupMembers = [];
        foreach ($userRef as $target) {
          $targetUser = Drupal::entityTypeManager()
            ->getStorage('user')
            ->load($target['target_id']);
          if (!empty($targetUser)) {
            $GroupMembers[] = $targetUser;
          }
          unset($targetUser);
        }
        foreach ($GroupMembers as $groupMember) {
          $rcUser = new RocketchatUser($rcState, $groupMember
            ->getAccountName(), $groupMember
            ->getEmail(), $groupMember
            ->getDisplayName());
          $rcUser
            ->getUserProxy($apiClient);

          //          $channel;
          $removed = $channel
            ->removeMember($apiClient, $rcUser);

          //TODO log result of removal
        }
        $channels = new Channels($rcState, $apiClient);
        $channels
          ->refreshCache(TRUE);
        return;
        break;
    }
    if ($apiClient
      ->isLoggedIn()) {
      $channelProxy = $channel
        ->getChannelProxy($apiClient);
      $rcState = new Drupal8State(Drupal::state());
      $user = new RocketchatUser($rcState, $userRefEntity
        ->getAccountName(), $userRefEntity
        ->getEmail(), $userRefEntity
        ->getDisplayName());
      $user
        ->getUserProxy($apiClient);
      $channel
        ->addMember($apiClient, $user);
      $users = [];
      $usersProxy = new Users($rcState, $apiClient);
      $users = $usersProxy
        ->getCache();
      $channel
        ->getChannelType();
    }
    else {
      Drupal::messenger()
        ->addWarning("Unable to do anything before you login to Rocket.Chat.");
    }
  }

  /**
   * Get the Machine name for the channel in this group.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *
   * @return string|null
   */
  public static function extractChannelMachineName(GroupInterface $group) {
    $fieldMachineName = NULL;
    $groupEntityTypeId = $group
      ->getEntityTypeId();
    $groupBundle = $group
      ->bundle();
    $fieldMapping = Drupal::service('entity_field.manager')
      ->getFieldMapByFieldType('channel');
    if (isset($fieldMapping[$groupEntityTypeId])) {
      $channelEntity = $fieldMapping[$groupEntityTypeId];
      foreach ($channelEntity as $fieldName => $fieldSettings) {
        if (isset($fieldSettings['bundles'])) {
          foreach ($fieldSettings['bundles'] as $bundle) {
            if (strcmp($groupBundle, $bundle) === 0) {
              $fieldMachineName = $fieldName;
              break;
            }
          }
        }
      }
    }
    return $fieldMachineName;
  }
  public static function themeRocketChannelBLock($existing, $type, $theme, $path) {
    return [
      'rocketChatChannelBlock' => [
        'variables' => [
          'url' => "https://demo.rocketchat.chat",
          'width' => "0px",
          'height' => "0px",
          'host' => "*",
          'app' => "rocketchat://demo.rocket.chat",
          'showDirectLink' => FALSE,
        ],
      ],
    ];
  }
  private static function addLogEntry(string $message, string $level = "info") {
    $Logger = Drupal::logger("Rocket Chat Group: ModuleHelper");
    $Logger
      ->log($level, $message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
moduleHelper::addLogEntry private static function
moduleHelper::extractChannelMachineName public static function Get the Machine name for the channel in this group.
moduleHelper::ProcessGroupContentUpdate public static function
moduleHelper::test function
moduleHelper::themeRocketChannelBLock public static function
moduleHelper::updateChannelType public static function