diff --git a/app/Models/Category.php b/app/Models/Category.php index 8e32a107c..ac8934ea1 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -35,4 +35,9 @@ class Category extends Model { return $this->hasMany(static::class, 'parentid'); } + + public function userExcludedCategory() + { + return $this->belongsTo('App\Models\UserExcludedCategory', 'categories_id'); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 54dc10c1f..e28b6f3d8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -22,20 +22,7 @@ class User extends Authenticatable /** * @var array */ - protected $fillable = [ - 'username', - 'password', - 'email', - 'user_roles_id', - 'created_at', - 'updated_at', - 'host', - 'rsstoken', - 'invites', - 'invitedby', - 'userseed', - 'notes', - ]; + protected $guarded = []; /** * @var array @@ -71,4 +58,9 @@ class User extends Authenticatable { return $this->hasMany('App\Models\DnzbFailure', 'users_id'); } + + public function excludedCategory() + { + return $this->hasMany('App\Models\UserExcludedCategory', 'users_id'); + } } diff --git a/app/Models/UserExcludedCategory.php b/app/Models/UserExcludedCategory.php index af32b641b..be559616f 100644 --- a/app/Models/UserExcludedCategory.php +++ b/app/Models/UserExcludedCategory.php @@ -6,5 +6,17 @@ use Illuminate\Database\Eloquent\Model; class UserExcludedCategory extends Model { - // + protected $dateFormat = false; + + protected $guarded = []; + + public function user() + { + return $this->belongsTo('App\Models\User', 'users_id'); + } + + public function category() + { + return $this->hasMany('App\Models\Category', 'categories_id'); + } } diff --git a/nntmux/Users.php b/nntmux/Users.php index c4ff0e2fd..9e6482182 100755 --- a/nntmux/Users.php +++ b/nntmux/Users.php @@ -2,6 +2,7 @@ namespace nntmux; +use App\Models\UserExcludedCategory; use nntmux\db\DB; use Carbon\Carbon; use App\Models\User; @@ -171,7 +172,7 @@ class Users */ public function delUserCategoryExclusions($uid): void { - $this->pdo->queryExec(sprintf('DELETE FROM user_excluded_categories WHERE users_id = %d', $uid)); + UserExcludedCategory::query()->where('users_id', $uid)->delete(); } /** @@ -987,7 +988,7 @@ class Users $this->delUserCategoryExclusions($uid); if (count($catids) > 0) { foreach ($catids as $catid) { - $this->pdo->queryInsert(sprintf('INSERT INTO user_excluded_categories (users_id, categories_id, created_at) VALUES (%d, %d, now())', $uid, $catid)); + UserExcludedCategory::query()->insertGetId(['users_id' => $uid, 'categories_id' => $catid, 'created_at' => Carbon::now()]); } } } @@ -1000,7 +1001,7 @@ class Users public function getRoleCategoryExclusion($role): array { $ret = []; - $categories = $this->pdo->query(sprintf('SELECT categories_id FROM role_excluded_categories WHERE role = %d', $role)); + $categories = UserExcludedCategory::query()->where('role', $role)->get(['categories_id']); foreach ($categories as $category) { $ret[] = $category['categories_id']; } @@ -1076,7 +1077,7 @@ class Users */ public function delCategoryExclusion($uid, $catid): void { - $this->pdo->queryExec(sprintf('DELETE FROM user_excluded_categories WHERE users_id = %d AND categories_id = %d', $uid, $catid)); + UserExcludedCategory::query()->where(['users_id'=> $uid, 'categories_id' => $catid])->delete(); } /**