You are here

oa_tour.module in Open Atrium Tours 7.2

Code for the Open Atrium Bootstrap Tour customizations.

File

oa_tour.module
View source
<?php

/**
 * @file
 * Code for the Open Atrium Bootstrap Tour customizations.
 */

/**
 * Implements hook_ctools_plugin_directory().
 */
function oa_tour_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'ctools' && $plugin_type == 'content_types') {
    return 'plugins/' . $plugin_type;
  }
}

/**
 * Implements hook_theme()
 */
function oa_tour_theme() {
  return array(
    'oa_tour_dropdown' => array(
      'template' => 'oa-tour-dropdown',
      'path' => drupal_get_path('module', 'oa_tour') . '/templates',
    ),
  );
}

/**
 * Implements hook_bootstrap_tour_alter().
 *
 * Dig into steps with special paths and alter them so that they either point
 * to the first node of a specific type if one exists, or the node add form of
 * that type if not.
 * 
 * Also, modify steps that need to add an oa_space to go directly to the the
 * first Space Blueprint, so that it doesn't break the tour.
 */
function oa_tour_bootstrap_tour_alter(&$tour) {
  global $user;
  $space_vocab = oa_core_taxonomy_vocabulary('space_type');
  $section_vocab = oa_core_taxonomy_vocabulary('section_type');
  $space_blueprints = taxonomy_get_tree($space_vocab->vid, 0, 1);
  $section_types = taxonomy_get_tree($section_vocab->vid, 0, 1);
  if (empty($tour) || empty($tour->steps)) {
    return;
  }
  foreach ($tour->steps as $key => $step) {
    if (empty($step->path)) {
      continue;
    }
    $path_parts = explode('/', $step->path);
    if (in_array($path_parts[0], array(
      'firstnode',
      'mynode',
    )) && count($path_parts) > 1) {

      // Special step path: firstnode/<node_type>/<node_add_tour>
      //
      // Examples:
      //   firstnode/oa_space/spaces
      //   firstnode/oa_space
      if ($path_parts[0] == 'firstnode') {
        $node_type = $path_parts[1];
        $node_add_tour = count($path_parts) > 2 ? $path_parts[2] : NULL;
        $redirect_path = NULL;
        $nid = db_select('node', 'n')
          ->fields('n', array(
          'nid',
        ))
          ->condition('type', $node_type)
          ->orderBy('nid', 'DESC')
          ->range(0, 1)
          ->execute()
          ->fetchField();
      }
      elseif ($path_parts[0] == 'mynode') {

        // Get the optional path to redirect to.
        $redirect_path = count($path_parts) > 2 ? implode('/', array_slice($path_parts, 2)) : NULL;

        // The node type can optionally list a tour (after a colon) to use when
        // adding that node type, if none exists, for example: oa_space:spaces.
        $node_type_parts = explode(':', $path_parts[1]);
        $node_type = $node_type_parts[0];
        $node_add_tour = count($node_type_parts) > 1 ? $node_type_parts[1] : NULL;
        $nid = db_select('node', 'n')
          ->fields('n', array(
          'nid',
        ))
          ->condition('type', $node_type)
          ->condition('uid', $user->uid)
          ->orderBy('nid', 'DESC')
          ->range(0, 1)
          ->execute()
          ->fetchField();
      }
      if (empty($nid)) {
        $node_add_path = 'node/add/' . str_replace('_', '-', $node_type);
        if ($node_type == 'oa_space') {
          $node_add_path .= '/' . $space_blueprints[0]->tid;
        }
        $node_add_path .= '?nexttour=' . $tour->name;

        // No nodes exist yet so let's send them to the node add form.
        $tour->steps[$key]->path = $node_add_path;
        if (!empty($node_add_tour)) {

          // If the URL has a 3rd part, that's the name of the tour to run on the node add form.
          $tour->steps[$key]->path .= '&tour=' . $node_add_tour;
        }
      }
      else {

        // A node does exist, so go ahead and make that the next step.
        if (!empty($redirect_path) && strpos($redirect_path, '%') !== FALSE) {
          $node_path = str_replace('%', $nid, $redirect_path);
        }
        else {
          $node_path = 'node/' . $nid;
          if (!empty($redirect_path)) {
            $node_path .= '/' . $path_parts[2];
          }
        }
        $tour->steps[$key]->path = drupal_get_path_alias($node_path);
        if (!empty($_SESSION['nexttour']) && $_SESSION['nexttour'] == $tour->name) {
          for ($i = 0; $i < $key; $i++) {
            unset($tour->steps[$i]);
          }
          $tour->steps = array_values($tour->steps);
        }
      }
    }
    elseif ($step->path == 'node/add/oa-space') {

      // Redirect straight to the first Space Blueprint.
      $tour->steps[$key]->path .= '/' . $space_blueprints[0]->tid;
    }
    elseif ($step->path == 'node/add/oa-section') {

      // Redirect straight to the first Section Type.
      $tour->steps[$key]->path .= '/' . $section_types[0]->tid;
    }
  }
}

