You are here

siteimprove.module in Siteimprove 7

Same filename and directory in other branches
  1. 8 siteimprove.module

Drupal Module: Siteimprove Plugin.

Adds the required Javascript to node forms pages for show a little box with the results from the latest SI scan of the current page.

File

siteimprove.module
View source
<?php

/**
 * @file
 * Drupal Module: Siteimprove Plugin.
 *
 * Adds the required Javascript to node forms pages for show a little box
 * with the results from the latest SI scan of the current page.
 */
define('SITEIMPROVE_PERMISSION_USE', 'use siteimprove');
define('SITEIMPROVE_PERMISSION_ADMINISTER', 'administer siteimprove');

/**
 * Implements hook_permission().
 */
function siteimprove_permission() {
  return array(
    SITEIMPROVE_PERMISSION_ADMINISTER => array(
      'description' => t('Configure Siteimprove Plugin token.'),
      'title' => t('Administer Siteimprove Plugin'),
    ),
    SITEIMPROVE_PERMISSION_USE => array(
      'title' => t('Access Siteimprove Plugin'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function siteimprove_menu() {
  $items['admin/config/system/siteimprove'] = array(
    'access arguments' => array(
      SITEIMPROVE_PERMISSION_ADMINISTER,
    ),
    'description' => 'Configure Siteimprove Plugin token.',
    'file' => 'siteimprove.admin.inc',
    'page arguments' => array(
      'siteimprove_admin_settings_form',
    ),
    'page callback' => 'drupal_get_form',
    'title' => 'Siteimprove Plugin',
    'type' => MENU_NORMAL_ITEM,
    'weight' => 99,
  );
  return $items;
}

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

  // Check if the user has access.
  if (user_access(SITEIMPROVE_PERMISSION_USE)) {

    // In node and taxonomy term forms, add siteimprove js.
    if (strpos($form_id, '_node_form') !== FALSE && isset($form_state['node']->nid) || $form_id == 'taxonomy_form_term' && isset($form_state['term']->tid)) {

      // Only show recheck button for published nodes or any term.
      if (strpos($form_id, '_node_form') !== FALSE && isset($form_state['node']->nid) && $form_state['node']->status != 0 || $form_id == 'taxonomy_form_term' && isset($form_state['term']->tid)) {

        // Add recheck button.
        $form['actions']['recheck'] = array(
          '#attributes' => array(
            'class' => array(
              'recheck-button',
            ),
          ),
          '#type' => 'button',
          '#value' => t('Siteimprove Recheck'),
          '#weight' => 7,
        );
      }
    }
  }
}

/**
 * Implements hook_node_insert().
 */
function siteimprove_node_insert($node) {
  if ($node->status == 1 && user_access(SITEIMPROVE_PERMISSION_USE)) {
    SiteimproveUtils::setSessionUrl($node, 'nid', 'node/');
  }
}

/**
 * Implements hook_node_update().
 */
function siteimprove_node_update($node) {
  if ($node->status == 1 && user_access(SITEIMPROVE_PERMISSION_USE)) {
    SiteimproveUtils::setSessionUrl($node, 'nid', 'node/');
  }
}

/**
 * Implements hook_taxonomy_term_insert().
 */
function siteimprove_taxonomy_term_insert($term) {
  if (user_access(SITEIMPROVE_PERMISSION_USE)) {
    SiteimproveUtils::setSessionUrl($term, 'tid', 'taxonomy/term/');
  }
}

/**
 * Implements hook_taxonomy_term_update().
 */
function siteimprove_taxonomy_term_update($term) {
  if (user_access(SITEIMPROVE_PERMISSION_USE)) {
    SiteimproveUtils::setSessionUrl($term, 'tid', 'taxonomy/term/');
  }
}

/**
 * Implements hook_init().
 */
function siteimprove_init() {

  // Check if user has access and ignore some paths.
  if (user_access(SITEIMPROVE_PERMISSION_USE) && (empty($_SERVER['REDIRECT_QUERY_STRING']) || $_SERVER['REDIRECT_QUERY_STRING'] != 'render=overlay' || path_is_admin(current_path())) && current_path() != 'batch') {

    // If exist siteimprove_url in SESSION, send to Siteimprove.
    if (!empty($_SESSION['siteimprove_url'])) {
      if (count($_SESSION['siteimprove_url']) > 1) {
        $url = url('<front>', array(
          'absolute' => TRUE,
        ));
        $method = 'recrawl';
      }
      else {
        $url = array_pop($_SESSION['siteimprove_url']);
        $method = 'recheck';
      }

      // Include all Siteimprove js scripts.
      SiteimproveUtils::includeJs($url, $method);
      unset($_SESSION['siteimprove_url']);
    }

    // Find the "siteimprove" path of the current page.
    $siteimprove_path = siteimprove_is_frontpage() ? '<front>' : current_path();

    // If node page or taxonomy term page, add input method, else domain method.
    if (preg_match('/(^node\\/\\d*$)|(^taxonomy\\/term\\/\\d*$)/', current_path())) {
      SiteimproveUtils::includeJs(url($siteimprove_path, array(
        'absolute' => TRUE,
      )), 'input');
    }
    elseif (preg_match('/(^node\\/\\d*(\\/edit)?$)|(^taxonomy\\/term\\/\\d*(\\/edit)?$)/', current_path())) {
      SiteimproveUtils::includeJs(url($siteimprove_path, array(
        'absolute' => TRUE,
      )), 'input');
      SiteimproveUtils::includeJs(url($siteimprove_path, array(
        'absolute' => TRUE,
      )), 'recheck', FALSE);
    }
    else {
      SiteimproveUtils::includeJs(url($siteimprove_path, array(
        'absolute' => TRUE,
      )), 'domain');
    }
  }
}

/**
 * Trying to detect if the current page is the frontpage.
 *
 * @return bool
 *   True if the page is the frontpage, false otherwise.
 */
function siteimprove_is_frontpage() {
  $frontpage = variable_get('site_frontpage', '');
  if (drupal_is_front_page()) {
    return TRUE;
  }
  elseif (!empty($frontpage) && preg_match('/(^node\\/\\d*(\\/edit)?$)/', current_path())) {
    $current_entity_path = 'node/' . arg(1);
    return $current_entity_path == $frontpage;
  }
  elseif (!empty($frontpage) && preg_match('/(^taxonomy\\/term\\/\\d*(\\/edit)?$)/', current_path())) {
    $current_entity_path = 'taxonomy/term/' . arg(2);
    return $current_entity_path == $frontpage;
  }
  else {
    return FALSE;
  }
}

Functions

Constants

Namesort descending Description
SITEIMPROVE_PERMISSION_ADMINISTER
SITEIMPROVE_PERMISSION_USE @file Drupal Module: Siteimprove Plugin.