advanced_help.module 26.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// $Id$
/**
 * @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
20
 * weight = the importance of the topic on the index page
21
22
23
24
 * 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
25
 * id. Links may be embedded as in the following example:
26
27
28
29
30
 *
 * @code
 * $output .= theme('advanced_help_topic', $module, $topic);
 * @endcode
 *
31
32
33
 * Link to other topics using <a href="topic:module/topic">. (Using
 * this format ensures the popup status remains consistent for all
 * links.)
34
35
36
37
38
39
 */

/**
 * Implementation of hook_menu().
 */
function advanced_help_menu() {
40
  // View help topic index.
41
42
43
44
45
46
47
48
49
50
51
  $items['admin/advanced_help'] = array(
    'title' => module_exists('help') ? 'Advanced help' : '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'),
merlinofchaos's avatar
merlinofchaos committed
52
    'access arguments' => array('view advanced help index'),
53
54
  );

55
  // View help topic.
56
57
58
59
60
61
62
63
64
65
66
  $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;
}

/**
67
 * Implementation of hook_theme().
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 */
function advanced_help_theme() {
  $hooks['advanced_help_topic'] = array(
    'arguments' => array('module' => NULL, 'topic' => NULL),
  );

  $hooks['advanced_help_popup'] = array(
    'arguments' => array('content' => NULL),
    'template' => 'advanced-help-popup',
  );

  return $hooks;
}

function advanced_help_uasort($id_a, $id_b) {
  $topics = advanced_help_get_topics();
84
85
86
87
  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];
88
89
90
91
92
93
94
95
96
97
98
99
100
101

  $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;
}

/**
102
 * Page callback for advanced help search.
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
 */
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', drupal_help_arg()));
      }
    }

    // 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) {
139
    $GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
140
    module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
141
142
143
144
145
146
    drupal_set_breadcrumb(array_reverse($breadcrumb));
    print theme('advanced_help_popup', $output);
    return;
  }

  $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb));
147
  drupal_set_breadcrumb($breadcrumb);
148
149
150
151
152
153
154
155
156
  return $output;
}

/**
 * Page callback to view the advanced help topic index.
 */
function advanced_help_index_page($module = '') {
  $topics = advanced_help_get_topics();

157
  // Print a search widget.
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  $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();
    }

172
173
    advanced_help_get_topic_hierarchy($topics);
    $items = advanced_help_get_tree($topics, $topics[$module]['']['children']);
174
175
176
177
178
179
180

    $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 {
181
    // Print a module index.
182
183
184
185
186
187
188
189
190
191
192
193
    $modules = array();
    $result = db_query("SELECT * FROM {system}");
    while ($info = db_fetch_object($result)) {
      $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])) {
194
195
196
197
198
199
200
201
202
203
        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");
204
205
206
207
208
209
210
211
212
      }
    }

    drupal_set_title(t('Module help index'));
    $output .= theme('item_list', $items);
  }

  $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  if ($popup) {
213
    $GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
214
    module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
215
216
217
218
219
220
221
222
223
224
225
    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;
}

226
227
228
229
/**
 * Build a tree of advanced help topics.
 */
function advanced_help_get_tree($topics, $topic_ids, $max_depth = -1, $depth = 0) {
230
231
  uasort($topic_ids, 'advanced_help_uasort');
  $items = array();
232
233
  foreach ($topic_ids as $info) {
    list($module, $topic) = $info;
234
    $item = advanced_help_l($topics[$module][$topic]['title'], "help/$module/$topic");
235
236
    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));
237
238
239
240
241
242
243
    }

    $items[] = $item;
  }

  return $items;
}
244

245
/**
246
 * Build a hierarchy for a single module's topics.
247
248
 */
function advanced_help_get_topic_hierarchy(&$topics) {
249
250
251
252
253
254
255
256
  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;
      }
257

258
      if (empty($info['parent'])) {
259
260
        $parent = '';
      }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
      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 = '';
        }
      }
275

276
277
278
279
280
      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);
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
    }
  }
}

