Implement simple search
This commit is contained in:
37
application/controllers/search.php
Normal file
37
application/controllers/search.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php defined('BASEPATH') || exit('No direct script access allowed');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search projects and experiments.
|
||||||
|
*
|
||||||
|
* @author Eike Foken <kontakt@eikefoken.de>
|
||||||
|
*/
|
||||||
|
class Search extends CI_Controller {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls the parent constructor.
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct();
|
||||||
|
$this->load->model('experiment');
|
||||||
|
$this->load->helper('typography');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function index() {
|
||||||
|
if ($this->input->get('query') != '') {
|
||||||
|
$query = explode(" ", $this->input->get('query'));
|
||||||
|
|
||||||
|
$data['projects'] = $this->project->search($this->input->get('query'));
|
||||||
|
$data['experiments'] = $this->experiment->search($this->input->get('query'));
|
||||||
|
|
||||||
|
$this->load->view('search/results', $data);
|
||||||
|
} else {
|
||||||
|
$this->load->view('search/new');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* End of file search.php */
|
||||||
|
/* Location: ./application/controllers/search.php */
|
||||||
@@ -152,14 +152,17 @@ class Experiment extends CI_Model {
|
|||||||
/**
|
/**
|
||||||
* Search for a specific experiment and return a list of possible results.
|
* Search for a specific experiment and return a list of possible results.
|
||||||
*
|
*
|
||||||
* @param string $needle The needle to look for in the haystack.
|
* @param string $needle The needle to look for in the haystack
|
||||||
|
* @param string $projectId
|
||||||
*/
|
*/
|
||||||
public function search($project, $needle) {
|
public function search($needle, $projectId = false) {
|
||||||
$query = $this->db->where('project_id', $project)
|
if ($projectId) {
|
||||||
->like('name', $needle)->get('experiments');
|
$this->db->where('project_id', $projectId);
|
||||||
$results = $query->result_array();
|
}
|
||||||
|
|
||||||
return $results;
|
$query = $this->db->like('name', $needle)->get('experiments');
|
||||||
|
|
||||||
|
return $query->result_array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -155,14 +155,15 @@ class Project extends CI_Model {
|
|||||||
/**
|
/**
|
||||||
* Search for a specific project and return a list of possible results.
|
* Search for a specific project and return a list of possible results.
|
||||||
*
|
*
|
||||||
* @param type $needle The needle to look for in the haystack.
|
* @param string $needle The needle to look for in the haystack.
|
||||||
*/
|
*/
|
||||||
public function search($needle) {
|
public function search($needle) {
|
||||||
// get matching projects that are public
|
// get matching projects that are public
|
||||||
$query = $this->db->query("SELECT * FROM `projects` WHERE `public`='1' AND `name` LIKE ".$this->db->escape('%'.$needle.'%'));
|
$query = $this->db->where('public', 1)->like('name', $needle)->get('projects');
|
||||||
$public_results = $query->result_array();
|
$public_results = $query->result_array();
|
||||||
|
|
||||||
// or belong directly to the user
|
// or belong directly to the user
|
||||||
|
$query = $this->db->where('owner', $this->session->userdata('user_id'));
|
||||||
$query = $this->db->query("SELECT * FROM `projects` WHERE `owner`=".$this->db->escape($this->session->userdata('user_id'))
|
$query = $this->db->query("SELECT * FROM `projects` WHERE `owner`=".$this->db->escape($this->session->userdata('user_id'))
|
||||||
." AND `name` LIKE ".$this->db->escape('%'.$needle.'%'));
|
." AND `name` LIKE ".$this->db->escape('%'.$needle.'%'));
|
||||||
$own_results = $query->result_array();
|
$own_results = $query->result_array();
|
||||||
@@ -177,7 +178,7 @@ class Project extends CI_Model {
|
|||||||
|
|
||||||
$shared_results = $query->result_array();
|
$shared_results = $query->result_array();
|
||||||
|
|
||||||
return $this->_addShortNames(array_merge($own_results, $shared_results));
|
return $this->_addShortNames(array_merge($public_results, $own_results, $shared_results));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -200,9 +200,9 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="box">
|
<div class="box">
|
||||||
<form id="search-form" method="get" action="#">
|
<form id="search-form" method="get" action="<?=site_url('search');?>">
|
||||||
<input type="text" name="search" id="search-input" class="search-input" />
|
<input type="text" name="query" id="search-input" class="search-input" />
|
||||||
<input type="image" src="<?=asset_url('images', 'button-search.gif');?>" id="search-submit" class="search-submit" />
|
<input type="submit" id="search-submit" class="search-submit" value="<?=_('Search');?>" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
10
application/views/search/index.html
Executable file
10
application/views/search/index.html
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>403 Forbidden</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<p>Directory access is forbidden.</p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
39
application/views/search/results.php
Normal file
39
application/views/search/results.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php $this->load->view('header');?>
|
||||||
|
|
||||||
|
<div id="content">
|
||||||
|
|
||||||
|
<div class="title">
|
||||||
|
<h2><?=_('Search results');?></h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="box">
|
||||||
|
<h3>Projects</h3>
|
||||||
|
<?php
|
||||||
|
foreach($projects as $project):
|
||||||
|
?>
|
||||||
|
<p>
|
||||||
|
<h4 style="margin-bottom: 5px;"><?=anchor('projects/detail/' . $project['id'], $project['name']);?></h4>
|
||||||
|
<?=character_limiter($project['description'], 255);?><br />
|
||||||
|
</p>
|
||||||
|
<?php
|
||||||
|
endforeach;
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="box">
|
||||||
|
<h3>Experiments</h3>
|
||||||
|
<?php
|
||||||
|
foreach($experiments as $experiment):
|
||||||
|
?>
|
||||||
|
<p>
|
||||||
|
<h4 style="margin-bottom: 5px;"><?=anchor('experiments/detail/' . $experiment['id'], $experiment['name']);?></h4>
|
||||||
|
<?=character_limiter($experiment['description'], 255);?><br />
|
||||||
|
</p>
|
||||||
|
<?php
|
||||||
|
endforeach;
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php $this->load->view('footer');?>
|
||||||
@@ -248,6 +248,13 @@ div.error {
|
|||||||
.search-submit {
|
.search-submit {
|
||||||
left: 160px;
|
left: 160px;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
border: 1px solid #d2d2d2;
|
||||||
|
border-left: 0;
|
||||||
|
height: 20px;
|
||||||
|
background-color: #f7f7f7;
|
||||||
|
font: 11px Arial, sans-serif;
|
||||||
|
line-height: 12px;
|
||||||
|
padding: 3px 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Blog */
|
/* Blog */
|
||||||
|
|||||||
Reference in New Issue
Block a user