username . ' attempted to update ' . $name . ' but does not have sufficient permissions','3'); } return false; } // update /** * has_access * This checks to see if the current user has access to modify this preference * as defined by the preference name */ public static function has_access($preference) { // Nothing for those demo thugs if (Config::get('demo_mode')) { return false; } $preference = Dba::escape($preference); $sql = "SELECT `level` FROM `preference` WHERE `name`='$preference'"; $db_results = Dba::query($sql); if ($GLOBALS['user']->has_access($data['level'])) { return true; } return false; } // has_access /** * id_from_name * This takes a name and returns the id */ public static function id_from_name($name) { $name = Dba::escape($name); $sql = "SELECT `id` FROM `preference` WHERE `name`='$name'"; $db_results = Dba::query($sql); $row = Dba::fetch_assoc($db_results); return $row['id']; } // id_from_name /** * name_from_id * This returns the name from an id, it's the exact opposite * of the function above it, amazing! */ public static function name_from_id($id) { $id = Dba::escape($id); $sql = "SELECT `name` FROM `preference` WHERE `id`='$id'"; $db_results = Dba::query($sql); $row = Dba::fetch_assoc($db_results); return $row['name']; } // name_from_id /** * get_catagories * This returns an array of the names of the different possible sections * it ignores the 'internal' catagory */ public static function get_catagories() { $sql = "SELECT `preference`.`catagory` FROM `preference` GROUP BY `catagory` ORDER BY `catagory`"; $db_results = Dba::query($sql); $results = array(); while ($row = Dba::fetch_assoc($db_results)) { if ($row['catagory'] != 'internal') { $results[] = $row['catagory']; } } // end while return $results; } // get_catagories /** * insert * This inserts a new preference into the preference table * it does NOT sync up the users, that should be done independtly */ public static function insert($name,$description,$default,$level,$type,$catagory) { // Clean em up $name = Dba::escape($name); $description = Dba::escape($description); $default = Dba::escape($default); $level = Dba::escape($level); $type = Dba::escape($type); $catagory = Dba::escape($catagory); $sql = "INSERT INTO `preference` (`name`,`description`,`value`,`level`,`catagory`) " . "VALUES ('$name','$description','$default','$level','$catagory')"; $db_results = Dba::query($sql); if (!$db_results) { return false; } return true; } // insert /** * delete * This deletes the specified preference, a name or a ID can be passed */ public static function delete($preference) { // First prepare if (!is_numeric($preference)) { $id = self::id_from_name($preference); $name = $preference; } else { $name = self::name_from_id($preference); $id = $preference; } $id = Dba::escape($id); // Remove the preference, then the user records of it $sql = "DELETE FROM `preference` WHERE `id`='$id'"; $db_results = Dba::query($sql); self::rebuild_preferences(); } // delete /** * rebuild_preferences * This removes any garbage and then adds back in anything missing preferences wise */ public static function rebuild_preferences() { // First remove garbage $sql = "DELETE FROM `user_preference` USING `user_preference` LEFT JOIN `preference` ON `preference`.`id`=`user_preference`.`preference` " . "WHERE `preference`.`id` IS NULL"; $db_results = Dba::query($sql); // Now add anything that we are missing back in, except System $sql = "SELECT * FROM `preference` WHERE `type`!='system'"; } // rebuild_preferences } // end Preference class