/**
 * Form builder callback to build the search form.
 */
function advanced_help_search_form(&$form_state, $keys = '') {
  $form = search_form($form_state, advanced_help_url('admin/advanced_help'), $keys, 'advanced_help', t('Search help'));
  require_once './' . drupal_get_path('module', 'search') . '/search.pages.inc';

  $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, 'popup=true');
  }
  else {
    $form_state['redirect'] = 'advanced_help/search/' . $keys;
  }
}


/**
 * Small helper function to get a module's proper name.
 */
function advanced_help_get_module_name($module) {
323
324
325
326
327
328
329
330
331
332
  $settings = advanced_help_get_settings();
  if (isset($settings[$module]['name'])) {
    $name = $settings[$module]['name'];
  }
  else {
    $info = db_fetch_object(db_query("SELECT * FROM {system} WHERE name = '%s'", $module));
    $info = unserialize($info->info);
    $name = t($info['name']);
  }
  return $name;
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
}

/**
 * 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']);

348
  // Set up breadcrumb.
349
350
351
352
353
  $breadcrumb = array();

  $parent = $info;
  $pmodule = $module;

354
  // Loop checker.
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  $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) {
386
    $GLOBALS['devel_shutdown'] = FALSE; // Prevent devel module from spewing.
387
    module_invoke('admin_menu', 'suppress'); // Suppress admin_menu.
388
389
390
391
392
    drupal_set_breadcrumb(array_reverse($breadcrumb));
    print theme('advanced_help_popup', $output);
    return;
  }

393
  drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help.css');
394
395
396
397
398
399
400
401
402
403
404
405
406
  $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');
}

/**
407
 * Display a help icon with a link to view the topic in a popup.
408
409
410
411
412
413
414
415
416
 *
 * @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!
417
 */
418
function theme_advanced_help_topic($module, $topic, $type = 'icon') {
419
420
421
422
423
  $info = advanced_help_get_topic($module, $topic);
  if (!$info) {
    return;
  }

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  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;
  }

439
  if (user_access('view advanced help popup')) {
merlinofchaos's avatar
merlinofchaos committed
440
    drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help-icon.css');
441
    return l($text, "help/$module/$topic", array(
442
      'attributes' => array(
443
        'class' => $class,
444
445
446
447
448
449
450
451
        'onclick' => "window.open(this.href, 'advanced_help_window', 'width=500,height=500,scrollbars,resizable'); return false;",
        'title' => $info['title']
      ),
      'query' => array('popup' => TRUE),
      'html' => TRUE)
    );
  }
  else {
452
    return l($text, "help/$module/$topic", array(
453
      'attributes' => array(
454
        'class' => $class,
455
456
457
458
459
460
461
462
        'title' => $info['title']
      ),
      'html' => TRUE)
    );
  }
}

/**
463
 * Load and render a help topic.
464
465
466
467
468
469
470
471
472
473
 */
function advanced_help_get_topic_filename($module, $topic) {
  init_theme();
  global $language;

  $info = advanced_help_get_topic($module, $topic);
  if (empty($info)) {
    return;
  }

474
  // Search paths:
475
  $paths = array(
476
    path_to_theme() . '/help', // Allow theme override.
477
    drupal_get_path('module', $module) . "/translations/help/$language->language", // Translations.
478
    $info['path'], // In same directory as .inc file.
479
480
481
482
483
484
485
486
487
488
  );

  foreach ($paths as $path) {
    if (file_exists("./$path/$info[file]")) {
      return "./$path/$info[file]";
    }
  }
}

/**
489
 * Load and render a help topic.
490
491
492
493
494
495
496
 */
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);
497
498
    // Make some exchanges. The strtr is because url() translates $ into %24
    // but we need to change it back for the regex replacement.
merlinofchaos's avatar
merlinofchaos committed
499

500
501
502
503
504
505
506
507
    // 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);
    }

merlinofchaos's avatar
merlinofchaos committed
508
    // Change 'topic:' to the URL for another help topic.
