Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an API endpoint to share categories #250

Merged
merged 2 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions includes/Common/BundleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,52 @@ public function getFieldsByBundleTerm($cv_name, $accession) {

return $fields;
}

/**
* Gets node types in the same format as bundles.
*
* @return array
* A list of node types.
*/
public function getNodeTypes() {
$node_types = db_select('node_type', 'nt')
->fields('nt', ['type', 'name'])
->execute()
->fetchAll();

$categories = [];

foreach ($node_types as $node_type) {
$categories[] = [
'name' => $node_type->type,
'label' => $node_type->name,
'type' => 'node',
'term_id' => NULL,
'cv_term' => NULL,
'accession' => NULL,
'fields' => [],
];
}

return $categories;
}

/**
* Get a list of bundles and attach fields.
*
* @return array
* A list of bundles with fields attached.
*/
public function getBundlesWithFields() {
$bundles = $this->getBundles();

$categories = [];
foreach ($bundles as $bundle) {
$fields = $this->getFieldsByBundle($bundle);
$bundle->fields = $fields;
$categories[] = $bundle;
}

return $categories;
}
}
7 changes: 7 additions & 0 deletions includes/Common/FieldsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ES\Common;

class FieldsHelper{

}
50 changes: 29 additions & 21 deletions includes/tripal_elasticsearch.advanced_search.form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {
$bundles = [];
}

$form_vals = $form_state['values'] ?? [];

$values = [
'term' => trim($_GET['term'] ?? $form_vals['term'] ?? ''),
'category' => $_GET['category'] ?? $form_vals['category'] ?? '',
'field' => $_GET['field'] ?? $form_vals['field'] ?? '',
];

$helper = new \ES\Common\BundleHelper();

$form['wrapper'] = [
'#prefix' => '<div id="advanced_search_wrapper" class="container-inline">',
'#prefix' => '<div id="advanced_search_wrapper">',
'#suffix' => '</div>',
];

$category = trim(
$_GET['category'] ?? $form_state['values']['category'] ?? ''
);

$form['wrapper']['category'] = [
'#type' => 'select',
'#options' => ['' => 'Any Category'] + $bundles,
'#ajax' => [
'callback' => 'tripal_elasticsearch_advanced_search_form_callback',
'wrapper' => 'advanced_search_wrapper',
],
'#default_value' => $category,
'#default_value' => $values['category'],
];

if (!empty($category)) {
$fields = $helper->getFieldsByBundle($helper->getBundleByName($category));
if (!empty($values['category'])) {
$fields = $helper->getFieldsByBundle($helper->getBundleByName($values['category']));
$field_options = [
'' => 'Any Field',
];
Expand All @@ -48,13 +52,13 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {
$form['wrapper']['field'] = [
'#type' => 'select',
'#options' => $field_options,
'#default_value' => trim($_GET['field'] ?? ''),
'#default_value' => $values['field'],
];
}

$form['wrapper']['term'] = [
'#type' => 'textfield',
'#default_value' => $_GET['term'] ?? '',
'#default_value' => $values['term'],
];

$form['wrapper']['submit'] = [
Expand All @@ -64,11 +68,9 @@ function tripal_elasticsearch_advanced_search_form($form, &$form_state) {

$form['#method'] = 'GET';

//if (isset($_GET['term']) && !empty($_GET['term'])) {
$form['wrapper']['results'] = [
'#markup' => tripal_elasticsearch_advanced_search_results($_GET),
'#markup' => tripal_elasticsearch_advanced_search_results($values),
];
//}

return $form;
}
Expand All @@ -86,7 +88,7 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
try {
$instance = new \ES\Common\Instance();
$results = tripal_elasticsearch_perform_advanced_search($values, 15);
if($results['total'] == 0 && !empty($values['term'])) {
if ($results['total'] == 0 && !empty($values['term'])) {
$values['term'] = strtolower($values['term']);
$results = tripal_elasticsearch_perform_advanced_search($values, 15);
}
Expand All @@ -101,13 +103,19 @@ function tripal_elasticsearch_advanced_search_results(array $values) {
);
}

$content = theme('elasticsearch_results_header', [
'page' => $results['page'],
'total' => $results['total'],
'pages' => $results['pages'],
'time' => $results['time']
]);
$content .= tripal_elasticsearch_get_website_search_result_table($hits, false);
$content = theme(
'elasticsearch_results_header',
[
'page' => $results['page'],
'total' => $results['total'],
'pages' => $results['pages'],
'time' => $results['time'],
]
);
$content .= tripal_elasticsearch_get_website_search_result_table(
$hits,
FALSE
);
$content .= $results['pager'];

return $content;
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/BundleHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,19 @@ public function testGettingFieldsByTerm() {
$this->assertObjectHasAttribute('name', $field);
$this->assertObjectHasAttribute('label', $field);
}

/** @test */
public function testGettingBundlesWithFields() {
$helper = new BundleHelper();

$bundles = $helper->getBundlesWithFields();
$this->assertNotEmpty($bundles);

$bundle = $bundles[0];
$this->assertObjectHasAttribute('fields', $bundle);
$this->assertObjectHasAttribute('name', $bundle);
$this->assertObjectHasAttribute('label', $bundle);
$this->assertObjectHasAttribute('cv_name', $bundle);
$this->assertObjectHasAttribute('accession', $bundle);
}
}
Loading