fulltext_directory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\search;
12
13
use \ernadoo\phpbbdirectory\core\helper;
14
15
class fulltext_directory extends helper
16
{
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/**
21
	* Constructor
22
	*
23
	* @param \phpbb\db\driver\driver_interface $db
24
	*/
25
	public function __construct(\phpbb\db\driver\driver_interface $db)
26
	{
27
		$this->db = $db;
28
	}
29
30
	/**
31
	* Performs a search on keywords depending on display specific params. You have to run split_keywords() first
32
	*
33
	* @param	array		$keywords_ary		contains each words to search
34
	* @param	string		$fields				contains either titleonly (link titles should be searched), desconly (only description bodies should be searched)
35
	* @param	string		$terms				is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
36
	* @param	array		$sort_by_sql		contains SQL code for the ORDER BY part of a query
37
	* @param	string		$sort_key			is the key of $sort_by_sql for the selected sorting
38
	* @param	string		$sort_dir			is either a or d representing ASC and DESC
39
	* @param	string		$sort_days			specifies the maximum amount of days a post may be old
40
	* @param	array		$ex_cid_ary			specifies an array of category ids which should not be searched
41
	* @param	int			$cat_id				is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
42
	* @param	array		&$id_ary			passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
43
	* @param	int			$start				indicates the first index of the page
44
	* @param	int			$per_page			number of ids each page is supposed to contain
45
	* @return	int								total number of results
46
	*/
47
	public function keyword_search($keywords_ary, $fields, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_cid_ary, $cat_id, &$id_ary, $start, $per_page)
48
	{
49
		$matches = array();
50
51
		switch ($fields)
52
		{
53
			case 'titleonly':
54
				$matches[] = 'l.link_name';
55
			break;
56
57
			case 'desconly':
58
				$matches[] = 'l.link_description';
59
			break;
60
61
			default:
62
				$matches = array('l.link_name', 'l.link_description');
63
		}
64
65
		$search_query = '';
66
67
		foreach ($keywords_ary as $word)
68
		{
69
			$match_search_query = '';
70
			foreach ($matches as $match)
71
			{
72
				$match_search_query .= (($match_search_query) ? ' OR ' : '') . 'LOWER('. $match . ') ';
73
				$match_search_query .= $this->db->sql_like_expression(str_replace('*', $this->db->get_any_char(), $this->db->get_any_char() . strtolower($word) . $this->db->get_any_char()));
74
			}
75
			$search_query .= ((!$search_query) ? '' : (($terms == 'all') ? ' AND ' : ' OR ')) . '(' . $match_search_query . ')';
76
		}
77
		$direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
78
79 View Code Duplication
		if (is_array($sort_by_sql[$sort_key]))
80
		{
81
			$sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
82
		}
83
		else
84
		{
85
			$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
86
		}
87
88
		$sql_array = array(
89
			'SELECT'	=> 'l.link_id',
90
			'FROM'		=> array(
91
					$this->links_table	=> 'l'),
92
			'WHERE'		=> 'l.link_active = 1
93
				' . (($search_query) ? 'AND (' . $search_query . ')' : '') . '
94
				' . (count($ex_cid_ary) ? ' AND ' . $this->db->sql_in_set('l.link_cat', $ex_cid_ary, true) : '') . '
95
				' . (($cat_id) ? ' AND ' . $this->db->sql_in_set('l.link_cat', $cat_id) : '') . '
96
				' . (($sort_days) ? ' AND l.link_time >= ' . (time() - ($sort_days * 86400)) : ''),
97
			'ORDER_BY'	=> $sql_sort_order
98
		);
99
100
		if ($sql_sort_order[0] == 'u')
101
		{
102
			$sql_array['LEFT_JOIN'][] = array(
103
				'FROM'	=> array(USERS_TABLE => 'u'),
104
				'ON'	=> 'u.user_id = l.link_user_id'
105
			);
106
		}
107
108
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
109
		$result = $this->db->sql_query($sql);
110
		while ($row = $this->db->sql_fetchrow($result))
111
		{
112
			$id_ary[] = $row['link_id'];
113
		}
114
115
		$this->db->sql_freeresult($result);
116
117
		$total_match_count = count($id_ary);
118
119
		$id_ary = array_slice($id_ary, $start, (int) $per_page);
120
121
		return $total_match_count;
122
	}
123
}
124