Bachir Soussi Chiadmi 6 years ago
parent
commit
9bfbd861ca
1 changed files with 56 additions and 32 deletions
  1. 56 32
      sites/all/modules/gui/materiobasemod/materio_search_api.module

+ 56 - 32
sites/all/modules/gui/materiobasemod/materio_search_api.module

@@ -742,7 +742,7 @@ function materio_search_api_search_form_submit($form, &$form_state){
  * materio_search_api_advanced_search_form()
  */
 function materio_search_api_advanced_search_form($form, &$form_state){
-  //  dsm($form_state, 'form_state');
+  dsm($form_state, 'form_state');
   // dsm($form, 'form');
   // global $user;
   // $form = array();
@@ -754,18 +754,22 @@ function materio_search_api_advanced_search_form($form, &$form_state){
   // dsm($voc, "voc");
 
   $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);
 
   // get the keywords from args and reconstruct the filter lines with default values
   $args_values = array();
   if($path == 'explore' && $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
@@ -782,8 +786,10 @@ function materio_search_api_advanced_search_form($form, &$form_state){
         }
       }
     }
-    // dsm($args_values, 'default_values');
   }
+  // dsm($args_values, 'args_values');
+  // TODO: show result count live
+  // TODO: filter next select form item
 
   // define number of lines,
   // 1 by default
@@ -824,16 +830,14 @@ function materio_search_api_advanced_search_form($form, &$form_state){
     if(strpos($trigger['#name'], 'rm-filter-') === 0){
       // $form_state['filterlines'] = $form_state['filterlines']+1;
       $rmline = $trigger['#return_value'];
+      dsm($rmline, "rmline");
     }
   }
 
-  // TODO: show result count live
-  // TODO: filter next select form item
-
   // build first level taxonomy tree and select options
   $taxotree = taxonomy_get_tree($vid, 0, 1, false);
-  $level_0_ops = array(0=>t("Choose a filter"));
   foreach ($taxotree as $index => $term) {
+    // TODO: get translated tag name
     $level_0_ops[$term->tid] = t($term->name);
   }
 
@@ -841,7 +845,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){
   for ($l=0; $l < $form_state['filterlines']; $l++) {
 
     // if rmline this line,
-    // continue the loop withour creating the line
+    // continue the loop without creating the line
     if($l === $rmline){
       continue;
     }
@@ -849,12 +853,12 @@ function materio_search_api_advanced_search_form($form, &$form_state){
     // 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){
+    if($rmline !== false && $l >= $rmline){
       $ll = $l-1;
     }else{
       $ll = $l;
     }
-    // dsm("l : ".$l." | ll : ".$ll);
+    dsm("l : ".$l." | ll : ".$ll);
 
     $form['filters']['filterline-'.$ll] = array(
       '#type' => 'container',
@@ -867,33 +871,38 @@ function materio_search_api_advanced_search_form($form, &$form_state){
     // from from_state values if ajax
     // or from url args
     // or 0
+    $default0 = null;
     if(isset($values['filter-'.$l.'-0']) && $values['filter-'.$l.'-0']){
-      $default = $values['filter-'.$l.'-0'];
-    }else{
-      $default = isset($args_values[$l][0]) ? $args_values[$l][0] : 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',
-      '#default_value' => $default,
+      '#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($default){
-        // $default = $values['filter-'.$l.'-0'];
-        // $form['filters']['filterline-'.$ll]['filter-'.$ll.'-0']['#default_value'] = $default;
+    if($default0){
 
         // build second level taxonomy tree and select options
-        $taxotree1 = taxonomy_get_tree($vid, $default, 1, false);
-        $level_1_ops = array(0=>t("Choose a filter"));
+        $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);
         }
 
@@ -901,38 +910,48 @@ function materio_search_api_advanced_search_form($form, &$form_state){
         // from from_state values if ajax
         // or from url args
         // or 0
+        $default1 = null;
         if(isset($values['filter-'.$l.'-1']) && $values['filter-'.$l.'-1']){
-          $default = $values['filter-'.$l.'-1'];
-        }else{
-          $default = isset($args_values[$l][1]) ? $args_values[$l][1] : 0;
+          $default1 = $values['filter-'.$l.'-1'];
+        }elseif(isset($args_values[$l][1])){
+          $default1 = $args_values[$l][1];
         }
-        // dsm($default, "default 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',
-          '#default_value' => $default,
+          '#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($default){
-            // $default = $values['filter-'.$l.'-1'];
-            // $form['filters']['filterline-'.$ll]['filter-'.$ll.'-1']['#default_value'] = $default;
+        if($default1){
 
             // build second level taxonomy tree and select options
-            $taxotree2 = taxonomy_get_tree($vid, $default, 1, false);
+            $taxotree2 = taxonomy_get_tree($vid, $default1, 1, false);
             if(count($taxotree2)){
-              $default = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
-              // dsm($default, "default 2");
+              // $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(0=>t("Choose a filter"));
+              $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
@@ -940,8 +959,12 @@ function materio_search_api_advanced_search_form($form, &$form_state){
                 '#type' => 'select',
                 '#options' => $level_2_ops,
                 '#name' => 'filter-'.$ll.'-2',
-                '#default_value' => $default,
+                '#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;
             }
         }
     }
@@ -1010,6 +1033,7 @@ function materio_search_api_advanced_search_form($form, &$form_state){
     '#return_value' => "search",
   );
 
+  dsm($form, 'form');
   return $form;
 }