Skip to content

Commit

Permalink
Adjust searching with visibility, add custom rule for the field (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kovah committed Jun 24, 2022
1 parent 22b7469 commit 87915d7
Show file tree
Hide file tree
Showing 36 changed files with 213 additions and 112 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/App/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getSearch(): View
'old_query' => null,
'search_title' => true,
'search_description' => true,
'private_only' => false,
'visibility' => null,
'broken_only' => false,
'empty_tags' => false,
'empty_lists' => false,
Expand Down Expand Up @@ -53,7 +53,7 @@ public function doSearch(SearchRequest $request): View
'old_query' => $this->searchQuery,
'search_title' => $this->searchTitle,
'search_description' => $this->searchDescription,
'private_only' => $this->searchPrivateOnly,
'visibility' => $this->searchVisibility,
'broken_only' => $this->searchBrokenOnly,
'only_lists' => $this->searchLists,
'only_tags' => $this->searchTags,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Guest/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LinkController extends Controller
public function index(Request $request): View
{
$links = Link::publicOnly()
->with('tags')
->with(['tags' => fn ($query) => $query->publicOnly()])
->orderBy(
$request->input('orderBy', 'created_at'),
$request->input('orderDir', 'desc')
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Guest/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ListController extends Controller
public function index(Request $request): View
{
$lists = LinkList::publicOnly()
->withCount('links')
->withCount(['links' => fn ($query) => $query->publicOnly()])
->orderBy(
$request->input('orderBy', 'name'),
$request->input('orderDir', 'asc')
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Guest/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TagController extends Controller
public function index(Request $request): View
{
$tags = Tag::publicOnly()
->withCount('links')
->withCount(['links' => fn ($query) => $query->publicOnly()])
->orderBy(
$request->input('orderBy', 'name'),
$request->input('orderDir', 'asc')
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Traits/SearchesLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait SearchesLinks
protected string|null $searchQuery;
protected bool $searchTitle = false;
protected bool $searchDescription = false;
protected bool $searchPrivateOnly = false;
protected ?int $searchVisibility = null;
protected bool $searchBrokenOnly = false;
protected string|null $searchLists = null;
protected string|null $searchTags = null;
Expand Down Expand Up @@ -59,8 +59,8 @@ protected function buildDatabaseQuery(SearchRequest $request): Builder
}

// Show private only if applicable
if ($this->searchPrivateOnly = (bool)$request->input('private_only', false)) {
$search->where('is_private', true);
if ($this->searchVisibility = $request->input('visibility')) {
$search->where('visibility', $this->searchVisibility);
}

// Show broken only if applicable
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Requests/Models/LinkStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Requests\Models;

use App\Enums\ModelAttribute;
use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

Expand Down Expand Up @@ -46,7 +48,7 @@ public function rules(): array
],
'visibility' => [
'sometimes',
'integer',
new ModelVisibility()
],
'check_disabled' => [
'sometimes',
Expand Down
29 changes: 22 additions & 7 deletions app/Http/Requests/Models/LinkUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace App\Http\Requests\Models;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class LinkUpdateRequest extends FormRequest
{
/** @var bool */
private $requireUniqueUrl;
private bool $requireUniqueUrl = false;

/**
* Determine if the user is authorized to make this request.
Expand All @@ -34,13 +34,28 @@ public function authorize(Request $request): bool
public function rules(): array
{
$rules = [
'url' => 'required|string',
'title' => 'nullable|string',
'description' => 'nullable|string',
'url' => [
'required',
'string',
],
'title' => [
'nullable',
'string',
],
'description' => [
'nullable',
'string',
],
'lists' => 'nullable',
'tags' => 'nullable',
'visibility' => 'sometimes|integer',
'check_disabled' => 'sometimes|boolean',
'visibility' => [
'sometimes',
new ModelVisibility(),
],
'check_disabled' => [
'sometimes',
'boolean',
],
];

if ($this->requireUniqueUrl) {
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/Models/ListStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests\Models;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;

class ListStoreRequest extends FormRequest
Expand Down Expand Up @@ -34,7 +35,7 @@ public function rules(): array
],
'visibility' => [
'sometimes',
'integer',
new ModelVisibility(),
],
];
}
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Requests/Models/ListUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests\Models;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
Expand Down Expand Up @@ -35,7 +36,10 @@ public function rules(): array
$rules = [
'name' => 'required|string',
'description' => 'nullable|string',
'visibility' => 'sometimes|integer',
'visibility' => [
'sometimes',
new ModelVisibility(),
],
];

if ($this->requireUniqueName) {
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/Models/NoteStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests\Models;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;

class NoteStoreRequest extends FormRequest
Expand Down Expand Up @@ -32,7 +33,7 @@ public function rules(): array
],
'visibility' => [
'sometimes',
'integer',
new ModelVisibility(),
],
];
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/Models/NoteUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests\Models;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;

class NoteUpdateRequest extends FormRequest
Expand Down Expand Up @@ -29,7 +30,7 @@ public function rules(): array
],
'visibility' => [
'sometimes',
'integer',
new ModelVisibility(),
],
];
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/Models/TagStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests\Models;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

Expand Down Expand Up @@ -31,7 +32,7 @@ public function rules(): array
],
'visibility' => [
'sometimes',
'integer',
new ModelVisibility(),
],
];
}
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Requests/Models/TagUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace App\Http\Requests\Models;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class TagUpdateRequest extends FormRequest
{
/** @var bool */
private $requireUniqueName = false;
private bool $requireUniqueName = false;

/**
* Determine if the user is authorized to make this request.
Expand Down Expand Up @@ -37,7 +37,7 @@ public function rules(): array
'name' => 'required',
'visibility' => [
'sometimes',
'integer',
new ModelVisibility(),
],
];

Expand Down
6 changes: 6 additions & 0 deletions app/Http/Requests/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests;

use App\Rules\ModelVisibility;
use Illuminate\Foundation\Http\FormRequest;

class SearchRequest extends FormRequest
Expand Down Expand Up @@ -36,6 +37,11 @@ public function rules(): array
'broken_only' => [
'required_without_all:query,only_lists,only_tags',
],
'visibility' => [
'sometimes',
'integer',
new ModelVisibility(),
],
];
}

Expand Down
45 changes: 45 additions & 0 deletions app/Rules/ModelVisibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Rules;

use App\Enums\ModelAttribute;
use Illuminate\Contracts\Validation\Rule;

class ModelVisibility implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
return in_array((int)$value, [
ModelAttribute::VISIBILITY_PUBLIC,
ModelAttribute::VISIBILITY_INTERNAL,
ModelAttribute::VISIBILITY_PRIVATE,
], true);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('validation.custom.visibility.visibility');
}
}
17 changes: 12 additions & 5 deletions app/View/Components/Forms/VisibilityToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

class VisibilityToggle extends Component
{
public function __construct(private ?int $existingValue = null, public string $inputClasses = '', public string $labelClasses = '')
{
public function __construct(
private ?int $existingValue = null,
private string $visibilitySetting = 'links_default_visibility',
public string $inputClasses = '',
public string $labelClasses = ''
) {
}

public function render(): View
Expand All @@ -23,11 +27,14 @@ public function render(): View
'internal' => $internal,
'private' => $private,
'publicSelected' => old('visibility', $this->existingValue) === $public
|| (old('visibility', $this->existingValue) === null && usersettings('links_default_visibility') === $public),
|| (old('visibility',
$this->existingValue) === null && usersettings($this->visibilitySetting) === $public),
'internalSelected' => old('visibility', $this->existingValue) === $internal
|| (old('visibility', $this->existingValue) === null && usersettings('links_default_visibility') === $internal),
|| (old('visibility',
$this->existingValue) === null && usersettings($this->visibilitySetting) === $internal),
'privateSelected' => old('visibility', $this->existingValue) === $private
|| (old('visibility', $this->existingValue) === null && usersettings('links_default_visibility') === $private),
|| (old('visibility',
$this->existingValue) === null && usersettings($this->visibilitySetting) === $private),
]);
}
}
2 changes: 1 addition & 1 deletion lang/en_US/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'query' => 'Search for...',
'search_title' => 'Search Title',
'search_description' => 'Search Description',
'private_only' => 'Private Links only',
'visibility' => 'Search by visibility...',
'broken_links' => 'Broken Links only',
'empty_tags' => 'without Tags',
'empty_lists' => 'without Lists',
Expand Down
4 changes: 2 additions & 2 deletions lang/en_US/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
*/

'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
'visibility' => [
'visibility' => 'The Visibility must bei either 1 (public), 2 (internal) or 3 (private).',
],
],

Expand Down
Loading

0 comments on commit 87915d7

Please sign in to comment.