advanced_help.module in Advanced Help 5
Same filename and directory in other branches
Pluggable system to provide advanced help facilities for Drupal and modules.
Modules utilizing this help system should create a 'help' directory in their module. Inside that directory place MODULENAME.help.ini which will be formatted like this:
[buses]
title = "How buses are tied into the system"
file = buses
[TOPIC_ID]
title = "Title of topic"
file = filename of topic, without the .html extension
weight = the importance of the topic on the index page
parent = the optional topic parent to use in the breadcrumb. Can be either topic or module%topic
All topics are addressed by the module that provides the topic, and the topic id. Links may be embedded as in the following example:
$output .= theme('advanced_help_topic', $module, $topic);
Link to other topics using <a href="topic:module/topic">. (Using this format ensures the popup status remains consistent for all links.)
File
advanced_help.moduleView source
<?php
/**
* @file advanced_help.module
*
* Pluggable system to provide advanced help facilities for Drupal and modules.
*
* Modules utilizing this help system should create a 'help' directory in their
* module. Inside that directory place MODULENAME.help.ini which will be formatted
* like this:
*
* @code
* [buses]
* title = "How buses are tied into the system"
* file = buses
*
* [TOPIC_ID]
* title = "Title of topic"
* file = filename of topic, without the .html extension
* weight = the importance of the topic on the index page
* parent = the optional topic parent to use in the breadcrumb. Can be either topic or module%topic
* @endcode
*
* All topics are addressed by the module that provides the topic, and the topic
* id. Links may be embedded as in the following example:
*
* @code
* $output .= theme('advanced_help_topic', $module, $topic);
* @endcode
*
* Link to other topics using <a href="topic:module/topic">. (Using
* this format ensures the popup status remains consistent for all
* links.)
*/
/**
* Implementation of hook_menu().
*/
function advanced_help_menu() {
// View help topic index.
$items[] = array(
'path' => 'admin/advanced_help',
'title' => module_exists('help') ? 'Advanced help' : 'Help',
'callback' => 'advanced_help_index_page',
'access' => user_access('view advanced help index'),
'weight' => 9,
);
$items[] = array(
'path' => 'advanced_help/search',
'callback' => 'advanced_help_search_view',
'access' => user_access('view advanced help index'),
'type' => MENU_CALLBACK,
);
/* Don't think this is required?
$items[] = array(
'path' => 'advanced_help/search/advanced_help',
'title' => 'Search help',
'access' => user_access('view advanced help index'),
'type' => MENU_NORMAL_ITEM,
);
*/
// View help topic.
$items[] = array(
'path' => 'help',
'callback' => 'advanced_help_topic_page',
'access' => user_access('view advanced help topic'),
'type' => MENU_CALLBACK,
);
return $items;
}
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')) {
drupal_not_found();
}
$breadcrumb[] = advanced_help_l('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 = search_data($keys, 'advanced_help');
if ($results) {
$results = theme('box', t('Search results'), $results);
}
else {
$results = theme('box', t('Your search yielded no results'), search_help('search#noresults'));
}
}
// Construct the search form.
$output = drupal_get_form('advanced_help_search_form', $keys);
$output .= $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.
drupal_set_breadcrumb(array_reverse($breadcrumb));
print theme('advanced_help_popup', $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.
*/
function advanced_help_index_page($module = '') {
$topics = advanced_help_get_topics();
// Print a search widget.
$output = '';
if (module_exists('search')) {
$output .= drupal_get_form('advanced_help_search_form');
}
else {
$output .= 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('Help', 'admin/advanced_help');
drupal_set_title(t('@module help index', array(
'@module' => advanced_help_get_module_name($module),
)));
$output .= theme('item_list', $items);
}
else {
// Print a module index.
$modules = array();
$result = db_query("SELECT name FROM {system}");
while ($info = db_fetch_object($result)) {
$modules[] = $info->name;
}
asort($modules);
$items = array();
foreach ($modules as $module) {
if (!empty($topics[$module])) {
$module_name = advanced_help_get_module_name($module);
$items[] = advanced_help_l($module_name, "admin/advanced_help/{$module}");
}
}
drupal_set_title(t('Module help index'));
$output .= theme('item_list', $items);
}
$popup = !empty($_GET['popup']) && user_access('view advanced help popup');
if ($popup) {
$GLOBALS['devel_shutdown'] = FALSE;
// Prevent devel module from spewing.
drupal_set_breadcrumb(array_reverse($breadcrumb));
print theme('advanced_help_popup', $output);
return;
}
$breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
drupal_set_breadcrumb($breadcrumb);
return $output;
}
/**
* Build a tree of advanced help topics.
*/
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', 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 = '';
}
else {
if (strpos($info['parent'], '%')) {
list($parent, $parent_module) = 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,
);
}
}
}
/**
* Form builder callback to build the search form.
*/
function advanced_help_search_form($keys = '') {
$form = search_form(advanced_help_url('admin/advanced_help'), $keys, 'advanced_help', t('Search help'));
return $form;
}
/**
* Process a search form validation.
* Only required for 5.x because 'processed_keys' was not being populated.
*/
function advanced_help_search_form_validate($form_id, $form_values, $form) {
search_form_validate($form_id, $form_values, $form);
}
/**
* Process a search form submission.
*/
function advanced_help_search_form_submit($form, $form_values) {
$keys = $form_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) {
return array(
'advanced_help/search/' . $keys,
'popup=true',
);
}
else {
return 'advanced_help/search/' . $keys;
}
}
/**
* Small helper function to get a module's proper name.
*/
function advanced_help_get_module_name($module) {
// Get name and filename from system table.
$file = db_fetch_object(db_query("SELECT * FROM {system} WHERE name = '%s'", $module));
// from module.inc line 106
$file->info = _module_parse_info_file(dirname($file->filename) . '/' . $file->name . '.info');
return t($file->info['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.
drupal_set_breadcrumb(array_reverse($breadcrumb));
print theme('advanced_help_popup', $output);
//print theme('page', $output);
return;
}
drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help.css');
$breadcrumb[] = l('Home', '');
drupal_set_breadcrumb(array_reverse($breadcrumb));
return $output;
}
/**
* Implementation of hook_perm().
*/
function advanced_help_perm() {
return array(
'view advanced help topic',
'view advanced help popup',
'view advanced help index',
);
}
/**
* Display a help icon with a link to view the topic in a popup.
*
* @param $module
* The module that owns this help topic.
* @param $topic
* The identifier for the topic
* @param $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($module, $topic, $type = 'icon') {
$info = advanced_help_get_topic($module, $topic);
if (!$info) {
return;
}
switch ($type) {
case 'icon':
$text = '<span>' . t('Help') . '</span>';
$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(
'class' => $class,
'onclick' => "window.open(this.href, 'advanced_help_window', 'width=500,height=500,scrollbars,resizable'); return false;",
'title' => $info['title'],
), 'popup=TRUE', NULL, FALSE, TRUE);
}
else {
return l($text, "help/{$module}/{$topic}", array(
'class' => $class,
'title' => $info['title'],
), NULL, NULL, FALSE, TRUE);
}
}
/**
* Load and render a help topic.
*/
function advanced_help_get_topic_filename($module, $topic) {
init_theme();
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'],
);
foreach ($paths as $path) {
if (file_exists("./{$path}/{$info['file']}")) {
return "./{$path}/{$info['file']}";
}
}
}
/**
* Load and render a help topic.
*/
function advanced_help_view_topic($module, $topic, $popup = FALSE) {
$file = advanced_help_get_topic_filename($module, $topic);
$info = advanced_help_get_topic($module, $topic);
if ($file) {
// @todo is this trusted output?
$output = file_get_contents($file);
// Make some exchanges. The strtr is because url() translates $ into %24
// but we need to change it back for the regex replacement.
// Run the line break filter if requested
if (!empty($info['line break'])) {
// Remove the header since it adds an extra <br /> to the filter.
$output = preg_replace('/^<!--[^\\n]*-->\\n/', '', $output);
$output = _filter_autop($output);
}
// Change 'topic:' to the URL for another help topic.
if ($popup) {
$output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1', 'popup=true'), array(
'%24' => '$',
)) . '"', $output);
$output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1', '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);
}
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);
// 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);
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', $items);
}
list($parent_module, $parent_topic) = $topics[$module][$topic]['_parent'];
$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 || $next) {
$navigation = '<div class="help-navigation clear-block">';
$navigation .= '<div class="help-previous">';
if ($prev) {
$navigation .= advanced_help_l('<< ' . $topics[$prev[0]][$prev[1]]['title'], "help/{$prev[0]}/{$prev[1]}");
}
$navigation .= '</div>';
$navigation .= '<div class="help-next">';
if ($next) {
$navigation .= advanced_help_l($topics[$next[0]][$next[1]]['title'] . ' >>', "help/{$next[0]}/{$next[1]}");
}
$navigation .= '</div>';
$navigation .= '</div>';
$output .= $navigation;
}
}
if (!empty($info['css'])) {
drupal_add_css($info['path'] . '/' . $info['css']);
}
return '<div class="advanced-help-topic">' . $output . '</div>';
}
}
/**
* Get the information for a single help topic.
*/
function advanced_help_get_topic($module, $topic) {
$topics = advanced_help_get_topics();
if (!empty($topics[$module][$topic])) {
return $topics[$module][$topic];
}
}
/**
* Search the system for all available help topics.
*/
function advanced_help_get_topics() {
static $topics = NULL;
if (!isset($topics)) {
$topics = array();
$help_path = drupal_get_path('module', 'advanced_help') . '/modules';
foreach (module_list() as $module) {
$module_path = drupal_get_path('module', $module);
$info = array();
if (file_exists("{$module_path}/help/{$module}.help.ini")) {
$path = "{$module_path}/help";
$info = parse_ini_file("./{$module_path}/help/{$module}.help.ini", TRUE);
}
else {
if (file_exists("{$help_path}/{$module}/{$module}.help.ini")) {
$path = "{$help_path}/{$module}";
$info = parse_ini_file("./{$help_path}/{$module}/{$module}.help.ini", TRUE);
}
}
if (!empty($info)) {
// Get translated titles:
global $language;
if (file_exists("{$module_path}/translations/help/{$language->language}/{$module}.help.ini")) {
$translation = parse_ini_file("{$module_path}/translations/help/{$language->language}/{$module}.help.ini", TRUE);
}
$settings = array();
if (!empty($info['advanced help settings'])) {
$settings = $info['advanced help settings'];
unset($info['advanced help settings']);
}
foreach ($info as $name => $topic) {
// Each topic should have a name, a title, a file and of course the path.
$file = !empty($topic['file']) ? $topic['file'] : $name;
$topics[$module][$name] = array(
'name' => $name,
'title' => !empty($translation[$name]['title']) ? $translation[$name]['title'] : $topic['title'],
'weight' => isset($topic['weight']) ? $topic['weight'] : 0,
'parent' => isset($topic['parent']) ? $topic['parent'] : 0,
'file' => $file . '.html',
// require extension
'path' => $path,
// not in .ini file
'line break' => isset($topic['line break']) ? $topic['line break'] : (isset($settings['line break']) ? $settings['line break'] : FALSE),
'navigation' => isset($topic['navigation']) ? $topic['navigation'] : (isset($settings['navigation']) ? $settings['navigation'] : TRUE),
'css' => isset($topic['css']) ? $topic['css'] : (isset($settings['css']) ? $settings['css'] : NULL),
);
}
}
}
}
return $topics;
}
/**
* Implementation of hook_search()
*/
function advanced_help_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t('Help');
case 'reset':
variable_del('advanced_help_last_cron');
return;
case 'search':
$topics = advanced_help_get_topics();
$find = do_search($keys, 'help');
if (!$find) {
return;
}
$results = array();
$placeholders = implode(', ', array_fill(0, count($find), '%d'));
foreach ($find as $item) {
$sids[] = $item->sid;
}
$result = db_query("SELECT * FROM {advanced_help_index} WHERE sid IN ({$placeholders})", $sids);
while ($sid = db_fetch_object($result)) {
// Guard against removed help topics that are still indexed.
if (empty($topics[$sid->module][$sid->topic])) {
continue;
}
$info = $topics[$sid->module][$sid->topic];
$text = advanced_help_view_topic($sid->module, $sid->topic);
$results[] = array(
'link' => advanced_help_url("help/{$sid->module}/{$sid->topic}"),
'title' => $info['title'],
'snippet' => search_excerpt($keys, $text),
);
}
return $results;
}
}
/**
* Get or create an sid (search id) that correllates to each topic for
* the search system.
*/
function advanced_help_get_sids(&$topics) {
global $language;
$result = db_query("SELECT * FROM {advanced_help_index} WHERE language = '%s'", $language->language);
while ($sid = db_fetch_object($result)) {
if (empty($topics[$sid->module][$sid->topic])) {
db_query("DELETE FROM {advanced_help_index} WHERE sid = %d", $sid->sid);
}
else {
$topics[$sid->module][$sid->topic]['sid'] = $sid->sid;
}
}
}
/**
* Implementation of hook_update_index().
*/
function advanced_help_update_index() {
global $language;
// If we got interrupted by limit, this will contain the last module
// and topic we looked at.
$last = variable_get('advanced_help_last_cron', array(
'time' => 0,
));
$limit = intval(variable_get('search_cron_limit', 100));
$topics = advanced_help_get_topics();
advanced_help_get_sids($topics);
$count = 0;
foreach ($topics as $module => $module_topics) {
// Fast forward if necessary.
if (!empty($last['module']) && $last['module'] != $module) {
continue;
}
foreach ($module_topics as $topic => $info) {
// Fast forward if necessary.
if (!empty($last['topic']) && $last['topic'] != $topic) {
continue;
}
// If we've been looking to catch up, and we have, reset so we
// stop fast forwarding.
if (!empty($last['module'])) {
unset($last['topic']);
unset($last['module']);
}
$file = advanced_help_get_topic_filename($module, $topic);
if ($file && (empty($info['sid']) || filemtime($file) > $last['time'])) {
if (empty($info['sid'])) {
$info['sid'] = db_next_id('{advanced_help_index}_sid');
db_query("INSERT INTO {advanced_help_index} (sid, module, topic, language) VALUES (%d, '%s', '%s', '%s')", $info['sid'], $module, $topic, $language->language);
}
search_index($info['sid'], 'help', '<h1>' . $info['title'] . '</h1>' . file_get_contents($file));
$count++;
if ($count >= $limit) {
$last['topic'] = $topic;
$last['module'] = $module;
// Don't change time if we stop.
variable_set('advanced_help_last_cron', $last);
return;
}
}
}
}
variable_set('advanced_help_last_cron', array(
'time' => time(),
));
}
/**
* Format a link but preserve popup identity.
*/
function advanced_help_l($text, $dest, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = FALSE) {
$popup = !empty($_GET['popup']) && user_access('view advanced help popup');
if ($popup) {
if (empty($query)) {
$query = 'popup=TRUE';
}
else {
$query .= '&popup=TRUE';
}
}
return l($text, $dest, $attributes, $query, $fragment, $absolute, $html);
}
/**
* Format a URL but preserve popup identity.
*/
function advanced_help_url($dest, $query = NULL, $fragment = NULL, $absolute = FALSE) {
$popup = !empty($_GET['popup']) && user_access('view advanced help popup');
if ($popup) {
if (empty($query)) {
$query = 'popup=TRUE';
}
else {
$query .= '&popup=TRUE';
}
}
return url($dest, $query, $fragment, $absolute);
}
Functions
Name | Description |
---|---|
advanced_help_get_module_name | Small helper function to get a module's proper name. |
advanced_help_get_sids | Get or create an sid (search id) that correllates to each topic for the search system. |
advanced_help_get_topic | Get the information for a single help topic. |
advanced_help_get_topics | Search the system for all available help topics. |
advanced_help_get_topic_filename | Load and render a help topic. |
advanced_help_get_topic_hierarchy | Build a hierarchy for a single module's topics. |
advanced_help_get_tree | Build a tree of advanced help topics. |
advanced_help_index_page | Page callback to view the advanced help topic index. |
advanced_help_l | Format a link but preserve popup identity. |
advanced_help_menu | Implementation of hook_menu(). |
advanced_help_perm | Implementation of hook_perm(). |
advanced_help_search | Implementation of hook_search() |
advanced_help_search_form | Form builder callback to build the search form. |
advanced_help_search_form_submit | Process a search form submission. |
advanced_help_search_form_validate | Process a search form validation. Only required for 5.x because 'processed_keys' was not being populated. |
advanced_help_search_view | Page callback for advanced help search. |
advanced_help_topic_page | Page callback to view a help topic. |
advanced_help_uasort | |
advanced_help_update_index | Implementation of hook_update_index(). |
advanced_help_url | Format a URL but preserve popup identity. |
advanced_help_view_topic | Load and render a help topic. |
theme_advanced_help_topic | Display a help icon with a link to view the topic in a popup. |