Added order by for index actions

Added order by setting
Order by for index
Updated showing 404 methods in serverController
This commit is contained in:
cp6 2022-10-04 14:35:46 +11:00
parent 263616ae6d
commit 4abbd451b9
10 changed files with 218 additions and 38 deletions

View File

@ -7,9 +7,7 @@ use App\Models\Labels;
use App\Models\Pricing; use App\Models\Pricing;
use App\Models\Server; use App\Models\Server;
use App\Models\Settings; use App\Models\Settings;
use App\Models\Yabs;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -29,22 +27,13 @@ class ServerController extends Controller
public function showServersPublic() public function showServersPublic()
{ {
$settings = Settings::getSettings(); $settings = Settings::getSettings();
Settings::setSettingsToSession($settings);
Session::put('timer_version_footer', $settings[0]->show_versions_footer ?? 1);
Session::put('show_servers_public', $settings[0]->show_servers_public ?? 0);
Session::put('show_server_value_ip', $settings[0]->show_server_value_ip ?? 0);
Session::put('show_server_value_hostname', $settings[0]->show_server_value_hostname ?? 0);
Session::put('show_server_value_price', $settings[0]->show_server_value_price ?? 0);
Session::put('show_server_value_yabs', $settings[0]->show_server_value_yabs ?? 0);
Session::put('show_server_value_provider', $settings[0]->show_server_value_provider ?? 0);
Session::put('show_server_value_location', $settings[0]->show_server_value_location ?? 0);
Session::save();
if ((Session::get('show_servers_public') === 1)) { if ((Session::get('show_servers_public') === 1)) {
$servers = Server::allPublicServers(); $servers = Server::allPublicServers();
return view('servers.public-index', compact('servers')); return view('servers.public-index', compact('servers'));
} }
return response()->view('errors.404', array("status" => 404, "title" => "Page not found", "message" => ""), 404); abort(404);
} }
public function create() public function create()
@ -228,13 +217,13 @@ class ServerController extends Controller
$server1_data = Server::server($server1); $server1_data = Server::server($server1);
if (!isset($server1_data[0]->yabs[0])) { if (!isset($server1_data[0]->yabs[0])) {
return response()->view('errors.404', array("status" => 404, "title" => "Page not found", "message" => "No server with YABs data was found for id '$server1'"), 404); abort(404);
} }
$server2_data = Server::server($server2); $server2_data = Server::server($server2);
if (!isset($server2_data[0]->yabs[0])) { if (!isset($server2_data[0]->yabs[0])) {
return response()->view('errors.404', array("status" => 404, "title" => "Page not found", "message" => "No server with YABs data was found for id '$server2'"), 404); abort(404);
} }
return view('servers.compare', compact('server1_data', 'server2_data')); return view('servers.compare', compact('server1_data', 'server2_data'));
} }

View File

@ -35,7 +35,8 @@ class SettingsController extends Controller
'default_server_os' => 'required', 'default_server_os' => 'required',
'due_soon_amount' => 'required|integer|between:0,12', 'due_soon_amount' => 'required|integer|between:0,12',
'recently_added_amount' => 'required|integer|between:0,12', 'recently_added_amount' => 'required|integer|between:0,12',
'currency' => 'required|string|size:3' 'currency' => 'required|string|size:3',
'sort_on' => 'required|integer|between:1,10',
]); ]);
DB::table('settings') DB::table('settings')
@ -56,15 +57,23 @@ class SettingsController extends Controller
'due_soon_amount' => $request->due_soon_amount, 'due_soon_amount' => $request->due_soon_amount,
'recently_added_amount' => $request->recently_added_amount, 'recently_added_amount' => $request->recently_added_amount,
'dashboard_currency' => $request->currency, 'dashboard_currency' => $request->currency,
'sort_on' => $request->sort_on,
]); ]);
Settings::setSettingsToSession($settings);
Cache::forget('due_soon');//Main page due_soon cache Cache::forget('due_soon');//Main page due_soon cache
Cache::forget('recently_added');//Main page recently_added cache Cache::forget('recently_added');//Main page recently_added cache
Cache::forget('pricing_breakdown');//Main page pricing breakdown Cache::forget('pricing_breakdown');//Main page pricing breakdown
Cache::forget('settings');//Main page settings cache Cache::forget('settings');//Main page settings cache
//Clear because they are affected by settings change (sort_on)
Cache::forget('all_servers');
Cache::forget('all_active_servers');
Cache::forget('all_shared');
Cache::forget('all_seedboxes');
Cache::forget('all_reseller');
Cache::forget('all_misc');
Settings::setSettingsToSession(Settings::getSettings());
return redirect()->route('settings.index') return redirect()->route('settings.index')
->with('success', 'Settings Updated Successfully.'); ->with('success', 'Settings Updated Successfully.');

