Passed
Pull Request — master (#132)
by Matt
01:16
created

livesearch   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 15
c 1
b 0
f 0
dl 0
loc 32
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A title_search() 0 19 2
1
<?php
2
/**
3
 *
4
 * Ideas extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ideas\factory;
12
13
/**
14
 * Class for handling multiple ideas
15
 */
16
class livesearch extends base
17
{
18
	/** @var array */
19
	protected $sql;
20
21
	/**
22
	 * Do a live search on idea titles. Return any matches based on a given search query.
23
	 *
24
	 * @param string $search The string of characters to search using LIKE
25
	 * @param int    $limit  The number of results to return
26
	 *
27
	 * @return array An array of matching idea id/key and title/values
28
	 */
29
	public function title_search($search, $limit = 10)
30
	{
31
		$results = [];
32
		$sql = 'SELECT idea_title, idea_id
33
			FROM ' . $this->table_ideas . '
34
			WHERE idea_title ' . $this->db->sql_like_expression($this->db->get_any_char() . $search . $this->db->get_any_char());
35
		$result = $this->db->sql_query_limit($sql, $limit);
36
		while ($row = $this->db->sql_fetchrow($result))
37
		{
38
			$results[] = [
39
				'idea_id'     => $row['idea_id'],
40
				'result'      => $row['idea_id'],
41
				'clean_title' => $row['idea_title'],
42
				'display'     => "<span>{$row['idea_title']}</span>", // spans are expected in phpBB's live search JS
43
			];
44
		}
45
		$this->db->sql_freeresult($result);
46
47
		return $results;
48
	}
49
}
50