Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
SearchAdaptor.php
1 <?php
2 
3 /**
4  *
5  **/
7 {
8  /**
9  * Number of page
10  *
11  * @public int
12  **/
13  public $pageNumber;
14 
15  /**
16  * Items per page
17  *
18  * @public int
19  **/
20  public $pageSize;
21 
22  public $searchApi;
23 
24  private $_filteredResults = array();
25 
26  private $_searchTotalResults;
27 
28  private $_searchOffset = 0;
29 
30  private $_term;
31 
32  private $_params;
33 
34  private $_usePersonalization;
35 
36 
37  public function __construct($term, $params = array(), $usePersonalization = false)
38  {
39  $this->_term = $term;
40  $this->_params = $params;
41  $this->_usePersonalization = $usePersonalization;
42  $this->searchApi = new SearchApi();
43  }
44 
45  /**
46  * Get current page results
47  *
48  * @return array
49  **/
50  public function fetchNext()
51  {
52  while((count($this->_filteredResults) < $this->pageSize * $this->pageNumber) && $this->existsMore()){
53  $portion = $this->fetchPortion();
54  if($portion === false){
55  return array();
56  } else {
57  $this->_filteredResults =array_merge($this->_filteredResults, $portion) ;
58  }
59  }
60  $next = array_slice($this->_filteredResults, $this->pageSize * $this->pageNumber - $this->pageSize, $this->pageSize);
61 
62  return $next;
63  }
64 
65  public function existsMore()
66  {
67  if($this->_searchTotalResults === null){
68  return true;
69  }
70  return $this->_searchTotalResults - $this->_searchOffset > 0 ||
71  $this->pageSize * $this->pageNumber < count($this->_filteredResults);
72  }
73 
74  protected function fetchPortion(){
75  $response = $this->searchApi->search($this->_term,$this->_params, $this->_searchOffset, $this->pageSize);
76  $this->_searchOffset = $response['meta']['start'] + count($response['results']);
77  $this->_searchTotalResults = $response['meta']['totalhits'];
78  if(!$response){
79  return false;
80  }
81  $results = array_values($response['results']);
82  if(empty($results)) {
83  return false;
84  }
85 
86  if($this->_usePersonalization) {
87  $results = $this->_personalize($results);
88  }
89  return $results;
90  }
91 
92  private function _personalize($data)
93  {
94  // get names of personalisation fields
95  $filters = Yii::app()->getModule('contentSource')->contentSource->personalisationFields;
96  foreach ($data as $key=>$resultItem) {
97  $persAttributes = array();
98  foreach ($filters as $filter) {
99  if (isset($resultItem['attributes'][$filter]) && trim($resultItem['attributes'][$filter])!='' && trim($resultItem['attributes'][$filter])!='NULL') {
100  $persAttributes = array_merge($persAttributes, explode(Yii::app()->getModule('search')->personalisationDelimiter, $resultItem['attributes'][$filter]));
101  }
102  }
103  /* check if current user can see the item */
104  if ( !Yii::app()->getModule('personalisation')->rule->checkAccess(Yii::app()->user->id, $persAttributes) ) {
105  unset($data[$key]);
106  Yii::trace('Skipped ' . $resultItem['contentid'].' Permissions: '.implode(',', $persAttributes), 'search.driver');
107  }
108  }
109  return $data;
110  }
111 
112 }