From bfddf1af9ac4c0906a892b9d55427a488abf98fe Mon Sep 17 00:00:00 2001 From: Karsten Heiken Date: Fri, 22 Apr 2011 01:02:39 +0200 Subject: [PATCH] Enable searching for projects --- application/controllers/projects.php | 15 +++++++++++++++ application/models/project.php | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/application/controllers/projects.php b/application/controllers/projects.php index 7687493..e897d6a 100644 --- a/application/controllers/projects.php +++ b/application/controllers/projects.php @@ -1,5 +1,8 @@ set_content_type('application/json') ->set_output(json_encode(array('result' => $result))); } + + /** + * Search for a specific project and return a list of possible results. + * + * @param type $needle The needle to look for in the haystack. + */ + public function search($needle) { + $results = $this->project->search($needle); + $this->output + ->set_content_type('application/json') + ->set_output(json_encode(array('count' => count($results), 'results' => $results))); + } } diff --git a/application/models/project.php b/application/models/project.php index c758780..8adad57 100644 --- a/application/models/project.php +++ b/application/models/project.php @@ -94,4 +94,29 @@ class Project extends CI_Model { return array('count' => $configuration_count, 'configs' => $configurations); } + + /** + * Search for a specific project and return a list of possible results. + * + * @param type $needle The needle to look for in the haystack. + */ + public function search($needle) { + + // get matching projects that are public or belong directly to the user + $query = $this->db->where('public','1') + ->or_where('owner', $this->session->userdata('id')) + ->like('name', $needle)->get('projects'); + $results = $query->result_array(); + + // get matching projects that are shared to the user + $this->db->select('*')->from('shares') + ->where(array('user_id' => $this->session->userdata('id'))) + ->like('name', $needle); + $this->db->join('projects', 'projects.id = shares.project_id'); + $query = $this->db->get(); + + $own_results = $query->result_array(); + + return array_merge($results, $own_results); + } } \ No newline at end of file