advanced_help.module

  1. cis7 sites/all/modules/ulmus/advanced_help/advanced_help.module
  2. cle7 sites/all/modules/ulmus/advanced_help/advanced_help.module
  3. ecd7 sites/all/modules/ulmus/advanced_help/advanced_help.module
  4. elmsmedia7 sites/all/modules/ulmus/advanced_help/advanced_help.module
  5. harmony7 sites/all/modules/ulmus/advanced_help/advanced_help.module
  6. icor7 sites/all/modules/ulmus/advanced_help/advanced_help.module
  7. meedjum_blog7 sites/all/modules/ulmus/advanced_help/advanced_help.module
  8. mooc7 sites/all/modules/ulmus/advanced_help/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:

[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.)

Functions

Namesort descending Description
advanced_help_form_system_modules_alter Implements hook_form_system_modules_alter().
advanced_help_get_module_name Small helper function to get a module's proper name.
advanced_help_get_settings
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_file_info 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 Implements hook_menu().
advanced_help_menu_alter Implements hook_menu_alter().
advanced_help_permission Implements hook_permission().
advanced_help_search_execute Implements hook_search_execute().
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_info Implements hook_search_info().
advanced_help_search_reset Implements hook_search_reset().
advanced_help_search_status Implements hook_search_status().
advanced_help_search_view Page callback for advanced help search.
advanced_help_theme Implements hook_theme().
advanced_help_topic_page Page callback to view a help topic.
advanced_help_uasort
advanced_help_update_index Implements hook_update_index().
advanced_help_url Format a URL but preserve popup identity.
advanced_help_view_topic Load and render a help topic.
template_preprocess_advanced_help_popup Fill in a bunch of page variables for our specialized popup page.
theme_advanced_help_topic Display a help icon with a link to view the topic in a popup.
_advanced_help_parse_ini

File

