cleaned up some queries

This commit is contained in:
flash 2016-08-06 16:09:01 +02:00
parent d8e3b5b0a0
commit e679b342bd
12 changed files with 33 additions and 44 deletions

View file

@ -26,13 +26,11 @@ class ActionCode
// Insert it // Insert it
DB::table('actioncodes') DB::table('actioncodes')
->insert( ->insert([
[ 'code_action' => $action,
'code_action' => $action, 'user_id' => $user,
'user_id' => $user, 'action_code' => $code,
'action_code' => $code, ]);
]
);
// Return the code // Return the code
return $code; return $code;

View file

@ -82,12 +82,10 @@ class Comment
// Get comment data from the database // Get comment data from the database
$data = DB::table('comments') $data = DB::table('comments')
->where('comment_id', $id) ->where('comment_id', $id)
->get(); ->first();
// Check if anything was returned and assign data // Check if anything was returned and assign data
if ($data) { if ($data) {
$data = $data[0];
$this->id = $data->comment_id; $this->id = $data->comment_id;
$this->category = $data->comment_category; $this->category = $data->comment_category;
$this->time = $data->comment_timestamp; $this->time = $data->comment_timestamp;

View file

@ -96,11 +96,10 @@ class File
// Attempt to get the database row // Attempt to get the database row
$fileRow = DB::table('uploads') $fileRow = DB::table('uploads')
->where('file_id', $fileId) ->where('file_id', $fileId)
->get(); ->first();
// If anything was returned populate the variables // If anything was returned populate the variables
if ($fileRow) { if ($fileRow) {
$fileRow = $fileRow[0];
$this->id = $fileRow->file_id; $this->id = $fileRow->file_id;
$this->user = User::construct($fileRow->user_id); $this->user = User::construct($fileRow->user_id);
$this->data = file_get_contents(ROOT . config('file.uploads_dir') . $fileRow->file_id . ".bin"); $this->data = file_get_contents(ROOT . config('file.uploads_dir') . $fileRow->file_id . ".bin");
@ -116,7 +115,11 @@ class File
*/ */
public function delete() public function delete()
{ {
unlink(ROOT . config('file.uploads_dir') . $this->id . ".bin"); $filename = ROOT . config('file.uploads_dir') . $this->id . ".bin";
if (file_exists($filename)) {
unlink($filename);
}
DB::table('uploads') DB::table('uploads')
->where('file_id', $this->id) ->where('file_id', $this->id)

View file

@ -103,14 +103,13 @@ class Forum
// Get the row from the database // Get the row from the database
$forumRow = DB::table('forums') $forumRow = DB::table('forums')
->where('forum_id', $forumId) ->where('forum_id', $forumId)
->get(); ->first();
// Create permissions object // Create permissions object
$this->permissionsCache = new Perms(Perms::FORUM); $this->permissionsCache = new Perms(Perms::FORUM);
// Populate the variables // Populate the variables
if ($forumRow) { if ($forumRow) {
$forumRow = $forumRow[0];
$this->id = intval($forumRow->forum_id); $this->id = intval($forumRow->forum_id);
$this->order = intval($forumRow->forum_order); $this->order = intval($forumRow->forum_order);
$this->name = $forumRow->forum_name; $this->name = $forumRow->forum_name;
@ -223,10 +222,10 @@ class Forum
->where('forum_id', $this->id) ->where('forum_id', $this->id)
->orderBy('post_id') ->orderBy('post_id')
->limit(1) ->limit(1)
->get(['post_id']); ->first(['post_id']);
// Create the post object // Create the post object
$post = new Post(empty($firstPost) ? 0 : $firstPost[0]->post_id); $post = new Post($firstPost->post_id ?? 0);
// Assign it to a "cache" variable // Assign it to a "cache" variable
$this->firstPostCache = $post; $this->firstPostCache = $post;
@ -251,10 +250,10 @@ class Forum
->where('forum_id', $this->id) ->where('forum_id', $this->id)
->orderBy('post_id', 'desc') ->orderBy('post_id', 'desc')
->limit(1) ->limit(1)
->get(['post_id']); ->first(['post_id']);
// Create the post object // Create the post object
$post = new Post(empty($lastPost) ? 0 : $lastPost[0]->post_id); $post = new Post($lastPost->post_id ?? 0);
// Assign it to a "cache" variable // Assign it to a "cache" variable
$this->lastPostCache = $post; $this->lastPostCache = $post;

View file

@ -100,11 +100,10 @@ class Post
// Attempt to get the database row // Attempt to get the database row
$postRow = DB::table('posts') $postRow = DB::table('posts')
->where('post_id', $postId) ->where('post_id', $postId)
->get(); ->first();
// Assign data if a row was returned // Assign data if a row was returned
if ($postRow) { if ($postRow) {
$postRow = $postRow[0];
$this->id = intval($postRow->post_id); $this->id = intval($postRow->post_id);
$this->topic = intval($postRow->topic_id); $this->topic = intval($postRow->topic_id);
$this->forum = intval($postRow->forum_id); $this->forum = intval($postRow->forum_id);

View file

@ -113,11 +113,10 @@ class Topic
// Attempt to get the database row // Attempt to get the database row
$topicRow = DB::table('topics') $topicRow = DB::table('topics')
->where('topic_id', $topicId) ->where('topic_id', $topicId)
->get(); ->first();
// Assign data if a row was returned // Assign data if a row was returned
if ($topicRow) { if ($topicRow) {
$topicRow = $topicRow[0];
$this->id = intval($topicRow->topic_id); $this->id = intval($topicRow->topic_id);
$this->forum = intval($topicRow->forum_id); $this->forum = intval($topicRow->forum_id);
$this->hidden = boolval($topicRow->topic_hidden); $this->hidden = boolval($topicRow->topic_hidden);
@ -262,10 +261,10 @@ class Topic
->where('topic_id', $this->id) ->where('topic_id', $this->id)
->orderBy('post_id') ->orderBy('post_id')
->limit(1) ->limit(1)
->get(['post_id']); ->first(['post_id']);
// Create the post class // Create the post class
$post = new Post($post ? $post[0]->post_id : 0); $post = new Post($post->post_id ?? 0);
// Assign it to the cache var // Assign it to the cache var
$this->firstPostCache = $post; $this->firstPostCache = $post;
@ -290,10 +289,10 @@ class Topic
->where('topic_id', $this->id) ->where('topic_id', $this->id)
->orderBy('post_id', 'desc') ->orderBy('post_id', 'desc')
->limit(1) ->limit(1)
->get(['post_id']); ->first(['post_id']);
// Create the post class // Create the post class
$post = new Post($post ? $post[0]->post_id : 0); $post = new Post($post->post_id ?? 0);
// Assign it to the cache var // Assign it to the cache var
$this->lastPostCache = $post; $this->lastPostCache = $post;

View file

@ -79,12 +79,10 @@ class Post
// Get comment data from the database // Get comment data from the database
$data = DB::table('news') $data = DB::table('news')
->where('news_id', $id) ->where('news_id', $id)
->get(); ->first();
// Check if anything was returned and assign data // Check if anything was returned and assign data
if ($data) { if ($data) {
$data = $data[0];
$this->id = $data->news_id; $this->id = $data->news_id;
$this->category = $data->news_category; $this->category = $data->news_category;
$this->user = $data->user_id; $this->user = $data->user_id;

View file

@ -76,12 +76,10 @@ class Notification
// Get notification data from the database // Get notification data from the database
$data = DB::table('notifications') $data = DB::table('notifications')
->where('alert_id', $id) ->where('alert_id', $id)
->get(); ->first();
// Check if anything was returned and assign data // Check if anything was returned and assign data
if ($data) { if ($data) {
$data = $data[0];
$this->id = intval($data->alert_id); $this->id = intval($data->alert_id);
$this->user = intval($data->user_id); $this->user = intval($data->user_id);
$this->time = intval($data->alert_timestamp); $this->time = intval($data->alert_timestamp);

View file

@ -103,11 +103,10 @@ class Rank
// Get the rank database row // Get the rank database row
$rankRow = DB::table('ranks') $rankRow = DB::table('ranks')
->where('rank_id', $rankId) ->where('rank_id', $rankId)
->get(); ->first();
// Check if the rank actually exists // Check if the rank actually exists
if ($rankRow) { if ($rankRow) {
$rankRow = $rankRow[0];
$this->id = $rankRow->rank_id; $this->id = $rankRow->rank_id;
$this->name = $rankRow->rank_name; $this->name = $rankRow->rank_name;
$this->hierarchy = $rankRow->rank_hierarchy; $this->hierarchy = $rankRow->rank_hierarchy;

View file

@ -113,7 +113,7 @@ class Session
$session = DB::table('sessions') $session = DB::table('sessions')
->where('user_id', $this->userId) ->where('user_id', $this->userId)
->where('session_key', $this->sessionId) ->where('session_key', $this->sessionId)
->get(); ->first();
// Check if we actually got something in return // Check if we actually got something in return
if (!$session) { if (!$session) {
@ -121,7 +121,7 @@ class Session
} }
// Check if the session expired // Check if the session expired
if ($session[0]->session_expire < time()) { if ($session->session_expire < time()) {
// ...and return false // ...and return false
return 0; return 0;
} }
@ -130,13 +130,13 @@ class Session
good thing is i can probably do CIDR based checking */ good thing is i can probably do CIDR based checking */
// If the remember flag is set extend the session time // If the remember flag is set extend the session time
if ($session[0]->session_remember) { if ($session->session_remember) {
DB::table('sessions') DB::table('sessions')
->where('session_id', $session[0]->session_id) ->where('session_id', $session[0]->session_id)
->update(['session_expire' => time() + 604800]); ->update(['session_expire' => time() + 604800]);
} }
// Return 2 if the remember flag is set and return 1 if not // Return 2 if the remember flag is set and return 1 if not
return $session[0]->session_remember ? 2 : 1; return $session->session_remember ? 2 : 1;
} }
} }

View file

@ -88,7 +88,7 @@ class Template
$key = basename($dir); $key = basename($dir);
if ($key === self::$name) { if ($key === self::$name) {
$key = '__main__'; $loader->addPath($dir, '__main__');
} }
$loader->addPath($dir, $key); $loader->addPath($dir, $key);

View file

@ -292,12 +292,11 @@ class User
// Get the user database row // Get the user database row
$userRow = DB::table('users') $userRow = DB::table('users')
->where('user_id', $userId) ->where('user_id', $userId)
->orWhere('username_clean', clean_string($userId, true, true)) ->orWhere('username_clean', clean_string($userId, true))
->get(); ->first();
// Populate the variables // Populate the variables
if ($userRow) { if ($userRow) {
$userRow = $userRow[0];
$this->id = intval($userRow->user_id); $this->id = intval($userRow->user_id);
$this->username = $userRow->username; $this->username = $userRow->username;
$this->usernameClean = $userRow->username_clean; $this->usernameClean = $userRow->username_clean;
@ -500,7 +499,6 @@ class User
->where('poster_id', $this->id) ->where('poster_id', $this->id)
->distinct() ->distinct()
->groupBy('topic_id') ->groupBy('topic_id')
->orderBy('post_time')
->count(); ->count();
return [ return [