123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- <?php
- /**
- * @file
- * Admin page callbacks for the Search pages module.
- */
- /**
- * Displays an overview of all defined search pages.
- */
- function search_api_page_admin_overview() {
- $base_path = drupal_get_path('module', 'search_api') . '/';
- drupal_add_css($base_path . 'search_api.admin.css');
- $header = array(t('Status'), t('Configuration'), t('Name'), t('Path'), t('Index'), t('Operations'));
- $rows = array();
- $t_enabled['data'] = array(
- '#theme' => 'image',
- '#path' => $base_path . 'enabled.png',
- '#alt' => t('enabled'),
- '#title' => t('enabled'),
- );
- $t_enabled['class'] = array('search-api-status');
- $t_disabled['data'] = array(
- '#theme' => 'image',
- '#path' => $base_path . 'disabled.png',
- '#alt' => t('disabled'),
- '#title' => t('disabled'),
- );
- $t_disabled['class'] = array('search-api-status');
- $t_enable = t('enable');
- $t_disable = t('disable');
- $t_edit = t('edit');
- $t_delete = t('delete');
- $pre = 'admin/config/search/search_api/page/';
- $pre_index = 'admin/config/search/search_api/index/';
- $enable = '/enable';
- $disable = '/disable';
- $edit = '/edit';
- $delete = '/delete';
- foreach (search_api_page_load_multiple() as $page) {
- $url = $pre . $page->machine_name;
- $index = search_api_index_load($page->index_id);
- $rows[] = array(
- $page->enabled ? $t_enabled : $t_disabled,
- theme('entity_status', array('status' => $page->status)),
- l($page->name, $page->path),
- l($page->path, $page->path),
- l($index->name, $pre_index . $index->machine_name),
- l($t_edit, $url . $edit),
- );
- }
- return array(
- '#theme' => 'table',
- '#header' => $header,
- '#rows' => $rows,
- '#empty' => t('There are no search pages defined yet.'),
- );
- }
- /**
- * Displays a form for adding a search page.
- */
- function search_api_page_admin_add(array $form, array &$form_state) {
- $form = array();
- if (empty($form_state['step_one'])) {
- $indexes = search_api_index_load_multiple(FALSE);
- if (!$indexes) {
- drupal_set_message(t('There are no searches indexes which can be searched. Please <a href="@url">create an index</a> first.', array('@url' => url('admin/config/search/search_api/add_index'))), 'warning');
- return array();
- }
- $index_options = array();
- foreach ($indexes as $index) {
- if ($index->enabled) {
- $index_options[$index->machine_name] = $index->name;
- }
- }
- foreach ($indexes as $index) {
- if (!$index->enabled) {
- $index_options[$index->machine_name] = $index->name . ' (' . t('disabled') . ')';
- }
- }
- $form['name'] = array(
- '#type' => 'textfield',
- '#title' => t('Search name'),
- '#maxlength' => 50,
- '#required' => TRUE,
- );
- $form['machine_name'] = array(
- '#type' => 'machine_name',
- '#maxlength' => 51,
- '#machine_name' => array(
- 'exists' => 'search_api_index_load',
- ),
- );
- $form['index_id'] = array(
- '#type' => 'select',
- '#title' => t('Index'),
- '#description' => t('Select the index this page should search. This cannot be changed later.'),
- '#options' => $index_options,
- '#required' => TRUE,
- );
- $form['enabled'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enabled'),
- '#description' => t('This will only take effect if the selected index is also enabled.'),
- '#default_value' => TRUE,
- );
- $form['description'] = array(
- '#type' => 'textarea',
- '#title' => t('Search description'),
- );
- $form['path'] = array(
- '#type' => 'textfield',
- '#title' => t('Path'),
- '#description' => t('Set the path under which the search page will be accessible, when enabled.'),
- '#maxlength' => 50,
- '#required' => TRUE,
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Create page'),
- );
- return $form;
- }
- $index = search_api_index_load($form_state['step_one']['index_id']);
- if ($index->enabled) {
- $modes = array();
- foreach ($index->query()->parseModes() as $mode => $info) {
- $modes[$mode] = $info['name'];
- }
- }
- else {
- $modes = array();
- $modes['direct'] = t('Direct query');
- $modes['single'] = t('Single term');
- $modes['terms'] = t('Multiple terms');
- }
- $form['mode'] = array(
- '#type' => 'select',
- '#title' => t('Query type'),
- '#description' => t('Select how the query will be parsed.'),
- '#options' => $modes,
- '#default_value' => 'terms',
- );
- $fields = array();
- $index_fields = $index->getFields();
- foreach ($index->getFulltextFields() as $name) {
- $fields[$name] = $index_fields[$name]['name'];
- }
- if (count($fields) > 1) {
- $form['fields'] = array(
- '#type' => 'select',
- '#title' => t('Searched fields'),
- '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
- '#options' => $fields,
- '#size' => min(4, count($fields)),
- '#multiple' => TRUE,
- );
- }
- else {
- $form['fields'] = array(
- '#type' => 'value',
- '#value' => array(),
- );
- }
- $form['per_page'] = array(
- '#type' => 'select',
- '#title' => t('Results per page'),
- '#description' => t('Select how many items will be displayed on one page of the search result.'),
- '#options' => drupal_map_assoc(array(5, 10, 20, 30, 40, 50, 60, 80, 100)),
- '#default_value' => 10,
- );
- $form['get_per_page'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow GET override'),
- '#description' => t('Allow the „Results per page“ setting to be overridden from the URL, using the "per_page" GET parameter.<br />' .
- 'Example: http://example.com/search_results?per_page=7'),
- '#default_value' => TRUE,
- );
- $form['result_page_search_form'] = array(
- '#type' => 'checkbox',
- '#title' => t('Show search form on result page'),
- '#description' => t('Enable or disable the search form on the result page'),
- '#default_value' => TRUE,
- );
- $view_modes = array(
- 'search_api_page_result' => t('Themed as search results'),
- );
- // For entities, we also add all entity view modes.
- if ($entity_info = entity_get_info($index->item_type)) {
- foreach ($entity_info['view modes'] as $mode => $mode_info) {
- $view_modes[$mode] = $mode_info['label'];
- }
- }
- if (count($view_modes) > 1) {
- $form['view_mode'] = array(
- '#type' => 'select',
- '#title' => t('View mode'),
- '#options' => $view_modes,
- '#description' => t('Select how search results will be displayed.'),
- '#size' => 1,
- '#default_value' => 'search_api_page_result',
- );
- }
- else {
- $form['view_mode'] = array(
- '#type' => 'value',
- '#value' => key($view_modes),
- );
- }
- if (module_exists('search_api_spellcheck') && ($server = $index->server()) && $server->supportsFeature('search_api_spellcheck')) {
- $form['search_api_spellcheck'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enable spell check'),
- '#description' => t('Display "Did you mean … ?" above search results.'),
- '#default_value' => TRUE,
- );
- }
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Create page'),
- );
- return $form;
- }
- /**
- * Validation callback for search_api_page_admin_add().
- */
- function search_api_page_admin_add_validate(array $form, array &$form_state) {
- if (empty($form_state['step_one'])) {
- $form_state['values']['path'] = drupal_strtolower(trim($form_state['values']['path']));
- if (search_api_page_load_multiple(FALSE, array('path' => $form_state['values']['path']))) {
- form_set_error('path', t('The entered path is already in use. Please enter a unique path.'));
- }
- }
- }
- /**
- * Submit callback for search_api_page_admin_add().
- */
- function search_api_page_admin_add_submit(array $form, array &$form_state) {
- form_state_values_clean($form_state);
- if (empty($form_state['step_one'])) {
- $form_state['step_one'] = $form_state['values'];
- $form_state['rebuild'] = TRUE;
- return;
- }
- $values = $form_state['step_one'];
- $values['options'] = $form_state['values'];
- search_api_page_insert($values);
- drupal_set_message(t('The search page was successfully created.'));
- $form_state['redirect'] = 'admin/config/search/search_api/page';
- }
- /**
- * Displays a form for editing or deleting a search page.
- */
- function search_api_page_admin_edit(array $form, array &$form_state, Entity $page) {
- $index = search_api_index_load($page->index_id);
- $form_state['page'] = $page;
- $form['name'] = array(
- '#type' => 'textfield',
- '#title' => t('Search name'),
- '#maxlength' => 50,
- '#required' => TRUE,
- '#default_value' => $page->name,
- );
- $form['enabled'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enabled'),
- '#description' => t('This will only take effect if the selected index is also enabled.'),
- '#default_value' => $page->enabled,
- '#disabled' => !$index->enabled,
- );
- $form['description'] = array(
- '#type' => 'textarea',
- '#title' => t('Search description'),
- '#default_value' => $page->description,
- );
- $form['index'] = array(
- '#type' => 'item',
- '#title' => t('Index'),
- '#description' => l($index->name, 'admin/config/search/search_api/index/' . $index->machine_name),
- );
- $form['path'] = array(
- '#type' => 'textfield',
- '#title' => t('Path'),
- '#description' => t('Set the path under which the search page will be accessible, when enabled.'),
- '#maxlength' => 50,
- '#default_value' => $page->path,
- );
- if ($index->enabled) {
- $modes = array();
- foreach ($index->query()->parseModes() as $mode => $info) {
- $modes[$mode] = $info['name'];
- }
- }
- else {
- $modes = array();
- $modes['direct'] = array(
- 'name' => t('Direct query'),
- 'description' => t("Don't parse the query, just hand it to the search server unaltered. " .
- "Might fail if the query contains syntax errors in regard to the specific server's query syntax."),
- );
- $modes['single'] = array(
- 'name' => t('Single term'),
- 'description' => t('The query is interpreted as a single keyword, maybe containing spaces or special characters.'),
- );
- $modes['terms'] = array(
- 'name' => t('Multiple terms'),
- 'description' => t('The query is interpreted as multiple keywords seperated by spaces. ' .
- 'Keywords containing spaces may be "quoted". Quoted keywords must still be seperated by spaces.'),
- );
- }
- $form['options']['#tree'] = TRUE;
- $form['options']['mode'] = array(
- '#type' => 'select',
- '#title' => t('Query type'),
- '#description' => t('Select how the query will be parsed.'),
- '#options' => $modes,
- '#default_value' => $page->options['mode'],
- );
- $fields = array();
- $index_fields = $index->getFields();
- foreach ($index->getFulltextFields() as $name) {
- $fields[$name] = $index_fields[$name]['name'];
- }
- if (count($fields) > 1) {
- $form['options']['fields'] = array(
- '#type' => 'select',
- '#title' => t('Searched fields'),
- '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
- '#options' => $fields,
- '#size' => min(4, count($fields)),
- '#multiple' => TRUE,
- '#default_value' => $page->options['fields'],
- );
- }
- else {
- $form['options']['fields'] = array(
- '#type' => 'value',
- '#value' => array(),
- );
- }
- $form['options']['per_page'] = array(
- '#type' => 'select',
- '#title' => t('Results per page'),
- '#description' => t('Select how many items will be displayed on one page of the search result.'),
- '#options' => drupal_map_assoc(array(5, 10, 20, 30, 40, 50, 60, 80, 100)),
- '#default_value' => $page->options['per_page'],
- );
- $form['options']['result_page_search_form'] = array(
- '#type' => 'checkbox',
- '#title' => t('Show search form on result page'),
- '#description' => t('Enable or disable the search form on the result page'),
- '#default_value' => isset($page->options['result_page_search_form']) ? $page->options['result_page_search_form'] : TRUE,
- );
- $form['options']['get_per_page'] = array(
- '#type' => 'checkbox',
- '#title' => t('Allow GET override'),
- '#description' => t('Allow the „Results per page“ setting to be overridden from the URL, using the "per_page" GET parameter.<br />' .
- 'Example: <code>http://example.com/search_results?per_page=7</code>'),
- '#default_value' => !empty($page->options['get_per_page']),
- );
- $view_modes = array(
- 'search_api_page_result' => t('Themed as search results'),
- );
- // For entities, we also add all entity view modes.
- if ($entity_info = entity_get_info($index->item_type)) {
- foreach ($entity_info['view modes'] as $mode => $mode_info) {
- $view_modes[$mode] = $mode_info['label'];
- }
- }
- if (count($view_modes) > 1) {
- $form['options']['view_mode'] = array(
- '#type' => 'select',
- '#title' => t('View mode'),
- '#options' => $view_modes,
- '#description' => t('Select how search results will be displayed.'),
- '#size' => 1,
- '#default_value' => isset($page->options['view_mode']) ? $page->options['view_mode'] : 'search_api_page_result',
- );
- }
- else {
- $form['options']['view_mode'] = array(
- '#type' => 'value',
- '#value' => key($view_modes),
- );
- }
- if (module_exists('search_api_spellcheck') && ($server = $index->server()) && $server->supportsFeature('search_api_spellcheck')) {
- $form['options']['search_api_spellcheck'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enable spell check'),
- '#description' => t('Display "Did you mean … ?" above search results.'),
- '#default_value' => !empty($page->options['search_api_spellcheck']),
- );
- }
- $form['actions']['#type'] = 'actions';
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save changes'),
- );
- if ($page->hasStatus(ENTITY_OVERRIDDEN)) {
- $form['actions']['revert'] = array(
- '#type' => 'fieldset',
- '#title' => t('Revert search page'),
- '#description' => t('This will revert all settings on this search page back to the defaults. This action cannot be undone.'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- 'revert' => array(
- '#type' => 'submit',
- '#value' => t('Revert search page'),
- ),
- );
- }
- elseif ($page->hasStatus(ENTITY_CUSTOM)) {
- $form['actions']['delete'] = array(
- '#type' => 'fieldset',
- '#title' => t('Delete search page'),
- '#description' => t('This will delete the search page along with all of its settings.'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- 'delete' => array(
- '#type' => 'submit',
- '#value' => t('Delete search page'),
- ),
- );
- }
- return $form;
- }
- /**
- * Validation callback for search_api_page_admin_edit().
- */
- function search_api_page_admin_edit_validate(array $form, array &$form_state) {
- if ($form_state['values']['op'] == t('Save changes')) {
- $form_state['values']['path'] = drupal_strtolower(trim($form_state['values']['path']));
- $pages = search_api_page_load_multiple(FALSE, array('path' => $form_state['values']['path']));
- if (count($pages) > 1 || (($page = array_shift($pages)) && $page->machine_name != $form_state['page']->machine_name)) {
- form_set_error('path', t('The entered path is already in use. Please enter a unique path.'));
- }
- }
- }
- /**
- * Submit callback for search_api_page_admin_edit().
- */
- function search_api_page_admin_edit_submit(array $form, array &$form_state) {
- $op = $form_state['values']['op'];
- form_state_values_clean($form_state);
- $form_state['redirect'] = 'admin/config/search/search_api/page';
- if ($op == t('Delete search page') || $op == t('Revert search page')) {
- $form_state['page']->delete();
- if ($op == t('Revert search page')) {
- drupal_set_message(t('The search page was successfully reverted.'));
- }
- else {
- drupal_set_message(t('The search page was successfully deleted.'));
- }
- return;
- }
- search_api_page_edit($form_state['page']->machine_name, $form_state['values']);
- drupal_set_message(t('The changes were successfully saved.'));
- }
|