<?php
// $Id: extlink.module,v 1.6 2010/10/17 00:38:24 quicksketch Exp $

function extlink_menu() {
  $items = array();
  $items['admin/config/user-interface/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');
  drupal_add_js($path .'/extlink.js');
  drupal_add_js(array('extlink' => 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', 'This link will take you to an external web site. We are not responsible for their content.'),
    'mailtoClass'   => variable_get('extlink_mailto_class', 'mailto'))), '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(array('path' => drupal_get_path('module', 'extlink') .'/extlink.png', 'alt' => t('External Links icon'), 'attributes' => array())))),
  );

  $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(array(
        'path' => drupal_get_path('module', 'extlink') . '/mailto.png',
        'alt' => t('Email links icon'),
        'attributes' => '',
      )),
    )),
  );

  $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.'),
  );

  $form['extlink_alert_text'] = array(
    '#type' => 'textarea',
    '#rows' => 3,
    '#title' => t('Pop-up warning text'),
    '#default_value' => variable_get('extlink_alert_text', 'This link will take you to an external web site. We are not responsible for their content.'),
    '#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);
}
