You are here

linkit.module in Linkit 6

Main file for linkit module.

File

linkit.module
View source
<?php

/**
 * @file
 * Main file for linkit module.
 */

/**
 * Implementation of hook_menu().
 */
function linkit_menu() {
  $items = array();
  $items['admin/linkit/dashboard/%'] = array(
    'title' => 'Linkit',
    'description' => 'Dashboard',
    'page callback' => 'linkit_dashboard_page',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'page arguments' => array(
      3,
    ),
  );
  $items['admin/linkit/autocomplete'] = array(
    'title' => 'Link to node',
    'page callback' => 'linkit_autocomplete',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'page arguments' => array(
      3,
    ),
  );
  $items['admin/linkit/geteditresult'] = array(
    'title' => 'Get search result styled link',
    'page callback' => 'linkit_search_styled_link',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/linkit'] = array(
    'title' => 'Linkit settings',
    'description' => 'Configure Linkit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'linkit_admin_settings',
    ),
    'access arguments' => array(
      'administer linkit',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'linkit.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_init().
 */
function linkit_init() {

  // Check that we have pathfilter or pathologic installed to parse the "internal:" links
  if (!module_exists('pathfilter') && !module_exists('pathologic')) {
    if (user_access('administer site configuration') || user_access('administer linkit')) {
      drupal_set_message(t('The Linkit module requires <a href="@pathfilter">pathfilter</a> <strong>OR</strong> <a href="@pathologic">pathologic</a> to parse "internal:" links. Be sure you have <strong>ONE</strong> of this modules installed.', array(
        '@pathfilter' => 'http://drupal.org/project/pathfilter',
        '@pathologic' => 'http://drupal.org/project/pathologic',
      )), 'warning');
    }
  }
}

/**
 * Implementation of hook_perm().
 */
function linkit_perm() {
  return array(
    'administer linkit',
  );
}

/**
 * Implementation of hook_theme().
 */
function linkit_theme($existing, $type, $theme, $path) {
  return array(
    'linkit_dashboard' => array(
      'arguments' => array(
        'content' => NULL,
      ),
      'template' => 'linkit-dashboard',
    ),
  );
}

/**
 * Implementation of hook_elements().
 */
function linkit_elements() {
  return array(
    'textarea' => array(
      '#after_build' => array(
        'linkit_textarea',
      ),
    ),
  );
}
function linkit_textarea($element, $form_state) {
  if (isset($element['#process'])) {
    if (in_array('bueditor_textarea', $element['#process'])) {
      _linkit_add_settings('bueditor');
      drupal_add_js(drupal_get_path('module', 'linkit') . '/editors/bueditor/editor_plugin.js');
    }
  }
  return $element;
}

/**
 * Implementation of hook_form_alter().
 */
function linkit_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#after_build'])) {
    if (count($form['#after_build'])) {
      foreach ($form['#after_build'] as $after_build) {
        if ($after_build == "ckeditor_process_form") {
          _linkit_add_settings('ckeditor');
        }
      }
    }
  }
}

/**
 * Implementation of hook_ckeditor_plugin().
 */
function linkit_ckeditor_plugin() {
  return array(
    'linkit' => array(
      // Name of the plugin used to write it
      'name' => 'Linkit',
      // Description of plugin - it would appear in plugins managment of profile settings
      'desc' => t('Support for Linkit module'),
      // The full path to the CKEditor plugin directory, with trailing slash.
      'path' => drupal_get_path('module', 'linkit') . '/editors/ckeditor/',
    ),
  );
}

/**
 * Template preprocess function for theme_linkit_dashboard().
 */
function template_preprocess_linkit_dashboard(&$variables) {

  // Construct page title
  $head_title = array(
    strip_tags(drupal_get_title()),
    variable_get('site_name', 'Drupal'),
  );
  $variables['head_title'] = implode(' | ', $head_title);
  $variables['base_path'] = base_path();
  $variables['front_page'] = url();
  $variables['head'] = drupal_get_html_head();
  $variables['help'] = theme('help');
  $variables['language'] = $GLOBALS['language'];
  $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
  $variables['messages'] = isset($variables['show_messages']) ? theme('status_messages') : '';
  $variables['styles'] = drupal_get_css();
  $variables['scripts'] = drupal_get_js();
  $variables['title'] = drupal_get_title();
}

/**
 * Creates the dashboard.
 */
