Files
newznab-tmux/app/Models/UserExcludedCategory.php
T
DariusIII a4a9a7f156 Apply fixes from StyleCI
[ci skip] [skip ci]
2018-01-11 08:53:45 +00:00

88 lines
2.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Model;
class UserExcludedCategory extends Model
{
/**
* @var bool
*/
protected $dateFormat = false;
/**
* @var array
*/
protected $guarded = [];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class, 'users_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo(Category::class, 'categories_id');
}
/**
* @param $uid
*/
public static function delUserCategoryExclusions($uid): void
{
self::query()->where('users_id', $uid)->delete();
}
/**
* @param $uid
* @param array $catids
*/
public static function addCategoryExclusions($uid, array $catids): void
{
self::delUserCategoryExclusions($uid);
if (\count($catids) > 0) {
foreach ($catids as $catid) {
self::query()->insertGetId(['users_id' => $uid, 'categories_id' => $catid, 'created_at' => Carbon::now()]);
}
}
}
/**
* Get list of category names excluded by the user.
*
* @param int $userID ID of the user.
*
* @return array
* @throws \Exception
*/
public static function getCategoryExclusionNames($userID): array
{
$categories = self::with('category')->where('users_id', $userID)->get();
$ret = [];
if ($categories !== null) {
foreach ($categories as $cat) {
$ret[] = $cat->category->title;
}
}
return $ret;
}
/**
* @param $uid
* @param $catid
*/
public static function delCategoryExclusion($uid, $catid): void
{
self::query()->where(['users_id'=> $uid, 'categories_id' => $catid])->delete();
}
}