added settings for advanced search vocabulary choice and hidden terms

This commit is contained in:
Bachir Soussi Chiadmi
2017-10-31 12:47:28 +01:00
parent e9146c29ce
commit 1e1a762b9b
2 changed files with 348 additions and 267 deletions

View File

@@ -751,291 +751,303 @@ function materio_search_api_advanced_search_form($form, &$form_state){
// $form = array();
// TODO: get vid from settings
$vid = 15; // onthologie
$voc = taxonomy_vocabulary_load($vid);
$voc_machinename = $voc->machine_name;
// dsm($voc, "voc");
$vid = variable_get('msa-advancedsearchvocabulary', null);
if($vid){
$values = isset($form_state['values']) ? $form_state['values'] : null;
// dsm($values, "values");
$voc = taxonomy_vocabulary_load($vid);
$voc_machinename = $voc->machine_name;
// dsm($voc, "voc");
$args = arg();
// dsm($args, 'args');
$hidden_terms = variable_get('msa-hiddentaxoterms', array());
// dsm($hidden_terms, 'hidden_terms');
// get the first elemt of url args
$path = array_shift($args);
if (isset($args[0]) && $args[0] == "filters") {
$path .= '/'.array_shift($args);
}
// get the keywords from args and reconstruct the filter lines with default values
$args_values = array();
if($path == 'explore/filters' && $args[0] !== "ajax"){
$keywords = explode("+", $args[0]);
// TODO: what if a keyword contains a +
// dsm($keywords, 'keywords');
foreach ($keywords as $key => $value) {
$name = str_replace('"', '', $value);
//Get the term
$terms = taxonomy_get_term_by_name($name, $voc_machinename);
$term = array_shift($terms);
// dsm($term, $term->tid.' : '.$term->name);
// get parents
if(isset($term->tid)){
$parents = taxonomy_get_parents_all($term->tid);
// dsm($parents, 'parents');
// build default values
for ($p=count($parents)-1; $p >=0 ; $p--) {
$args_values[$key][] = $parents[$p]->tid;
$values = isset($form_state['values']) ? $form_state['values'] : null;
// dsm($values, "values");
$args = arg();
// dsm($args, 'args');
// get the first elemt of url args
$path = array_shift($args);
if (isset($args[0]) && $args[0] == "filters") {
$path .= '/'.array_shift($args);
}
// get the keywords from args and reconstruct the filter lines with default values
$args_values = array();
if($path == 'explore/filters' && $args[0] !== "ajax"){
$keywords = explode("+", $args[0]);
// TODO: what if a keyword contains a +
// dsm($keywords, 'keywords');
foreach ($keywords as $key => $value) {
$name = str_replace('"', '', $value);
//Get the term
$terms = taxonomy_get_term_by_name($name, $voc_machinename);
$term = array_shift($terms);
// dsm($term, $term->tid.' : '.$term->name);
// get parents
if(isset($term->tid)){
$parents = taxonomy_get_parents_all($term->tid);
// dsm($parents, 'parents');
// build default values
for ($p=count($parents)-1; $p >=0 ; $p--) {
$args_values[$key][] = $parents[$p]->tid;
}
}
}
}
}
// dsm($args_values, 'args_values');
// TODO: show result count live
// TODO: filter next select form item
// dsm($args_values, 'args_values');
// TODO: show result count live
// TODO: filter next select form item
// define number of lines,
// 1 by default
// or from url arguments
// or let it as it is if already chanched by ajax
if(!isset($form_state['filterlines'])){
if(count($args_values)){
$form_state['filterlines'] = count($args_values);
}else{
$form_state['filterlines'] = 1;
// define number of lines,
// 1 by default
// or from url arguments
// or let it as it is if already chanched by ajax
if(!isset($form_state['filterlines'])){
if(count($args_values)){
$form_state['filterlines'] = count($args_values);
}else{
$form_state['filterlines'] = 1;
}
}
}
// dsm($form_state['filterlines'], "form_state['filterlines']");
// dsm($form_state['filterlines'], "form_state['filterlines']");
// filters form container
$form['filters'] = array(
'#type' => 'container',
'#prefix' => '<div id="advancedsearch-filters">',
'#suffix' => '</div>',
);
$trigger = FALSE;
if (!empty($form_state['triggering_element']['#name'])) {
$trigger = $form_state['triggering_element'];
}
$rmline = false;
if ($trigger) {
// dsm($form_state['triggering_element'], $form_state['triggering_element']['#name']);
// dsm($values, 'form state values');
// add a line if needed
if($trigger['#name'] == 'addfilters'){
$form_state['filterlines'] = $form_state['filterlines']+1;
}
// remove a line if needed
if(strpos($trigger['#name'], 'rm-filter-') === 0){
// $form_state['filterlines'] = $form_state['filterlines']+1;
$rmline = $trigger['#return_value'];
// dsm($rmline, "rmline");
}
}
// build first level taxonomy tree and select options
$taxotree = taxonomy_get_tree($vid, 0, 1, false);
foreach ($taxotree as $index => $term) {
// TODO: get translated tag name
$level_0_ops[$term->tid] = t($term->name);
}
// build filters lines (ajax enabled)
for ($l=0; $l < $form_state['filterlines']; $l++) {
// if rmline this line,
// continue the loop without creating the line
if($l === $rmline){
continue;
}
// if we are after the removed line,
// create a second counter ($ll) to keep a continuous order for form items
// and keep the current counter ($l) to retrieve de current values
// if before removed line or no removed line at all, $l = $ll
if($rmline !== false && $l >= $rmline){
$ll = $l-1;
}else{
$ll = $l;
}
// dsm("l : ".$l." | ll : ".$ll);
$form['filters']['filterline-'.$ll] = array(
// filters form container
$form['filters'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array("filter-line")
)
'#prefix' => '<div id="advancedsearch-filters">',
'#suffix' => '</div>',
);
// get the default value
// from from_state values if ajax
// or from url args
// or 0
$default0 = null;
if(isset($values['filter-'.$l.'-0']) && $values['filter-'.$l.'-0']){
$default0 = $values['filter-'.$l.'-0'];
}elseif(isset($args_values[$l][0])){
$default0 = $args_values[$l][0];
}
// dsm($default0, "default 0");
// form select element for first level
$form['filters']['filterline-'.$ll]['filter-'.$ll.'-0'] = array(
'#type' => 'select',
'#options' => $level_0_ops,
'#name' => 'filter-'.$ll.'-0',
'#empty_option' => t("Choose a filter"),
// '#empty_values' => 0,
'#default_value' => $default0,
'#ajax' => array(
'callback' => 'materio_search_api_advanced_search_select_callback',
'wrapper' => 'advancedsearch-filters',
),
);
// without this line, form api will mess with default values
$form_state["input"]['filter-'.$ll.'-0'] = $default0;
// add level 1 filter if level 0 chosen
if($default0){
// build second level taxonomy tree and select options
$taxotree1 = taxonomy_get_tree($vid, $default0, 1, false);
$level_1_ops = array();
foreach ($taxotree1 as $index => $term) {
// TODO: get translated tag name
$level_1_ops[$term->tid] = t($term->name);
}
// get the default value
// from from_state values if ajax
// or from url args
// or 0
$default1 = null;
if(isset($values['filter-'.$l.'-1']) && $values['filter-'.$l.'-1']){
$default1 = $values['filter-'.$l.'-1'];
}elseif(isset($args_values[$l][1])){
$default1 = $args_values[$l][1];
}
// dsm($default1, "default 1");
// form select element for second level
$form['filters']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
'#type' => 'select',
'#options' => $level_1_ops,
'#name' => 'filter-'.$ll.'-1',
'#empty_option' => t("Choose a filter"),
// '#empty_values' => 0,
'#default_value' => $default1,
'#ajax' => array(
'callback' => 'materio_search_api_advanced_search_select_callback',
'wrapper' => 'advancedsearch-filters',
),
);
// without this line, form api will mess with default values
$form_state["input"]['filter-'.$ll.'-1'] = $default1;
// add level 2 filter if level 1 chosen
if($default1){
// build second level taxonomy tree and select options
$taxotree2 = taxonomy_get_tree($vid, $default1, 1, false);
if(count($taxotree2)){
// $default2 = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
$default2 = null;
if(isset($values['filter-'.$l.'-2']) && $values['filter-'.$l.'-2']){
$default2 = $values['filter-'.$l.'-2'];
}elseif(isset($args_values[$l][2])){
$default2 = $args_values[$l][2];
}
// dsm($default2, "default 2");
$level_2_ops = array();
foreach ($taxotree2 as $index => $term) {
// TODO: get translated tag name
$level_2_ops[$term->tid] = t($term->name);
}
// form select element for third level
$form['filters']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
'#type' => 'select',
'#options' => $level_2_ops,
'#name' => 'filter-'.$ll.'-2',
'#empty_option' => t("Choose a filter"),
// '#empty_values' => 0,
'#default_value' => $default2,
);
// without this line, form api will mess with default values
$form_state["input"]['filter-'.$ll.'-2'] = $default2;
}
}
$trigger = FALSE;
if (!empty($form_state['triggering_element']['#name'])) {
$trigger = $form_state['triggering_element'];
}
// add remove the line button
$form['filters']['filterline-'.$ll]['rm-filter-'.$ll] = array(
$rmline = false;
if ($trigger) {
// dsm($form_state['triggering_element'], $form_state['triggering_element']['#name']);
// dsm($values, 'form state values');
// add a line if needed
if($trigger['#name'] == 'addfilters'){
$form_state['filterlines'] = $form_state['filterlines']+1;
}
// remove a line if needed
if(strpos($trigger['#name'], 'rm-filter-') === 0){
// $form_state['filterlines'] = $form_state['filterlines']+1;
$rmline = $trigger['#return_value'];
// dsm($rmline, "rmline");
}
}
// build first level taxonomy tree and select options
$taxotree = taxonomy_get_tree($vid, 0, 1, false);
foreach ($taxotree as $index => $term) {
// TODO: get translated tag name
if(!in_array($term->tid, $hidden_terms)){
$level_0_ops[$term->tid] = t($term->name);
}
}
// build filters lines (ajax enabled)
for ($l=0; $l < $form_state['filterlines']; $l++) {
// if rmline this line,
// continue the loop without creating the line
if($l === $rmline){
continue;
}
// if we are after the removed line,
// create a second counter ($ll) to keep a continuous order for form items
// and keep the current counter ($l) to retrieve de current values
// if before removed line or no removed line at all, $l = $ll
if($rmline !== false && $l >= $rmline){
$ll = $l-1;
}else{
$ll = $l;
}
// dsm("l : ".$l." | ll : ".$ll);
$form['filters']['filterline-'.$ll] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array("filter-line")
)
);
// get the default value
// from from_state values if ajax
// or from url args
// or 0
$default0 = null;
if(isset($values['filter-'.$l.'-0']) && $values['filter-'.$l.'-0']){
$default0 = $values['filter-'.$l.'-0'];
}elseif(isset($args_values[$l][0])){
$default0 = $args_values[$l][0];
}
// dsm($default0, "default 0");
// form select element for first level
$form['filters']['filterline-'.$ll]['filter-'.$ll.'-0'] = array(
'#type' => 'select',
'#options' => $level_0_ops,
'#name' => 'filter-'.$ll.'-0',
'#empty_option' => t("Choose a filter"),
// '#empty_values' => 0,
'#default_value' => $default0,
'#ajax' => array(
'callback' => 'materio_search_api_advanced_search_select_callback',
'wrapper' => 'advancedsearch-filters',
),
);
// without this line, form api will mess with default values
$form_state["input"]['filter-'.$ll.'-0'] = $default0;
// add level 1 filter if level 0 chosen
if($default0){
// build second level taxonomy tree and select options
$taxotree1 = taxonomy_get_tree($vid, $default0, 1, false);
$level_1_ops = array();
foreach ($taxotree1 as $index => $term) {
// TODO: get translated tag name
$level_1_ops[$term->tid] = t($term->name);
}
// get the default value
// from from_state values if ajax
// or from url args
// or 0
$default1 = null;
if(isset($values['filter-'.$l.'-1']) && $values['filter-'.$l.'-1']){
$default1 = $values['filter-'.$l.'-1'];
}elseif(isset($args_values[$l][1])){
$default1 = $args_values[$l][1];
}
// dsm($default1, "default 1");
// form select element for second level
$form['filters']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
'#type' => 'select',
'#options' => $level_1_ops,
'#name' => 'filter-'.$ll.'-1',
'#empty_option' => t("Choose a filter"),
// '#empty_values' => 0,
'#default_value' => $default1,
'#ajax' => array(
'callback' => 'materio_search_api_advanced_search_select_callback',
'wrapper' => 'advancedsearch-filters',
),
);
// without this line, form api will mess with default values
$form_state["input"]['filter-'.$ll.'-1'] = $default1;
// add level 2 filter if level 1 chosen
if($default1){
// build second level taxonomy tree and select options
$taxotree2 = taxonomy_get_tree($vid, $default1, 1, false);
if(count($taxotree2)){
// $default2 = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
$default2 = null;
if(isset($values['filter-'.$l.'-2']) && $values['filter-'.$l.'-2']){
$default2 = $values['filter-'.$l.'-2'];
}elseif(isset($args_values[$l][2])){
$default2 = $args_values[$l][2];
}
// dsm($default2, "default 2");
$level_2_ops = array();
foreach ($taxotree2 as $index => $term) {
// TODO: get translated tag name
$level_2_ops[$term->tid] = t($term->name);
}
// form select element for third level
$form['filters']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
'#type' => 'select',
'#options' => $level_2_ops,
'#name' => 'filter-'.$ll.'-2',
'#empty_option' => t("Choose a filter"),
// '#empty_values' => 0,
'#default_value' => $default2,
);
// without this line, form api will mess with default values
$form_state["input"]['filter-'.$ll.'-2'] = $default2;
}
}
}
// add remove the line button
$form['filters']['filterline-'.$ll]['rm-filter-'.$ll] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'materio_search_api') . '/images/rmfilter.png',
'#title' => t('Remove this filter line'),
'#name' => 'rm-filter-'.$ll,
'#return_value' => $ll,
'#executes_submit_callback' => false,
'#ajax' => array(
'callback' => 'materio_search_api_advanced_search_rmline_callback',
'wrapper' => 'advancedsearch-filters',
// 'trigger_as' => array("name"=>"rmfilters".$l),
),
"#attributes" => array('class'=>array('rm-btn')),
);
} // end of lines loop
if($rmline !== false){
$form_state['filterlines'] = $form_state['filterlines']-1;
}
// button to add a new line of criterias
$form['filters']['addfilters'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'materio_search_api') . '/images/rmfilter.png',
'#title' => t('Remove this filter line'),
'#name' => 'rm-filter-'.$ll,
'#return_value' => $ll,
'#executes_submit_callback' => false,
'#src' => drupal_get_path('module', 'materio_search_api') . '/images/addfilter.png',
'#title' => t('Add a filter line'),
'#name' => "addfilters",
'#return_value' => "add",
'#ajax' => array(
'callback' => 'materio_search_api_advanced_search_rmline_callback',
'callback' => 'materio_search_api_advanced_search_addfilter_callback',
'wrapper' => 'advancedsearch-filters',
// 'trigger_as' => array("name"=>"rmfilters".$l),
),
"#attributes" => array('class'=>array('rm-btn')),
'#executes_submit_callback' => false,
'#attributes' => array(
'class'=>array('add-filter')
),
);
} // end of lines loop
// buttons form container
$form['rightcol'] = array(
'#type' => 'container',
);
// add a back to search/base home bouton if on explore/foo
if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
$link = l('<i class="fi-home"></i>', base_path().'actuality', array(
'html'=>true,
'attributes'=>array('class'=>'back-search-home')
));
$form['rightcol']['actu'] = array(
'#type' => 'markup',
'#markup' => $link,
);
}
if($rmline !== false){
$form_state['filterlines'] = $form_state['filterlines']-1;
}
// submit the search
$form['rightcol']['search'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
'#title' => t('Search'),
'#name' => "search",
'#return_value' => "search",
);
// button to add a new line of criterias
$form['filters']['addfilters'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'materio_search_api') . '/images/addfilter.png',
'#title' => t('Add a filter line'),
'#name' => "addfilters",
'#return_value' => "add",
'#ajax' => array(
'callback' => 'materio_search_api_advanced_search_addfilter_callback',
'wrapper' => 'advancedsearch-filters',
),
'#executes_submit_callback' => false,
'#attributes' => array(
'class'=>array('add-filter')
),
);
// buttons form container
$form['rightcol'] = array(
'#type' => 'container',
);
// add a back to search/base home bouton if on explore/foo
if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
$link = l('<i class="fi-home"></i>', base_path().'actuality', array(
'html'=>true,
'attributes'=>array('class'=>'back-search-home')
));
$form['rightcol']['actu'] = array(
'#type' => 'markup',
'#markup' => $link,
}else{
$form['novoc'] = array(
'#markup' => "Please choose a vocabulary to search from in materio search api settings"
);
}
// submit the search
$form['rightcol']['search'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
'#title' => t('Search'),
'#name' => "search",
'#return_value' => "search",
);
// dsm($form, 'form');
return $form;
}
@@ -1391,11 +1403,6 @@ function template_preprocess_materio_search_api_actuality(&$vars){
// $vars['actualities_infos'] = t('Actualities by materiO\'');
$vars['actualities_infos'] = t('');
}
//
// function template_preprocess_materio_search_api_advanced_search_block(&$vars){
// // dsm($vars, "vars");
// $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
// }
function theme_materio_search_api_form_element($variables) {
// dsm($variables, 'variables');