sites/all/modules/ulmus/advanced_help/advanced_help.module
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Pluggable system to provide advanced help facilities for Drupal and modules.
  6. *
  7. * Modules utilizing this help system should create a 'help' directory in their
  8. * module. Inside that directory place MODULENAME.help.ini which will be formatted
  9. * like this:
  10. *
  11. * @code
  12. * [buses]
  13. * title = "How buses are tied into the system"
  14. * file = buses
  15. *
  16. * [TOPIC_ID]
  17. * title = "Title of topic"
  18. * file = filename of topic, without the .html extension
  19. * weight = the importance of the topic on the index page
  20. * parent = the optional topic parent to use in the breadcrumb. Can be either topic or module%topic
  21. * @endcode
  22. *
  23. * All topics are addressed by the module that provides the topic, and the topic
  24. * id. Links may be embedded as in the following example:
  25. *
  26. * @code
  27. * $output .= theme('advanced_help_topic', $module, $topic);
  28. * @endcode
  29. *
  30. * Link to other topics using <a href="topic:module/topic">. (Using
  31. * this format ensures the popup status remains consistent for all
  32. * links.)
  33. */
  34. /**
  35. * Implements hook_menu().
  36. */
  37. function advanced_help_menu() {
  38. // View help topic index.
  39. // This is structured a little oddly so POTX can properly handle the translation.
  40. if (module_exists('help')) {
  41. $items['admin/advanced_help'] = array(
  42. 'title' => 'Advanced help',
  43. 'page callback' => 'advanced_help_index_page',
  44. 'access arguments' => array('view advanced help index'),
  45. 'weight' => 9,
  46. );
  47. }
  48. else {
  49. $items['admin/advanced_help'] = array(
  50. 'title' => 'Help',
  51. 'page callback' => 'advanced_help_index_page',
  52. 'access arguments' => array('view advanced help index'),
  53. 'weight' => 9,
  54. );
  55. }
  56. $items['advanced_help/search/%menu_tail'] = array(
  57. 'title' => 'Search help',
  58. 'page callback' => 'advanced_help_search_view',
  59. 'page arguments' => array('advanced_help'),
  60. 'access arguments' => array('view advanced help index'),
  61. );
  62. // View help topic.
  63. $items['help/%/%'] = array(
  64. 'page callback' => 'advanced_help_topic_page',
  65. 'page arguments' => array(1, 2),
  66. 'access arguments' => array('view advanced help topic'),
  67. 'type' => MENU_CALLBACK,
  68. );
  69. return $items;
  70. }
  71. /**
  72. * Implements hook_menu_alter().
  73. */
  74. function advanced_help_menu_alter(&$callbacks) {
  75. // We need to fix the menu item provided by search module to restrict access.
  76. $callbacks['search/advanced_help/%menu_tail']['access callback'] = 'user_access';
  77. $callbacks['search/advanced_help/%menu_tail']['access arguments'] = array('view advanced help index');
  78. }
  79. /**
  80. * Implements hook_theme().
  81. */
  82. function advanced_help_theme() {
  83. $hooks['advanced_help_topic'] = array(
  84. 'variables' => array(
  85. 'module' => NULL,
  86. 'topic' => NULL,
  87. 'type' => 'icon',
  88. ),
  89. );
  90. $hooks['advanced_help_popup'] = array(
  91. 'render element' => 'content',
  92. 'template' => 'advanced-help-popup',
  93. );
  94. return $hooks;
  95. }
  96. function advanced_help_uasort($id_a, $id_b) {
  97. $topics = advanced_help_get_topics();
  98. list($module_a, $topic_a) = $id_a;
  99. $a = $topics[$module_a][$topic_a];
  100. list($module_b, $topic_b) = $id_b;
  101. $b = $topics[$module_b][$topic_b];
  102. $a_weight = isset($a['weight']) ? $a['weight'] : 0;
  103. $b_weight = isset($b['weight']) ? $b['weight'] : 0;
  104. if ($a_weight != $b_weight) {
  105. return ($a_weight < $b_weight) ? -1 : 1;
  106. }
  107. if ($a['title'] != $b['title']) {
  108. return ($a['title'] < $b['title']) ? -1 : 1;
  109. }
  110. return 0;
  111. }
  112. /**
  113. * Page callback for advanced help search.
  114. */
  115. function advanced_help_search_view() {
  116. if (!module_exists('search')) {
  117. return drupal_not_found();
  118. }
  119. $breadcrumb[] = advanced_help_l(t('Help'), 'admin/advanced_help');
  120. if (!isset($_POST['form_id'])) {
  121. $keys = search_get_keys();
  122. // Only perform search if there is non-whitespace search term:
  123. $results = '';
  124. if (trim($keys)) {
  125. // Collect the search results:
  126. $results = array(
  127. '#type' => 'markup',
  128. '#markup' => search_data($keys, 'advanced_help'),
  129. );
  130. }
  131. // Construct the search form.
  132. $output['advanced_help_search_form'] = drupal_get_form('advanced_help_search_form', $keys);
  133. $output['results'] = $results;
  134. }
  135. else {
  136. $output = drupal_get_form('advanced_help_search_form', empty($keys) ? '' : $keys);
  137. }
  138. $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  139. if ($popup) {
  140. $GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
  141. module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
  142. drupal_set_breadcrumb(array_reverse($breadcrumb));
  143. print theme('advanced_help_popup', array('content' => $output));
  144. return;
  145. }
  146. $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
  147. drupal_set_breadcrumb($breadcrumb);
  148. return $output;
  149. }
  150. /**
  151. * Page callback to view the advanced help topic index.
  152. *
  153. * @param string $module
  154. * @return array
  155. */
  156. function advanced_help_index_page($module = '') {
  157. $topics = advanced_help_get_topics();
  158. $settings = advanced_help_get_settings();
  159. $output = array();
  160. // Print a search widget.
  161. $output['advanced_help_search'] = module_exists('search')
  162. ? drupal_get_form('advanced_help_search_form')
  163. : t('Enable the search module to search help.');
  164. $breadcrumb = array();
  165. if ($module) {
  166. if (empty($topics[$module])) {
  167. return drupal_not_found();
  168. }
  169. advanced_help_get_topic_hierarchy($topics);
  170. $items = advanced_help_get_tree($topics, $topics[$module]['']['children']);
  171. $breadcrumb[] = advanced_help_l(t('Help'), 'admin/advanced_help');
  172. drupal_set_title(t('@module help index', array('@module' => advanced_help_get_module_name($module))));
  173. $output['items-module'] = array(
  174. '#theme' => 'item_list',
  175. '#items' => $items,
  176. );
  177. }
  178. else {
  179. // Print a module index.
  180. $modules = array();
  181. $result = db_query('SELECT * FROM {system}');
  182. foreach ($result as $info) {
  183. $module_info = unserialize($info->info);
  184. $modules[$info->name] = $module_info['name'];
  185. }
  186. asort($modules);
  187. $items = array();
  188. foreach ($modules as $module => $module_name) {
  189. if (!empty($topics[$module]) && empty($settings[$module]['hide'])) {
  190. if (isset($settings[$module]['index name'])) {
  191. $name = $settings[$module]['index name'];
  192. }
  193. elseif (isset($settings[$module]['name'])) {
  194. $name = $settings[$module]['name'];
  195. }
  196. else {
  197. $name = t($module_name);
  198. }
  199. $items[] = advanced_help_l($name, "admin/advanced_help/$module");
  200. }
  201. }
  202. drupal_set_title(t('Module help index'));
  203. $output['items-nomodule'] = array(
  204. '#theme' => 'item_list',
  205. '#items' => $items,
  206. );
  207. }
  208. $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  209. if ($popup) {
  210. $GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
  211. module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
  212. drupal_set_breadcrumb(array_reverse($breadcrumb));
  213. print theme('advanced_help_popup', array('content' => $output));
  214. return;
  215. }
  216. $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
  217. drupal_set_breadcrumb($breadcrumb);
  218. return $output;
  219. }
  220. /**
  221. * Build a tree of advanced help topics.
  222. *
  223. * @param array $topics
  224. * @param array $topic_ids
  225. * @param int $max_depth
  226. * @param int $depth
  227. * @return array
  228. */
  229. function advanced_help_get_tree($topics, $topic_ids, $max_depth = -1, $depth = 0) {
  230. uasort($topic_ids, 'advanced_help_uasort');
  231. $items = array();
  232. foreach ($topic_ids as $info) {
  233. list($module, $topic) = $info;
  234. $item = advanced_help_l($topics[$module][$topic]['title'], "help/$module/$topic");
  235. if (!empty($topics[$module][$topic]['children']) && ($max_depth == -1 || $depth < $max_depth)) {
  236. $item .= theme('item_list', array('items' =>
  237. advanced_help_get_tree($topics, $topics[$module][$topic]['children'], $max_depth, $depth + 1)));
  238. }
  239. $items[] = $item;
  240. }
  241. return $items;
  242. }
  243. /**
  244. * Build a hierarchy for a single module's topics.
  245. */
  246. function advanced_help_get_topic_hierarchy(&$topics) {
  247. foreach ($topics as $module => $module_topics) {
  248. foreach ($module_topics as $topic => $info) {
  249. $parent_module = $module;
  250. // We have a blank topic that we don't want parented to itself.
  251. if (!$topic) {
  252. continue;
  253. }
  254. if (empty($info['parent'])) {
  255. $parent = '';
  256. }
  257. elseif (strpos($info['parent'], '%')) {
  258. list($parent_module, $parent) = explode('%', $info['parent']);
  259. if (empty($topics[$parent_module][$parent])) {
  260. // If it doesn't exist, top level.
  261. $parent = '';
  262. }
  263. }
  264. else {
  265. $parent = $info['parent'];
  266. if (empty($module_topics[$parent])) {
  267. // If it doesn't exist, top level.
  268. $parent = '';
  269. }
  270. }
  271. if (!isset($topics[$parent_module][$parent]['children'])) {
  272. $topics[$parent_module][$parent]['children'] = array();
  273. }
  274. $topics[$parent_module][$parent]['children'][] = array($module, $topic);
  275. $topics[$module][$topic]['_parent'] = array($parent_module, $parent);
  276. }
  277. }
  278. }
  279. /**
  280. * Implements hook_form_system_modules_alter().
  281. *
  282. * Add advanced help links to the modules page.
  283. */
  284. function advanced_help_form_system_modules_alter(&$form, &$form_state) {
  285. $advanced_help_modules = drupal_map_assoc(array_keys(advanced_help_get_topics()));
  286. foreach (element_children($form['modules']) as $group) {
  287. foreach (element_children($form['modules'][$group]) as $module) {
  288. if (isset($advanced_help_modules[$module])) {
  289. $form['modules'][$group][$module]['links']['help'] = array(
  290. '#type' => 'link',
  291. '#title' => t('Help'),
  292. '#href' => "admin/advanced_help/$module",
  293. '#options' => array('attributes' => array('class' => array('module-link', 'module-link-help'), 'title' => t('Help'))),
  294. );
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * Form builder callback to build the search form.
  301. *
  302. * Load search/search.pages so that its template preprocess functions are
  303. * visible and can be invoked.
  304. */
  305. function advanced_help_search_form($form, &$form_state, $keys = '') {
  306. module_load_include('inc', 'search', 'search.pages');
  307. $form = search_form($form, $form_state, advanced_help_url('admin/advanced_help'), $keys, 'advanced_help', t('Search help'));
  308. $form['basic']['inline']['submit']['#validate'] = array('search_form_validate');
  309. $form['basic']['inline']['submit']['#submit'] = array('advanced_help_search_form_submit');
  310. return $form;
  311. }
  312. /**
  313. * Process a search form submission.
  314. */
  315. function advanced_help_search_form_submit($form, &$form_state) {
  316. $keys = $form_state['values']['processed_keys'];
  317. if ($keys == '') {
  318. form_set_error('keys', t('Please enter some keywords.'));
  319. return;
  320. }
  321. $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  322. if ($popup) {
  323. $form_state['redirect'] = array('advanced_help/search/' . $keys, array('query' => array('popup' => 'true')));
  324. }
  325. else {
  326. $form_state['redirect'] = 'advanced_help/search/' . $keys;
  327. }
  328. }
  329. /**
  330. * Small helper function to get a module's proper name.
  331. *
  332. * @param $module string
  333. * @return string
  334. */
  335. function advanced_help_get_module_name($module) {
  336. $settings = advanced_help_get_settings();
  337. if (isset($settings[$module]['name'])) {
  338. $name = $settings[$module]['name'];
  339. }
  340. else {
  341. $info = db_query("SELECT s.info FROM {system} s WHERE s.name = :name",
  342. array(':name' => $module))
  343. ->fetchField();
  344. $info = unserialize($info);
  345. $name = t($info['name']);
  346. }
  347. return $name;
  348. }
  349. /**
  350. * Page callback to view a help topic.
  351. */
  352. function advanced_help_topic_page($module, $topic) {
  353. $info = advanced_help_get_topic($module, $topic);
  354. if (!$info) {
  355. return drupal_not_found();
  356. }
  357. $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  358. drupal_set_title($info['title']);
  359. // Set up breadcrumb.
  360. $breadcrumb = array();
  361. $parent = $info;
  362. $pmodule = $module;
  363. // Loop checker.
  364. $checked = array();
  365. while (!empty($parent['parent'])) {
  366. if (strpos($parent['parent'], '%')) {
  367. list($pmodule, $ptopic) = explode('%', $parent['parent']);
  368. }
  369. else {
  370. $ptopic = $parent['parent'];
  371. }
  372. if (!empty($checked[$pmodule][$ptopic])) {
  373. break;
  374. }
  375. $checked[$pmodule][$ptopic] = TRUE;
  376. $parent = advanced_help_get_topic($pmodule, $ptopic);
  377. if (!$parent) {
  378. break;
  379. }
  380. $breadcrumb[] = advanced_help_l($parent['title'], "help/$pmodule/$ptopic");
  381. }
  382. $breadcrumb[] = advanced_help_l(advanced_help_get_module_name($pmodule), "admin/advanced_help/$pmodule");
  383. $breadcrumb[] = advanced_help_l(t('Help'), "admin/advanced_help");
  384. $output = advanced_help_view_topic($module, $topic, $popup);
  385. if (empty($output)) {
  386. $output = t('Missing help topic.');
  387. }
  388. if ($popup) {
  389. $GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
  390. module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
  391. drupal_set_breadcrumb(array_reverse($breadcrumb));
  392. print theme('advanced_help_popup', array('content' => $output));
  393. return;
  394. }
  395. drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help.css');
  396. $breadcrumb[] = l(t('Home'), '');
  397. drupal_set_breadcrumb(array_reverse($breadcrumb));
  398. return $output;
  399. }
  400. /**
  401. * Implements hook_permission().
  402. */
  403. function advanced_help_permission() {
  404. return array(
  405. 'view advanced help topic' => array('title' => t('View help topics')),
  406. 'view advanced help popup' => array('title' => t('View help popups')),
  407. 'view advanced help index' => array('title' => t('View help index')),
  408. );
  409. }
  410. /**
  411. * Display a help icon with a link to view the topic in a popup.
  412. *
  413. * @param $variables
  414. * An associative array containing:
  415. * - module: The module that owns this help topic.
  416. * - topic: The identifier for the topic
  417. * - type
  418. * - 'icon' to display the question mark icon
  419. * - 'title' to display the topic's title
  420. * - any other text to display the text. Be sure to t() it!
  421. */
  422. function theme_advanced_help_topic($variables) {
  423. $module = $variables['module'];
  424. $topic = $variables['topic'];
  425. $type = $variables['type'];
  426. $info = advanced_help_get_topic($module, $topic);
  427. if (!$info) {
  428. return;
  429. }
  430. switch ($type) {
  431. case 'icon':
  432. $text = '<span>' . t('Help') . '</span>';
  433. $class = 'advanced-help-link';
  434. break;
  435. case 'title':
  436. $text = $info['title'];
  437. $class = 'advanced-help-title';
  438. break;
  439. default:
  440. $class = 'advanced-help-title';
  441. $text = $type;
  442. break;
  443. }
  444. if (user_access('view advanced help popup')) {
  445. drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help-icon.css');
  446. return l($text, "help/$module/$topic", array(
  447. 'attributes' => array(
  448. 'class' => $class,
  449. 'onclick' => "var w=window.open(this.href, 'advanced_help_window', 'width=" . $info['popup width'] . ",height=" . $info['popup height'] . ",scrollbars,resizable'); w.focus(); return false;",
  450. 'title' => $info['title']
  451. ),
  452. 'query' => array('popup' => TRUE),
  453. 'html' => TRUE)
  454. );
  455. }
  456. else {
  457. return l($text, "help/$module/$topic", array(
  458. 'attributes' => array(
  459. 'class' => $class,
  460. 'title' => $info['title']
  461. ),
  462. 'html' => TRUE)
  463. );
  464. }
  465. }
  466. /**
  467. * Load and render a help topic.
  468. */
  469. function advanced_help_get_topic_filename($module, $topic) {
  470. $info = advanced_help_get_topic_file_info($module, $topic);
  471. if ($info) {
  472. return "./$info[path]/$info[file]";
  473. }
  474. }
  475. /**
  476. * Load and render a help topic.
  477. */
  478. function advanced_help_get_topic_file_info($module, $topic) {
  479. global $language;
  480. $info = advanced_help_get_topic($module, $topic);
  481. if (empty($info)) {
  482. return;
  483. }
  484. // Search paths:
  485. $paths = array(
  486. path_to_theme() . '/help', // Allow theme override.
  487. drupal_get_path('module', $module) . "/translations/help/$language->language", // Translations.
  488. $info['path'], // In same directory as .inc file.
  489. );
  490. foreach ($paths as $path) {
  491. if (file_exists("./$path/$info[file]")) {
  492. return array('path' => $path, 'file' => $info['file']);
  493. }
  494. }
  495. }
  496. /**
  497. * Load and render a help topic.
  498. *
  499. * @param string $module
  500. * @param string $topic
  501. * @param boolean $popup
  502. * @return array
  503. */
  504. function advanced_help_view_topic($module, $topic, $popup = FALSE) {
  505. $file_info = advanced_help_get_topic_file_info($module, $topic);
  506. if ($file_info) {
  507. $info = advanced_help_get_topic($module, $topic);
  508. $file = "./$file_info[path]/$file_info[file]";
  509. $output = file_get_contents($file);
  510. if (isset($info['readme file']) && $info['readme file']) {
  511. // Readme files are treated as plain text: filter accordingly.
  512. $output = check_plain($output);
  513. }
  514. // Make some exchanges. The strtr is because url() translates $ into %24
  515. // but we need to change it back for the regex replacement.
  516. // Change 'topic:' to the URL for another help topic.
  517. if ($popup) {
  518. $output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1', array('query' => array('popup' => 'true'))), array('%24' => '$')) . '"', $output);
  519. $output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1', array('query' => array('popup' => 'true'))), array('%24' => '$')) . '"', $output);
  520. $output = preg_replace('/&topic:([^"]+)&/', strtr(url('help/$1', array('query' => array('popup' => 'true'))), array('%24' => '$')) , $output);
  521. }
  522. else {
  523. $output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1'), array('%24' => '$')) . '"', $output);
  524. $output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1'), array('%24' => '$')) . '"', $output);
  525. $output = preg_replace('/&topic:([^"]+)&/', strtr(url('help/$1'), array('%24' => '$')), $output);
  526. }
  527. global $base_path;
  528. // Change 'path:' to the URL to the base help directory.
  529. $output = preg_replace('/href="path:([^"]+)"/', 'href="' . $base_path . $info['path'] . '/$1"', $output);
  530. $output = preg_replace('/src="path:([^"]+)"/', 'src="' . $base_path . $info['path'] . '/$1"', $output);
  531. $output = str_replace('&path&', $base_path . $info['path'] . '/', $output);
  532. // Change 'trans_path:' to the URL to the actual help directory.
  533. $output = preg_replace('/href="trans_path:([^"]+)"/', 'href="' . $base_path . $file_info['path'] . '/$1"', $output);
  534. $output = preg_replace('/src="trans_path:([^"]+)"/', 'src="' . $base_path . $file_info['path'] . '/$1"', $output);
  535. $output = str_replace('&trans_path&', $base_path . $file_info['path'] . '/', $output);
  536. // Change 'base_url:' to the URL to the site.
  537. $output = preg_replace('/href="base_url:([^"]+)"/', 'href="' . strtr(url('$1'), array('%24' => '$')) . '"', $output);
  538. $output = preg_replace('/src="base_url:([^"]+)"/', 'src="' . strtr(url('$1'), array('%24' => '$')) . '"', $output);
  539. $output = preg_replace('/&base_url&([^"]+)"/', strtr(url('$1'), array('%24' => '$')) . '"', $output);
  540. // Run the line break filter if requested
  541. if (!empty($info['line break'])) {
  542. // Remove the header since it adds an extra <br /> to the filter.
  543. $output = preg_replace('/^<!--[^\n]*-->\n/', '', $output);
  544. $output = _filter_autop($output);
  545. }
  546. if (!empty($info['navigation'])) {
  547. $topics = advanced_help_get_topics();
  548. advanced_help_get_topic_hierarchy($topics);
  549. if (!empty($topics[$module][$topic]['children'])) {
  550. $items = advanced_help_get_tree($topics, $topics[$module][$topic]['children']);
  551. $output .= theme('item_list', array('items' => $items));
  552. }
  553. list($parent_module, $parent_topic) = $topics[$module][$topic]['_parent'];
  554. if ($parent_topic) {
  555. $parent = $topics[$module][$topic]['_parent'];
  556. $up = "help/$parent[0]/$parent[1]";
  557. }
  558. else {
  559. $up = "admin/advanced_help/$module";
  560. }
  561. $siblings = $topics[$parent_module][$parent_topic]['children'];
  562. uasort($siblings, 'advanced_help_uasort');
  563. $prev = $next = NULL;
  564. $found = FALSE;
  565. foreach ($siblings as $sibling) {
  566. list($sibling_module, $sibling_topic) = $sibling;
  567. if ($found) {
  568. $next = $sibling;
  569. break;
  570. }
  571. if ($sibling_module == $module && $sibling_topic == $topic) {
  572. $found = TRUE;
  573. continue;
  574. }
  575. $prev = $sibling;
  576. }
  577. if ($prev || $up || $next) {
  578. $navigation = '<div class="help-navigation clear-block">';
  579. if ($prev) {
  580. $navigation .= advanced_help_l('<< ' . $topics[$prev[0]][$prev[1]]['title'], "help/$prev[0]/$prev[1]", array('attributes' => array('class' => 'help-left')));
  581. }
  582. if ($up) {
  583. $navigation .= advanced_help_l(t('Up'), $up, array('attributes' => array('class' => $prev ? 'help-up' : 'help-up-noleft')));
  584. }
  585. if ($next) {
  586. $navigation .= advanced_help_l($topics[$next[0]][$next[1]]['title'] . ' >>', "help/$next[0]/$next[1]", array('attributes' => array('class' => 'help-right')));
  587. }
  588. $navigation .= '</div>';
  589. $output .= $navigation;
  590. }
  591. }
  592. if (!empty($info['css'])) {
  593. drupal_add_css($info['path'] . '/' . $info['css']);
  594. }
  595. $output = '<div class="advanced-help-topic">' . $output . '</div>';
  596. drupal_alter('advanced_help_topic', $output, $popup);
  597. return $output;
  598. }
  599. }
  600. /**
  601. * Get the information for a single help topic.
  602. */
  603. function advanced_help_get_topic($module, $topic) {
  604. $topics = advanced_help_get_topics();
  605. if (!empty($topics[$module][$topic])) {
  606. return $topics[$module][$topic];
  607. }
  608. }
  609. /**
  610. * Search the system for all available help topics.
  611. */
  612. function advanced_help_get_topics() {
  613. $cache = _advanced_help_parse_ini();
  614. return $cache['topics'];
  615. }
  616. function advanced_help_get_settings() {
  617. $cache = _advanced_help_parse_ini();
  618. return $cache['settings'];
  619. }
  620. function _advanced_help_parse_ini() {
  621. static $cache = NULL;
  622. if (!isset($cache)) {
  623. $cache = array('topics' => array(), 'settings' => array());
  624. $help_path = drupal_get_path('module', 'advanced_help') . '/modules';
  625. foreach (module_list() as $module) {
  626. $module_path = drupal_get_path('module', $module);
  627. $info = array();
  628. if (file_exists("$module_path/help/$module.help.ini")) {
  629. $path = "$module_path/help";
  630. $info = parse_ini_file("./$module_path/help/$module.help.ini", TRUE);
  631. }
  632. elseif (file_exists("$help_path/$module/$module.help.ini")) {
  633. $path = "$help_path/$module";
  634. $info = parse_ini_file("./$help_path/$module/$module.help.ini", TRUE);
  635. }
  636. elseif (!file_exists("$module_path/help")) {
  637. // Look for one or more README files.
  638. $files = file_scan_directory("./$module_path",
  639. '/^(README|readme).*\.(txt|TXT)$/', array('.', '..', 'CVS'),
  640. 0, FALSE);
  641. $path = "./$module_path";
  642. foreach ($files as $name => $fileinfo) {
  643. $info[$fileinfo->filename] = array(
  644. 'line break' => TRUE,
  645. 'readme file' => TRUE,
  646. 'file' => $fileinfo->filename,
  647. 'title' => $fileinfo->name,
  648. );
  649. }
  650. }
  651. if (!empty($info)) {
  652. // Get translated titles:
  653. global $language;
  654. $translation = array();
  655. if (file_exists("$module_path/translations/help/$language->language/$module.help.ini")) {
  656. $translation = parse_ini_file("$module_path/translations/help/$language->language/$module.help.ini", TRUE);
  657. }
  658. $cache['settings'][$module] = array();
  659. if (!empty($info['advanced help settings'])) {
  660. $cache['settings'][$module] = $info['advanced help settings'];
  661. unset($info['advanced help settings']);
  662. // Check translated strings for translatable global settings.
  663. if (isset($translation['advanced help settings']['name'])) {
  664. $cache['settings']['name'] = $translation['advanced help settings']['name'];
  665. }
  666. if (isset($translation['advanced help settings']['index name'])) {
  667. $cache['settings']['index name'] = $translation['advanced help settings']['index name'];
  668. }
  669. }
  670. foreach ($info as $name => $topic) {
  671. // Each topic should have a name, a title, a file and of course the path.
  672. $file = !empty($topic['file']) ? $topic['file'] : $name;
  673. $cache['topics'][$module][$name] = array(
  674. 'name' => $name,
  675. 'title' => !empty($translation[$name]['title']) ? $translation[$name]['title'] : $topic['title'],
  676. 'weight' => isset($topic['weight']) ? $topic['weight'] : 0,
  677. 'parent' => isset($topic['parent']) ? $topic['parent'] : 0,
  678. 'popup width' => isset($topic['popup width']) ? $topic['popup width'] : 500,
  679. 'popup height' => isset($topic['popup height']) ? $topic['popup height'] : 500,
  680. 'file' => isset($topic['readme file']) ? $file : $file . '.html', // require extension
  681. 'path' => $path, // not in .ini file
  682. 'line break' => isset($topic['line break']) ? $topic['line break'] : (isset($cache['settings'][$module]['line break']) ? $cache['settings'][$module]['line break'] : FALSE),
  683. 'navigation' => isset($topic['navigation']) ? $topic['navigation'] : (isset($cache['settings'][$module]['navigation']) ? $cache['settings'][$module]['navigation'] : TRUE),
  684. 'css' => isset($topic['css']) ? $topic['css'] : (isset($cache['settings'][$module]['css']) ? $cache['settings'][$module]['css'] : NULL),
  685. 'readme file' => isset($topic['readme file']) ? $topic['readme file'] : FALSE,
  686. );
  687. }
  688. }
  689. }
  690. drupal_alter('advanced_help_topic_info', $cache);
  691. }
  692. return $cache;
  693. }
  694. /**
  695. * Implements hook_search_info().
  696. *
  697. * @return array
  698. */
  699. function advanced_help_search_info() {
  700. return array(
  701. 'title' => t('Help'),
  702. 'path' => 'advanced_help',
  703. );
  704. }
  705. /**
  706. * Implements hook_search_execute().
  707. */
  708. function advanced_help_search_execute($keys = NULL) {
  709. $topics = advanced_help_get_topics();
  710. $query = db_select('search_index', 'i', array('target' => 'slave'))
  711. ->extend('SearchQuery')
  712. ->extend('PagerDefault');
  713. $query->join('advanced_help_index', 'ahi', 'i.sid = ahi.sid');
  714. $query->searchExpression($keys, 'help');
  715. // Only continue if the first pass query matches.
  716. if (!$query->executeFirstPass()) {
  717. return array();
  718. }
  719. $results = array();
  720. $find = $query->execute();
  721. foreach ($find as $item) {
  722. $sids[] = $item->sid;
  723. }
  724. $query = db_select('advanced_help_index', 'ahi');
  725. $result = $query
  726. ->fields('ahi')
  727. ->condition('sid', $sids, 'IN')
  728. ->execute();
  729. foreach ($result as $sid) {
  730. // Guard against removed help topics that are still indexed.
  731. if (empty($topics[$sid->module][$sid->topic])) {
  732. continue;
  733. }
  734. $info = $topics[$sid->module][$sid->topic];
  735. $text = advanced_help_view_topic($sid->module, $sid->topic);
  736. $results[] = array(
  737. 'link' => advanced_help_url("help/$sid->module/$sid->topic"),
  738. 'title' => $info['title'],
  739. 'snippet' => search_excerpt($keys, $text),
  740. );
  741. }
  742. return $results;
  743. }
  744. /**
  745. * Implements hook_search_reset().
  746. */
  747. function advanced_help_search_reset() {
  748. variable_del('advanced_help_last_cron');
  749. }
  750. /**
  751. * Implements hook_search_status().
  752. */
  753. function advanced_help_search_status() {
  754. $topics = advanced_help_get_topics();
  755. $total = 0;
  756. foreach ($topics as $module => $module_topics) {
  757. foreach ($module_topics as $topic => $info) {
  758. $file = advanced_help_get_topic_filename($module, $topic);
  759. if ($file) {
  760. $total++;
  761. }
  762. }
  763. }
  764. $last_cron = variable_get('advanced_help_last_cron', array('time' => 0));
  765. $indexed = 0;
  766. if ($last_cron['time'] != 0) {
  767. $indexed = db_query("SELECT COUNT(*) FROM {search_dataset} sd WHERE sd.type = 'help' AND sd.sid IS NOT NULL AND sd.reindex = 0")->fetchField();
  768. }
  769. return array('remaining' => $total - $indexed, 'total' => $total);
  770. }
  771. /**
  772. * Get or create an sid (search id) that correllates to each topic for
  773. * the search system.
  774. */
  775. function advanced_help_get_sids(&$topics) {
  776. global $language;
  777. $result = db_query("SELECT * FROM {advanced_help_index} WHERE language = :language",
  778. array(':language' => $language->language));
  779. foreach ($result as $sid) {
  780. if (empty($topics[$sid->module][$sid->topic])) {
  781. db_query("DELETE FROM {advanced_help_index} WHERE sid = :sid",
  782. array(':sid' => $sid->sid));
  783. }
  784. else {
  785. $topics[$sid->module][$sid->topic]['sid'] = $sid->sid;
  786. }
  787. }
  788. }
  789. /**
  790. * Implements hook_update_index().
  791. */
  792. function advanced_help_update_index() {
  793. global $language;
  794. // If we got interrupted by limit, this will contain the last module
  795. // and topic we looked at.
  796. $last = variable_get('advanced_help_last_cron', array('time' => 0));
  797. $limit = intval(variable_get('search_cron_limit', 100));
  798. $topics = advanced_help_get_topics();
  799. advanced_help_get_sids($topics);
  800. $count = 0;
  801. foreach ($topics as $module => $module_topics) {
  802. // Fast forward if necessary.
  803. if (!empty($last['module']) && $last['module'] != $module) {
  804. continue;
  805. }
  806. foreach ($module_topics as $topic => $info) {
  807. // Fast forward if necessary.
  808. if (!empty($last['topic']) && $last['topic'] != $topic) {
  809. continue;
  810. }
  811. // If we've been looking to catch up, and we have, reset so we
  812. // stop fast forwarding.
  813. if (!empty($last['module'])) {
  814. unset($last['topic']);
  815. unset($last['module']);
  816. }
  817. $file = advanced_help_get_topic_filename($module, $topic);
  818. if ($file && (empty($info['sid']) || filemtime($file) > $last['time'])) {
  819. if (empty($info['sid'])) {
  820. $info['sid'] = db_insert('advanced_help_index')
  821. ->fields(array(
  822. 'module' => $module,
  823. 'topic' => $topic,
  824. 'language' => $language->language,
  825. ))
  826. ->execute();
  827. }
  828. search_index($info['sid'], 'help', '<h1>' . $info['title'] . '</h1>' . file_get_contents($file));
  829. $count++;
  830. if ($count >= $limit) {
  831. $last['topic'] = $topic;
  832. $last['module'] = $module;
  833. // Don't change time if we stop.
  834. variable_set('advanced_help_last_cron', $last);
  835. return;
  836. }
  837. }
  838. }
  839. }
  840. variable_set('advanced_help_last_cron', array('time' => time()));
  841. }
  842. /**
  843. * Fill in a bunch of page variables for our specialized popup page.
  844. */
  845. function template_preprocess_advanced_help_popup(&$variables) {
  846. // Add favicon.
  847. if (theme_get_setting('toggle_favicon')) {
  848. drupal_add_html_head('<link rel="shortcut icon" href="' . check_url(theme_get_setting('favicon')) . '" type="image/x-icon" />');
  849. }
  850. global $theme;
  851. // Construct page title.
  852. if (drupal_get_title()) {
  853. $head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'Drupal'));
  854. }
  855. else {
  856. $head_title = array(variable_get('site_name', 'Drupal'));
  857. if (variable_get('site_slogan', '')) {
  858. $head_title[] = variable_get('site_slogan', '');
  859. }
  860. }
  861. drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help-popup.css');
  862. drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help.css');
  863. $variables['head_title'] = implode(' | ', $head_title);
  864. $variables['base_path'] = base_path();
  865. $variables['front_page'] = url();
  866. $variables['breadcrumb'] = theme('breadcrumb', array('breadcrumb' => drupal_get_breadcrumb()));
  867. $variables['feed_icons'] = drupal_get_feeds();
  868. $variables['head'] = drupal_get_html_head();
  869. $variables['language'] = $GLOBALS['language'];
  870. $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
  871. $variables['logo'] = theme_get_setting('logo');
  872. $variables['messages'] = theme('status_messages');
  873. $variables['site_name'] = (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : '');
  874. $variables['css'] = drupal_add_css();
  875. $css = drupal_add_css();
  876. // Remove theme css.
  877. foreach ($css as $media => $types) {
  878. if (isset($css[$media]['theme'])) {
  879. $css[$media]['theme'] = array();
  880. }
  881. }
  882. $variables['styles'] = drupal_get_css($css);
  883. $variables['scripts'] = drupal_get_js();
  884. $variables['title'] = drupal_get_title();
  885. // this function can be called either with a render array or an already rendered string
  886. if (is_array($variables['content'])) {
  887. $variables['content'] = drupal_render($variables['content']);
  888. }
  889. // Closure should be filled last.
  890. $variables['closure'] = theme('closure');
  891. }
  892. /**
  893. * Format a link but preserve popup identity.
  894. */
  895. function advanced_help_l($text, $dest, $options = array()) {
  896. $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  897. if ($popup) {
  898. if (empty($options['query'])) {
  899. $options['query'] = array();
  900. }
  901. if (is_array($options['query'])) {
  902. $options['query'] += array('popup' => TRUE);
  903. }
  904. else {
  905. $options['query'] += '&popup=TRUE';
  906. }
  907. }
  908. return l($text, $dest, $options);
  909. }
  910. /**
  911. * Format a URL but preserve popup identity.
  912. */
  913. function advanced_help_url($dest, $options = array()) {
  914. $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  915. if ($popup) {
  916. if (empty($options['query'])) {
  917. $options['query'] = array();
  918. }
  919. $options['query'] += array('popup' => TRUE);
  920. }
  921. return url($dest, $options);
  922. }