From e9a136db4c1c63ce66e727383499237370cc3027 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Wed, 16 Jul 2014 17:07:56 +0200 Subject: [PATCH 1/9] added admin settings for advanced search --- materio_search_api.admin.inc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/materio_search_api.admin.inc b/materio_search_api.admin.inc index ec77f236..74bc19ba 100755 --- a/materio_search_api.admin.inc +++ b/materio_search_api.admin.inc @@ -7,11 +7,11 @@ function materio_search_api_settings(){ foreach ($indexes as $machine_name => $index) { if($index->status == 1) $index_options[$index->machine_name] = $index->name; - } + } $languages = locale_language_list(); // dsm($languages, 'languages'); - + foreach ($languages as $lcode => $name) { $form['mainsearchindex_'.$lcode] = array( '#type'=>'select', @@ -36,7 +36,7 @@ function materio_search_api_settings(){ } - // TODO: select the activated viewmodes for change view mode and selected view mode + // TODO: select the activated viewmodes for change view mode and selected view mode $entity_infos = entity_get_info(); // dsm($entity_infos, 'entity_infos'); foreach ($entity_infos['node']['view modes'] as $viewmode => $value) { @@ -68,6 +68,19 @@ function materio_search_api_settings(){ ); } + $vocabularies = taxonomy_get_vocabularies(); + // dsm($vocabularies, 'vocabularies'); + foreach ($vocabularies as $vid => $voc){ + $vocs_options[$vid] = $voc->name; + } + + $form['advancedsearchvocs'] = array( + '#type'=>'select', + '#options'=>$vocs_options, + '#default_value' => variable_get('advancedsearchvocs', array()), + '#title' => t('Advanced Search Vocabularies'), + '#multiple' => true, + ); return system_settings_form($form); } \ No newline at end of file From 7ee6c9dc12cbc10333fe1eef18281b44beba0eaf Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Wed, 16 Jul 2014 17:09:23 +0200 Subject: [PATCH 2/9] created advanced search block --- materio_search_api.module | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/materio_search_api.module b/materio_search_api.module index c83e866a..f886684c 100755 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -38,6 +38,10 @@ function materio_search_api_permission() { 'title' => t('use Materio search api filters'), 'description' => t('Use materio search api filters.'), ), + 'use materio search api advanced search' => array( + 'title' => t('use materio search api advanced search'), + 'description' => t('Use materio search api advanced search.'), + ), ); } @@ -365,12 +369,16 @@ function materio_search_api_term_property_dup_name($term){ * Implements hook_block_info(). */ function materio_search_api_block_info() { - // This example comes from node.module. $blocks['materio_search_api_search'] = array( 'info' => t('Materio search api search'), 'cache' => DRUPAL_NO_CACHE ); + $blocks['materio_search_api_advanced_search'] = array( + 'info' => t('Materio search api advanced search'), + 'cache' => DRUPAL_NO_CACHE + ); + $blocks['materio_search_api_viewmode'] = array( 'info' => t('Materio search api view mode selection'), 'cache' => DRUPAL_NO_CACHE @@ -420,6 +428,12 @@ function materio_search_api_block_view($delta = '') { // $block['content'] = theme('materio_search_api_filters_block', array()); // } // break; + case 'materio_search_api_advanced_search': + if(user_access('use materio search api advanced search')){ + $block['subject'] = t('Advanced Search'); + $block['content'] = theme('materio_search_api_advanced_search_block', array()); + } + break; } return $block; } @@ -656,6 +670,11 @@ function materio_search_api_theme($existing, $type, $theme, $path) { 'pager' => NULL, 'count' => 0, ) + ), + 'materio_search_api_advanced_search_block' => array( + 'template' => 'materio-search-api-advanced-search-block', + 'path' => drupal_get_path('module', 'materio_search_api').'/templates', + 'arguments' => array(), ) ); } @@ -842,7 +861,6 @@ function template_preprocess_materio_search_api_results(array &$vars) { //dsm($vars, '$vars'); } - function template_preprocess_materio_search_api_actuality(&$vars){ // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars'); @@ -850,6 +868,12 @@ function template_preprocess_materio_search_api_actuality(&$vars){ $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"); + +} + /** * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - - */ From 124e1a7bc250dbc4657b18719771004581ca2b7a Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 29 Jul 2014 12:27:02 +0200 Subject: [PATCH 3/9] created advanced search form --- materio_search_api.module | 254 +++++++++++++++++++++++++++++++++++++- 1 file changed, 248 insertions(+), 6 deletions(-) diff --git a/materio_search_api.module b/materio_search_api.module index f886684c..6ddb4d08 100755 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -374,7 +374,7 @@ function materio_search_api_block_info() { 'cache' => DRUPAL_NO_CACHE ); - $blocks['materio_search_api_advanced_search'] = array( + $blocks['materio_sa_adv_search'] = array( 'info' => t('Materio search api advanced search'), 'cache' => DRUPAL_NO_CACHE ); @@ -428,7 +428,7 @@ function materio_search_api_block_view($delta = '') { // $block['content'] = theme('materio_search_api_filters_block', array()); // } // break; - case 'materio_search_api_advanced_search': + case 'materio_sa_adv_search': if(user_access('use materio search api advanced search')){ $block['subject'] = t('Advanced Search'); $block['content'] = theme('materio_search_api_advanced_search_block', array()); @@ -596,10 +596,175 @@ function materio_search_api_search_form_submit($form, &$form_state){ } +/** + * materiobase_search_form() + */ +function materio_search_api_advanced_search_form($form, &$form_state){ + // dsm($form_state, 'form_state'); + // dsm($form, 'form'); + // global $user; + + $form = array(); + + $vocabularies = variable_get('advancedsearchvocs', array()); + + foreach ($vocabularies as $vid) { + $voc = taxonomy_vocabulary_load($vid); + // dsm($voc, 'voc'); + + // $form['voc-'.$vid] = array( + // '#type' => 'fieldset', + // '#title' => $voc->name, + // '#tree' => true, + // ); + + $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'])) { + + // } + + 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"); + + $form['tree-'.$term->tid] = array( + '#type' => 'fieldset', + // '#title' => $term->name, + '#attributes' => array("class"=>array("treelevel1")), + '#tree' => true, + ); + + $form['tree-'.$term->tid]['children'] = array( + '#type' => 'select', + '#title' => $term->name, + '#attributes' => array( + "class"=>array("treelevel1"), + "title"=>"-Please select-", + ), //, "form-control" + '#theme_wrappers' => array('materio_search_api_form_element'), + '#empty_option' => '-Please select-', + '#empty_value' => 1, + ); + + $options = array(); + foreach ($children as $tid => $child) { + // $child = taxonomy_term_load($child->tid); + // if($child->field_used_on_advanced_search['und'][0]['value'] == 0){ + // continue; + // } + + $children2 = taxonomy_get_children($child->tid, $vid); + // dsm($children2, "children2"); + + if(count($children2)) + $options[$tid] = $child->name; + + $options2 = []; + foreach ($children2 as $tid2 => $child2) { + // $child2 = taxonomy_term_load($child2->tid); + // if($child2->field_used_on_advanced_search['und'][0]['value'] == 0){ + // continue; + // } + + $options2[$tid2] = $child2->name; + } + + if(count($options2)) + $form['tree-'.$term->tid]['tid-'.$child->tid] = array( + '#type' => 'select', + '#title' => $child->name, + '#multiple' => true, + '#attributes' => array( + "class" => array("treelevel2", $child->tid), // , "form-control" + "title" => "-Please select-", + ), + '#theme_wrappers' => array('materio_search_api_form_element'), + '#empty_option' => '-Please select-', + '#empty_value' => 1, + '#options' => $options2, + ); + + // $form['tree-'.$term->tid]['child-'.$tid] = array( + // '#type' => 'checkbox', + // '#title' => $child->name, + // // '#options' => $options, + // ); + } + $form['tree-'.$term->tid]['children']['#options'] = $options; + // break; + } + + } + + $form['show'] = array( + '#type' => 'submit',//'image_button', + // '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png', + '#value' => t('Find'), + '#attributes' => array("class"=>array("btn","btn-lg")), + '#prefix' => '
', + '#suffix' => '
', + ); + return $form; +} + +function materio_search_api_advanced_search_form_validate($form, &$form_state){ + // dsm($form, '$form'); + // dsm($form_state, '$form_state'); + // dsm(strlen($form_state['values']['searchfield'])); + // if (strlen(trim($form_state['values']['searchfield'])) <= 1) { + // form_set_error('searchfield', 'Please enter at least 2 characters keyword.'); + // } +} + +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; + + // # 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 ($indexed_bundles as $bundle) { + // $bundles[] = $bundle; + // } + // } + + // user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles))); + + // $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield']; +} + + /** * viewmode */ - function _materio_search_api_change_viewmode($vm){ // dsm($vm, '_materio_search_api_change_viewmode'); @@ -675,7 +840,10 @@ function materio_search_api_theme($existing, $type, $theme, $path) { 'template' => 'materio-search-api-advanced-search-block', 'path' => drupal_get_path('module', 'materio_search_api').'/templates', 'arguments' => array(), - ) + ), + 'materio_search_api_form_element' => array( + 'render element' => 'element', + ), ); } @@ -869,11 +1037,85 @@ function template_preprocess_materio_search_api_actuality(&$vars){ } function template_preprocess_materio_search_api_advanced_search_block(&$vars){ - dsm($vars, "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'); + + $element = &$variables['element']; + + // This function is invoked as theme wrapper, but the rendered form element + // may not necessarily have been processed by form_builder(). + $element += array( + '#title_display' => 'before', + ); + + // Add element #id for #type 'item'. + if (isset($element['#markup']) && !empty($element['#id'])) { + $attributes['id'] = $element['#id']; + } + // Add element's #type and #name as class to aid with JS/CSS selectors. + $attributes['class'] = array('form-item'); + if (!empty($element['#type'])) { + $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-'); + } + if (!empty($element['#name'])) { + $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => '')); + } + // Add a class for disabled elements to facilitate cross-browser styling. + if (!empty($element['#attributes']['disabled'])) { + $attributes['class'][] = 'form-disabled'; + } + + # adding element class to wrapper + foreach ($element['#attributes']['class'] as $value) { + $attributes['class'][] = $value; + } + + $output = '' . "\n"; + + + // If #title is not set, we don't display any label or required marker. + if (!isset($element['#title'])) { + $element['#title_display'] = 'none'; + } + $prefix = isset($element['#field_prefix']) ? '' . $element['#field_prefix'] . ' ' : ''; + $suffix = isset($element['#field_suffix']) ? ' ' . $element['#field_suffix'] . '' : ''; + + switch ($element['#title_display']) { + case 'before': + case 'invisible': + $output .= ' ' . theme('form_element_label', $variables); + $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n"; + break; + + case 'after': + $output .= ' ' . $prefix . $element['#children'] . $suffix; + $output .= ' ' . theme('form_element_label', $variables) . "\n"; + break; + + case 'none': + case 'attribute': + // Output no label and no required marker, only the children. + $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n"; + break; + } + + if (!empty($element['#description'])) { + $output .= '
' . $element['#description'] . "
\n"; + } + + $output .= "\n"; + + return $output; +} + + + + + /** * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - - */ From 6fdb4e0dc950ad2cbe572381f19943754d18c297 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Wed, 27 Aug 2014 15:27:15 +0200 Subject: [PATCH 4/9] 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 From afce4662bdbb942dc231b949ec2dbe293e0aec9e Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Wed, 3 Sep 2014 14:14:19 +0200 Subject: [PATCH 5/9] fixe --- materio_search_api.module | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/materio_search_api.module b/materio_search_api.module index 1e28d276..3c654c85 100755 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -705,11 +705,11 @@ function materio_search_api_advanced_search_form($form, &$form_state){ $voc = taxonomy_vocabulary_load($vid); // dsm($voc, 'voc'); - // $form['voc-'.$vid] = array( - // '#type' => 'fieldset', - // '#title' => $voc->name, - // '#tree' => true, - // ); + $form['voc-'.$vid] = array( + '#type' => 'fieldset', + '#title' => $voc->name, + '#tree' => true, + ); $tree = taxonomy_get_tree($vid, 0, 1); // dsm($tree, 'tree'); From c5f5eb4f6409ba3b315931db69829adbbfac81e1 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Fri, 5 Sep 2014 11:37:36 +0200 Subject: [PATCH 6/9] added some comments --- materio_search_api.pages.inc | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/materio_search_api.pages.inc b/materio_search_api.pages.inc index eb228985..61ade9da 100755 --- a/materio_search_api.pages.inc +++ b/materio_search_api.pages.inc @@ -189,47 +189,52 @@ function materio_search_api_results_search(){ // TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful try { - $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full'); - // dsm($viewmode, 'viewmode'); + # retrieve viewmode and then use it to define the query range + $viewmode = isset($user->data['materiosearchapi_viewmode']) + ? $user->data['materiosearchapi_viewmode'] + : variable_get('defaultviewmode', 'full'); $limit = variable_get($viewmode.'_limite', '10'); $offset = pager_find_page() * $limit; //$page*$limit;// if(isset($index)){ - // $index_machine_name = variable_get('brevessearchindex_'.$language->language, -1); - // dsm($index_machine_name, '$index_machine_name'); + # define default bundle option (materiaux, breves) $default_bundles = array(); if(isset($index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'])){ $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles']; foreach ($indexed_bundles as $bundle) { $default_bundles[] = $bundle; } } + # choose solr query bundle option + $bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) + ? $user->data['materiosearchapi_bundlesfilter'] + : $default_bundles; - $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'); - } + # choose solr query options + $query_options = $advanced + ? array('conjunction'=>'AND', 'parse mode'=>'direct') + : array('conjunction'=>'OR', 'parse mode'=>'direct'); + #create the solr query $query = search_api_query($index_machine_name, $query_options) ->keys($typed) ->range($offset, $limit); + # apply bundle options to solr query if usefull if(count($bundles_filter)){ $filter = $query->createFilter('OR'); - foreach ($bundles_filter as $type) { + foreach ($bundles_filter as $type) $filter->condition('type', $type, '='); - } - // dsm($filter, 'filter'); + $query->filter($filter); } // $query->setOption('search_api_bypass_access', true); + + # add user access solr query option $query->setOption('search_api_access_account', $user); + # execute solr query and record results $results = $query->execute(); } From 8f54fcd944d10f399807f0acd58b2243569cf4e2 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 16 Sep 2014 16:46:55 +0800 Subject: [PATCH 7/9] some changes in the form structure --- materio_search_api.module | 47 +++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/materio_search_api.module b/materio_search_api.module index 3c654c85..7c6cd81a 100755 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -693,21 +693,27 @@ function materio_search_api_advanced_search_form($form, &$form_state){ $form = array( 'title' => array( - '#prefix' => "

", + '#prefix' => "

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

", + '#suffix' => "", ) ); $vocabularies = variable_get('advancedsearchvocs', array()); + $form['slider'] = array( + '#type' => 'fieldset', + // '#title' => $voc->name, + '#tree' => true, + ); + foreach ($vocabularies as $vid) { $voc = taxonomy_vocabulary_load($vid); // dsm($voc, 'voc'); - $form['voc-'.$vid] = array( + $form['slider']['voc-'.$vid] = array( '#type' => 'fieldset', - '#title' => $voc->name, + // '#title' => $voc->name, '#tree' => true, ); @@ -728,7 +734,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){ || !count($children)) continue; - $form['tree-'.$term->tid] = array( + $form['slider']['voc-'.$vid]['tree-'.$term->tid] = array( '#type' => 'fieldset', // '#title' => $term->name, '#attributes' => array("class"=>array("treelevel1")), @@ -736,7 +742,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){ ); $placeholder = t("Select an option"); - $form['tree-'.$term->tid]['children'] = array( + $form['slider']['voc-'.$vid]['tree-'.$term->tid]['children'] = array( '#type' => 'select', '#title' => $term->name, '#attributes' => array( @@ -785,7 +791,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){ if(count($dflt_values2)) $classes2[] = 'visible'; - $form['tree-'.$term->tid]['tid-'.$child->tid] = array( + $form['slider']['voc-'.$vid]['tree-'.$term->tid]['tid-'.$child->tid] = array( '#type' => 'select', '#title' => $child->name, '#multiple' => true, @@ -803,19 +809,19 @@ function materio_search_api_advanced_search_form($form, &$form_state){ } } if(count($options)){ - $form['tree-'.$term->tid]['children']['#options'] = $options; + $form['slider']['voc-'.$vid]['tree-'.$term->tid]['children']['#options'] = $options; }else{ - unset($form['tree-'.$term->tid]); + unset($form['slider']['voc-'.$vid]['tree-'.$term->tid]); } } } - $form['show'] = array( - '#type' => 'submit',//'image_button', - // '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png', + $form['slider']['show'] = array( + '#type' => 'image_button', + '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png', '#value' => t('Find'), - '#attributes' => array("class"=>array("btn","btn-lg")), + // '#attributes' => array("class"=>array("btn","btn-lg")), '#prefix' => '
', '#suffix' => '
', ); @@ -834,14 +840,17 @@ function materio_search_api_advanced_search_form_validate($form, &$form_state){ function materio_search_api_advanced_search_form_submit($form, &$form_state){ dsm($form, 'form'); dsm($form_state, 'form_state'); - $fs_values = $form_state['values']; + $fs_values = $form_state['values']['slider']; + unset($fs_values['show']); dsm($fs_values, 'values'); - foreach($fs_values as $tree => $values){ - foreach ($values as $values => $selected) { - if(is_array($selected) && count($selected)){ - foreach ($selected as $tid) { - $tids[] = $tid; + foreach($fs_values as $voc => $trees){ + foreach($trees as $tree => $values){ + foreach ($values as $values => $selected) { + if(is_array($selected) && count($selected)){ + foreach ($selected as $tid) { + $tids[] = $tid; + } } } } From 0bba2d06577d893aaba70277ed105f1b08feb450 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 16 Sep 2014 21:09:06 +0800 Subject: [PATCH 8/9] bugfixe : i18n was filtrering to much terms with taxonomy_get_tree fixed in materio_taxonomy module with query alter --- materio_search_api.module | 5 ++-- materio_taxonomy.module | 52 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/materio_search_api.module b/materio_search_api.module index 7c6cd81a..b0d579d9 100755 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -708,6 +708,8 @@ function materio_search_api_advanced_search_form($form, &$form_state){ ); foreach ($vocabularies as $vid) { + // dsm($vid, "vid"); + $voc = taxonomy_vocabulary_load($vid); // dsm($voc, 'voc'); @@ -717,14 +719,13 @@ function materio_search_api_advanced_search_form($form, &$form_state){ '#tree' => true, ); - $tree = taxonomy_get_tree($vid, 0, 1); + $tree = taxonomy_get_tree($vid, 0, 1, TRUE); // dsm($tree, 'tree'); if(!count($tree)) continue; foreach ($tree as $term) { - $term = taxonomy_term_load($term->tid); // dsm($term, "term"); $children = taxonomy_get_children($term->tid, $vid); diff --git a/materio_taxonomy.module b/materio_taxonomy.module index 07a7a404..2e5a987d 100755 --- a/materio_taxonomy.module +++ b/materio_taxonomy.module @@ -1,4 +1,4 @@ - t('access taxonomy terms page'), 'description' => t(''), ); - + return $perms; } @@ -26,4 +26,52 @@ function materio_taxonomy_menu_alter(&$items) { $items['taxonomy/term/%taxonomy_term']['access arguments'] = array('access taxonomy terms page'); $items['taxonomy/term/%taxonomy_term/view']['access arguments'] = array('access taxonomy terms page'); $items['taxonomy/term/%taxonomy_term/feed']['access arguments'] = array('access taxonomy terms page'); +} + + + +/** + * Implementation of hook_query_term_access_alter(). + * + * debug de i18n_select_query_term_access_alter() + * qui filtre les terms par language mm pour les vocabulaires en mode localized. + * utile pour la recherche avancée de materio_search_api materio_search_api_advanced_search_form() + * + */ +function materio_taxonomy_query_term_access_alter(QueryAlterableInterface $query) { + // dsm($query, 'materio taxo query'); + $conditions =& $query->conditions(); + + # roll over condition first time to determine i18n_voc_mode + $i18n_voc_mode = 0; + foreach ($conditions as $condition) { + if (is_array($condition)) { + // dsm($condition, 'conditon '.$condition['field']); + if($condition['field'] == 't.vid'){ + $vid = $condition['value']; + $i18n_voc_mode = i18n_taxonomy_vocabulary_mode($vid); + // dsm($i18n_voc_mode, 'i18n_voc_mode'); + } + } + } + + # roll over conditions a second time to re-alter/fixe language selection + # i18n vocabulary mode is 1 for localized terms + if($i18n_voc_mode == 1){ + foreach ($conditions as $index => $condition) { + if (is_array($condition)) { + // dsm($condition, 'conditon '.$condition['field']); + if($condition['field'] == 't.language' && $i18n_voc_mode == 1){ + $l = array('und'); + $ll = i18n_language_list(); + // dsm($ll, 'list language'); + foreach ($ll as $langcode => $langname) { + $l[] = $langcode; + } + $conditions[$index]['value'] = $l; + } + } + } + } + } \ No newline at end of file From 1cc9a63c46aa3a26944f88a36f3444d77094391d Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Sat, 27 Sep 2014 09:58:09 +0800 Subject: [PATCH 9/9] code cleaning --- materio_search_api.module | 11 +++++++---- materio_search_api.pages.inc | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/materio_search_api.module b/materio_search_api.module index b0d579d9..c51b58f2 100755 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -445,6 +445,7 @@ function materio_search_api_block_info() { 'cache' => DRUPAL_NO_CACHE ); + # NOT USED $blocks['materio_search_api_viewmode'] = array( 'info' => t('Materio search api view mode selection'), 'cache' => DRUPAL_NO_CACHE @@ -494,6 +495,8 @@ function materio_search_api_block_view($delta = '') { // $block['content'] = theme('materio_search_api_filters_block', array()); // } // break; + + # NOT USED case 'materio_sa_adv_search': if(user_access('use materio search api advanced search')){ $block['subject'] = t('Advanced Search'); @@ -675,7 +678,7 @@ function materio_search_api_search_form_submit($form, &$form_state){ /** - * materiobase_search_form() + * materiobase_advancedsearch_form() */ function materio_search_api_advanced_search_form($form, &$form_state){ // dsm($form_state, 'form_state'); @@ -839,11 +842,11 @@ function materio_search_api_advanced_search_form_validate($form, &$form_state){ } function materio_search_api_advanced_search_form_submit($form, &$form_state){ - dsm($form, 'form'); - dsm($form_state, 'form_state'); + // dsm($form, 'form'); + // dsm($form_state, 'form_state'); $fs_values = $form_state['values']['slider']; unset($fs_values['show']); - dsm($fs_values, 'values'); + // dsm($fs_values, 'values'); foreach($fs_values as $voc => $trees){ foreach($trees as $tree => $values){ diff --git a/materio_search_api.pages.inc b/materio_search_api.pages.inc index 61ade9da..52e47bd6 100755 --- a/materio_search_api.pages.inc +++ b/materio_search_api.pages.inc @@ -159,7 +159,7 @@ function materio_search_api_results_search(){ } 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) + # 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){ case "fulltext": @@ -180,13 +180,13 @@ function materio_search_api_results_search(){ } if(!user_access('use materio search api advanced search') && !user_access('use materio search api')){ - // potential results index for anonymous and free user + # 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 + # TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful try { # retrieve viewmode and then use it to define the query range