Passed
Push — master ( f4b5df...d5260a )
by Aimeos
06:06
created

SQLSrv::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
nc 2
nop 1
dl 0
loc 38
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020
6
 * @package MShop
7
 * @subpackage Index
8
 */
9
10
11
namespace Aimeos\MShop\Index\Manager\Text;
12
13
14
/**
15
 * SQL Server based index text for searching in product tables.
16
 *
17
 * @package MShop
18
 * @subpackage Index
19
 */
20
class SQLSrv
21
	extends \Aimeos\MShop\Index\Manager\Text\Standard
22
{
23
	private $searchConfig = array(
24
		'index.text:relevance' => array(
25
			'code' => 'index.text:relevance()',
26
			'label' => 'Product texts, parameter(<language ID>,<search term>)',
27
			'type' => 'float',
28
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
29
			'public' => false,
30
		),
31
		'sort:index.text:relevance' => array(
32
			'code' => 'sort:index.text:relevance()',
33
			'label' => 'Product text sorting, parameter(<language ID>,<search term>)',
34
			'type' => 'float',
35
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
36
			'public' => false,
37
		),
38
	);
39
40
41
	/**
42
	 * Initializes the object
43
	 *
44
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
45
	 */
46
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
47
	{
48
		parent::__construct( $context );
49
50
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
51
		$level = $context->getConfig()->get( 'mshop/index/manager/sitemode', $level );
52
53
		if( $context->getConfig()->get( 'mshop/index/manager/text/sqlsrv/fulltext', false ) )
54
		{
55
			$search = ':site AND mindte."langid" = $1 AND (
56
				SELECT mindte_ft.RANK
57
				FROM CONTAINSTABLE("mshop_index_text", "content", $2) AS mindte_ft
58
				WHERE mindte."id" = mindte_ft."KEY"
59
			)';
60
			$sort = 'mindte_ft.RANK';
61
62
			$func = $this->getFunctionRelevance();
63
		}
64
		else
65
		{
66
			$search = ':site AND mindte."langid" = $1 AND CHARINDEX( $2, content )';
67
			$sort = '-CHARINDEX( $2, content )';
68
69
			$func = function( $source, array $params ) {
70
71
				if( isset( $params[1] ) ) {
72
					$params[1] = mb_strtolower( $params[1] );
73
				}
74
75
				return $params;
76
			};
77
		}
78
79
		$expr = $this->getSiteString( 'mindte."siteid"', $level );
80
81
		$this->searchConfig['index.text:relevance']['internalcode'] = str_replace( ':site', $expr, $search );
82
		$this->searchConfig['sort:index.text:relevance']['internalcode'] = $sort;
83
		$this->searchConfig['index.text:relevance']['function'] = $func;
84
	}
85
86
87
	/**
88
	 * Returns a list of objects describing the available criterias for searching.
89
	 *
90
	 * @param bool $withsub Return also attributes of sub-managers if true
91
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attriubte items
92
	 */
93
	public function getSearchAttributes( bool $withsub = true ) : array
94
	{
95
		$list = parent::getSearchAttributes( $withsub );
96
97
		foreach( $this->searchConfig as $key => $fields ) {
98
			$list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $fields );
99
		}
100
101
		return $list;
102
	}
103
104
105
	/**
106
	 * Returns the search function for searching by relevance
107
	 *
108
	 * @return \Closure Relevance search function
109
	 */
110
	protected function getFunctionRelevance()
111
	{
112
		return function( $source, array $params ) {
113
114
			if( isset( $params[1] ) )
115
			{
116
				$strings = [];
117
				$regex = '/(\&|\||\!|\-|\+|\>|\<|\(|\)|\~|\*|\:|\"|\'|\@|\\| )+/';
118
				$search = trim( preg_replace( $regex, ' ', $params[1] ), "' \t\n\r\0\x0B" );
119
120
				foreach( explode( ' ', $search ) as $part )
121
				{
122
					$len = strlen( $part );
123
124
					if( $len > 0 ) {
125
						$strings[] = '"' . mb_strtolower( $part ) . '*"';
126
					}
127
				}
128
129
				$params[1] = '\'' . join( ' | ', $strings ) . '\'';
130
			}
131
132
			return $params;
133
		};
134
	}
135
}
136