You are here

opigno_context.module in Opigno 7.0

Contains all hook_implementations and module specific API.

File

modules/opigno_context/opigno_context.module
View source
<?php

/**
 * @file
 * Contains all hook_implementations and module specific API.
 */
function opigno_context_build_menu($context = NULL) {
  $menu = array();
  if (!isset($context)) {
    $context = opigno_context();
  }
  if (preg_match('/^og:[0-9]$/', $context)) {
    $menu = opigno_context_trail($context);
  }
  elseif (preg_match('/^user(:[0-9])?$/', $context)) {
    dpm('user');
  }
  return $menu;
}
function opigno_context_trail($context = NULL) {
  $trail = array();
  if (!isset($context)) {
    $context = opigno_context();
  }
  $split = explode('/', request_path());
  for ($i = 0, $len = count($split); $i < $len; $i++) {
    $url = '';
    $j = $i + 1;
    $k = 0;
    while ($k < $j) {
      $url .= ($k ? '/' : '') . $split[$k];
      $k++;
    }
    $source = drupal_lookup_path('source', $url);
    if ($source) {
      $scheme = _menu_find_router_path($source);
    }
    else {
      $scheme = _menu_find_router_path($url);
    }
    if ($scheme) {
      $trail[] = array(
        'scheme' => $scheme,
        'href' => $url,
        'source' => $source,
      );
    }
  }
  return $trail;
}
function opigno_context_get_title_from_scheme($scheme, $url = NULL) {
  $routers = menu_get_router();
  $router = $routers[$scheme];
  if ($router['title callback'] == 't' && !empty($router['title'])) {
    return locale($router['title']);
  }
  elseif ($router['title callback'] && $router['title arguments'] && isset($url)) {
    $args = array();
    $split = explode('/', $url);
    foreach ($router['title arguments'] as $i) {
      if (isset($split[$i])) {
        if (isset($router['_load_functions'][$i])) {
          $args[] = call_user_func($router['_load_functions'][$i], $split[$i]);
        }
        else {
          $args[] = $split[$i];
        }
      }
      else {
        $args[] = 0;
      }
    }
    return call_user_func_array($router['title callback'], $args);
  }
  elseif ($router['page callback'] == 'views_page') {
    $cache = cache_get('ctools_export:views_view:og_ressource_pages', 'cache_views');
    dpm($cache);
  }
}
function opigno_context() {
  $context =& drupal_static(__FUNCTION__, NULL);
  if (isset($context)) {
    return $context;
  }
  $node = menu_get_object();
  if (empty($node)) {
    $path_scheme = _menu_find_router_path($_GET['q']);
    $page_callback = db_select('menu_router', 'm')
      ->fields('m', array(
      'page_callback',
    ))
      ->condition('path', $path_scheme)
      ->execute()
      ->fetchField();
    if ($page_callback == 'views_page') {
      $context = opigno_context_view_context($path_scheme);
    }
    else {
      $context = arg(0) . (@is_numeric(arg(1)) ? ':' . arg(1) : '');
    }
  }
  else {
    if (isset($node->group_audience[LANGUAGE_NONE][0]['gid'])) {
      $context = 'og:' . $node->group_audience[LANGUAGE_NONE][0]['gid'];
    }
    elseif (og_is_group_type('node', $node->type) && isset($node->group_group[LANGUAGE_NONE][0]['value'])) {
      $context = 'og:' . $node->group_group[LANGUAGE_NONE][0]['value'];
    }
    else {
      $context = 'node';
    }
  }
  return $context;
}
function opigno_context_view_context($path_scheme) {
  if (in_array($path_scheme, array(
    'course/%/glossaries',
    'course/%/wiki-page',
  ))) {
    return 'og:' . arg(1);
  }
  else {
    return 'view';
  }
}

/**
 * Implements hook_token_info()
 */
function opigno_context_token_info() {
  return array(
    'tokens' => array(
      'node' => array(
        'group-audience-gid' => array(
          'name' => t("Group audience GID"),
          'description' => t("The parent OG GID"),
        ),
      ),
    ),
  );
}

/**
 * Implements hook_tokens()
 */
function opigno_context_tokens($type, $tokens, $data = array(), $options = array()) {
  $replacements = array();
  if ($type == 'node' && !empty($data['node']) && !empty($data['node']->group_audience[LANGUAGE_NONE][0]['gid'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'group-audience-gid':
          $replacements[$original] = $data['node']->group_audience[LANGUAGE_NONE][0]['gid'];
          break;
      }
    }
  }
  return $replacements;
}