1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * Responsible for handling saved searches
7 class Saved_searches_Model
extends Model
9 const tablename
= 'ninja_saved_searches'; /**< Name of saved searches table */
12 * Save a search after validating that it doesn't
13 * already exist. Update if it does.
15 public function save_search($query=false, $name=false, $description=false, $id=false)
17 $query = trim($query);
19 $description = trim($description);
21 if (empty($query) ||
empty($name)) {
25 if (!empty($id) && self
::get_search_by_id($id) != false) {
27 self
::update_search($id, $query, $name, $description);
31 if($previously_stored_searches_same_query = self
::get_search_by_query($query)) {
32 foreach($previously_stored_searches_same_query as $previously_stored_search_same_query) {
33 self
::remove_search($previously_stored_search_same_query->id
);
37 $db = Database
::instance();
38 $user = Auth
::instance()->get_user()->username
;
39 $sql = "INSERT INTO ".self
::tablename
." (username, search_name, search_query, search_description) ".
40 "VALUES(".$db->escape($user).", ".$db->escape($name).", ".$db->escape($query).", ".$db->escape($description).")";
42 $res = $db->query($sql);
43 return $res->insert_id();
47 * Update a saved search
49 public function update_search($id=false, $query=false, $name=false, $description=false)
51 $db = Database
::instance();
52 $user = Auth
::instance()->get_user()->username
;
53 $sql = "UPDATE ".self
::tablename
." SET username=".$db->escape($user).", search_name=".$db->escape($name).
54 ", search_query=".$db->escape($query).", search_description=".$db->escape($description)." ".
57 $res = $db->query($sql);
62 * Get a saved search by id
64 public function get_search_by_id($id=false)
71 $db = Database
::instance();
72 $user = Auth
::instance()->get_user()->username
;
73 $sql = 'SELECT * FROM '.self
::tablename
.' WHERE id='.$id.' AND username = '.$db->escape($user);
74 $res = $db->query($sql);
75 return $res ?
$res : false;
79 * Get a saved search by query
81 public function get_search_by_query($query)
83 $db = Database
::instance();
84 $user = Auth
::instance()->get_user()->username
;
85 $sql = 'SELECT * FROM '.self
::tablename
.' WHERE search_query ='.$db->escape($query).' AND username = '.$db->escape($user);
86 $res = $db->query($sql);
87 return $res ?
$res : false;
91 * Fetch all the saved searches for current user
93 public static function get_saved_searches()
95 $db = Database
::instance();
96 $user = Auth
::instance()->get_user()->username
;
98 $sql = "SELECT * FROM ".self
::tablename
." WHERE username=".$db->escape($user)." ORDER BY search_name";
99 $res = $db->query($sql);
100 return $res ?
$res : false;
104 * Delete a saved search
106 public function remove_search($id=false)
110 if (self
::get_search_by_id($id) !== false) {
111 $db = Database
::instance();
112 $user = Auth
::instance()->get_user()->username
;
113 $sql = "DELETE FROM ".self
::tablename
." WHERE id=".$id." AND username=".$db->escape($user);
114 $res = $db->query($sql);