Implement simple search

This commit is contained in:
Eike Foken
2011-09-15 05:12:40 +02:00
parent 5e539daebd
commit aefc4e0387
7 changed files with 109 additions and 12 deletions

View 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 */

View File

@@ -152,14 +152,17 @@ class Experiment extends CI_Model {
/**
* 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) {
$query = $this->db->where('project_id', $project)
->like('name', $needle)->get('experiments');
$results = $query->result_array();
public function search($needle, $projectId = false) {
if ($projectId) {
$this->db->where('project_id', $projectId);
}
return $results;
$query = $this->db->like('name', $needle)->get('experiments');
return $query->result_array();
}
}

View File

@@ -155,14 +155,15 @@ class Project extends CI_Model {
/**
* 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) {
// 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();
// 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'))
." AND `name` LIKE ".$this->db->escape('%'.$needle.'%'));
$own_results = $query->result_array();
@@ -177,7 +178,7 @@ class Project extends CI_Model {
$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));
}
/**

View File

@@ -200,9 +200,9 @@
</div>
<div class="box">
<form id="search-form" method="get" action="#">
<input type="text" name="search" id="search-input" class="search-input" />
<input type="image" src="<?=asset_url('images', 'button-search.gif');?>" id="search-submit" class="search-submit" />
<form id="search-form" method="get" action="<?=site_url('search');?>">
<input type="text" name="query" id="search-input" class="search-input" />
<input type="submit" id="search-submit" class="search-submit" value="<?=_('Search');?>" />
</form>
</div>

View File

@@ -0,0 +1,10 @@
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>

View 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');?>

View File

@@ -248,6 +248,13 @@ div.error {
.search-submit {
left: 160px;
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 */