You are here

function ng_lightbox_path_callback in NG Lightbox 7

Ajax callback for the ng_lightbox.

1 string reference to 'ng_lightbox_path_callback'
ng_lightbox_menu in ./ng_lightbox.module
Implements hook_menu().

File

./ng_lightbox.module, line 148
The NG Lightbox module.

Code

function ng_lightbox_path_callback() {

  // Currently we require a POST request, if you get here it's likely because
  // something (maybe a form) accidently re-directed here.
  if (!isset($_POST['ng_lightbox_path'])) {
    return 'NG Lightbox error';
  }
  $lightbox_path = $_POST['ng_lightbox_path'];
  global $base_path;

  // Parse out the path parts and query string, taking into account clean urls
  // and making sure to preserve query params for the next request.
  $url_parts = parse_url($lightbox_path);
  if (isset($url_parts['query'])) {
    parse_str($url_parts['query'], $query);
    $_GET = $query;
  }

  // If clean urls are enabled, grab the path.
  if (variable_get('clean_url', 0)) {
    $path = substr($url_parts['path'], strlen($base_path));
  }
  else {

    // Clean urls are disabled so we're using the "q" param. We know we have
    // $query because clean_urls are off, otherwise something went very wrong.
    $path = $query['q'];
  }

  // Decode the path to ensure everything works with non-latin characters.
  $path = urldecode($path);

  // Normalize the path for Drupal.
  $path = drupal_get_normal_path($path) ?: $path;

  // We override these globals to make current_path() work and so that any
  // forms that are rendered get the correct action set.
  $_GET['q'] = $path;
  $_SERVER['REQUEST_URI'] = $path;
  $_SERVER['SCRIPT_NAME'] = $path;

  // Execute the active menu handler.
  $result = menu_execute_active_handler($path, FALSE);

  // Integer results map to HTTP statuses.
  if (is_int($result)) {
    switch ($result) {
      case MENU_NOT_FOUND:
        $result = array(
          '#title' => t('Page not found'),
          '#markup' => t('Sorry, the content does not exist.'),
        );
        break;
      case MENU_ACCESS_DENIED:
        $result = array(
          '#title' => t('Access Denied'),
          '#markup' => t('Sorry, you do not have permission to view this content.'),
        );
        break;
      case MENU_SITE_OFFLINE:
        $result = array(
          '#title' => t('Service unavailable'),
          '#markup' => t('Sorry, the service is currently unavailable'),
        );
        break;
    }
  }

  // We need to support strings inline with cores hook_menu() implementation.
  if (is_string($result)) {
    $html = $result;
    $result = array(
      '#markup' => $html,
    );
  }

  // Setup our lightbox with some wrappers, default title and CSS.
  $result['#theme_wrappers'][] = 'ng_lightbox_form';
  $result['#title'] = isset($result['#title']) ? $result['#title'] : drupal_get_title();
  $result['#modifier'] = isset($result['#modifier']) ? $result['#modifier'] : 'plain';

  // If the default css is not disabled then add it.
  if (!variable_get('ng_lightbox_disable_css', FALSE)) {
    $result['#attached']['css'][] = drupal_get_path('module', 'ng_lightbox') . '/stylesheets/lightbox.css';
  }

  // http_build_url() would have been perfect but that lives in PECL.
  $url = $url_parts['path'];
  if (isset($url_parts['query'])) {
    $url .= '?' . $url_parts['query'];
  }

  // We just set this regardless so forms have the right action.
  $result['#action'] = $url;
  return $result;
}