From 6fdb4e0dc950ad2cbe572381f19943754d18c297 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Wed, 27 Aug 2014 15:27:15 +0200 Subject: [PATCH] setup solr properties for advanced search --- materio_search_api.admin.inc | 7 + materio_search_api.module | 226 +++++++++++++----- materio_search_api.pages.inc | 78 ++++-- .../materio-search-api-search-block.tpl.php | 13 +- 4 files changed, 242 insertions(+), 82 deletions(-) diff --git a/materio_search_api.admin.inc b/materio_search_api.admin.inc index 74bc19ba..1e1032c3 100755 --- a/materio_search_api.admin.inc +++ b/materio_search_api.admin.inc @@ -33,6 +33,13 @@ function materio_search_api_settings(){ '#default_value' => variable_get('autocompletesearchindex_'.$lcode, -1), '#title' => t('Autocomplete search api index for %lang contents.', array('%lang'=>$name)), ); + + $form['advancedsearchindex_'.$lcode] = array( + '#type'=>'select', + '#options'=>$index_options, + '#default_value' => variable_get('advancedsearchindex_'.$lcode, -1), + '#title' => t('Advanced search api index for %lang contents.', array('%lang'=>$name)), + ); } diff --git a/materio_search_api.module b/materio_search_api.module index 6ddb4d08..1e28d276 100755 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -128,6 +128,12 @@ function materio_search_api_search_api_data_type_info() { 'prefix' => 'temmap', 'always multiValued' => TRUE, ), + // 'term_tid' => array( + // 'name' => t('Taxonomy term tid'), + // 'fallback' => 'integer', + // 'prefix' => 'im', + // 'always multiValued' => TRUE, + // ), ); } @@ -146,8 +152,11 @@ function materio_search_api_search_api_solr_dynamic_field_info() { 'prefix' => 'temmap', 'always multiValued' => TRUE, ), + // 'term_tid' => array( + // 'prefix' => 'im', + // 'always multiValued' => TRUE, + // ), ); - } @@ -168,33 +177,39 @@ function materio_search_api_entity_property_info_alter(&$info){ $node_props['materio_search_api_onthologie_term_'.$i.'_text'] = array( 'type'=>'text', 'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'), - // 'query callback'=>'entity_metadata_table_query', 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text', ); + $node_props['materio_search_api_onthologie_term_'.$i.'_tid'] = array( + 'type'=>'text', + 'label'=> t('Main onthologie term '.$i. ' as tid'), + 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_tid', + ); } $node_props['materio_search_api_onthologie_term_others_text'] = array( 'type'=>'list', 'label'=> t('Others onthologie terms as text (+ synonyms)'), - // 'query callback'=>'entity_metadata_table_query', 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text', ); + $node_props['materio_search_api_onthologie_term_others_tid'] = array( + 'type'=>'list', + 'label'=> t('Others onthologie terms as tid'), + 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_tid', + ); + $node_props['materio_search_api_taglibres_text'] = array( 'type'=>'list', 'label'=> t('Tag libres terms as text (+ synonyms)'), - // 'query callback'=>'entity_metadata_table_query', 'getter callback'=>'materio_search_api_get_taglibres_terms_text', ); - $node_materiau_props = &$info['node']['bundles']['materiau']['properties']; $node_materiau_props['materio_search_api_node_propertie_companie'] = array( 'type'=>'list', 'label'=> t('Manufacturers and distributors companies localised (dont addresse)'), - // 'query callback'=>'entity_metadata_table_query', 'getter callback'=>'materio_search_api_get_node_propertie_companie', ); @@ -208,9 +223,11 @@ function materio_search_api_entity_property_info_alter(&$info){ 'type' => 'text', 'getter callback' => 'materio_search_api_term_property_dup_name', ); - } +/** +* taxonomy propoerties as text + synonymes +*/ function materio_search_api_get_onthologie_term_1_text($item){ // dsm($item, 'item'); return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 0); @@ -292,6 +309,55 @@ function materio_search_api_get_taxo_term_field_text($item, $field_name, $delta) return null; } +/** +* taxonomy properties as tid +*/ +function materio_search_api_get_onthologie_term_1_tid($item){ + // dsm($item, 'item'); + return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 0); +} +function materio_search_api_get_onthologie_term_2_tid($item){ + // dsm($item, 'item'); + return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 1); +} +function materio_search_api_get_onthologie_term_3_tid($item){ + // dsm($item, 'item'); + return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 2); +} +function materio_search_api_get_onthologie_term_4_tid($item){ + // dsm($item, 'item'); + return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 3); +} +function materio_search_api_get_onthologie_term_5_tid($item){ + // dsm($item, 'item'); + return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 4); +} + +function materio_search_api_get_taxonomy_terms_others_tid($item){ + // dsm($item, 'item'); + $delta = 5; + $terms = array(); + while( isset($item->field_onthologie['und'][$delta]) ){ + $terms[] = materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", $delta); + $delta++; + } + return $terms; +} + +function materio_search_api_get_taxo_term_field_tid($item, $field_name, $delta){ + // dsm($item, 'item'); + // dsm($delta, 'delta'); + $tid = null; + if(isset($item->{$field_name}['und'][$delta])) + $tid = $item->{$field_name}['und'][$delta]['tid']; + + // print 'tid = '.$tid."\n"; + return $tid; +} + +/** +* +*/ function materio_search_api_get_node_propertie_companie($item){ // dsm($item, '$item'); $lang = "fr";//$item->language; @@ -507,7 +573,20 @@ function materio_search_api_search_form($form, &$form_state){ $args = arg(); $path = array_shift($args); - $keys = implode('/', $args); + // dsm($args, 'args'); + if($args[0] == "advanced"){ + $advanced = true; + array_shift($args); + // dsm($args, 'shifted arsg'); + // foreach ($args as $arg) { + // $typed[] = $arg;//(integer)$arg; + // } + $keys = implode(' +', $args); + }else{ + $keys = implode('/', $args); + } + + if(user_access('use materio search api autocomplete')){ // use materio search api autocomplete | use materio search api filters $query = new EntityFieldQuery(); @@ -523,7 +602,6 @@ function materio_search_api_search_form($form, &$form_state){ $form['searchfield'] = array( '#type' => 'textfield', '#default_value' => $path == 'explore' ? $keys : $default_value, // TODO: set the search page path global or a variable in settings - // '#value' => $keys, '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi', //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect', '#size' => 30, @@ -604,7 +682,22 @@ function materio_search_api_advanced_search_form($form, &$form_state){ // dsm($form, 'form'); // global $user; - $form = array(); + $keys = arg(); + $path = array_shift($keys); + // dsm($keys, 'keys'); + if($path == 'explore' && $keys[0] == "advanced"){ + $advanced = true; + array_shift($keys); + $default_keys = $keys; + } + + $form = array( + 'title' => array( + '#prefix' => "