function linkit_dashboard_page($editor) {
  module_invoke('admin_menu', 'suppress');

  // Add CSS.
  drupal_add_css(drupal_get_path('module', 'linkit') . '/css/linkit.css');

  // Add JS
  static $defualt_settings_added = FALSE;

  // Add default settings
  if (!$defualt_settings_added) {
    $defualt_settings_added = TRUE;
    $settings = array(
      'linkit' => array(
        'ajaxcall' => url('admin/linkit/geteditresult'),
      ),
    );

    // Check if we have IMCE and access to it.
    $imce = module_invoke('imce', 'access');
    if ($imce) {
      $settings['linkit']['IMCEurl'] = url('imce', array(
        'query' => array(
          'app' => 'Linkit|url@edit-link',
        ),
      ));
    }
    drupal_add_js($settings, 'setting');
  }
  drupal_add_js(drupal_get_path('module', 'linkit') . '/linkit.js');
  switch ($editor) {
    case 'wysiwyg_tinymce':

      // Add JavaScript.
      drupal_add_js(wysiwyg_get_path('tinymce') . '/jscripts/tiny_mce/tiny_mce_popup.js');
      drupal_add_js(drupal_get_path('module', 'linkit') . '/editors/tinymce/linkit.js');
      break;
    case 'ckeditor':
    case 'wysiwyg_ckeditor':

      // Add JavaScript.
      drupal_add_js(drupal_get_path('module', 'linkit') . '/editors/ckeditor/linkitDialog.js');
      break;
    case 'fckeditor':
    case 'wysiwyg_fckeditor':

      // Add JavaScript.
      drupal_add_js(drupal_get_path('module', 'linkit') . '/editors/fckeditor/linkit/linkitDialog.js');
      break;
    case 'bueditor':

      // Add JavaScript.
      drupal_add_js(drupal_get_path('module', 'linkit') . '/editors/bueditor/linkit.js');
      break;
  }
  print theme('linkit_dashboard', drupal_get_form('_linkit_form'));
  exit;
}

/**
 * Autocomplete callback function.
 */
