Add general search and refactor header
This commit is contained in:
53
application/controllers/Search.php
Normal file
53
application/controllers/Search.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
use Coduo\PHPHumanizer\DateTimeHumanizer;
|
||||
|
||||
class Search extends MY_Controller
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('home', 'profile');
|
||||
$this->load->model('SearchModel', '', TRUE);
|
||||
$this->load->model('UserModel', '', TRUE);
|
||||
$this->load->model('PostsModel', '', TRUE);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$query = html_escape($this->input->get('q'));
|
||||
|
||||
$this->load->view('header', ['active' => 'search', 'title' => "Suche - $query", 'additionalStyles' => []]);
|
||||
$this->load->view('search', ['query' => $query]);
|
||||
$this->load->view('footer', ['additionalScripts' => ['search.js']]);
|
||||
}
|
||||
|
||||
public function getSearchResults() {
|
||||
header('Content-Type: application/json;charset=utf-8');
|
||||
|
||||
$query = $this->input->post('query');
|
||||
|
||||
if(strlen($query) < 3) {
|
||||
echo json_encode(['error' => true, 'message' => 'search-term']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$offset = filter_var($this->input->post('offset'), FILTER_VALIDATE_INT);
|
||||
$filters = is_array($this->input->post('filters')) ? $this->input->post('filters') : [];
|
||||
|
||||
$results = $this->SearchModel->search($query, 5, $offset, $filters);
|
||||
|
||||
if(empty($results)) {
|
||||
if($offset > 0) {
|
||||
$message = 'no-further-results';
|
||||
} else {
|
||||
$message = 'no-results';
|
||||
}
|
||||
echo json_encode(['error' => true, 'message' => $message]);
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode($results, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user