Skip to content

Commit 853ca43

Browse files
committed
Add live title search controller
1 parent bc2a2e2 commit 853ca43

5 files changed

Lines changed: 67 additions & 0 deletions

File tree

config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ phpbb_ideas_list_controller:
1717
phpbb_ideas_post_controller:
1818
path: /ideas/post
1919
defaults: { _controller: phpbb.ideas.post_controller:post }
20+
21+
phpbb_ideas_livesearch_controller:
22+
path: /ideas/livesearch/title
23+
defaults: { _controller: phpbb.ideas.livesearch_controller:title_search }

config/services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ services:
6262
class: phpbb\ideas\controller\idea_controller
6363
parent: phpbb.ideas.controller.base
6464

65+
phpbb.ideas.livesearch_controller:
66+
class: phpbb\ideas\controller\livesearch_controller
67+
parent: phpbb.ideas.controller.base
68+
6569
phpbb.ideas.ideas:
6670
class: phpbb\ideas\factory\ideas
6771
arguments:
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\controller;
12+
13+
/**
14+
* Ideas live search controller
15+
*/
16+
class livesearch_controller extends base
17+
{
18+
public function title_search()
19+
{
20+
$title_chars = $this->request->variable('duplicateeditinput', '', true);
21+
22+
$matches = $this->ideas->ideas_title_livesearch($title_chars, 10);
23+
24+
$json_response = new \phpbb\json_response();
25+
$json_response->send(array(
26+
'keyword' => $title_chars,
27+
'results' => $matches,
28+
));
29+
}
30+
}

event/listener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ public function show_idea($event)
253253
'U_EDIT_TICKET' => $this->link_helper->get_idea_link($idea['idea_id'], 'ticket', true),
254254
'U_REMOVE_VOTE' => $this->link_helper->get_idea_link($idea['idea_id'], 'removevote', true),
255255
'U_IDEA_VOTE' => $this->link_helper->get_idea_link($idea['idea_id'], 'vote', true),
256+
'U_TITLE_LIVESEARCH'=> $this->helper->route('phpbb_ideas_livesearch_controller'),
256257
));
257258

258259
// Use Ideas breadcrumbs

factory/ideas.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,34 @@ public function get_idea_by_topic_id($id)
265265
return $this->get_idea($idea_id);
266266
}
267267

268+
/**
269+
* Do a live search on idea titles. Return any matches based on a given search query.
270+
*
271+
* @param string $search The string of characters to search using LIKE
272+
* @param int $limit The number of results to return
273+
* @return array An array of matching idea id/key and title/values
274+
*/
275+
public function ideas_title_livesearch($search, $limit = 10)
276+
{
277+
$results = [];
278+
$sql = 'SELECT idea_title, idea_id
279+
FROM ' . $this->table_ideas . '
280+
WHERE idea_title ' . $this->db->sql_like_expression($search . $this->db->get_any_char());
281+
$result = $this->db->sql_query_limit($sql, $limit);
282+
while ($row = $this->db->sql_fetchrow($result))
283+
{
284+
$results[] = [
285+
'idea_id' => $row['idea_id'],
286+
'result' => $row['idea_id'],
287+
'clean_title' => $row['idea_title'],
288+
'display' => "<span>{$row['idea_title']}</span>",
289+
];
290+
}
291+
$this->db->sql_freeresult($result);
292+
293+
return $results;
294+
}
295+
268296
/**
269297
* Returns the status name from the status ID specified.
270298
*

0 commit comments

Comments
 (0)