sla: Stupid Rasmus and his needles and haystacks
[ninja.git] / application / models / saved_searches.php
blobe82b8dc9ac4cd6b078b05fb2eec09924c2e65a67
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * Saved searches model
5 * Responsible for handling saved searches
6 */
7 class Saved_searches_Model extends Model
9 const tablename = 'ninja_saved_searches'; /**< Name of saved searches table */
11 /**
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);
18 $name = trim($name);
19 $description = trim($description);
21 if (empty($query) || empty($name)) {
22 return false;
25 if (!empty($id) && self::get_search_by_id($id) != false) {
26 # update report
27 self::update_search($id, $query, $name, $description);
28 return $id;
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();
46 /**
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)." ".
55 "WHERE id=".(int)$id;
57 $res = $db->query($sql);
58 return true;
61 /**
62 * Get a saved search by id
64 public function get_search_by_id($id=false)
66 $id = (int)$id;
67 if (!$id) {
68 return 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;
78 /**
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;
90 /**
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)
108 $id = trim($id);
109 $id = (int)$id;
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);
115 return true;
117 return false;