You are here

invite.module in Invite 8

Default file for Invite Project.

File

invite.module
View source
<?php

/**
 * @file
 * Default file for Invite Project.
 */
use Drupal\invite\InviteConstants;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\User;
use Drupal\invite\Entity\Invite;

/**
 * Implements hook_user_presave().
 */
function invite_user_presave(User $account) {
  if (!empty($_SESSION['invite_code'])) {

    // Update invite when user accepts.
    $id = Drupal::entityQuery('invite')
      ->condition('reg_code', $_SESSION['invite_code'])
      ->execute();
    $invite = Invite::load(reset($id));
    $invite
      ->setJoined(\Drupal::time()
      ->getRequestTime());
    $invite
      ->setStatus(InviteConstants::INVITE_USED);
    $invite
      ->save();

    // Add target role to user.
    $data = unserialize(\Drupal::config('invite.invite_type.' . $invite
      ->get('type')->value)
      ->get('data'));
    if ($data['target_role'] != '0') {
      $account
        ->addRole($data['target_role']);
    }
    $account
      ->activate();
  }
}

/**
 * Implements hook_user_insert().
 */
function invite_user_insert(User $account) {
  if (!empty($_SESSION['invite_code'])) {

    // Update invite when user accepts.
    $id = Drupal::entityQuery('invite')
      ->condition('reg_code', $_SESSION['invite_code'])
      ->execute();
    $invite = Invite::load(reset($id));
    $invite
      ->setInvitee($account);
    $invite
      ->save();
    unset($_SESSION['invite_code']);
  }
}

/**
 * Implements hook_help().
 */
function invite_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.invite':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Invite is an invitation system that lets you and your site members invite more people to join the site. Invitations are important to encourage expansion of your network and to provide an exponential growth of community interest. This project provides an “Invite a Friend” feature that allows your users to send and track invitations.') . '</p>';
      $output = '<h3>' . t('Configuration') . '</h3>';
      $output .= '<p>' . t('The configuration can be accessed in <i>Structure</i>  » <i>Invite types</i>.') . '</p>';
      $output .= '<p>' . t('Enable the invite and invite_by_email.') . '</p>';
      $output .= '<p>' . t('Navigate <i>Structure</i> » <i>Invite types</i> » Add Invite type and add an invite type.') . '</p>';
      $output .= '<p>' . t('Navigate <i>Structure</i> » Block layout and place the invite type block that you named.') . '</p>';
      $output .= '<h3>' . t('Usage') . '</h3>';
      $output .= '<p>' . t('Go to the page where the block was inserted.') . '</p>';
      $output .= '<p>' . t('Enter an email address in the block and click Send Invitation.') . '</p>';
      return $output;
  }
}

Functions

Namesort descending Description
invite_help Implements hook_help().
invite_user_insert Implements hook_user_insert().
invite_user_presave Implements hook_user_presave().