<?php

/**
 * @file
 * Views related hooks and handlers for Views Current Path.
 */

/**
 * Implements hook_views_data().
 */
function views_current_path_views_data() {
  $data['views']['current_path'] = array(
    'title' => t('Current Path'),
    'help' => t('The path of the page currently being viewed.'),
    'field' => array(
      'handler' => 'views_current_path_views_handler_field_current_path',
    ),
  );
  return $data;
}

/**
 * Views field handler for current path.
 *
 * @ingroup views_field_handlers
 */
class views_current_path_views_handler_field_current_path extends views_handler_field {

  /**
   * {@inheritdoc}
   */
  function query() {
    // Do nothing -- to override the parent query.
  }

  /**
   * {@inheritdoc}
   */
  function option_definition() {
    $options = parent::option_definition();

    $options['path_format'] = array('default' => 'raw-internal');

    return $options;
  }

  /**
   * {@inheritdoc}
   */
  function options_form(&$form, &$form_state) {
    $alias_required_message = module_exists('path') ? '' : '<em>'
      . t('Note: the Path module must be enabled for the "Alias" options to work.') . '</em>';
    
    // Determine the URL prefix.
    global $base_url;
    $url_options = array();
    if (function_exists('locale_language_url_rewrite_url')) {
      locale_language_url_rewrite_url(url(current_path()), $url_options);
    }
    $raw_relative_prefix = base_path() . (isset($url_options['prefix']) ? $url_options['prefix'] : '');
    $raw_absolute_prefix = $base_url . $raw_relative_prefix;

    $raw_example = 'node/215';
    $alias_example = 'pages/example-path';
    
    $form['path_format'] = array(
      '#type' => 'radios',
      '#title' => t('Output style'),
      '#description' =>   $alias_required_message,
      '#options' => array(
        'raw-internal' => t('Raw internal path (e.g. @example)',
          array('@example' => $raw_example)),
        'raw-relative' => t('Raw relative URL (e.g. @example)',
          array('@example' => $raw_relative_prefix . $raw_example)),
        'raw-absolute' => t('Raw absolute URL (e.g. @example)',
          array('@example' => $raw_absolute_prefix . $raw_example)),
        'alias-internal' => t('Alias internal path (e.g. @example)',
          array('@example' => $alias_example)),
        'alias-relative' => t('Alias relative URL (e.g. @example)',
          array('@example' => $raw_relative_prefix . $alias_example)),
        'alias-absolute' => t('Alias absolute URL (e.g. @example)',
          array('@example' => $raw_absolute_prefix . $alias_example)),
      ),
      '#default_value' => $this->options['path_format'],
    );
    $form['view_edit_notice'] = array(
      '#markup' => '<p>Note: ' . t('The placeholder @placeholder will be used for the field value while editing the view.',
        array('@placeholder' => '[' . $this->options['id'] . ']')) . '</p>',
    );

    parent::options_form($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  function render() {
    // Display a placeholder (i.e. field id) when editing the view.
    if (strpos(current_path(), 'admin/structure/views/nojs/preview/' . $this->view->name . '/') === 0) {
      return '[' . $this->options['id'] . ']';
    }

    $path_format = $this->options['path_format'];

    // In case the path module has been disabled, revert "alias" to "raw".
    if (strpos($path_format, 'alias') === 0 && !module_exists('path')) {
      $path_format = str_replace('alias-', 'raw-', $path_format);
    }

    // Determine the URL prefix.
    global $base_url;
    $url_options = array();
    if (function_exists('locale_language_url_rewrite_url')) {
      locale_language_url_rewrite_url(url(current_path()), $url_options);
    }
    $raw_relative_prefix = base_path() . (isset($url_options['prefix']) ? $url_options['prefix'] : '');
    $raw_absolute_prefix = $base_url . $raw_relative_prefix;

    // Determine the path.
    $output = current_path();
    if ($path_format == 'raw-internal') {
      $output = current_path();
    }
    else if ($path_format == 'raw-relative') {
      $output = $raw_relative_prefix . current_path();
    }
    else if ($path_format == 'raw-absolute') {
      $output = $raw_absolute_prefix . current_path();
    }
    else if ($path_format == 'alias-internal') {
      $output = request_path();
    }
    else if ($path_format == 'alias-relative') {
      $output = request_uri();
    }
    else if ($path_format == 'alias-absolute') {
      $output = url(current_path(), array('absolute' => TRUE));
    }

    return $output;
  }

}
