1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
4 * Handle page data - saving and fetching
6 class Ninja_setting_Model
extends Model
8 const USERFIELD
= 'username'; /**< The DB field that provides the user name */
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)
22 $value = trim($value);
23 $user = empty($username) ? @Auth
::instance()->get_user()->username
: $username;
27 $db = Database
::instance();
30 } catch (Exception
$ex) {
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);
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);
49 $sql = "INSERT INTO ninja_settings(page, type, setting, ".self
::USERFIELD
.") ".
50 "VALUES(".$db->escape($page).", ".$db->escape($type).", ".$db->escape($value).
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)
72 $db = Database
::instance();
75 } catch (Exception
$ex) {
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);
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);
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);
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)
117 $db = Database
::instance();
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;