Skip to content

Commit

Permalink
alpha version
Browse files Browse the repository at this point in the history
  • Loading branch information
MingChen0919 committed Feb 16, 2017
1 parent fdfc0ea commit 43531d1
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 132 deletions.
3 changes: 1 addition & 2 deletions api/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

require_once drupal_get_path('module', 'tripal_elasticsearch') . '/includes/search/build_search_forms_form.inc';
require_once drupal_get_path('module', 'tripal_elasticsearch') . '/includes/search/link_results_to_pages_form.inc';
require_once drupal_get_path('module', 'tripal_elasticsearch') . '/includes/search/alter_search_forms_form.inc';
require_once drupal_get_path('module', 'tripal_elasticsearch') . '/includes/search/delete_search_forms_form.inc';
require_once drupal_get_path('module', 'tripal_elasticsearch') . '/includes/search/view_search_forms_form.inc';
require_once drupal_get_path('module', 'tripal_elasticsearch') . '/includes/search/sitewide_search_box_form.inc';
require_once drupal_get_path('module', 'tripal_elasticsearch') . '/includes/search/website_search_box_form.inc';
9 changes: 0 additions & 9 deletions includes/search/alter_search_forms_form.inc

This file was deleted.

122 changes: 121 additions & 1 deletion includes/search/link_results_to_pages_form.inc
Original file line number Diff line number Diff line change
@@ -1,9 +1,129 @@
<?php

