rc-1.0.x-dev/rc.module
rc.module
<?php
/**
* @file
* Contains rc.module.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function rc_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the rc module.
case 'help.page.rc':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('An integration with Rocket.Chat using the official release of rcocket.chat PHP library.') . '</p>';
return $output;
default:
}
}
/**
* Custom function to unify the hook_theme default variables.
* @return array
*/
function rc_theme_base_variables(): array {
return [
'url' => NULL,
'server_url' => NULL,
'iframe_id' => NULL,
'token' => NULL,
'width' => '350px',
'height' => '750px',
'mobile' => NULL,
'web' => NULL,
'new_window_url' => NULL,
'open_new_window' => NULL,
'new_window_width' => NULL,
'new_window_height' => NULL,
];
}
/**
* Implements hook_theme().
*/
function rc_theme($existing, $type, $theme, $path) {
$templates = [];
// The base variables that will be used in all template files.
$baseVariables = rc_theme_base_variables();
$templates['rc_user_block'] = [
'render element' => 'children',
'template' => 'rc-user-block',
'path' => $path . '/templates',
'variables' => $baseVariables,
];
$templates['rc_user_popup_block'] = [
'render element' => 'children',
'template' => 'rc-user-popup-block',
'path' => $path . '/templates',
'variables' => $baseVariables +
[
'button_element' => NULL,
'button_icon' => NULL,
'button_icon_path' => NULL,
'button_text' => NULL,
],
];
return $templates;
}
/**
* Implements hook_toolbar_alter().
*/
function rc_toolbar_alter(&$items) {
$items['administration']['#attached']['library'][] = 'rc/toolbar';
}
/**
* Create new chat account when a new user entity is created.
*
* Implements hook_ENTITY_TYPE_presave() for user entities.
*
* @param \Drupal\user\UserInterface $user
*/
function rc_user_presave(UserInterface $user) {
// Load Rocket chat user settings.
$rcSettings = \Drupal::config('rc.settings');
// If user is new.
if ($rcSettings->get('user.create_user_register') && $user->isNew()) {
// Load RcUser class.
$rcUsers = \Drupal::service('rc.user');
// Create Rocket Chat user.
$rcUsers->createUser($user);
// If user is being updated and config is active, chat account while user is being updated.
}
elseif ($rcSettings->get('user.create_user_edit') && !$user->isNew() && $user->original) {
// Load RCUsers class.
$rcUsers = \Drupal::service('rc.user');
$userUpdate = $rcUsers->updateUser($user);
if (!$userUpdate) {
// Create Rocket Chat user.
$rcUsers->createUser($user);
}
}
}
/**
* This hook is used to show or hide the Rocket Chat ID field on the user form.
*
* Implements hook_form_FORM_ID_alter().
*/
function rc_form_user_form_alter(&$form, FormStateInterface $form_state) {
$rcSettings = \Drupal::config('rc.settings');
$rcIdVisibility = $rcSettings->get('user.visibility_rcid_form');
if (!$rcIdVisibility) {
unset($form['field_rcid']);
unset($form['field_rc_token']);
}
}
/**
* Show or hide the Rocket Chat ID field on the user view page.
*
* Implements hook_preprocess_HOOK().
*/
function rc_preprocess_user(&$variables) {
$rcSettings = \Drupal::config('rc.settings');
$rcIdVisibility = $rcSettings->get('user.visibility_rcid_display');
if (!$rcIdVisibility) {
unset($variables['content']['field_rcid']);
unset($variables['content']['field_rc_token']);
}
}
/**
* Deactivate user chat account when user entity is blocked.
*
* Implements hook_user_cancel().
*
* @param $edit
* @param \Drupal\Core\Session\AccountInterface $account
* @param $method
*/
function rc_user_cancel($edit, AccountInterface $account, $method) {
$disableOptions = [
'user_cancel_block',
'user_cancel_block_unpublish',
];
if (in_array($method, $disableOptions)) {
$rcUser = \Drupal::service('rc.user');
$rcUser->updateUser($account);
}
}
/**
* Delete user chat account when user entity is deleted.
*
* Implements hook_user_delete().
*
* @param \Drupal\user\UserInterface $user
*/
function rc_user_delete(AccountInterface $account) {
$rcUser = \Drupal::service('rc.user');
$rcUser->deleteUser($account);
}
/**
* Alter the RC Block form to add Rocket Chat server URL as a prefix for the URL
* field in the RC User Block.
*
* Implements hook_form_FORM_ID_alter().
*/
function rc_form_rc_user_block_form_alter(&$form, FormStateInterface $form_state) {
// Get the Rocket Chat server URL.
$rcSettings = \Drupal::config('rc.settings');
$rcUrl = $rcSettings->get('access.server');
$form['field_rc_url']['widget'][0]['value']['#field_prefix'] = '<div>' . $rcUrl . '</div>';
}
/**
* Implements hook_user_login().
*/
function rc_user_login(UserInterface $account) {
// Get RC Settings.
$rcConfig = \Drupal::config('rc.settings');
$createUser = $rcConfig->get('user.create_user_login');
$rcUsers = \Drupal::service('rc.user');
$checkIfUserExist = $rcUsers->checkIfUserExist($account);
if ($checkIfUserExist) {
$rcId = $account->field_rcid->value;
if (!$rcId) {
$account->field_rcid->value = $checkIfUserExist->getUserId();
}
if (!$account->field_rc_token->value) {
$rcUsers->generatePersonalAccessToken($account);
}
}
elseif ($createUser) {
// Create Rocket Chat user.
$rcUsers->createUser($account);
}
}
/**
* Create and update chat accounts for users.
*
* Implements hook_cron().
*/
function rc_cron() {
// Create chat account for users who do not have chat accounts.
$rcSettings = \Drupal::config('rc.settings');
if ($rcSettings->get('user.create_user_cron') || $rcSettings->get('user.update_user_cron')) {
$rcUsers = \Drupal::service('rc.user');
}
else {
return;
}
$uIdsToCreate = [];
if ($rcSettings->get('user.create_user_cron')) {
// Query users to get the ones that do not have Rocket Chat ID field value.
$query = \Drupal::entityQuery('user')
->condition('status', 1)
->notExists('field_rcid.0.value');
$uIdsToCreate = $query->execute();
$usersToCreate = \Drupal::entityTypeManager()->getStorage('user')
->loadMultiple($uIdsToCreate);
foreach ($usersToCreate as $user) {
// Create Rocket Chat user.
$rcUsers->createUser($user, TRUE);
}
// @todo Clean up chat accounts that does not have users on Drupal Except Master account.
}
if ($rcSettings->get('user.update_user_cron')) {
// Update all users who has rocket chat ID. This will help to change the
// password and refresh the accounts.
// Query users to get the ones that have Rocket Chat ID field value.
$query = \Drupal::entityQuery('user')
->condition('status', 1)
->exists('field_rcid.0.value');
// Get the User IDs that match the condition.
$uIdsToUpdate = $query->execute();
// Exclude the users created in the previous step.
$uIdsToUpdate = array_diff($uIdsToUpdate, $uIdsToCreate);
// Load the user entities to perform the Rocket Chat accounts update.
$usersToUpdate = \Drupal::entityTypeManager()->getStorage('user')
->loadMultiple($uIdsToUpdate);
foreach ($usersToUpdate as $user) {
// Update Rocket Chat user.
$rcUsers->updateUser($user, TRUE);
}
}
}
