search is ok! (remains to sync search and corpus map

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-19 15:07:19 +01:00
parent 973483a6eb
commit a7f736f919
29 changed files with 656 additions and 147 deletions
@@ -592,12 +592,16 @@
// show opened audio node
function openNode(id){
closeNode();
_node_opened_id = id;
_nodes[id].open();
if(typeof _nodes[id] !== 'undefined'){
_node_opened_id = id;
_nodes[id].open();
}
}
function closeNode(){
if(_node_opened_id != -1){
_nodes[_node_opened_id].close();
// if(typeof _nodes[_node_opened_id] !== 'undefined'){
_nodes[_node_opened_id].close();
// }
_node_opened_id = -1;
}
}
@@ -794,18 +798,17 @@
scrambleCollection();
_$body.trigger({'type':'close_entree', 'tid':tid});
}
return false;
});
// $('body').trigger({
// 'type':'chutier-action-done',
// 'action_done':data.action_done,
// 'target_id':$link.attr('target_id'),
// });
_$body.on('chutier-action-done', function(e) {
_nodes_by_nid[e.target_id].chutier_action = e.new_action;
});
_$body
.on('chutier-action-done', function(e) {
_nodes_by_nid[e.target_id].chutier_action = e.new_action;
})
.on('search-results-loaded', function(e){
console.log("Edlp Corpus, search-results-loaded",e.results);
// TODO: filter map's nodes
});
};
// _ _ _ ___ _ _
@@ -592,12 +592,16 @@
// show opened audio node
function openNode(id){
closeNode();
_node_opened_id = id;
_nodes[id].open();
if(typeof _nodes[id] !== 'undefined'){
_node_opened_id = id;
_nodes[id].open();
}
}
function closeNode(){
if(_node_opened_id != -1){
_nodes[_node_opened_id].close();
// if(typeof _nodes[_node_opened_id] !== 'undefined'){
_nodes[_node_opened_id].close();
// }
_node_opened_id = -1;
}
}
@@ -794,18 +798,17 @@
scrambleCollection();
_$body.trigger({'type':'close_entree', 'tid':tid});
}
return false;
});
// $('body').trigger({
// 'type':'chutier-action-done',
// 'action_done':data.action_done,
// 'target_id':$link.attr('target_id'),
// });
_$body.on('chutier-action-done', function(e) {
_nodes_by_nid[e.target_id].chutier_action = e.new_action;
});
_$body
.on('chutier-action-done', function(e) {
_nodes_by_nid[e.target_id].chutier_action = e.new_action;
})
.on('search-results-loaded', function(e){
console.log("Edlp Corpus, search-results-loaded",e.results);
// TODO: filter map's nodes
});
};
// _ _ _ ___ _ _
@@ -1,9 +1,84 @@
(function ($, Drupal, drupalSettings) {
var settings = drupalSettings.edlp_studio;
var settings = drupalSettings.edlp_search;
var _$body = $('body');
var _$container;
var _$form;
function init(){
console.log('Edlp Search Init');
console.log('EdlpSearch Init', settings);
initEvents();
initAjax();
};
function initEvents(){
$('body')
.on('new-content-ajax-loaded', initAjax);
};
function initAjax(){
console.log('EdlpSearch initAjaxForm()');
_$form = $('#edlp-search-form:not(.ajax-enabled)')
.on('submit', onSubmitForm)
.addClass('ajax-enabled');
_$container = _$form.parents('.row');
if(!_$container.length){
_$container = _$form.parent();
}
};
function onSubmitForm(e){
e.preventDefault();
// console.log('onSubmitForm',e);
var args = {};
// search for key words
args.keys = $('input[type="search"]', this).val();
// entries filter
args.entries = [];
$('input[type="checkbox"]:checked', '#edit-entries').each(function(index, el) {
args.entries.push($(this).val());
});
loadResults(args)
return false;
};
function loadResults(args){
console.log('EdlpSearch loadResults() : args', args);
_$form.addClass('ajax-loading');
var path = window.location.origin + drupalSettings.path.baseUrl +settings.results_ajax_url;
$.getJSON(path, args)
.done(function(data){
onResultsLoaded(data);
})
.fail(function(jqxhr, textStatus, error){
onResultsLoadFail(jqxhr, textStatus, error);
});
};
function onResultsLoaded(data){
console.log('EdlpSearch onResultsLoaded()', data);
// insert results col
$prev_results = $('[theme="edlp_search_results"]', _$container);
if($prev_results.length){
$prev_results.replaceWith(data.rendered);
}else{
_$container.append(data.rendered);
}
// trigger event
_$body.trigger({
'type':'search-results-loaded',
'results':data.results_nids
});
};
function onResultsLoadFail(jqxhr, textStatus, error){
console.warn('EdlpSearch : search results ajax load failed : '+error, jqxhr.responseText);
};
init();
@@ -30,7 +30,7 @@ function edlp_search_help($route_name, RouteMatchInterface $route_match) {
*/
function edlp_search_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'edlp_search/edlp_search-library';
$url = Url::fromRoute('edlp_search.edlp_search_controller_searchResults_ajax', [], ['absolute' => TRUE]);
$url = Url::fromRoute('edlp_search.edlp_search_controller_search_results_ajax');
$attachments['#attached']['drupalSettings']['edlp_search']['results_ajax_url'] = $url->getInternalPath();
}
@@ -46,5 +46,13 @@ function edlp_search_theme($existing, $type, $theme, $path) {
'form' => null,
),
),
'edlp_search_results' => array(
'file' => 'includes/edlp_search_results.inc',
'variables' => array(
'items' => null,
'no_results_found' => '',
'search_help' => ''
),
),
);
}
@@ -15,7 +15,7 @@ edlp_search.edlp_search_controller_searchForm_ajax:
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_searchResults:
edlp_search.edlp_search_controller_search_results:
path: '/edlp_search/searchresults'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchResults'
@@ -23,7 +23,7 @@ edlp_search.edlp_search_controller_searchResults:
requirements:
_permission: 'access content'
edlp_search.edlp_search_controller_searchResults_ajax:
edlp_search.edlp_search_controller_search_results_ajax:
path: '/edlp_search/searchresults/ajax'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchResultsJson'
@@ -0,0 +1,17 @@
<?php
function template_preprocess_edlp_search_results(&$vars){
// dpm($vars);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$items = [];
if(count($vars['items'])){
foreach ($vars['items'] as $item) {
$items[] = $view_builder->view($item, 'search_index');
}
}
$vars['items'] = $items;
}
@@ -4,9 +4,9 @@ namespace Drupal\edlp_search\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormBuilder;
use Drupal\search_api\Entity\Index;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
@@ -62,6 +62,9 @@ class EdlpSearchController extends ControllerBase {
return $this->renderable;
}
/**
* searchFormJson
*/
public function searchFormJson(){
$this->buildRenderable();
@@ -76,27 +79,152 @@ class EdlpSearchController extends ControllerBase {
}
/**
* Searchresults.
*
* @return string
* Return Hello string.
*/
public function searchResults() {
return [
'#type' => 'markup',
'#markup' => $this->t('Implement method: searchResults')
];
* searchResults
*/
public function searchResults(Request $request) {
$this->request = $request;
// TODO: what if no results
return $this->getrenderable();
}
/**
* searchResultsJson
*/
public function searchResultsJson(Request $request){
$keys = $request->query->get('keys');
$this->request = $request;
$renderable = $this->getrenderable();
$rendered = render($renderable);
// build an array of results's nids
$results_nids = [];
foreach ($this->items as $item) {
$results_nids[] = $item->id();
}
// TODO: what if no results
$response = new JsonResponse();
$response->setData([
'test'=>'search results',
'keys'=>$keys
// 'rendered'=> $rendered,
'keys'=>$this->keys,
'entries' => $this->entries,
'rendered'=> $rendered,
'results_nids'=>$results_nids,
]);
return $response;
}
/**
* getRenderable
*/
private function getRenderable(){
$this->query();
$build = array(
'#theme' => 'edlp_search_results',
'#items' => $this->items
);
if(!count($this->items)){
$build['#no_results_found'] = array(
'#markup' => t('Your search yielded no results.')
);
// $build['#search_help'] = array(
// '#markup' => $this->t('<ul>
// <li>Check if your spelling is correct.</li>
// <li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>&quot;bike shed&quot;</em>.</li>
// <li>Consider loosening your query with <em>OR</em>. <em>bike OR shed</em> will often show more results than <em>bike shed</em>.</li>
// </ul>')
// );
}
return $build;
}
/**
* query
*/
private function query(){
$this->getRequestArgs();
// TODO: get the sapi index id with settings
/* @var $sapi_index \Drupal\search_api\IndexInterface */
$sapi_index = Index::load('collection');
// dpm($sapi_index);
// Create the query.
$query = $sapi_index->query();
// dpm($query);
$query->setSearchID('edlp_search:ajaxsearch');
$parse_mode = \Drupal::getContainer()
->get('plugin.manager.search_api.parse_mode')
->createInstance('direct');
$query->setParseMode($parse_mode);
// workflow
$wf_condition_group = $query->createConditionGroup('OR');
$wf_condition_group->addCondition('field_workflow', 'corpus_documents_publie', "=");
// TODO: add condition with the other workflow field
$query->addConditionGroup($wf_condition_group);
// Search for keys.
if (!empty($this->keys)) {
$query->keys($this->keys);
}
// entries
if (!empty($this->entries)){
$entries_condition_group = $query->createConditionGroup();
foreach ($this->entries as $tid) {
$entries_condition_group->addCondition('field_entrees', (int)$tid, "=");
}
// dpm($entries_condition_group);
$query->addConditionGroup($entries_condition_group);
}
// TODO: genres
// TODO: locuteurs
// TODO: langues
$result = $query->execute();
$items = $result->getResultItems();
// dpm($items);
$this->items = [];
foreach ($items as $item) {
try {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $item->getOriginalObject()->getValue();
}
catch (SearchApiException $e) {
continue;
}
if (!$entity) {
continue;
}
$this->items[] = $entity;
}
// dpm($this->items);
}
/**
* getRequestArgs
*/
private function getRequestArgs(){
$this->keys = $this->request->query->get('keys');
$this->entries = $this->request->query->get('entries');
}
}
@@ -0,0 +1,3 @@
{{ items }}
{{ no_results_found }}
{{ search_help }}