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.
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
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');?>
|
||||
Reference in New Issue
Block a user