. (Using
* this format ensures the popup status remains consistent for all
* links.)
*/
/**
* Implements hook_menu().
*/
function advanced_help_menu() {
// View help topic index.
// This is structured a little oddly so POTX can properly handle the translation.
if (module_exists('help')) {
$items['admin/advanced_help'] = array(
'title' => 'Advanced help',
'page callback' => 'advanced_help_index_page',
'access arguments' => array('view advanced help index'),
'weight' => 9,
);
}
else {
$items['admin/advanced_help'] = array(
'title' => 'Help',
'page callback' => 'advanced_help_index_page',
'access arguments' => array('view advanced help index'),
'weight' => 9,
);
}
$items['advanced_help/search/%menu_tail'] = array(
'title' => 'Search help',
'page callback' => 'advanced_help_search_view',
'page arguments' => array('advanced_help'),
'access arguments' => array('view advanced help index'),
);
// View help topic.
$items['help/%/%'] = array(
'page callback' => 'advanced_help_topic_page',
'page arguments' => array(1, 2),
'access arguments' => array('view advanced help topic'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function advanced_help_menu_alter(&$callbacks) {
// We need to fix the menu item provided by search module to restrict access.
$callbacks['search/advanced_help/%menu_tail']['access callback'] = 'user_access';
$callbacks['search/advanced_help/%menu_tail']['access arguments'] = array('view advanced help index');
}
/**
* Implements hook_theme().
*/
function advanced_help_theme() {
$hooks['advanced_help_topic'] = array(
'variables' => array(
'module' => NULL,
'topic' => NULL,
'type' => 'icon',
),
);
$hooks['advanced_help_popup'] = array(
'render element' => 'content',
'template' => 'advanced-help-popup',
);
return $hooks;
}
function advanced_help_uasort($id_a, $id_b) {
$topics = advanced_help_get_topics();
list($module_a, $topic_a) = $id_a;
$a = $topics[$module_a][$topic_a];
list($module_b, $topic_b) = $id_b;
$b = $topics[$module_b][$topic_b];
$a_weight = isset($a['weight']) ? $a['weight'] : 0;
$b_weight = isset($b['weight']) ? $b['weight'] : 0;
if ($a_weight != $b_weight) {
return ($a_weight < $b_weight) ? -1 : 1;
}
if ($a['title'] != $b['title']) {
return ($a['title'] < $b['title']) ? -1 : 1;
}
return 0;
}
/**
* Page callback for advanced help search.
*/
function advanced_help_search_view() {
if (!module_exists('search')) {
return drupal_not_found();
}
$breadcrumb[] = advanced_help_l(t('Help'), 'admin/advanced_help');
if (!isset($_POST['form_id'])) {
$keys = search_get_keys();
// Only perform search if there is non-whitespace search term:
$results = '';
if (trim($keys)) {
// Collect the search results:
$results = array(
'#type' => 'markup',
'#markup' => search_data($keys, 'advanced_help'),
);
}
// Construct the search form.
$output['advanced_help_search_form'] = drupal_get_form('advanced_help_search_form', $keys);
$output['results'] = $results;
}
else {
$output = drupal_get_form('advanced_help_search_form', empty($keys) ? '' : $keys);
}
$popup = !empty($_GET['popup']) && user_access('view advanced help popup');
if ($popup) {
$GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
drupal_set_breadcrumb(array_reverse($breadcrumb));
print theme('advanced_help_popup', array('content' => $output));
return;
}
$breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
drupal_set_breadcrumb($breadcrumb);
return $output;
}
/**
* Page callback to view the advanced help topic index.
*
* @param string $module
* @return array
*/
function advanced_help_index_page($module = '') {
$topics = advanced_help_get_topics();
$settings = advanced_help_get_settings();
$output = array();
// Print a search widget.
$output['advanced_help_search'] = module_exists('search')
? drupal_get_form('advanced_help_search_form')
: t('Enable the search module to search help.');
$breadcrumb = array();
if ($module) {
if (empty($topics[$module])) {
return drupal_not_found();
}
advanced_help_get_topic_hierarchy($topics);
$items = advanced_help_get_tree($topics, $topics[$module]['']['children']);
$breadcrumb[] = advanced_help_l(t('Help'), 'admin/advanced_help');
drupal_set_title(t('@module help index', array('@module' => advanced_help_get_module_name($module))));
$output['items-module'] = array(
'#theme' => 'item_list',
'#items' => $items,
);
}
else {
// Print a module index.
$modules = array();
$result = db_query('SELECT * FROM {system}');
foreach ($result as $info) {
$module_info = unserialize($info->info);
$modules[$info->name] = $module_info['name'];
}
asort($modules);
$items = array();
foreach ($modules as $module => $module_name) {
if (!empty($topics[$module]) && empty($settings[$module]['hide'])) {
if (isset($settings[$module]['index name'])) {
$name = $settings[$module]['index name'];
}
elseif (isset($settings[$module]['name'])) {
$name = $settings[$module]['name'];
}
else {
$name = t($module_name);
}
$items[] = advanced_help_l($name, "admin/advanced_help/$module");
}
}
drupal_set_title(t('Module help index'));
$output['items-nomodule'] = array(
'#theme' => 'item_list',
'#items' => $items,
);
}
$popup = !empty($_GET['popup']) && user_access('view advanced help popup');
if ($popup) {
$GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
drupal_set_breadcrumb(array_reverse($breadcrumb));
print theme('advanced_help_popup', array('content' => $output));
return;
}
$breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
drupal_set_breadcrumb($breadcrumb);
return $output;
}
/**
* Build a tree of advanced help topics.
*
* @param array $topics
* @param array $topic_ids
* @param int $max_depth
* @param int $depth
* @return array
*/
function advanced_help_get_tree($topics, $topic_ids, $max_depth = -1, $depth = 0) {
uasort($topic_ids, 'advanced_help_uasort');
$items = array();
foreach ($topic_ids as $info) {
list($module, $topic) = $info;
$item = advanced_help_l($topics[$module][$topic]['title'], "help/$module/$topic");
if (!empty($topics[$module][$topic]['children']) && ($max_depth == -1 || $depth < $max_depth)) {
$item .= theme('item_list', array('items' =>
advanced_help_get_tree($topics, $topics[$module][$topic]['children'], $max_depth, $depth + 1)));
}
$items[] = $item;
}
return $items;
}
/**
* Build a hierarchy for a single module's topics.
*/
function advanced_help_get_topic_hierarchy(&$topics) {
foreach ($topics as $module => $module_topics) {
foreach ($module_topics as $topic => $info) {
$parent_module = $module;
// We have a blank topic that we don't want parented to itself.
if (!$topic) {
continue;
}
if (empty($info['parent'])) {
$parent = '';
}
elseif (strpos($info['parent'], '%')) {
list($parent_module, $parent) = explode('%', $info['parent']);
if (empty($topics[$parent_module][$parent])) {
// If it doesn't exist, top level.
$parent = '';
}
}
else {
$parent = $info['parent'];
if (empty($module_topics[$parent])) {
// If it doesn't exist, top level.
$parent = '';
}
}
if (!isset($topics[$parent_module][$parent]['children'])) {
$topics[$parent_module][$parent]['children'] = array();
}
$topics[$parent_module][$parent]['children'][] = array($module, $topic);
$topics[$module][$topic]['_parent'] = array($parent_module, $parent);
}
}
}
/**
* Implements hook_form_system_modules_alter().
*
* Add advanced help links to the modules page.
*/
function advanced_help_form_system_modules_alter(&$form, &$form_state) {
$advanced_help_modules = drupal_map_assoc(array_keys(advanced_help_get_topics()));
foreach (element_children($form['modules']) as $group) {
foreach (element_children($form['modules'][$group]) as $module) {
if (isset($advanced_help_modules[$module])) {
$form['modules'][$group][$module]['links']['help'] = array(
'#type' => 'link',
'#title' => t('Help'),
'#href' => "admin/advanced_help/$module",
'#options' => array('attributes' => array('class' => array('module-link', 'module-link-help'), 'title' => t('Help'))),
);
}
}
}
}
/**
* Form builder callback to build the search form.
*
* Load search/search.pages so that its template preprocess functions are
* visible and can be invoked.
*/
function advanced_help_search_form($form, &$form_state, $keys = '') {
module_load_include('inc', 'search', 'search.pages');
$form = search_form($form, $form_state, advanced_help_url('admin/advanced_help'), $keys, 'advanced_help', t('Search help'));
$form['basic']['inline']['submit']['#validate'] = array('search_form_validate');
$form['basic']['inline']['submit']['#submit'] = array('advanced_help_search_form_submit');
return $form;
}
/**
* Process a search form submission.
*/
function advanced_help_search_form_submit($form, &$form_state) {
$keys = $form_state['values']['processed_keys'];
if ($keys == '') {
form_set_error('keys', t('Please enter some keywords.'));
return;
}
$popup = !empty($_GET['popup']) && user_access('view advanced help popup');
if ($popup) {
$form_state['redirect'] = array('advanced_help/search/' . $keys, array('query' => array('popup' => 'true')));
}
else {
$form_state['redirect'] = 'advanced_help/search/' . $keys;
}
}
/**
* Small helper function to get a module's proper name.
*
* @param $module string
* @return string
*/
function advanced_help_get_module_name($module) {
$settings = advanced_help_get_settings();
if (isset($settings[$module]['name'])) {
$name = $settings[$module]['name'];
}
else {
$info = db_query("SELECT s.info FROM {system} s WHERE s.name = :name",
array(':name' => $module))
->fetchField();
$info = unserialize($info);
$name = t($info['name']);
}
return $name;
}
/**
* Page callback to view a help topic.
*/
function advanced_help_topic_page($module, $topic) {
$info = advanced_help_get_topic($module, $topic);
if (!$info) {
return drupal_not_found();
}
$popup = !empty($_GET['popup']) && user_access('view advanced help popup');
drupal_set_title($info['title']);
// Set up breadcrumb.
$breadcrumb = array();
$parent = $info;
$pmodule = $module;
// Loop checker.
$checked = array();
while (!empty($parent['parent'])) {
if (strpos($parent['parent'], '%')) {
list($pmodule, $ptopic) = explode('%', $parent['parent']);
}
else {
$ptopic = $parent['parent'];
}
if (!empty($checked[$pmodule][$ptopic])) {
break;
}
$checked[$pmodule][$ptopic] = TRUE;
$parent = advanced_help_get_topic($pmodule, $ptopic);
if (!$parent) {
break;
}
$breadcrumb[] = advanced_help_l($parent['title'], "help/$pmodule/$ptopic");
}
$breadcrumb[] = advanced_help_l(advanced_help_get_module_name($pmodule), "admin/advanced_help/$pmodule");
$breadcrumb[] = advanced_help_l(t('Help'), "admin/advanced_help");
$output = advanced_help_view_topic($module, $topic, $popup);
if (empty($output)) {
$output = t('Missing help topic.');
}
if ($popup) {
$GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
drupal_set_breadcrumb(array_reverse($breadcrumb));
print theme('advanced_help_popup', array('content' => $output));
return;
}
drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help.css');
$breadcrumb[] = l(t('Home'), '');
drupal_set_breadcrumb(array_reverse($breadcrumb));
return $output;
}
/**
* Implements hook_permission().
*/
function advanced_help_permission() {
return array(
'view advanced help topic' => array('title' => t('View help topics')),
'view advanced help popup' => array('title' => t('View help popups')),
'view advanced help index' => array('title' => t('View help index')),
);
}
/**
* Display a help icon with a link to view the topic in a popup.
*
* @param $variables
* An associative array containing:
* - module: The module that owns this help topic.
* - topic: The identifier for the topic
* - type
* - 'icon' to display the question mark icon
* - 'title' to display the topic's title
* - any other text to display the text. Be sure to t() it!
*/
function theme_advanced_help_topic($variables) {
$module = $variables['module'];
$topic = $variables['topic'];
$type = $variables['type'];
$info = advanced_help_get_topic($module, $topic);
if (!$info) {
return;
}
switch ($type) {
case 'icon':
$text = '' . t('Help') . '';
$class = 'advanced-help-link';
break;
case 'title':
$text = $info['title'];
$class = 'advanced-help-title';
break;
default:
$class = 'advanced-help-title';
$text = $type;
break;
}
if (user_access('view advanced help popup')) {
drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help-icon.css');
return l($text, "help/$module/$topic", array(
'attributes' => array(
'class' => $class,
'onclick' => "var w=window.open(this.href, 'advanced_help_window', 'width=" . $info['popup width'] . ",height=" . $info['popup height'] . ",scrollbars,resizable'); w.focus(); return false;",
'title' => $info['title']
),
'query' => array('popup' => TRUE),
'html' => TRUE)
);
}
else {
return l($text, "help/$module/$topic", array(
'attributes' => array(
'class' => $class,
'title' => $info['title']
),
'html' => TRUE)
);
}
}
/**
* Load and render a help topic.
*/
function advanced_help_get_topic_filename($module, $topic) {
$info = advanced_help_get_topic_file_info($module, $topic);
if ($info) {
return "./$info[path]/$info[file]";
}
}
/**
* Load and render a help topic.
*/
function advanced_help_get_topic_file_info($module, $topic) {
global $language;
$info = advanced_help_get_topic($module, $topic);
if (empty($info)) {
return;
}
// Search paths:
$paths = array(
path_to_theme() . '/help', // Allow theme override.
drupal_get_path('module', $module) . "/translations/help/$language->language", // Translations.
$info['path'], // In same directory as .inc file.
);
foreach ($paths as $path) {
if (file_exists("./$path/$info[file]")) {
return array('path' => $path, 'file' => $info['file']);
}
}
}
/**
* Load and render a help topic.
*
* @param string $module
* @param string $topic
* @param boolean $popup
* @return array
*/
function advanced_help_view_topic($module, $topic, $popup = FALSE) {
$file_info = advanced_help_get_topic_file_info($module, $topic);
if ($file_info) {
$info = advanced_help_get_topic($module, $topic);
$file = "./$file_info[path]/$file_info[file]";
$output = file_get_contents($file);
if (isset($info['readme file']) && $info['readme file']) {
// Readme files are treated as plain text: filter accordingly.
$output = check_plain($output);
}
// Make some exchanges. The strtr is because url() translates $ into %24
// but we need to change it back for the regex replacement.
// Change 'topic:' to the URL for another help topic.
if ($popup) {
$output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1', array('query' => array('popup' => 'true'))), array('%24' => '$')) . '"', $output);
$output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1', array('query' => array('popup' => 'true'))), array('%24' => '$')) . '"', $output);
$output = preg_replace('/&topic:([^"]+)&/', strtr(url('help/$1', array('query' => array('popup' => 'true'))), array('%24' => '$')) , $output);
}
else {
$output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1'), array('%24' => '$')) . '"', $output);
$output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1'), array('%24' => '$')) . '"', $output);
$output = preg_replace('/&topic:([^"]+)&/', strtr(url('help/$1'), array('%24' => '$')), $output);
}
global $base_path;
// Change 'path:' to the URL to the base help directory.
$output = preg_replace('/href="path:([^"]+)"/', 'href="' . $base_path . $info['path'] . '/$1"', $output);
$output = preg_replace('/src="path:([^"]+)"/', 'src="' . $base_path . $info['path'] . '/$1"', $output);
$output = str_replace('&path&', $base_path . $info['path'] . '/', $output);
// Change 'trans_path:' to the URL to the actual help directory.
$output = preg_replace('/href="trans_path:([^"]+)"/', 'href="' . $base_path . $file_info['path'] . '/$1"', $output);
$output = preg_replace('/src="trans_path:([^"]+)"/', 'src="' . $base_path . $file_info['path'] . '/$1"', $output);
$output = str_replace('&trans_path&', $base_path . $file_info['path'] . '/', $output);
// Change 'base_url:' to the URL to the site.
$output = preg_replace('/href="base_url:([^"]+)"/', 'href="' . strtr(url('$1'), array('%24' => '$')) . '"', $output);
$output = preg_replace('/src="base_url:([^"]+)"/', 'src="' . strtr(url('$1'), array('%24' => '$')) . '"', $output);
$output = preg_replace('/&base_url&([^"]+)"/', strtr(url('$1'), array('%24' => '$')) . '"', $output);
// Run the line break filter if requested
if (!empty($info['line break'])) {
// Remove the header since it adds an extra
to the filter.
$output = preg_replace('/^\n/', '', $output);
$output = _filter_autop($output);
}
if (!empty($info['navigation'])) {
$topics = advanced_help_get_topics();
advanced_help_get_topic_hierarchy($topics);
if (!empty($topics[$module][$topic]['children'])) {
$items = advanced_help_get_tree($topics, $topics[$module][$topic]['children']);
$output .= theme('item_list', array('items' => $items));
}
list($parent_module, $parent_topic) = $topics[$module][$topic]['_parent'];
if ($parent_topic) {
$parent = $topics[$module][$topic]['_parent'];
$up = "help/$parent[0]/$parent[1]";
}
else {
$up = "admin/advanced_help/$module";
}
$siblings = $topics[$parent_module][$parent_topic]['children'];
uasort($siblings, 'advanced_help_uasort');
$prev = $next = NULL;
$found = FALSE;
foreach ($siblings as $sibling) {
list($sibling_module, $sibling_topic) = $sibling;
if ($found) {
$next = $sibling;
break;
}
if ($sibling_module == $module && $sibling_topic == $topic) {
$found = TRUE;
continue;
}
$prev = $sibling;
}
if ($prev || $up || $next) {
$navigation = '