Avail feature updated
[ninja.git] / application / models / ninja_setting.php
blob5e6dfe12f4176ff7adfda3c8919f8ff3737346fb
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * Handle page data - saving and fetching
5 */
6 class Ninja_setting_Model extends Model
8 const USERFIELD = 'username'; /**< The DB field that provides the user name */
9 /**
10 * Save page setting for a user
12 * @param $type string: {widget_order, widget, etc...}
13 * @param $page string: The page we're looking at.
14 * @param $value mixed: The value to set.
15 * @param $username string: Username if not current user
16 * @return False on error. True on success.
18 public static function save_page_setting($type='widget_order', $page=false, $value=false, $username=false)
20 $type = trim($type);
21 $page = trim($page);
22 $value = trim($value);
23 $user = empty($username) ? @Auth::instance()->get_user()->username : $username;
24 if (empty($type))
25 return false;
27 $db = Database::instance();
28 try {
29 @$db->connect();
30 } catch (Exception $ex) {
31 return false;
34 if (empty($user))
35 $user = "''";
36 else
37 $user = $db->escape($user);
39 $sql = "SELECT * FROM ninja_settings WHERE ".self::USERFIELD."=".$user.
40 " AND page=".$db->escape($page)." AND type=".$db->escape($type);
42 # does this setting exist? (i.e should we do insert or update)
43 $res = $db->query($sql);
44 if (count($res)!=0) {
45 $sql = "UPDATE ninja_settings SET setting=".$db->escape($value).
46 " WHERE ".self::USERFIELD."=".$user." AND type=".
47 $db->escape($type)." AND page=".$db->escape($page);
48 } else {
49 $sql = "INSERT INTO ninja_settings(page, type, setting, ".self::USERFIELD.") ".
50 "VALUES(".$db->escape($page).", ".$db->escape($type).", ".$db->escape($value).
51 ", ".$user.")";
53 unset($res);
54 $db->query($sql);
55 return true;
58 /**
59 * Fetch page setting for a user. Assumes only one value is returned.
61 * @param $type string: {widget_order, widget, etc...}
62 * @param $page string: The page we're looking at.
63 * @param $default bool: Request default or not.
65 public static function fetch_page_setting($type='widget_order', $page=false, $default=false)
67 $type = trim($type);
68 $page = trim($page);
69 if (empty($type))
70 return false;
72 $db = Database::instance();
73 try {
74 @$db->connect();
75 } catch (Exception $ex) {
76 return false;
78 $res = false;
79 $sql_base = "SELECT * FROM ninja_settings";
80 if ($default === true) {
81 # We have a request for default value
82 $sql = $sql_base." WHERE (".self::USERFIELD."='' OR ".self::USERFIELD." IS NULL) AND page=".
83 $db->escape($page)." AND type=".$db->escape($type);
84 } else {
85 $user = @Auth::instance()->get_user()->username;
86 # first, try user setting
87 $sql = $sql_base." WHERE ".self::USERFIELD."=".$db->escape($user)." AND page=".$db->escape($page).
88 " AND type=".$db->escape($type);
90 $res = $db->query($sql);
91 if (count($res)==0) {
92 # try default if nothing found
93 $sql = $sql_base." WHERE (".self::USERFIELD."='' OR ".self::USERFIELD." IS NULL) AND page=".
94 $db->escape($page)." AND type=".$db->escape($type);
95 $res = false;
98 $result = ($res!== false && count($res)) ? $res : $db->query($sql);
99 return count($result) !=0 ? $result->current() : false;
103 * Fetch page setting for a specifik user.
104 * Assumes only one value is returned.
106 * @param $type string: {widget_order, widget, etc...}
107 * @param $page string: The page we're looking at.
108 * @param $username string: User to fetch setting for
110 public function fetch_user_page_setting($type='widget_order', $page=false, $username=false)
112 $type = trim($type);
113 $page = trim($page);
114 if (empty($type))
115 return false;
117 $db = Database::instance();
118 $res = false;
119 $sql_base = "SELECT * FROM ninja_settings";
120 $user = empty($username) ? Auth::instance()->get_user()->username : $username;
122 $sql = $sql_base." WHERE ".self::USERFIELD."=".$db->escape($user)." AND page=".$db->escape($page).
123 " AND type=".$db->escape($type);
125 $res = $db->query($sql);
126 $result = ($res!== false && count($res)) ? $res : $db->query($sql);
127 return count($result) !=0 ? $result->current() : false;