/**
 * Implements hook_form_FORM_BASE_ID_alter().
 *
 * Add the nexttour parameter as a hidden form field so that we can use it in
 * the submit callback to resume the previous tour on the newly created node's
 * page.
 */
function oa_tour_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (!empty($_GET['nexttour'])) {
    $form['nexttour'] = array(
      '#type' => 'hidden',
      '#value' => $_GET['nexttour'],
    );
  }
}

/**
 * Implements hook_node_insert().
 */
function oa_tour_node_insert($node) {

  // Fetch the next tour to run from the form fields and then run it.
  if (!empty($node->nexttour)) {
    $_SESSION['nexttour'] = $node->nexttour;
  }
}

/**
 * Implements hook_bootstrap_tour_access().
 */
function oa_tour_bootstrap_tour_access($tour, $account, $current_access) {
  if ($items = field_get_items('bootstrap_tour', $tour, 'field_bootstrap_tour_spaces')) {
    $space_context = oa_core_get_space_context();
    if (!$space_context) {
      return FALSE;
    }
    foreach ($items as $item) {
      if ($item['target_id'] == $space_context) {
        return TRUE;
      }
    }
    return FALSE;
  }
}

/**
 * Implements hook_bootstrap_tour_presave().
 */
function oa_tour_bootstrap_tour_presave($tour) {

  // Convert existing tours spaces to new tours fields.
  if (!empty($tour->old_tour['btid']) && db_table_exists('bootstrap_tour_old')) {
    $query = db_select('bootstrap_tour_old', 'b')
      ->fields('b', array(
      'spaces',
    ))
      ->condition('b.btid', $tour->old_tour['btid']);
    $spaces = $query
      ->execute()
      ->fetchColumn();
    if ($spaces) {

      // Since this condition only happens during an update, features revert
      // likely hasn;t happened yet so we gotta revert ourselves.
      if (!field_info_field('field_bootstrap_tour_spaces')) {
        features_revert_module('oa_tour');
        if (!field_info_field('field_bootstrap_tour_spaces')) {
          watchdog('oa_tour', 'Unable to create bootstrap spaces field', array(), WATCHDOG_ERROR);
          return;
        }
      }
      foreach (explode(',', $spaces) as $nid) {
        $tour->field_bootstrap_tour_spaces[LANGUAGE_NONE][] = array(
          'target_id' => $nid,
        );
      }
    }
  }
}

Functions

Namesort descending Description
oa_tour_bootstrap_tour_access Implements hook_bootstrap_tour_access().
oa_tour_bootstrap_tour_alter Implements hook_bootstrap_tour_alter().
oa_tour_bootstrap_tour_presave Implements hook_bootstrap_tour_presave().
oa_tour_ctools_plugin_directory Implements hook_ctools_plugin_directory().
oa_tour_form_node_form_alter Implements hook_form_FORM_BASE_ID_alter().
oa_tour_node_insert Implements hook_node_insert().
oa_tour_theme Implements hook_theme()