View source
<?php
define('LINKIT_DEFAULT_WEIGHT', 0);
define('LINKIT_DEFAULT_PROFILE_MACHINE_NAME', 'default');
require_once dirname(__FILE__) . '/linkit.field.inc';
function linkit_menu() {
$items = array();
$items['linkit/dashboard/%'] = array(
'title' => 'Linkit',
'description' => 'Dashboard',
'page callback' => '_linkit_dashboard',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'page arguments' => array(
2,
),
'theme callback' => '_linkit_dashboard_theme',
);
$items['linkit/autocomplete'] = array(
'title' => 'Linkit autocomplete response function',
'page callback' => '_linkit_autocomplete',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['admin/config/content/linkit/reorder'] = array(
'title' => 'Reorder',
'description' => 'Reorder Linkit profiles',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'linkit_profiles_reorder',
),
'access arguments' => array(
'administer linkit',
),
'file path' => drupal_get_path('module', 'linkit') . '/plugins/export_ui',
'file' => 'linkit_profiles.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function linkit_menu_alter(&$items) {
$items['admin/config/content/linkit/add']['title'] = 'Add new profile';
$items['admin/config/content/linkit/import']['title'] = 'Import profiles';
$items['admin/config/content/linkit/add']['type'] = MENU_LOCAL_TASK;
$items['admin/config/content/linkit/import']['type'] = MENU_LOCAL_TASK;
}
function _linkit_dashboard_theme() {
return variable_get('admin_theme', 'seven');
}
function linkit_permission() {
$permissions = array();
$permissions['administer linkit'] = array(
'title' => t('Administer Linkit'),
'description' => t('Perform administration tasks for Linkit.'),
);
return $permissions;
}
function linkit_theme($existing, $type, $theme, $path) {
return array(
'linkit_profiles_export_ui_form' => array(
'render element' => 'form',
'file' => 'linkit.theme.inc',
),
'linkit_profiles_reorder' => array(
'render element' => 'form',
'file' => 'linkit.theme.inc',
),
'_linkit_theme_profile_form_table' => array(
'variables' => array(),
'file' => 'linkit.theme.inc',
),
'linkit_reverse_menu_trail' => array(
'variables' => array(
'menu_trail' => NULL,
'separator' => ' « ',
),
'file' => 'linkit.theme.inc',
),
);
}
function linkit_element_info_alter(&$types) {
if (isset($types['text_format']['#pre_render']) && is_array($types['text_format']['#pre_render'])) {
if (in_array('ckeditor_pre_render_text_format', $types['text_format']['#pre_render'])) {
$types['text_format']['#pre_render'][] = 'linkit_pre_render_ckeditor_element';
}
}
foreach (linkit_allowed_elements() as $element) {
if (isset($types[$element])) {
$types[$element]['#process'][] = 'linkit_process_widget';
}
}
}
function linkit_pre_render_ckeditor_element($element) {
_linkit_add_settings('ckeditor');
return $element;
}
function linkit_ckeditor_plugin() {
return array(
'linkit' => array(
'name' => 'linkit',
'desc' => t('Support for Linkit module'),
'path' => drupal_get_path('module', 'linkit') . '/editors/ckeditor/',
'buttons' => array(
'linkit' => array(
'label' => 'Linkit',
'icon' => 'linkit.png',
),
),
),
);
}
function linkit_library() {
$path = drupal_get_path('module', 'linkit');
$common = array(
'website' => 'http://drupal.org/project/linkit',
'version' => '7.2',
);
$libraries['linkit_bac'] = array(
'title' => 'Linkit Better Autocomplete',
'js' => array(
$path . '/better-autocomplete/jquery.better-autocomplete.js' => array(
'group' => JS_LIBRARY,
),
array(
'type' => 'setting',
'data' => array(
'linkit' => array(
'autocompletePath' => url('linkit/autocomplete', array(
'query' => array(
's' => '',
),
'absolute' => TRUE,
)),
),
),
),
),
'css' => array(
$path . '/better-autocomplete/better-autocomplete.css' => array(
'group' => CSS_DEFAULT,
),
),
);
$libraries['linkit_base'] = array(
'title' => 'Linkit base',
'js' => array(
$path . '/js/linkit.js' => array(
'group' => JS_DEFAULT,
),
$path . '/js/linkit.dialog.js' => array(
'group' => JS_DEFAULT,
),
array(
'type' => 'setting',
'data' => array(
'linkit' => array(
'modulepath' => drupal_get_path('module', 'linkit'),
),
),
),
),
'css' => array(
$path . '/css/linkit.css' => array(
'group' => CSS_DEFAULT,
),
),
'dependencies' => array(
array(
'system',
'ui.dialog',
),
array(
'system',
'drupal.collapse',
),
array(
'linkit',
'linkit_bac',
),
),
);
$libraries['linkit_ckeditor'] = array(
'title' => 'Linkit CKeditor',
'js' => array(
$path . '/editors/ckeditor/linkitDialog.js' => array(
'group' => JS_DEFAULT,
),
),
);
$libraries['linkit_tinymce'] = array(
'title' => 'Linkit TinyMCE',
'js' => array(
$path . '/editors/tinymce/linkitDialog.js' => array(
'group' => JS_DEFAULT,
),
),
);
$libraries['linkit_field'] = array(
'title' => 'Linkit Field UI',
'js' => array(
$path . '/js/linkit.field.js' => array(
'group' => JS_DEFAULT,
),
$path . '/js/linkitFieldDialog.js' => array(
'group' => JS_DEFAULT,
),
),
'dependencies' => array(
array(
'linkit',
'linkit_base',
),
),
);
foreach ($libraries as &$library) {
$library += $common;
}
return $libraries;
}
function _linkit_dashboard($editor) {
$form = drupal_get_form('linkit_dashboard_form');
print drupal_render($form);
drupal_exit();
}
function _linkit_autocomplete($search = NULL) {
$search = $_GET['s'];
$enabled_plugins = linkit_get_profile_plugins();
if ($result = _linkit_result_from_url($search, $enabled_plugins)) {
$results = array(
$result,
);
}
elseif (!($results = _linkit_autocomplete_search($search, $enabled_plugins))) {
$results = array(
array(
'title' => t('No results'),
'addClass' => 'status-notice',
'disabled' => TRUE,
),
);
}
drupal_json_output($results);
drupal_exit();
}
function _linkit_autocomplete_search($search_string, $enabled_plugins) {
$matches = array();
if ($search_string) {
$profile = linkit_get_dashboard_profile();
foreach ($enabled_plugins as $plugin) {
$handler = linkit_get_plugin_handler($plugin, $profile);
if ($handler
->broken()) {
continue;
}
$handler
->setSearchString($search_string);
$results = $handler
->autocomplete_callback();
$matches = array_merge($matches, $results);
}
if (strpos($search_string, 'front') !== FALSE) {
$results = array(
array(
'title' => t('Frontpage'),
'description' => 'The frontpage for this site.',
'path' => url('<front>'),
'group' => t('System'),
),
);
$matches = array_merge($matches, $results);
}
}
if (!empty($matches)) {
return $matches;
}
return FALSE;
}
function linkit_scan_url($url) {
global $base_url;
$parts = parse_url(trim($url, '/'));
if (!isset($parts['scheme']) || !isset($parts['host'])) {
return FALSE;
}
$path_info = array();
$path_info += $parts;
$path_info['url'] = $url;
if (!isset($path_info['query'])) {
$path_info['query'] = '';
}
parse_str($path_info['query'], $path_info['query']);
if (isset($path_info['query']['q'])) {
$path_info['path'] = $path_info['query']['q'];
unset($path_info['query']['q']);
}
if (trim($path_info['scheme'] . '://' . $path_info['host'] . base_path(), '/') == $base_url) {
$path_info['target'] = 'internal';
if (base_path() != "/") {
$path_info['path'] = trim(preg_replace(base_path(), '', $path_info['path'], 1), '/');
}
$path_info['path'] = trim($path_info['path'], '/');
if (empty($path_info['path'])) {
$path_info['frontpage'] = TRUE;
$path_info['path'] = variable_get('site_frontpage', 'node');
}
if (!($processed_path = drupal_lookup_path('source', $path_info['path']))) {
$processed_path = $path_info['path'];
}
$path_info['system_path'] = drupal_valid_path($processed_path) ? $processed_path : FALSE;
}
else {
$path_info['target'] = 'external';
if (preg_match('~^https?$~', $parts['scheme'])) {
$path_info['safe_url'] = $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
}
}
return $path_info;
}
function _linkit_result_from_url($url, $enabled_plugins) {
if (!($path_info = linkit_scan_url($url))) {
return FALSE;
}
$clean_urls = !empty($GLOBALS['conf']['clean_url']);
$url_options = array(
'alias' => TRUE,
);
$url_options['fragment'] = isset($path_info['fragment']) ? $path_info['fragment'] : '';
$url_options['query'] = isset($path_info['query']) ? $path_info['query'] : '';
$result = array();
if (isset($path_info['system_path']) && (bool) $path_info['system_path']) {
$menu_item = menu_get_item($path_info['system_path']);
$result = array(
'path' => url($path_info['system_path'], $url_options),
'title' => $menu_item['title'] ? check_plain($menu_item['title']) : check_plain($path_info['system_path']),
'description' => t('This is an internal path.'),
'addClass' => 'status-ok',
);
}
elseif ($path_info['target'] == 'internal') {
$result = array(
'path' => url($path_info['path'], $url_options),
'title' => t('Page not found'),
'description' => t('This page does not exist or you do not have access to it.'),
'addClass' => 'status-warning',
);
}
elseif ($path_info['target'] == 'external') {
$result = array(
'title' => t('No information available'),
'description' => t("This is an external URL, but we don't know where it leads."),
'path' => $path_info['url'],
'addClass' => 'status-notice',
);
}
return $result;
}
function linkit_dashboard_form($form, &$form_state) {
$profile = linkit_get_dashboard_profile();
if (is_null($profile)) {
drupal_set_message(t('There is no Linkit profile associated with your role.'), 'warning');
$form['message'] = array(
'#markup' => theme('status_messages') . '<a id="linkit-cancel" href="#">' . t('Close') . '</a>',
);
return $form;
}
$form['linkit_search'] = array(
'#type' => 'textfield',
'#title' => t('Search content'),
'#description' => t('Start typing to find content or paste a URI. Use the arrow keys to navigate.'),
'#maxlength' => 255,
'#size' => 60,
'#default_value' => '',
'#weight' => -10,
);
$form['linkit_path'] = array(
'#type' => 'textfield',
'#title' => t('Target path'),
'#description' => t('Examples: <strong>node/123</strong>, <strong>http://www.example.com/path#anchor</strong>'),
'#required' => TRUE,
'#maxlength' => NULL,
'#size' => 60,
'#default_value' => '',
'#weight' => -1,
);
$enabled_attributes = linkit_get_profile_attributes();
if (count($enabled_attributes)) {
$form['linkit_attributes'] = array(
'#type' => 'fieldset',
'#title' => t('Attributes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 10,
);
foreach ($enabled_attributes as $name => $attribute) {
$form['linkit_attributes']['linkit_' . $name] = $attribute;
}
}
$form['linkit_insert'] = array(
'#type' => 'button',
'#value' => t('Insert link'),
'#suffix' => '<a id="linkit-cancel" href="#">' . t('Cancel') . '</a>',
'#weight' => 100,
);
return $form;
}
function linkit_wysiwyg_plugin($editor, $version) {
$plugin = array();
_linkit_add_settings($editor);
$plugin['linkit'] = array(
'path' => drupal_get_path('module', 'linkit') . '/editors/' . $editor,
'buttons' => array(
'linkit' => t('Linkit'),
),
'url' => 'http://drupal.org/project/linkit',
'load' => TRUE,
);
if ($editor == 'tinymce') {
$plugin['linkit']['filename'] = 'editor_plugin.js';
}
return $plugin;
}
function _linkit_add_settings($editor) {
static $globalsettings, $set_urls;
drupal_add_library('linkit', 'linkit_base');
$profile = linkit_get_dashboard_profile();
if (!$globalsettings && $profile) {
if (module_invoke('imce', 'access') && isset($profile->data['imce']['use_imce']) && $profile->data['imce']['use_imce'] == 1) {
$settings['IMCEurl'] = url('imce', array(
'query' => array(
'app' => 'Linkit|sendto@Drupal.linkit.dialog.IMCECallback',
'absolute' => TRUE,
),
));
$settings['publicFilesDirectory'] = variable_get('file_public_path', conf_path() . '/files');
}
$settings['autocomplete'] = array_filter($profile->data['autocomplete']);
$settings['autocomplete'] += array(
'caseSensitive' => TRUE,
);
drupal_add_js(array(
'linkit' => $settings,
), 'setting');
$globalsettings = TRUE;
}
if (!isset($set_urls[$editor])) {
$settings = array(
'linkit' => array(
'url' => array(
$editor => url('linkit/dashboard/' . $editor),
),
),
);
drupal_add_js($settings, 'setting');
$set_urls[$editor] = TRUE;
}
drupal_add_library('linkit', 'linkit_' . $editor);
}
function linkit_help($path, $arg) {
switch ($path) {
case 'admin/help#linkit':
$output = file_get_contents(drupal_get_path('module', 'linkit') . '/README.txt');
return nl2br($output);
}
}
function linkit_profile_exists($profile_name) {
$profile = linkit_profile_load($profile_name);
return is_object($profile);
}
function linkit_image_default_styles() {
$styles = array();
$styles['linkit_thumb'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 50,
'height' => 50,
'upscale' => 0,
),
'weight' => 0,
),
),
);
return $styles;
}
function linkit_get_attributes($profile = NULL) {
if (is_null($profile)) {
$profile = linkit_get_dashboard_profile();
}
$attributes = module_invoke_all('linkit_attributes', $profile);
drupal_alter('linkit_attributes', $attributes);
return $attributes;
}
function linkit_linkit_attributes($profile) {
$attributes['accesskey'] = array(
'#type' => 'textfield',
'#title' => 'accesskey',
);
$attributes['class'] = array(
'#type' => 'textfield',
'#title' => 'class',
);
$attributes['id'] = array(
'#type' => 'textfield',
'#title' => 'id',
);
$attributes['rel'] = array(
'#type' => 'textfield',
'#title' => 'rel',
);
$attributes['title'] = array(
'#type' => 'textfield',
'#title' => 'title',
);
foreach ($attributes as $key => &$attribute) {
$attribute += array(
'#maxlength' => 255,
'#size' => 40,
'#default_value' => '',
'#weight' => isset($profile->data['attributes'][$key]['weight']) ? $profile->data['attributes'][$key]['weight'] : 0,
);
}
return $attributes;
}
function linkit_get_profile_attributes() {
$profile = linkit_get_dashboard_profile();
$enabled_attributes = array();
if (isset($profile->data['attributes'])) {
foreach (linkit_get_attributes($profile) as $name => $attribute) {
if (isset($profile->data['attributes'][$name]['enabled']) && $profile->data['attributes'][$name]['enabled']) {
$attribute['#attributes']['class'][] = 'linkit-attribute';
$enabled_attributes[$name] = $attribute;
}
}
}
return $enabled_attributes;
}
function linkit_get_profile_plugins() {
$installed_plugins = linkit_get_plugins();
$profile = linkit_get_dashboard_profile();
if (is_null($profile)) {
return array();
}
$enabled_plugins = array();
foreach ($installed_plugins as $name => $plugin) {
if ($profile->data['plugins'][$name]['enabled']) {
$enabled_plugins[$name] = $plugin;
}
}
return $enabled_plugins;
}
function linkit_get_plugins() {
ctools_include('plugins');
$plugins = ctools_get_plugins('linkit', 'linkit_plugins');
drupal_alter('linkit_plugins', $plugins);
return $plugins;
}
function linkit_get_plugin($plugin_name) {
ctools_include('plugins');
$plugin = ctools_get_plugins('linkit', 'linkit_plugins', $plugin_name);
drupal_alter('linkit_plugin', $plugin);
return $plugin;
}
function linkit_get_dashboard_profile() {
global $user;
$best_profile =& drupal_static(__FUNCTION__);
if (!isset($best_profile)) {
$best_profile = NULL;
$profiles = linkit_profile_load_all();
usort($profiles, '_linkit_sort_profiles_by_weight');
foreach ($profiles as $profile) {
if (isset($profile->disabled) && $profile->disabled) {
continue;
}
foreach ($profile->role_rids as $rid => $value) {
if (array_key_exists($rid, $user->roles)) {
$best_profile = $profile;
break 2;
}
}
}
}
return $best_profile;
}
function _linkit_sort_profiles_by_weight($a, $b) {
return $a->weight >= $b->weight;
}
function _linkit_get_profile_names() {
$profiles = linkit_profile_load_all();
$profile_names = array();
foreach ($profiles as $profile) {
$profile_names[$profile->name] = $profile->admin_title;
}
return $profile_names;
}
function linkit_profile_load($name) {
$cache =& drupal_static('__FUNCTION__', array());
if (!isset($cache[$name])) {
ctools_include('export');
$result = ctools_export_load_object('linkit_profiles', 'names', array(
$name,
));
if (isset($result[$name])) {
$cache[$name] = $result[$name];
}
else {
return FALSE;
}
}
return $cache[$name];
}
function linkit_profile_load_all() {
ctools_include('export');
$profiles = ctools_export_load_object('linkit_profiles');
return $profiles;
}
function linkit_profile_save($profile) {
$schema = ctools_export_get_schema('linkit_profiles');
$export = $schema['export'];
if (empty($export['primary key'])) {
return FALSE;
}
$key = $export['primary key'];
if ($profile->export_type & EXPORT_IN_DATABASE) {
$update = array(
$key,
);
}
else {
$update = array();
$profile->export_type = EXPORT_IN_DATABASE;
}
return drupal_write_record('linkit_profiles', $profile, $update);
}
function linkit_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/" . $plugin;
}
if ($module == 'linkit' && !empty($plugin)) {
return "plugins/" . $plugin;
}
}
function linkit_ctools_plugin_type() {
$plugins['linkit_plugins'] = array(
'child plugins' => TRUE,
'classes' => array(
'handler',
),
);
$plugins['linkit_insert'] = array(
'classes' => array(
'handler',
),
);
return $plugins;
}
function linkit_get_plugin_handler($plugin, $profile = NULL) {
$object_cache = drupal_static(__FUNCTION__);
$identifier = $plugin['name'];
if (!isset($object_cache[$identifier])) {
ctools_include('plugins');
$class = ctools_plugin_get_class($plugin, 'handler');
if (class_exists($class)) {
$object_cache[$identifier] = new $class($plugin, $profile);
}
else {
$object_cache[$identifier] = new LinkitPluginBroken($plugin, $profile);
if (isset($plugin['entity fallback handler'])) {
$fallback_class = ctools_plugin_get_class($plugin, 'entity fallback handler');
if (class_exists($fallback_class)) {
$object_cache[$identifier] = new $fallback_class($plugin, $profile);
}
}
}
}
return $object_cache[$identifier];
}
function linkit_extract_tokens($type) {
$tokens = token_info();
if (!isset($tokens['tokens'][$type])) {
return array();
}
$available_tokens = array();
foreach ($tokens['tokens'][$type] as $token_key => $token) {
$available_tokens[] = '[' . $type . ':' . $token_key . ']';
}
return $available_tokens;
}
function linkit_entity_info_alter(&$entity_info) {
$allowed = array(
'node',
'user',
'taxonomy_term',
'file',
);
foreach ($allowed as $entity_name) {
if (isset($entity_info[$entity_name])) {
$entity_info[$entity_name]['linkit'] = TRUE;
}
}
}
function linkit_linkit_plugin_entities_alter(&$plugins) {
$alter_plugins = array(
'entity:node',
'entity:user',
'entity:taxonomy_term',
'entity:file',
);
$path = drupal_get_path('module', 'linkit') . '/plugins/linkit_plugins';
foreach ($alter_plugins as $plugin) {
if (isset($plugins[$plugin])) {
$handler = array(
'class' => 'LinkitPlugin' . drupal_ucfirst($plugins[$plugin]['entity_type']),
'file' => 'linkit-plugin-' . $plugins[$plugin]['entity_type'] . '.class.php',
'path' => $path,
);
$plugins[$plugin]['handler'] = $handler;
}
}
}