| 
					
				 | 
			
			
				@@ -21,11 +21,14 @@ class EdlpSearchController extends ControllerBase { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    protected $formBuilder; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   protected $sapi_index_id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    * Construct. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   public function __construct(FormBuilder $formBuilder) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $this->formBuilder = $formBuilder; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->sapi_index_id = "collection"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   /** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -78,6 +81,98 @@ class EdlpSearchController extends ControllerBase { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  public function autocomplete(Request $request){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // TODO: get field_name parameter 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->request = $request; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->getRequestAutocompleteArgs(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // run a search on selected field and get the fields unique values 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->autocompleteQuery(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $response = new JsonResponse($this->matches); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return $response; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // return array( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    //   "#markup" => "autocomplete" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // ); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  * autocompleteQuery 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  private function autocompleteQuery(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // TODO: get the sapi index id with settings 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /* @var $sapi_index \Drupal\search_api\IndexInterface */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $sapi_index = Index::load($this->sapi_index_id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // dpm($sapi_index); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // Create the query. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $query = $sapi_index->query(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $query->setSearchID('edlp_search:autocomplete'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $parse_mode = \Drupal::getContainer() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ->get('plugin.manager.search_api.parse_mode') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ->createInstance('direct'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $query->setParseMode($parse_mode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // $query->range(0, 20); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 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)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      // dpm($this->keys); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      $query->keys($this->keys); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // select the field in which search will be performed 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $query->setFulltextFields(array($this->field_name)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $result = $query->execute(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $items = $result->getResultItems(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->matches = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    foreach ($items as $item) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        /** @var \Drupal\Core\Entity\EntityInterface $entity */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $fields = $item->getField($this->field_name)->getValues(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      }catch (SearchApiException $e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if (!$fields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      $field_text = $fields[0]->getText(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if( in_array($field_text, $this->matches) ){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      $this->matches[] = $field_text; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if(count($this->matches) > 14){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // dpm($this->matches); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  * getRequestArgs 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  private function getRequestAutocompleteArgs(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // dpm($this->request); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->field_name = $this->request->query->get('field_name'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->keys = $this->request->query->get('q'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   * searchResults 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   */ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -85,7 +180,7 @@ class EdlpSearchController extends ControllerBase { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $this->request = $request; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // TODO: what if no results 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->getRequestSearchArgs(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     return $this->getrenderable(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   } 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -97,6 +192,8 @@ class EdlpSearchController extends ControllerBase { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $this->request = $request; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $this->getRequestSearchArgs(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $renderable = $this->getrenderable(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $rendered = render($renderable); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -106,8 +203,6 @@ class EdlpSearchController extends ControllerBase { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       $results_nids[] = $item->id(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // TODO: what if no results 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $response = new JsonResponse(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $response->setData([ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       'test'=>'search results', 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -151,11 +246,9 @@ class EdlpSearchController extends ControllerBase { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   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'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    $sapi_index = Index::load($this->sapi_index_id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     // dpm($sapi_index); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     // Create the query. 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -222,7 +315,8 @@ class EdlpSearchController extends ControllerBase { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   * getRequestArgs 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  private function getRequestArgs(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  private function getRequestSearchArgs(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // dpm($this->request); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $this->keys = $this->request->query->get('keys'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     $this->entries = $this->request->query->get('entries'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   } 
			 |