function link_results_to_pages_form ($form, &$form_state) {
$form = [
$connection = (new ElasticConnection(["localhost:9201"]))->make();
$elastic_index = (new ElasticIndex($connection));
$indices_options = $elastic_index->GetIndices();

$form['indices'] = array(
'#type' => 'select',
'#title' => t('Select an index'),
'#options' => ['none' => 'Select an index'] + $indices_options,
'#ajax' => [
'callback' => 'display_form_fields_ajax',
'wrapper' => 'display_form_fields'
]
);

$index_name = !empty($form_state['values']['indices']) ? $form_state['values']['indices'] : 'Select an index';

$form['form_fields'] = [
'#type' => 'fieldset',
'#title' => t('Form Fields'),
'#tree' => true,
'#prefix' => '<div id="display_form_fields">',
'#suffix' => '</div>',
];

try{
// get fields for a select index
$index_mappings = $elastic_index->GetMappings($index_name);
foreach ($index_mappings as $field) {
$form['form_fields'][$field] = [
'#type' => 'textfield',
'#title' => t($field),
'#collapsible' => true,
'#collapsed' => true,
];
}
} catch (Exception $exception) {
//drupal_set_message($exception->getMessage(), 'warning');
}

return $form;
}


/*
* display_form_fields_ajax callback
*/
function display_form_fields_ajax ($form, &$form_state) {
return $form['form_fields'];
}


/*
* link_results_to_pages_form submit
*/
function link_results_to_pages_form_submit ($form, &$form_state) {

$index_name = $form_state['values']['indices'];
$table_name = preg_replace('/^chado_/', 'chado.', $index_name);

// Delete the table and its fields in the database if that table name already exists
db_delete('tripal_elasticsearch_search_forms')
->condition('table_name', $table_name)
->execute();

$table_fields = array_keys($form_state['values']['form_fields']);
foreach ($table_fields as $field) {
$record['table_name'] = $table_name;
$record['table_field'] = $field;
$record['page_links'] = $form_state['values']['form_fields'][$field];

drupal_write_record('tripal_elasticsearch_add_links', $record);
}

drupal_set_message(t('A search form block has been created for table '. $table_name));
}


/*
* instruction form for link results to pages
*/
function link_results_to_pages_example_form($form, &$form_state){
$form['add_links'] = array(
'#type' => 'fieldset',
'#title' => 'Example: add page links to search results',
'#collapsible' => true,
'#collapsed' => true,
);
$description = '<h2>Add page links to search results</h2>';
$description .= '<p>This page allows you to add dynamic URL to search results. ';
$description .= 'Here dynamic URL means that the URL dependens on the seach result values. ';
$description .= 'For example, the search form for organism has four fields: <b>abbreviation</b>, <b>common_name</b>, <b>genus</b>, <b>species</b>. ';
$description .= 'We want to link some search results to corresponding oranism pages. ';
$description .= 'Assuming that all organism pages have the same pattern: <b>organism/genus/species</b> ';
$description .= '(all URLs have the same part "organism/" but are different at "genus" and "species"),';
$description .= 'and the different parts in URLs only depend on the values of search results, ';
$description .= 'in this case, we can link search results to their corresponding organism pages by adding dynamic links to the fields. </p>';
$description .= '<p><b>A dynamic link is created by replacing variable values in URL with corresponding field names within "[]".</b></p>';
$description .= '<p>The example below shows how to add links to search results in fields <b>abbreviation</b> and <b>species</b>.</p>';
$form['add_links']['description'] = array(
'#type' => 'item',
'#markup' => $description,
);
$form['add_links']['abbreviation'] = array(
'#type' => 'textfield',
'#title' => t('abbreviation'),
'#title_display' => 'after',
'#default_value' => t('organism/[genus]/[species]'),
);
$form['add_links']['common_name'] = array(
'#type' => 'textfield',
'#title' => t('common_name'),
'#title_display' => 'after',
);
$form['add_links']['genus'] = array(
'#type' => 'textfield',
'#title' => t('genus'),
'#title_display' => 'after',
);
$form['add_links']['species'] = array(
'#type' => 'textfield',
'#title' => t('species'),
'#title_display' => 'after',
'#default_value' => t('organism/[genus]/[species]'),
);
return $form;
}
46 changes: 0 additions & 46 deletions includes/search/view_search_forms_form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -68,52 +68,6 @@ function view_search_forms_form ($form, &$form_state, $table_name) {
}


/*
function view_search_forms_form_submit ($form, &$form_state) {
$connection = (new ElasticConnection(["127.0.0.1:9201"]))->make();
$elastic_search = new ElasticSearch($connection);
$table_name = $form_state['storage']['table_name'];
$index = preg_replace('/^chado./', 'chado_', $table_name);
$type = $table_name;
foreach ($form_state['storage']['fields'] as $field) {
$field_content_pairs[$field] = $form_state['values'][$field];
}
$query = $elastic_search->build_search_query_from_field_content_pairs($field_content_pairs);
$params = $elastic_search->build_table_search_params($index = $index, $type = $type, $query = $query);
$search_res = $elastic_search->search($params);
dpm($search_res);
// display search results in a table
$per_page = 10;
foreach ($form_state['storage']['fields'] as $field) {
$header[] = [
'data' => $field,
'field' => $field,
];
}
$rows = $search_res;
$current_page = pager_default_initialize(count($rows), $per_page);
$chunks = array_chunk($rows, $per_page, TRUE);
$output = theme('table', array('header' => $header, 'rows' => $chunks[$current_page]));
$output .= theme('pager', array('quantity', count($rows)));
$form['search_results'] = [
'#markup' => $output,
];
$form_state['rebuild'] = true;
}
*/




Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

function sitewide_search_box_form ($form, &$form_state) {
$form['sitewide_search_box'] = [
function website_search_box_form ($form, &$form_state) {
$form['website_search_box'] = [
'#type' => 'textfield',
];

Expand Down
15 changes: 6 additions & 9 deletions tripal_elasticsearch.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function delete_indices_page()
function build_search_forms_page ()
{
// create a connection port
$connection = (new ElasticConnection(["127.0.0.1:9201"]))->make();
// $connection = (new ElasticConnection(["127.0.0.1:9201"]))->make();
return drupal_get_form ('build_search_forms_form');

/*
Expand All @@ -116,14 +116,11 @@ function build_search_forms_page ()
*/
function link_results_to_pages_page ()
{
// create a connection port
$connection = (new ElasticConnection(["127.0.0.1:9201"]))->make();
if ($connection) {
return drupal_get_form ('link_results_to_pages_form');
} else {
drupal_set_message ('Elasticsearch cluster is disconnected.', 'warning');
return '';
}
$example_form = drupal_get_form('link_results_to_pages_example_form');
$form = drupal_get_form('link_results_to_pages_form');
$output = drupal_render($example_form).drupal_render($form);

return $output;

}

Expand Down
Loading

0 comments on commit 43531d1

Please sign in to comment.