509
    if ($popup) {
510
511
      $output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1', array('query' => 'popup=true')), array('%24' => '$')) . '"', $output);
      $output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1', array('query' => 'popup=true')), array('%24' => '$')) . '"', $output);
512
513
    }
    else {
514
515
      $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);
516
517
    }

518
519
    global $base_path;

merlinofchaos's avatar
merlinofchaos committed
520
    // Change 'path:' to the URL to the base help directory.
521
522
    $output = preg_replace('/href="path:([^"]+)"/', 'href="' . $base_path . $info['path'] . '/$1"', $output);
    $output = preg_replace('/src="path:([^"]+)"/', 'src="' . $base_path . $info['path'] . '/$1"', $output);
523

merlinofchaos's avatar
merlinofchaos committed
524
    // Change 'base_url:' to the URL to the site.
525
526
    $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);
merlinofchaos's avatar
merlinofchaos committed
527

528
529
530
531
532
533
534
535
536
537
    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'];
538
      uasort($siblings, 'advanced_help_uasort');
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
      $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;
      }
    }

575
576
577
578
    if (!empty($info['css'])) {
      drupal_add_css($info['path'] . '/' . $info['css']);
    }

579
    return '<div class="advanced-help-topic">' . $output . '</div>';
580
581
582
583
584
585
586
587
588
589
590
591
592
593
  }
}

/**
 * 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];
  }
}

/**
594
 * Search the system for all available help topics.
595
596
 */
function advanced_help_get_topics() {
597
598
599
600
601
602
603
604
605
606
  list ($topics, $settings) = _advanced_help_parse_ini();
  return $topics;
}

function advanced_help_get_settings() {
  list ($topic, $settings) = _advanced_help_parse_ini();
  return $settings;
}

function _advanced_help_parse_ini() {
607
  static $topics = NULL;
608
609
  static $settings = NULL;

610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  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)) {
625
626
        // Get translated titles:
        global $language;
627
628
        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);
629
        }
630
        $settings[$module] = array();
631
        if (!empty($info['advanced help settings'])) {
632
          $settings[$module] = $info['advanced help settings'];
633
634
635
          unset($info['advanced help settings']);
        }

636
        foreach ($info as $name => $topic) {
637
          // Each topic should have a name, a title, a file and of course the path.
638
          $file = !empty($topic['file']) ? $topic['file'] : $name;
639
640
          $topics[$module][$name] = array(
            'name' => $name,
641
            'title' => !empty($translation[$name]['title']) ? $translation[$name]['title'] : $topic['title'],
642
643
            'weight' => isset($topic['weight']) ? $topic['weight'] : 0,
            'parent' => isset($topic['parent']) ? $topic['parent'] : 0,
644
            'file' => $file . '.html', // require extension
645
            'path' => $path, // not in .ini file
646
647
648
            'line break' => isset($topic['line break']) ? $topic['line break'] : (isset($settings[$module]['line break']) ? $settings[$module]['line break'] : FALSE),
            'navigation' => isset($topic['navigation']) ? $topic['navigation'] : (isset($settings[$module]['navigation']) ? $settings[$module]['navigation'] : TRUE),
            'css' => isset($topic['css']) ? $topic['css'] : (isset($settings[$module]['css']) ? $settings[$module]['css'] : NULL),
649
650
651
652
653
          );
        }
      }
    }
  }
654
  return array($topics, $settings);
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
}

/**
 * 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)) {
683
        // Guard against removed help topics that are still indexed.
684
685
686
687
688
689
690
691
692
693
694
695
696
697
        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;
  }
}

/**
698
 * Get or create an sid (search id) that correllates to each topic for
699
700
701
 * the search system.
 */