", + '#markup' => t("Advanced Search"), + '#suffix' => "

", + ) + ); $vocabularies = variable_get('advancedsearchvocs', array()); @@ -621,30 +714,20 @@ function materio_search_api_advanced_search_form($form, &$form_state){ $tree = taxonomy_get_tree($vid, 0, 1); // dsm($tree, 'tree'); - # don't know how to add parent term condition - // $query = new EntityFieldQuery(); - // $result = $query->entityCondition('entity_type', 'taxonomy_term') - // ->propertyCondition('vid', $vid) - // // ->propertyCondition('parent', 0) - // // ->fieldCondition('field_used_on_advanced_search', 'value', 0, "!=") - // ->propertyOrderBy('weight') - // ->execute(); - - // dsm($result, 'result'); - - // if (!empty($result['taxonomy_term'])) { - - // } + if(!count($tree)) + continue; foreach ($tree as $term) { $term = taxonomy_term_load($term->tid); // dsm($term, "term"); - if($term->field_used_on_advanced_search['und'][0]['value'] == 0){ - continue; - } + $children = taxonomy_get_children($term->tid, $vid); // dsm($children, "children"); + if($term->field_used_on_advanced_search['und'][0]['value'] == 0 + || !count($children)) + continue; + $form['tree-'.$term->tid] = array( '#type' => 'fieldset', // '#title' => $term->name, @@ -652,15 +735,17 @@ function materio_search_api_advanced_search_form($form, &$form_state){ '#tree' => true, ); + $placeholder = t("Select an option"); $form['tree-'.$term->tid]['children'] = array( '#type' => 'select', - '#title' => $term->name, + '#title' => $term->name, '#attributes' => array( "class"=>array("treelevel1"), - "title"=>"-Please select-", + "title"=>$placeholder, + "data-placeholder" => $placeholder, ), //, "form-control" '#theme_wrappers' => array('materio_search_api_form_element'), - '#empty_option' => '-Please select-', + '#empty_option' => $placeholder, '#empty_value' => 1, ); @@ -677,7 +762,10 @@ function materio_search_api_advanced_search_form($form, &$form_state){ if(count($children2)) $options[$tid] = $child->name; + $classes2 = array("treelevel2", $child->tid); + $options2 = []; + $dflt_values2 = []; foreach ($children2 as $tid2 => $child2) { // $child2 = taxonomy_term_load($child2->tid); // if($child2->field_used_on_advanced_search['und'][0]['value'] == 0){ @@ -685,31 +773,40 @@ function materio_search_api_advanced_search_form($form, &$form_state){ // } $options2[$tid2] = $child2->name; + + // default Values + if(in_array($tid2, $default_keys)) + $dflt_values2[] = $tid2; + } - if(count($options2)) + if(count($options2)){ + + if(count($dflt_values2)) + $classes2[] = 'visible'; + $form['tree-'.$term->tid]['tid-'.$child->tid] = array( '#type' => 'select', '#title' => $child->name, '#multiple' => true, '#attributes' => array( - "class" => array("treelevel2", $child->tid), // , "form-control" + "class" => $classes2, // , "form-control" "title" => "-Please select-", + "tid" => $child->tid, ), '#theme_wrappers' => array('materio_search_api_form_element'), - '#empty_option' => '-Please select-', - '#empty_value' => 1, + // '#empty_option' => '-Please select-', + // '#empty_value' => 1, '#options' => $options2, + '#default_value' => $dflt_values2, ); - - // $form['tree-'.$term->tid]['child-'.$tid] = array( - // '#type' => 'checkbox', - // '#title' => $child->name, - // // '#options' => $options, - // ); + } + } + if(count($options)){ + $form['tree-'.$term->tid]['children']['#options'] = $options; + }else{ + unset($form['tree-'.$term->tid]); } - $form['tree-'.$term->tid]['children']['#options'] = $options; - // break; } } @@ -735,30 +832,22 @@ function materio_search_api_advanced_search_form_validate($form, &$form_state){ } function materio_search_api_advanced_search_form_submit($form, &$form_state){ - // dsm($form_state, 'form_state'); - // global $user; - // if(user_access('use materio search api filters')){ - // foreach($form_state['values']['bundles_filter'] as $bundle => $value) - // if($value) - // $bundles[] = $bundle; + dsm($form, 'form'); + dsm($form_state, 'form_state'); + $fs_values = $form_state['values']; + dsm($fs_values, 'values'); - // # if no filter checked we checked them all by default - // if(!isset($bundles)) - // foreach($form_state['values']['bundles_filter'] as $bundle => $value) - // $bundles[] = $bundle; - // }else{ - // # if user have no access to filters - // $index = search_api_index_load(variable_get('mainsearchindex', -1)); - // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles']; + foreach($fs_values as $tree => $values){ + foreach ($values as $values => $selected) { + if(is_array($selected) && count($selected)){ + foreach ($selected as $tid) { + $tids[] = $tid; + } + } + } + } - // foreach ($indexed_bundles as $bundle) { - // $bundles[] = $bundle; - // } - // } - - // user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles))); - - // $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield']; + $form_state['redirect'] = 'explore/advanced/'.implode('/', $tids); } @@ -864,6 +953,9 @@ function materio_search_api_theme($existing, $type, $theme, $path) { function template_preprocess_materio_search_api_search_block(&$vars){ // dsm($vars, '$vars'); $vars['searchform'] = drupal_get_form("materio_search_api_search_form"); + if(user_access('use materio search api advanced search')){ + $vars['advancedsearchform'] = drupal_get_form("materio_search_api_advanced_search_form"); + } } function template_preprocess_materio_search_api_select_viewmode_block(&$vars){ @@ -1074,6 +1166,12 @@ function theme_materio_search_api_form_element($variables) { $attributes['class'][] = $value; } + foreach ($element['#attributes'] as $attr => $value) { + if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){ + $attributes[$attr] = $value; + } + } + $output = '' . "\n"; diff --git a/materio_search_api.pages.inc b/materio_search_api.pages.inc index 6e401492..eb228985 100755 --- a/materio_search_api.pages.inc +++ b/materio_search_api.pages.inc @@ -124,9 +124,21 @@ function materio_search_api_autocomplete_searchapi($typed = ''){ function materio_search_api_results_search(){ //dsm("materio_search_api_results_search"); - // retreive typed words separated by slahes as a sentence - $args = func_get_args(); - $typed = implode('/', $args); + // retreive typed words separated by slashes as a sentence + $keys = func_get_args(); + // dsm($args, 'args'); + if($keys[0] == "advanced"){ + $advanced = true; + array_shift($keys); + // dsm($keys, 'shifted keys'); + // foreach ($args as $arg) { + // $typed[] = $arg;//(integer)$arg; + // } + $typed = implode(' +', $keys); + }else{ + $typed = implode('/', $keys); + } + // remove query page params preg_match_all('/\?page=([0-9]+)/', $typed, $pages); @@ -139,7 +151,14 @@ function materio_search_api_results_search(){ global $language; global $user; - if(user_access('use materio search api')){ + if($advanced && user_access('use materio search api advanced search')){ + // dsm('advanced search'); + $index_machine_name = variable_get('advancedsearchindex_'.$language->language, -1); + // dsm($index_machine_name, '$index_machine_name'); + $index = search_api_index_load($index_machine_name); + } + else if(user_access('use materio search api')){ + // dsm('normal search'); // switch index depending on key words type full text or taxonomy term (autocomplete selection) $searchmode = isset($user->data['materiosearchapi_searchmode']) ? $user->data['materiosearchapi_searchmode'] : "fulltext"; switch($searchmode){ @@ -152,15 +171,19 @@ function materio_search_api_results_search(){ // break; } $index = search_api_index_load($index_machine_name); - }else if(user_access('use materio search api for breves')){ + } + else if(user_access('use materio search api for breves')){ + // dsm('limited search'); $index_machine_name = variable_get('brevessearchindex_'.$language->language, -1); // dsm($index_machine_name, '$index_machine_name'); $index = search_api_index_load($index_machine_name); } - // potential results index for anonymous and free user - $could_index_machine_name = variable_get('mainsearchindex_'.$language->language, -1); - $could_index = search_api_index_load($index_machine_name); + if(!user_access('use materio search api advanced search') && !user_access('use materio search api')){ + // potential results index for anonymous and free user + $could_index_machine_name = variable_get('mainsearchindex_'.$language->language, -1); + $could_index = search_api_index_load($index_machine_name); + } if ($typed) { // TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful @@ -184,9 +207,14 @@ function materio_search_api_results_search(){ $bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles; // dsm($bundles_filter, 'bundles_filter'); + if($advanced){ + $query_options = array('conjunction'=>'AND', 'parse mode'=>'direct'); + } + else{ + $query_options = array('conjunction'=>'OR', 'parse mode'=>'direct'); + } - $query = search_api_query($index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct')) - // ->keys(implode(' ', $keys)) + $query = search_api_query($index_machine_name, $query_options) ->keys($typed) ->range($offset, $limit); @@ -205,7 +233,7 @@ function materio_search_api_results_search(){ $results = $query->execute(); } - # in case of utilisateur search, run a real search to indicate how much items you could find + # in case of free user search, run a real search to indicate how much items you could find if(isset($could_index)){ $could_query = search_api_query($could_index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct')) // ->keys(implode(' ', $keys)) @@ -223,11 +251,17 @@ function materio_search_api_results_search(){ } // dsm($results, 'results'); - if(user_access('use materio search api for breves') || user_access('use materio search api')){ - if(user_access('use materio search api')){ - $items = $index->loadItems(array_keys($results['results'])); + if(user_access('use materio search api for breves') + || user_access('use materio search api') + || user_access('use materio search api advanced search')) + { + if(user_access('use materio search api') || user_access('use materio search api advanced search')){ + if(is_array($results['results'])){ + $items = $index->loadItems(array_keys($results['results'])); + }else{ + $items = array(); + } // dsm($items, 'items'); - // $count = $results['result count']; } else{ $items = array(); @@ -272,8 +306,20 @@ function materio_search_api_results_search(){ $ret['results']['#view_mode'] = $viewmode; - drupal_set_title(''.check_plain($typed), PASS_THROUGH); + // page title + if($advanced){ + foreach($keys as $tid){ + $t = taxonomy_term_load($tid); + $ptk[] = $t->name; + } + $page_title = implode(' +', $ptk); + } + else{ + $page_title = $typed; + } + drupal_set_title(''.check_plain($page_title), PASS_THROUGH); + // render results if(isset($results)){ // Load pager. // if ($results['result count'] > $page->options['per_page']) { diff --git a/templates/materio-search-api-search-block.tpl.php b/templates/materio-search-api-search-block.tpl.php index b4c0f557..9d63766f 100755 --- a/templates/materio-search-api-search-block.tpl.php +++ b/templates/materio-search-api-search-block.tpl.php @@ -1,3 +1,12 @@ - -print render($searchform); \ No newline at end of file + + + \ No newline at end of file