function linkit_autocomplete($string = NULL) {
  $matches = array();
  $hook_matches = array();
  if ($string) {
    $hook_matches = module_invoke_all('linkit_load_plugins', $string);
    foreach ($hook_matches as $module => $values) {
      if (module_exists('linkit_permissions')) {
        if (user_access('create ' . $module . ' links') || user_access('create all links')) {
          $matches = array_merge($matches, $hook_matches[$module]);
        }
      }
      else {
        $matches = array_merge($matches, $hook_matches[$module]);
      }
    }
  }

  // hook_linkit_load_plugins_alter() , let other modules change the matches array
  drupal_alter('linkit_load_plugins', $matches);
  $results = array();
  foreach ($matches as $match) {

    // Build the val
    $val = $match['title'] . ' [path:' . $match['path'] . ']';

    // Get the extra information text
    $information = array();
    foreach ($match['information'] as $extra_info_key => $extra_info_val) {

      // Coder is telling us that "The first parameter to t() should be a literal string. There should be no variables, concatenation, constants or other non-literal strings there"
      // But the "$extra_info_key" is hard coded and cant be change by users or administratios so its ok to use t() this way.
      $information[] = t(drupal_ucfirst($extra_info_key)) . ':<span>' . $extra_info_val . '</span>';
    }

    // Build the text
    $text = '<div class="clear-block">';
    $text .= '<div class="auto-item-title">' . check_plain($match['title']) . '</div>';
    if (count($information)) {
      $text .= '<div class="auto-item-info">[' . implode(", ", $information) . ']</div>';
    }
    $text .= '</div>';
    $results[$val] = $text;
  }
  drupal_json($results);
}
function linkit_search_styled_link() {
  drupal_set_header('Content-Type: text/plain; charset=utf-8');
  if (isset($_GET['string'])) {
    $string = check_plain($_GET['string']);
    $matches = module_invoke_all('linkit_get_search_styled_link', $string);
    echo isset($matches[0]) ? $matches[0] : '';
  }
}
function _linkit_form() {

  // Check if we have IMCE and access to it.
  $imce = module_invoke('imce', 'access');
  $form['link'] = array(
    '#type' => 'fieldset',
    '#title' => '',
    '#weight' => 0,
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#attributes' => array(
      'class' => 'clear-block',
    ),
  );
  $form['link']['link'] = array(
    '#type' => 'textfield',
    '#title' => t('Link to'),
    '#description' => t('Search for your internal content <b>OR</b> type the URL (http://www.example.com)'),
    '#maxlength' => 255,
    '#size' => 80,
    '#autocomplete_path' => 'admin/linkit/autocomplete',
    '#default_value' => '',
    '#weight' => 1,
  );
  if ($imce) {
    $imcebutton = array(
      '#type' => 'button',
      '#value' => t('Browse server'),
      '#name' => 'linkit-imce',
      '#id' => 'linkit-imce',
    );

    // Add extra class to edit-link field when we can use IMCE
    $form['link']['link']['#attributes'] = array(
      'class' => 'with_imce',
    );

    // Add the imce buttom
    $form['link']['link']['#field_suffix'] = drupal_render($imcebutton);
  }
  $form['link']['attributes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Attributes'),
    '#weight' => 1,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => array(
      'class' => 'clear-block',
    ),
    '#weight' => 2,
  );
  $form['link']['attributes']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#maxlength' => 255,
    '#size' => 40,
    '#default_value' => '',
    '#weight' => 0,
  );
  $form['link']['attributes']['id'] = array(
    '#type' => 'textfield',
    '#title' => t('ID'),
    '#maxlength' => 255,
    '#size' => 40,
    '#default_value' => '',
    '#weight' => 1,
  );
  $form['link']['attributes']['class'] = array(
    '#type' => 'textfield',
    '#title' => t('Class'),
    '#maxlength' => 255,
    '#size' => 40,
    '#default_value' => '',
    '#weight' => 2,
  );
  $form['link']['attributes']['rel'] = array(
    '#type' => 'textfield',
    '#title' => t('Rel'),
    '#maxlength' => 255,
    '#size' => 40,
    '#default_value' => '',
    '#weight' => 3,
  );
  $form['link']['attributes']['accesskey'] = array(
    '#type' => 'textfield',
    '#title' => t('Accesskey'),
    '#maxlength' => 255,
    '#size' => 40,
    '#default_value' => '',
    '#weight' => 4,
  );
  $form['link']['attributes']['anchor'] = array(
    '#type' => 'textfield',
    '#title' => t('Anchor'),
    '#maxlength' => 255,
    '#size' => 40,
    '#default_value' => '',
    '#weight' => 5,
    '#description' => t('Without the hash (#)'),
  );
  $form['link']['cancel'] = array(
    '#type' => 'button',
    '#value' => t('Cancel'),
    '#weight' => 10,
  );
  $form['link']['insert'] = array(
    '#type' => 'button',
    '#value' => t('Insert'),
    '#weight' => 11,
  );
  return $form;
}
function linkit_wysiwyg_plugin($editor, $version) {
  $plugins = array();
  _linkit_add_settings('wysiwyg_' . $editor);
  switch ($editor) {
    case 'tinymce':
      $plugins['linkit'] = array(
        'path' => drupal_get_path('module', 'linkit') . '/editors/tinymce/',
        'filename' => 'editor_plugin.js',
        'buttons' => array(
          'linkit' => t('Linkit'),
        ),
        'url' => 'http://drupal.org/project/linkit',
        'load' => TRUE,
      );
      break;
    case 'ckeditor':

      // notice: Big "L" in the name here! important
      $plugins['Linkit'] = array(
        'path' => drupal_get_path('module', 'linkit') . '/editors/ckeditor/',
        'buttons' => array(
          'Linkit' => t('Linkit'),
        ),
        'url' => 'http://drupal.org/project/linkit',
        'load' => TRUE,
      );
      break;
    case 'fckeditor':
      $plugins['linkit'] = array(
        'path' => drupal_get_path('module', 'linkit') . '/editors/fckeditor/',
        'buttons' => array(
          'linkit' => t('Linkit'),
        ),
        'url' => 'http://drupal.org/project/linkit',
        'load' => TRUE,
      );
      break;
  }
  return $plugins;
}
function _linkit_add_settings($editor) {
  static $editor_settings_added = array();
  static $global_settings_added = FALSE;
  if (!isset($editor_settings_added[$editor])) {
    $editor_settings_added[$editor] = TRUE;

    // Add popup url
    $settings = array(
      'linkit' => array(
        'url' => array(
          $editor => url('admin/linkit/dashboard/' . $editor),
        ),
      ),
    );
    drupal_add_js($settings, 'setting');
  }
  if (!$global_settings_added) {
    $global_settings_added = TRUE;

    // Add global settings for Linkit
    $settings = array(
      'linkit' => array(
        'modulepath' => drupal_get_path('module', 'linkit'),
      ),
    );
    drupal_add_js($settings, 'setting');
  }
}

Functions

Namesort descending Description
linkit_autocomplete Autocomplete callback function.
linkit_ckeditor_plugin Implementation of hook_ckeditor_plugin().
linkit_dashboard_page Creates the dashboard.
linkit_elements Implementation of hook_elements().
linkit_form_alter Implementation of hook_form_alter().
linkit_init Implementation of hook_init().
linkit_menu Implementation of hook_menu().
linkit_perm Implementation of hook_perm().
linkit_search_styled_link
linkit_textarea
linkit_theme Implementation of hook_theme().
linkit_wysiwyg_plugin
template_preprocess_linkit_dashboard Template preprocess function for theme_linkit_dashboard().
_linkit_add_settings
_linkit_form