diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index 882d80d..0593f86 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -7,9 +7,7 @@ use App\Models\Labels; use App\Models\Pricing; use App\Models\Server; use App\Models\Settings; -use App\Models\Yabs; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Session; use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; @@ -29,22 +27,13 @@ class ServerController extends Controller public function showServersPublic() { $settings = Settings::getSettings(); - - 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(); + Settings::setSettingsToSession($settings); if ((Session::get('show_servers_public') === 1)) { $servers = Server::allPublicServers(); 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() @@ -227,13 +216,13 @@ class ServerController extends Controller $server1_data = Server::server($server1); 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); 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')); } diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 9314c2d..42428e4 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -35,7 +35,8 @@ class SettingsController extends Controller 'default_server_os' => 'required', 'due_soon_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') @@ -56,15 +57,23 @@ class SettingsController extends Controller 'due_soon_amount' => $request->due_soon_amount, 'recently_added_amount' => $request->recently_added_amount, 'dashboard_currency' => $request->currency, + 'sort_on' => $request->sort_on, ]); - Settings::setSettingsToSession($settings); - Cache::forget('due_soon');//Main page due_soon cache Cache::forget('recently_added');//Main page recently_added cache Cache::forget('pricing_breakdown');//Main page pricing breakdown 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') ->with('success', 'Settings Updated Successfully.'); diff --git a/app/Models/Misc.php b/app/Models/Misc.php index 91af70b..859c303 100644 --- a/app/Models/Misc.php +++ b/app/Models/Misc.php @@ -2,9 +2,11 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Session; class Misc extends Model { @@ -18,6 +20,18 @@ class Misc extends Model 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() {//All misc and relationships (no using joins) return Cache::remember("all_misc", now()->addMonth(1), function () { @@ -35,6 +49,9 @@ class Misc extends Model 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'); } diff --git a/app/Models/Reseller.php b/app/Models/Reseller.php index 9b3ae08..88eec65 100644 --- a/app/Models/Reseller.php +++ b/app/Models/Reseller.php @@ -2,10 +2,12 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Session; class Reseller extends Model { @@ -19,6 +21,18 @@ class Reseller extends Model 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() {//All reseller hosting and relationships (no using joins) return Cache::remember("all_reseller", now()->addMonth(1), function () { @@ -51,6 +65,9 @@ class Reseller extends Model 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'); } diff --git a/app/Models/SeedBoxes.php b/app/Models/SeedBoxes.php index 7df318f..17b025b 100644 --- a/app/Models/SeedBoxes.php +++ b/app/Models/SeedBoxes.php @@ -2,10 +2,12 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Session; 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 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() {//All seedboxes and relationships (no using joins) return Cache::remember("all_seedboxes", now()->addMonth(1), function () { @@ -46,6 +60,9 @@ class SeedBoxes extends Model 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'); } diff --git a/app/Models/Server.php b/app/Models/Server.php index 4bdff9a..d42689e 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Session; +use Illuminate\Database\Eloquent\Builder; class Server extends Model { @@ -26,6 +28,18 @@ class Server extends Model */ 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() {//All servers and relationships (no using joins) return Cache::remember("all_servers", now()->addMonth(1), function () { @@ -45,8 +59,7 @@ class Server extends Model {//All ACTIVE servers and relationships replaces activeServersDataIndexPage() return Cache::remember("all_active_servers", now()->addMonth(1), function () { return Server::where('active', '=', 1) - ->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label']) - ->get(); + ->with(['location', 'provider', 'os', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label', 'price'])->get(); }); } @@ -203,6 +216,9 @@ class Server extends Model 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'); } diff --git a/app/Models/Settings.php b/app/Models/Settings.php index 913d183..009bca9 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -19,31 +19,57 @@ class Settings extends Model public static function getSettings() { return Cache::remember('settings', now()->addWeek(1), function () { - return DB::table('settings') - ->where('id', '=', 1) - ->get(); + return DB::table('settings')->where('id', '=', 1) + ->first(); }); } public static function setSettingsToSession($settings): void { - Session::put('dark_mode', $settings[0]->dark_mode ?? 0); - 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::put('save_yabs_as_txt', $settings[0]->save_yabs_as_txt ?? 0); - Session::put('default_currency', $settings[0]->default_currency ?? 'USD'); - Session::put('default_server_os', $settings[0]->default_server_os ?? 1); - Session::put('due_soon_amount', $settings[0]->due_soon_amount ?? 6); - Session::put('recently_added_amount', $settings[0]->recently_added_amount ?? 6); - Session::put('dashboard_currency', $settings[0]->dashboard_currency ?? 'USD'); + Session::put('dark_mode', $settings->dark_mode ?? 0); + Session::put('timer_version_footer', $settings->show_versions_footer ?? 1); + Session::put('show_servers_public', $settings->show_servers_public ?? 0); + Session::put('show_server_value_ip', $settings->show_server_value_ip ?? 0); + Session::put('show_server_value_hostname', $settings->show_server_value_hostname ?? 0); + Session::put('show_server_value_price', $settings->show_server_value_price ?? 0); + Session::put('show_server_value_yabs', $settings->show_server_value_yabs ?? 0); + Session::put('show_server_value_provider', $settings->show_server_value_provider ?? 0); + Session::put('show_server_value_location', $settings->show_server_value_location ?? 0); + Session::put('save_yabs_as_txt', $settings->save_yabs_as_txt ?? 0); + Session::put('default_currency', $settings->default_currency ?? 'USD'); + Session::put('default_server_os', $settings->default_server_os ?? 1); + Session::put('due_soon_amount', $settings->due_soon_amount ?? 6); + Session::put('recently_added_amount', $settings->recently_added_amount ?? 6); + Session::put('dashboard_currency', $settings->dashboard_currency ?? 'USD'); + Session::put('sort_on', $settings->sort_on ?? 1); 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']; + } + } diff --git a/app/Models/Shared.php b/app/Models/Shared.php index 769e766..4d2eec8 100644 --- a/app/Models/Shared.php +++ b/app/Models/Shared.php @@ -2,10 +2,12 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Session; class Shared extends Model { @@ -19,6 +21,18 @@ class Shared extends Model 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() {//All shared hosting and relationships (no using joins) return Cache::remember("all_shared", now()->addMonth(1), function () { @@ -51,6 +65,9 @@ class Shared extends Model 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'); } diff --git a/database/migrations/2022_10_04_002543_add_sort_on_to_settings_table.php b/database/migrations/2022_10_04_002543_add_sort_on_to_settings_table.php new file mode 100644 index 0000000..6d88f6c --- /dev/null +++ b/database/migrations/2022_10_04_002543_add_sort_on_to_settings_table.php @@ -0,0 +1,22 @@ +tinyInteger('sort_on')->default(1)->after('dashboard_currency'); + }); + } + + public function down() + { + Schema::table('settings', function (Blueprint $table) { + $table->dropColumn('sort_on'); + }); + } +}; diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php index 2a7c9ab..697ea46 100644 --- a/resources/views/settings/index.blade.php +++ b/resources/views/settings/index.blade.php @@ -224,6 +224,56 @@ +
+
+
+
Default order by
+ +
+
+
Update settings