function advanced_help_get_sids(&$topics) {
702
703
  global $language;
  $result = db_query("SELECT * FROM {advanced_help_index} WHERE language = '%s'", $language->language);
704
705
706
707
708
709
710
711
712
713
714
715
716
717
  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() {
718
719
  global $language;

720
721
722
723
724
725
726
727
728
729
  // 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) {
730
    // Fast forward if necessary.
731
732
733
734
735
    if (!empty($last['module']) && $last['module'] != $module) {
      continue;
    }

    foreach ($module_topics as $topic => $info) {
736
      // Fast forward if necessary.
737
738
739
740
      if (!empty($last['topic']) && $last['topic'] != $topic) {
        continue;
      }

741
      // If we've been looking to catch up, and we have, reset so we
742
743
744
745
746
747
748
749
750
      // 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'])) {
751
          db_query("INSERT INTO {advanced_help_index} (module, topic, language) VALUES ('%s', '%s', '%s')", $module, $topic, $language->language);
752
753
754
          $info['sid'] = db_last_insert_id('advanced_help_index', 'sid');
        }

755
        search_index($info['sid'], 'help', '<h1>' . $info['title'] . '</h1>' . file_get_contents($file));
756
757
758
759
        $count++;
        if ($count >= $limit) {
          $last['topic'] = $topic;
          $last['module'] = $module;
760
          // Don't change time if we stop.
761
762
763
764
765
766
767
768
769
770
          variable_set('advanced_help_last_cron', $last);
          return;
        }
      }
    }
  }
  variable_set('advanced_help_last_cron', array('time' => time()));
}

/**
771
 * Fill in a bunch of page variables for our specialized popup page.
772
773
 */
function template_preprocess_advanced_help_popup(&$variables) {
774
  // Add favicon.
775
776
777
778
779
  if (theme_get_setting('toggle_favicon')) {
    drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />');
  }

  global $theme;
780
  // Construct page title.
781
782
783
784
785
786
787
788
789
790
  if (drupal_get_title()) {
    $head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'Drupal'));
  }
  else {
    $head_title = array(variable_get('site_name', 'Drupal'));
    if (variable_get('site_slogan', '')) {
      $head_title[] = variable_get('site_slogan', '');
    }
  }

791
  drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help-popup.css');
merlinofchaos's avatar
merlinofchaos committed
792
  drupal_add_css(drupal_get_path('module', 'advanced_help') . '/help.css');
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843

  $variables['head_title']        = implode(' | ', $head_title);
  $variables['base_path']         = base_path();
  $variables['front_page']        = url();
  $variables['breadcrumb']        = theme('breadcrumb', drupal_get_breadcrumb());
  $variables['feed_icons']        = drupal_get_feeds();
  $variables['head']              = drupal_get_html_head();
  $variables['language']          = $GLOBALS['language'];
  $variables['language']->dir     = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
  $variables['logo']              = theme_get_setting('logo');
  $variables['messages']          = theme('status_messages');
  $variables['site_name']         = (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : '');
  $variables['css']               = drupal_add_css();
  $css = drupal_add_css();

  // Remove theme css.
  foreach ($css as $media => $types) {
    if (isset($css[$media]['theme'])) {
      $css[$media]['theme'] = array();
    }
  }

  $variables['styles']            = drupal_get_css($css);
  $variables['scripts']           = drupal_get_js();
  $variables['title']             = drupal_get_title();
  // Closure should be filled last.
  $variables['closure']           = theme('closure');
}

/**
 * Format a link but preserve popup identity.
 */
function advanced_help_l($text, $dest, $options = array()) {
  $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  if ($popup) {
    if (empty($options['query'])) {
      $options['query'] = array();
    }

    if (is_array($options['query'])) {
      $options['query'] += array('popup' => TRUE);
    }
    else {
      $options['query'] += '&popup=TRUE';
    }
  }

  return l($text, $dest, $options);
}

/**
844
 * Format a URL but preserve popup identity.
845
846
847
848
849
850
851
852
853
854
855
856
857
 */
function advanced_help_url($dest, $options = array()) {
  $popup = !empty($_GET['popup']) && user_access('view advanced help popup');
  if ($popup) {
    if (empty($options['query'])) {
      $options['query'] = array();
    }

    $options['query'] += array('popup' => TRUE);
  }

  return url($dest, $options);
}