search_form.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. if (module_exists('search')) {
  3. /**
  4. * Plugins are described by creating a $plugin array which will be used
  5. * by the system that includes this file.
  6. */
  7. $plugin = array(
  8. 'single' => TRUE,
  9. 'title' => t('Advanced search form'),
  10. 'icon' => 'icon_search.png',
  11. 'description' => t('A search form with advanced options.'),
  12. 'required context' => new ctools_context_optional(t('Keywords'), 'string'),
  13. 'category' => t('Widgets'),
  14. 'defaults' => array(
  15. 'type' => 'node',
  16. 'form' => 'advanced',
  17. 'path_type' => 'default',
  18. 'path' => '',
  19. 'override_prompt' => FALSE,
  20. 'prompt' => '',
  21. ),
  22. );
  23. }
  24. /**
  25. * Render the custom content type.
  26. */
  27. function ctools_search_form_content_type_render($subtype, $conf, $panel_args, $context) {
  28. if (empty($context) || empty($context->data)) {
  29. $keys = '';
  30. }
  31. else {
  32. $keys = $context->data;
  33. }
  34. // Build the content type block.
  35. $block = new stdClass();
  36. $block->module = 'search';
  37. $block->delta = 'form';
  38. $block->title = '';
  39. switch ($conf['path_type']) {
  40. default:
  41. case 'default':
  42. $path = 'search/' . $conf['type'];
  43. break;
  44. case 'same':
  45. $path = $_GET['q'];
  46. $path = str_replace($keys, '', $path);
  47. break;
  48. case 'custom':
  49. $path = $conf['path'];
  50. break;
  51. }
  52. $prompt = $conf['override_prompt'] ? $conf['prompt'] : NULL;
  53. $form_state = array(
  54. 'build_info' => array(
  55. 'args' => array($path, $keys, $conf['type'], $prompt),
  56. ),
  57. );
  58. module_load_include('inc', 'search', 'search.pages');
  59. $block->content = drupal_build_form('search_form', $form_state);
  60. if ($conf['form'] == 'simple' && isset($block->content['advanced'])) {
  61. $block->content['advanced']['#access'] = FALSE;
  62. }
  63. return $block;
  64. }
  65. /**
  66. * Returns an edit form for custom type settings.
  67. */
  68. function ctools_search_form_content_type_edit_form($form, &$form_state) {
  69. $conf = $form_state['conf'];
  70. $types = array();
  71. foreach (search_get_info() as $module => $info) {
  72. $types[$module] = $info['title'];
  73. }
  74. $form['type'] = array(
  75. '#type' => 'select',
  76. '#title' => t('Search type'),
  77. '#options' => $types,
  78. '#default_value' => $conf['type'],
  79. );
  80. $form['form'] = array(
  81. '#type' => 'select',
  82. '#title' => t('Search form'),
  83. '#options' => array(
  84. 'simple' => t('Simple'),
  85. 'advanced' => t('Advanced'),
  86. ),
  87. '#default_value' => $conf['form'],
  88. '#description' => t('The advanced form may have additional options based upon the search type. For example the advanced content (node) search form will allow searching by node type and taxonomy term.'),
  89. );
  90. $form['path_type'] = array(
  91. '#prefix' => '<div class="container-inline">',
  92. '#type' => 'select',
  93. '#title' => t('Path'),
  94. '#options' => array(
  95. 'default' => t('Default'),
  96. 'same' => t('Same page'),
  97. 'custom' => t('Custom'),
  98. ),
  99. '#default_value' => $conf['path_type'],
  100. );
  101. $form['path'] = array(
  102. '#type' => 'textfield',
  103. '#default_value' => $conf['path'],
  104. '#dependency' => array('edit-path-type' => array('custom')),
  105. '#suffix' => '</div>',
  106. );
  107. $form['override_prompt'] = array(
  108. '#prefix' => '<div class="container-inline">',
  109. '#type' => 'checkbox',
  110. '#default_value' => $conf['override_prompt'],
  111. '#title' => t('Override default prompt'),
  112. );
  113. $form['prompt'] = array(
  114. '#type' => 'textfield',
  115. '#default_value' => $conf['prompt'],
  116. '#dependency' => array('edit-override-prompt' => array(1)),
  117. '#suffix' => '</div>',
  118. );
  119. return $form;
  120. }
  121. /**
  122. * Submit handler for search form.
  123. */
  124. function ctools_search_form_content_type_edit_form_submit($form, &$form_state) {
  125. // Copy everything from our defaults.
  126. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  127. $form_state['conf'][$key] = $form_state['values'][$key];
  128. }
  129. }
  130. /**
  131. * Returns the administrative title for a type.
  132. */
  133. function ctools_search_form_content_type_admin_title($subtype, $conf, $context) {
  134. $info = search_get_info();
  135. $type = isset($info[$conf['type']]['title']) ? $info[$conf['type']]['title'] : t('Missing/broken type');
  136. return t('@type search form', array('@type' => $type));
  137. }