1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: gordon |
6
|
|
|
* Date: 25/3/2561 |
7
|
|
|
* Time: 17:01 น. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch\Helper; |
11
|
|
|
|
12
|
|
|
use SilverStripe\Control\Controller; |
13
|
|
|
|
14
|
|
|
class FacetLinkHelper |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $query; |
18
|
|
|
|
19
|
|
|
/** @var array<string,string|int|float|bool> */ |
20
|
|
|
private $params = []; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $facetInContext = ''; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$controller = Controller::curr(); |
28
|
|
|
$params = $controller->getRequest()->getVars(); |
29
|
|
|
$this->query = isset($params['q']) |
30
|
|
|
? $params['q'] |
31
|
|
|
: ''; |
32
|
|
|
$this->params = $params; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function setFacetInContext(string $facetInContext): void |
37
|
|
|
{ |
38
|
|
|
$this->facetInContext = $facetInContext; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** @param bool|float|int|string $key */ |
43
|
|
|
public function isSelectedFacet($key): bool |
44
|
|
|
{ |
45
|
|
|
return isset($this->params[$this->facetInContext]) && $this->params[$this->facetInContext] === $key; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** @param bool|float|int|string $facetKey */ |
50
|
|
|
public function getDrillDownFacetLink(string $searchPageLink, $facetKey): string |
51
|
|
|
{ |
52
|
|
|
$result = $searchPageLink . '?'; |
53
|
|
|
$facetParams = \array_merge($this->params, [$this->facetInContext => $facetKey]); |
54
|
|
|
foreach ($facetParams as $key => $value) { |
55
|
|
|
$result .= $key .'=' . ($value) .'&'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$result = \rtrim($result, '&'); |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** @param bool|float|int|string $facetKey */ |
65
|
|
|
public function getClearFacetLink(string $searchPageLink, $facetKey): string |
66
|
|
|
{ |
67
|
|
|
echo 'FK=' . $facetKey . '<br/>'; |
68
|
|
|
$result = $searchPageLink . '?'; |
69
|
|
|
foreach ($this->params as $key => $value) { |
70
|
|
|
if ($key === $facetKey) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$result .= $key .'=' . ($value) .'&'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$result = \rtrim($result, '&'); |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|