View File

@ -2,9 +2,11 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
class Misc extends Model class Misc extends Model
{ {
@ -18,6 +20,18 @@ class Misc extends Model
protected $fillable = ['id', 'name', 'owned_since']; protected $fillable = ['id', 'name', 'owned_since'];
protected static function boot()
{
parent::boot();
static::addGlobalScope('order', function (Builder $builder) {
$array = Settings::orderByProcess(Session::get('sort_on'));
if (!in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
$builder->orderBy($array[0], $array[1]);
}
});
}
public static function allMisc() public static function allMisc()
{//All misc and relationships (no using joins) {//All misc and relationships (no using joins)
return Cache::remember("all_misc", now()->addMonth(1), function () { return Cache::remember("all_misc", now()->addMonth(1), function () {
@ -35,6 +49,9 @@ class Misc extends Model
public function price() public function price()
{ {
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
return $this->hasOne(Pricing::class, 'service_id', 'id')->orderBy(Settings::orderByProcess(Session::get('sort_on'))[0], Settings::orderByProcess(Session::get('sort_on'))[1]);
}
return $this->hasOne(Pricing::class, 'service_id', 'id'); return $this->hasOne(Pricing::class, 'service_id', 'id');
} }

View File

@ -2,10 +2,12 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class Reseller extends Model class Reseller extends Model
{ {
@ -19,6 +21,18 @@ class Reseller extends Model
public $incrementing = false; public $incrementing = false;
protected static function boot()
{
parent::boot();
static::addGlobalScope('order', function (Builder $builder) {
$array = Settings::orderByProcess(Session::get('sort_on'));
if (!in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
$builder->orderBy($array[0], $array[1]);
}
});
}
public static function allResellerHosting() public static function allResellerHosting()
{//All reseller hosting and relationships (no using joins) {//All reseller hosting and relationships (no using joins)
return Cache::remember("all_reseller", now()->addMonth(1), function () { return Cache::remember("all_reseller", now()->addMonth(1), function () {
@ -51,6 +65,9 @@ class Reseller extends Model
public function price() public function price()
{ {
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
return $this->hasOne(Pricing::class, 'service_id', 'id')->orderBy(Settings::orderByProcess(Session::get('sort_on'))[0], Settings::orderByProcess(Session::get('sort_on'))[1]);
}
return $this->hasOne(Pricing::class, 'service_id', 'id'); return $this->hasOne(Pricing::class, 'service_id', 'id');
} }

View File

@ -2,10 +2,12 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class SeedBoxes extends Model class SeedBoxes extends Model
{ {
@ -19,6 +21,18 @@ class SeedBoxes extends Model
protected $fillable = ['id', 'active', 'title', 'hostname', 'seed_box_type', 'provider_id', 'location_id', 'bandwidth', 'port_speed', 'disk', 'disk_type', 'disk_as_gb', 'was_promo', 'owned_since']; protected $fillable = ['id', 'active', 'title', 'hostname', 'seed_box_type', 'provider_id', 'location_id', 'bandwidth', 'port_speed', 'disk', 'disk_type', 'disk_as_gb', 'was_promo', 'owned_since'];
protected static function boot()
{
parent::boot();
static::addGlobalScope('order', function (Builder $builder) {
$array = Settings::orderByProcess(Session::get('sort_on'));
if (!in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
$builder->orderBy($array[0], $array[1]);
}
});
}
public static function allSeedboxes() public static function allSeedboxes()
{//All seedboxes and relationships (no using joins) {//All seedboxes and relationships (no using joins)
return Cache::remember("all_seedboxes", now()->addMonth(1), function () { return Cache::remember("all_seedboxes", now()->addMonth(1), function () {
@ -46,6 +60,9 @@ class SeedBoxes extends Model
public function price() public function price()
{ {
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
return $this->hasOne(Pricing::class, 'service_id', 'id')->orderBy(Settings::orderByProcess(Session::get('sort_on'))[0], Settings::orderByProcess(Session::get('sort_on'))[1]);
}
return $this->hasOne(Pricing::class, 'service_id', 'id'); return $this->hasOne(Pricing::class, 'service_id', 'id');
} }

View File

@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Database\Eloquent\Builder;
class Server extends Model class Server extends Model
{ {
@ -26,6 +28,18 @@ class Server extends Model
*/ */
private $id; private $id;
protected static function boot()
{
parent::boot();
static::addGlobalScope('order', function (Builder $builder) {
$array = Settings::orderByProcess(Session::get('sort_on'));
if (!in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
$builder->orderBy($array[0], $array[1]);
}
});
}
public static function allServers() public static function allServers()
{//All servers and relationships (no using joins) {//All servers and relationships (no using joins)
return Cache::remember("all_servers", now()->addMonth(1), function () { return Cache::remember("all_servers", now()->addMonth(1), function () {
@ -45,8 +59,7 @@ class Server extends Model
{//All ACTIVE servers and relationships replaces activeServersDataIndexPage() {//All ACTIVE servers and relationships replaces activeServersDataIndexPage()
return Cache::remember("all_active_servers", now()->addMonth(1), function () { return Cache::remember("all_active_servers", now()->addMonth(1), function () {
return Server::where('active', '=', 1) return Server::where('active', '=', 1)
->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label']) ->with(['location', 'provider', 'os', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label', 'price'])->get();
->get();
}); });
} }
@ -258,6 +271,9 @@ class Server extends Model
public function price() public function price()
{ {
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
return $this->hasOne(Pricing::class, 'service_id', 'id')->orderBy(Settings::orderByProcess(Session::get('sort_on'))[0], Settings::orderByProcess(Session::get('sort_on'))[1]);
}
return $this->hasOne(Pricing::class, 'service_id', 'id'); return $this->hasOne(Pricing::class, 'service_id', 'id');
} }

View File

@ -19,31 +19,57 @@ class Settings extends Model
public static function getSettings() public static function getSettings()
{ {
return Cache::remember('settings', now()->addWeek(1), function () { return Cache::remember('settings', now()->addWeek(1), function () {
return DB::table('settings') return DB::table('settings')->where('id', '=', 1)
->where('id', '=', 1) ->first();
->get();
}); });
} }
public static function setSettingsToSession($settings): void public static function setSettingsToSession($settings): void
{ {
Session::put('dark_mode', $settings[0]->dark_mode ?? 0); Session::put('dark_mode', $settings->dark_mode ?? 0);
Session::put('timer_version_footer', $settings[0]->show_versions_footer ?? 1); Session::put('timer_version_footer', $settings->show_versions_footer ?? 1);
Session::put('show_servers_public', $settings[0]->show_servers_public ?? 0); Session::put('show_servers_public', $settings->show_servers_public ?? 0);
Session::put('show_server_value_ip', $settings[0]->show_server_value_ip ?? 0); Session::put('show_server_value_ip', $settings->show_server_value_ip ?? 0);
Session::put('show_server_value_hostname', $settings[0]->show_server_value_hostname ?? 0); Session::put('show_server_value_hostname', $settings->show_server_value_hostname ?? 0);
Session::put('show_server_value_price', $settings[0]->show_server_value_price ?? 0); Session::put('show_server_value_price', $settings->show_server_value_price ?? 0);
Session::put('show_server_value_yabs', $settings[0]->show_server_value_yabs ?? 0); Session::put('show_server_value_yabs', $settings->show_server_value_yabs ?? 0);
Session::put('show_server_value_provider', $settings[0]->show_server_value_provider ?? 0); Session::put('show_server_value_provider', $settings->show_server_value_provider ?? 0);
Session::put('show_server_value_location', $settings[0]->show_server_value_location ?? 0); Session::put('show_server_value_location', $settings->show_server_value_location ?? 0);
Session::put('save_yabs_as_txt', $settings[0]->save_yabs_as_txt ?? 0); Session::put('save_yabs_as_txt', $settings->save_yabs_as_txt ?? 0);
Session::put('default_currency', $settings[0]->default_currency ?? 'USD'); Session::put('default_currency', $settings->default_currency ?? 'USD');
Session::put('default_server_os', $settings[0]->default_server_os ?? 1); Session::put('default_server_os', $settings->default_server_os ?? 1);
Session::put('due_soon_amount', $settings[0]->due_soon_amount ?? 6); Session::put('due_soon_amount', $settings->due_soon_amount ?? 6);
Session::put('recently_added_amount', $settings[0]->recently_added_amount ?? 6); Session::put('recently_added_amount', $settings->recently_added_amount ?? 6);
Session::put('dashboard_currency', $settings[0]->dashboard_currency ?? 'USD'); Session::put('dashboard_currency', $settings->dashboard_currency ?? 'USD');
Session::put('sort_on', $settings->sort_on ?? 1);
Session::save(); Session::save();
} }
public static function orderByProcess(int $value): array
{
if ($value === 1) {//Created_at ASC
return ['created_at', 'asc'];
} elseif ($value === 2) {//Created_at DESC
return ['created_at', 'desc'];
} elseif ($value === 3) {//next_due_date ASC
return ['next_due_date', 'asc'];
} elseif ($value === 4) {//next_due_date DESC
return ['next_due_date', 'desc'];
} elseif ($value === 5) {//as_usd ASC
return ['as_usd', 'asc'];
} elseif ($value === 6) {//as_usd DESC
return ['as_usd', 'desc'];
} elseif ($value === 7) {//owned_since ASC
return ['owned_since', 'asc'];
} elseif ($value === 8) {//owned_since DESC
return ['owned_since', 'desc'];
} elseif ($value === 9) {//updated_at ASC
return ['updated_at', 'asc'];
} elseif ($value === 10) {//updated_at DESC
return ['updated_at', 'desc'];
}
return ['created_at', 'desc'];
}
} }

View File

@ -2,10 +2,12 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
class Shared extends Model class Shared extends Model
{ {
@ -19,6 +21,18 @@ class Shared extends Model
public $incrementing = false; public $incrementing = false;
protected static function boot()
{
parent::boot();
static::addGlobalScope('order', function (Builder $builder) {
$array = Settings::orderByProcess(Session::get('sort_on'));
if (!in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
$builder->orderBy($array[0], $array[1]);
}
});
}
public static function allSharedHosting() public static function allSharedHosting()
{//All shared hosting and relationships (no using joins) {//All shared hosting and relationships (no using joins)
return Cache::remember("all_shared", now()->addMonth(1), function () { return Cache::remember("all_shared", now()->addMonth(1), function () {
@ -51,6 +65,9 @@ class Shared extends Model
public function price() public function price()
{ {
if (in_array(Session::get('sort_on'), [3, 4, 5, 6], true)) {
return $this->hasOne(Pricing::class, 'service_id', 'id')->orderBy(Settings::orderByProcess(Session::get('sort_on'))[0], Settings::orderByProcess(Session::get('sort_on'))[1]);
}
return $this->hasOne(Pricing::class, 'service_id', 'id'); return $this->hasOne(Pricing::class, 'service_id', 'id');
} }

View File

@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::table('settings', function (Blueprint $table) {
$table->tinyInteger('sort_on')->default(1)->after('dashboard_currency');
});
}
public function down()
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('sort_on');
});
}
};

View File

@ -224,6 +224,56 @@
</div> </div>
</div> </div>
</div> </div>
<div class="row mt-3">
<div class="col-12 col-md-6 mb-3">
<div class="input-group">
<div class="input-group-prepend"><span
class="input-group-text">Default order by</span></div>
<select class="form-control" name="sort_on">
<option
value="1" {{ ($setting[0]->sort_on === 1) ? 'selected' : '' }}>
created_at ASC
</option>
<option
value="2" {{ ($setting[0]->sort_on === 2) ? 'selected' : '' }}>
created_at DESC
</option>
<option
value="3" {{ ($setting[0]->sort_on === 3) ? 'selected' : '' }}>
next_due_date ASC
</option>
<option
value="4" {{ ($setting[0]->sort_on === 4) ? 'selected' : '' }}>
next_due_date DESC
</option>
<option
value="5" {{ ($setting[0]->sort_on === 5) ? 'selected' : '' }}>
as_usd ASC
</option>
<option
value="6" {{ ($setting[0]->sort_on === 6) ? 'selected' : '' }}>
as_usd DESC
</option>
<option
value="7" {{ ($setting[0]->sort_on === 7) ? 'selected' : '' }}>
owned_since ASC
</option>
<option
value="8" {{ ($setting[0]->sort_on === 8) ? 'selected' : '' }}>
owned_since DESC
</option>
<option
value="9" {{ ($setting[0]->sort_on === 9) ? 'selected' : '' }}>
updated_at ASC
</option>
<option
value="10" {{ ($setting[0]->sort_on === 10) ? 'selected' : '' }}>
updated_at DESC
</option>
</select>
</div>
</div>
</div>
<div class="row"> <div class="row">
<div class="col-12 col-lg-4"> <div class="col-12 col-lg-4">
<x-submit-button>Update settings</x-submit-button> <x-submit-button>Update settings</x-submit-button>