<?php
// $Id: extlink.module,v 1.3.2.8 2010/05/26 01:23:47 quicksketch Exp $

function extlink_menu() {
  $items = array();
  $items['admin/settings/extlink'] = array(
    'title' => 'External links',
    'description' => 'Alter the display of external links on the site.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('extlink_admin_settings'),
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration'),
  );
  return $items;
}

/**
 * Implementation of hook_init().
 */
function extlink_init() {
  $path = drupal_get_path('module', 'extlink');
  $settings = array(
    'extTarget'     => variable_get('extlink_target', 0),
    'extClass'      => variable_get('extlink_class', 'ext'),
    'extSubdomains' => variable_get('extlink_subdomains', 1),
    'extExclude'    => variable_get('extlink_exclude', ''),
    'extInclude'    => variable_get('extlink_include', ''),
    'extAlert'      => variable_get('extlink_alert', 0),
    'extAlertText'  => variable_get('extlink_alert_text', NULL),
    'mailtoClass'   => variable_get('extlink_mailto_class', 'mailto'),
  );
  // The default alert text must be translated separately to avoid caching.
  if ($settings['extAlertText'] === NULL) {
    $settings['extAlertText'] = t('This link will take you to an external web site. We are not responsible for their content.');
  }

  drupal_add_js($path . '/extlink.js');
  drupal_add_js(array('extlink' => $settings), 'setting');
  if (variable_get('extlink_class', 'ext') == 'ext' || variable_get('extlink_mailto_class', 'mailto') == 'mailto') {
    drupal_add_css($path . '/extlink.css');
  }
}

function extlink_admin_settings() {
  $form = array();

  $form['extlink_class'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add icon to external links'),
    '#return_value' => 'ext',
    '#default_value' => variable_get('extlink_class', 'ext'),
    '#description' => t('Places an !icon icon next to external links.', array('!icon' => theme_image(drupal_get_path('module', 'extlink') .'/extlink.png'))),
  );

  $form['extlink_mailto_class'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add icon to mailto links'),
    '#return_value' => 'mailto',
    '#default_value' => variable_get('extlink_mailto_class', 'mailto'),
    '#description' => t('Places an !icon icon next to mailto links.', array('!icon' => theme_image(drupal_get_path('module', 'extlink') .'/mailto.png'))),
  );

  $form['extlink_subdomains'] = array(
    '#type' => 'checkbox',
    '#title' => t('Consider subdomains internal'),
    '#default_value' => variable_get('extlink_subdomains', 1),
    '#description' => t('If checked, links with the same primary domain will all be considered internal. A link from www.example.com to my.example.com would be considered internal. Links between the www. and non-www. domain are always considered internal.'),
  );

  $form['extlink_target'] = array(
    '#type' => 'checkbox',
    '#title' => t('Open external links in a new window'),
    '#return_value' => '_blank',
    '#default_value' => variable_get('extlink_target', 0),
    '#description' => t('Should all external links be opened in a new window?'),
  );

  $form['extlink_alert'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display pop-up warnings'),
    '#return_value' => '_blank',
    '#default_value' => variable_get('extlink_alert', 0),
    '#description' => t('Displays a pop-up warning when any external link is clicked.'),
  );

  // The default alert text must be translated separately to avoid caching.
  $extlink_alert_text = variable_get('extlink_alert_text', NULL);
  if ($extlink_alert_text === NULL) {
    $extlink_alert_text = t('This link will take you to an external web site. We are not responsible for their content.');
  }
  $form['extlink_alert_text'] = array(
    '#type' => 'textarea',
    '#rows' => 3,
    '#title' => t('Pop-up warning text'),
    '#default_value' => $extlink_alert_text,
    '#description' => t('Text to display in the pop-up external link warning box.'),
    '#wysiwyg' => FALSE,
  );

  $patterns = array(
    '<em>(example\.com)</em> ' . t('Matches example.com.'),
    '<em>(example\.com)|(example.net)</em> ' . t('Multiple patterns can be strung together by using a pipe. Matches example.com OR example.net.'),
    '<em>(links/goto/[0-9]+/[0-9]+)</em> ' . t('Matches links that go through the <a href="http://drupal.org/project/links">Links module</a> redirect.'),
  );

  $wildcards = array(
    '<em>.</em> ' . t('Matches any character.'),
    '<em>?</em> ' . t('The previous character or set is optional.'),
    '<em>\d</em> ' . t('Matches any digit (0-9).'),
    '<em>[a-z]</em> ' . t('Brackets may be used to match a custom set of characters. This matches any alphabetic letter.'),
  );

  $form['patterns'] = array(
    '#tree' => FALSE,
    '#type' => 'fieldset',
    '#title' => t('Pattern matching'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' =>
      '<p>' . t('External links uses patterns (regular expressions) to match the "href" property of links.') . '</p>' .
      t('Here are some common patterns.') .
      theme('item_list', $patterns) .
      t('Common special characters:') .
      theme('item_list', $wildcards) .
      '<p>' . t('All special characters (<em>^ $ . ? ( ) | * +</em>) must also be escaped with backslashes. Patterns are not case-sensitive. Any <a href="http://www.javascriptkit.com/javatutors/redev2.shtml">pattern supported by JavaScript</a> may be used.') . '</p>',
  );

  $form['patterns']['extlink_exclude'] = array(
    '#type' => 'textfield',
    '#title' => t('Exclude links matching the pattern'),
    '#maxlength' => NULL,
    '#default_value' => variable_get('extlink_exclude', ''),
    '#description' => t('Enter a regular expression for links that you wish to exclude from being considered external.'),
  );

  $form['patterns']['extlink_include'] = array(
    '#type' => 'textfield',
    '#title' => t('Include links matching the pattern'),
    '#maxlength' => NULL,
    '#default_value' => variable_get('extlink_include', ''),
    '#description' => t('Enter a regular expression for internal links that you wish to be considered external.'),
  );

  return system_settings_form($form);
}
