You are here

event_log_track.module in Events Log Track 8

Same filename and directory in other branches
  1. 8.2 event_log_track.module

Track the logs of form submissions or other actions that performed by user.

File

event_log_track.module
View source
<?php

/**
 * @file
 * Track the logs of form submissions or other actions that performed by user.
 */
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function event_log_track_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.event_log_track':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t("You can track logs of specific events that you'd like to log. The events  by the user (using the forms) are saved in the database and can be viewed on the page admin/reports/events-track. You could use this to track number of times the CUD operation performed by which users. This module required by: Events Log Track User Authentication, Events Log Track Menu, Events Log Track Node, Events Log Track Taxonomy, Events Log Track User.") . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Events Log Track Menu') . '</dt>';
      $output .= '<dd>' . t('Using this submodule you can logs menu CUD events performed by the user. This module requires: Events Log Track.') . '</dd>';
      $output .= '</dl>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Events Log Track Node') . '</dt>';
      $output .= '<dd>' . t('Using this submodule you can logs node CUD events performed by the user. This module requires: Events Log Track.') . '</dd>';
      $output .= '</dl>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Events Log Track Taxonomy') . '</dt>';
      $output .= '<dd>' . t('Using this submodule you can logs taxonomy vocabulary and term CUD events performed by the user. This module requires: Events Log Track.') . '</dd>';
      $output .= '</dl>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Events Log Track User') . '</dt>';
      $output .= '<dd>' . t('Using this submodule you can logs user CUD events performed by the user. This module requires: Events Log Track.') . '</dd>';
      $output .= '</dl>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Events Log Track User Authentication') . '</dt>';
      $output .= '<dd>' . t('Using this submodule you can logs user authentication (login logout and request password). This module requires: Events Log Track.') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Inserts the log record in the event log track. Sets the lid.
 *
 * @param array $log
 *   The log record to be saved. This record contains the following fields:
 *   - {string} type
 *     The event type. This is usually the object type that is described by this
 *     event. Example: 'node' or 'user'. Required.
 *   - {string} operation
 *     The operation being performed. Example: 'insert'. Required.
 *   - {string} description
 *     A textual description of the event. Required.
 *   - {string} ref_numeric
 *     Reference to numeric id. Optional.
 *   - {string} ref_char
 *     Reference to alphabetical id. Optional.
 */
function event_log_track_insert(array &$log) {
  if (PHP_SAPI == 'cli') {

    // Ignore CLI requests.
    return;
  }
  if (empty($log['created'])) {
    $log['created'] = REQUEST_TIME;
  }
  if (empty($log['uid'])) {
    $account = \Drupal::currentUser();
    $log['uid'] = $account
      ->id();
  }
  $ip = Drupal::request()
    ->getClientIp();
  if (empty($log['ip']) && !empty($ip)) {
    $log['ip'] = $ip;
  }
  if (empty($log['path'])) {
    $log['path'] = Url::fromRoute('<current>')
      ->getInternalPath();
  }
  if (empty($log['ref_numeric'])) {
    $log['ref_numeric'] = NULL;
  }
  \Drupal::database()
    ->merge('event_log_track')
    ->key('lid')
    ->fields(array(
    'type' => $log['type'],
    'operation' => $log['operation'],
    'description' => $log['description'],
    'created' => $log['created'],
    'uid' => $log['uid'],
    'ip' => $log['ip'],
    'path' => $log['path'],
    'ref_char' => $log['ref_char'],
    'ref_numeric' => $log['ref_numeric'],
  ))
    ->execute();
}

/**
 * Returns all existing event handlers.
 *
 * @return array
 *   An array with the event log handlers.
 */
function event_log_track_get_event_handlers() {
  $handlers = drupal_static(__FUNCTION__);
  if ($handlers === NULL) {
    $handlers = Drupal::moduleHandler()
      ->invokeAll('event_log_track_handlers');
    \Drupal::moduleHandler()
      ->alter('event_log_track_handlers', $handlers);
  }
  return $handlers;
}

/**
 * Form submission callback.
 */
function event_log_track_form_submit(&$form, FormStateInterface $form_state) {
  if (!empty($form_state->__event_log_track_logged)) {

    // Some forms are submitted twice, for instance the node_form.
    // We will only call the submit callback once.
    return;
  }
  $form_state->__event_log_track_logged = TRUE;

  // Get form id.
  $form_id = $form['#form_id'];

  // Dispatch the submission to the correct event handler.
  $handlers = event_log_track_get_event_handlers();
  foreach ($handlers as $type => $handler) {
    $dispatch = FALSE;
    if (!empty($handler['form_ids']) && in_array($form_id, $handler['form_ids'])) {
      $dispatch = TRUE;
    }
    elseif (!empty($handler['form_ids_regexp'])) {
      foreach ($handler['form_ids_regexp'] as $regexp) {
        if (preg_match($regexp, $form_id)) {
          $dispatch = TRUE;
          break;
        }
      }
    }
    if ($dispatch) {

      // Dispatch!
      $function = $handler['form_submit_callback'];
      if (function_exists($function)) {
        $log = $function($form, $form_state, $form_id);
        if (!empty($log)) {

          // Log the event.
          $log['type'] = $type;
          event_log_track_insert($log);
        }
      }
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function event_log_track_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Add submit callback to ANY form action.
  event_log_track_add_submit_handler($form, 'event_log_track_form_submit');
}

/**
 * Adds a submit handler to all submit hooks in the form tree.
 *
 * @param array &$element
 *   A form element or the form itself.
 * @param string $callback
 *   The callback to be added.
 */
function event_log_track_add_submit_handler(array &$element, $callback) {
  if (array_key_exists("#submit", $element)) {
    $element["#submit"][] = $callback;
  }
  $keys = Element::children($element);
  foreach ($keys as $key) {
    if (is_array($element[$key])) {
      event_log_track_add_submit_handler($element[$key], $callback);
    }
  }
}

Functions

Namesort descending Description
event_log_track_add_submit_handler Adds a submit handler to all submit hooks in the form tree.
event_log_track_form_alter Implements hook_form_alter().
event_log_track_form_submit Form submission callback.
event_log_track_get_event_handlers Returns all existing event handlers.
event_log_track_help Implements hook_help().
event_log_track_insert Inserts the log record in the event log track. Sets the lid.