diff --git a/app/Http/Controllers/Models/NoteController.php b/app/Http/Controllers/Models/NoteController.php index 2d281636..accef8ba 100644 --- a/app/Http/Controllers/Models/NoteController.php +++ b/app/Http/Controllers/Models/NoteController.php @@ -28,7 +28,7 @@ class NoteController extends Controller abort(403); } - $data = $request->except(['_token']); + $data = $request->validated(); NoteRepository::create($data); flash(trans('note.added_successfully'), 'success'); diff --git a/app/Http/Requests/Models/NoteStoreRequest.php b/app/Http/Requests/Models/NoteStoreRequest.php index 28800488..e93e352d 100644 --- a/app/Http/Requests/Models/NoteStoreRequest.php +++ b/app/Http/Requests/Models/NoteStoreRequest.php @@ -30,9 +30,9 @@ class NoteStoreRequest extends FormRequest 'note' => [ 'required', ], - 'is_private' => [ + 'visibility' => [ 'sometimes', - 'boolean', + 'integer', ], ]; } diff --git a/app/Http/Requests/Models/NoteUpdateRequest.php b/app/Http/Requests/Models/NoteUpdateRequest.php index e8fc2e73..4280a2f9 100644 --- a/app/Http/Requests/Models/NoteUpdateRequest.php +++ b/app/Http/Requests/Models/NoteUpdateRequest.php @@ -27,9 +27,9 @@ class NoteUpdateRequest extends FormRequest 'note' => [ 'required', ], - 'is_private' => [ + 'visibility' => [ 'sometimes', - 'boolean', + 'integer', ], ]; } diff --git a/app/Models/LinkList.php b/app/Models/LinkList.php index 17874a7b..2e3d6587 100644 --- a/app/Models/LinkList.php +++ b/app/Models/LinkList.php @@ -30,9 +30,10 @@ use OwenIt\Auditing\Contracts\Auditable; * @property string|null $deleted_at * @property-read Collection|Link[] $links * @property-read User $user - * @method static Builder|Tag byUser(int $user_id = null) - * @method static Builder|Tag privateOnly() - * @method static Builder|Tag publicOnly() + * @method static Builder|LinkList byUser(int $user_id = null) + * @method static Builder|LinkList privateOnly() + * @method static Builder|LinkList internalOnly() + * @method static Builder|LinkList publicOnly() */ class LinkList extends Model implements Auditable { diff --git a/app/Models/Note.php b/app/Models/Note.php index 52c6b8bc..c32213ee 100644 --- a/app/Models/Note.php +++ b/app/Models/Note.php @@ -2,12 +2,12 @@ namespace App\Models; -use Illuminate\Support\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; /** @@ -18,30 +18,34 @@ use Illuminate\Support\Str; * @property int $user_id * @property int $link_id * @property string $note - * @property int $is_private + * @property int $visibility * @property Carbon|null $created_at * @property Carbon|null $updated_at * @property string|null $deleted_at * @property-read Link $link * @property-read User $user - * @method static Builder|Link byUser($user_id = null) + * @method static Builder|Note byUser($user_id = null) + * @method static Builder|Note privateOnly() + * @method static Builder|Note internalOnly() + * @method static Builder|Note publicOnly() */ class Note extends Model { - use SoftDeletes; use HasFactory; + use ScopesVisibility; + use SoftDeletes; public $fillable = [ 'user_id', 'link_id', 'note', - 'is_private', + 'visibility', ]; protected $casts = [ 'user_id' => 'integer', 'link_id' => 'integer', - 'is_private' => 'boolean', + 'visibility' => 'integer', ]; /* diff --git a/app/Repositories/NoteRepository.php b/app/Repositories/NoteRepository.php index 4b468d17..4163c4e1 100644 --- a/app/Repositories/NoteRepository.php +++ b/app/Repositories/NoteRepository.php @@ -26,8 +26,6 @@ class NoteRepository */ public static function update(Note $note, array $data): Note { - $data['is_private'] ??= false; - $note->update($data); return $note; diff --git a/app/View/Components/Forms/VisibilityToggle.php b/app/View/Components/Forms/VisibilityToggle.php index 356ab585..ebf907d5 100644 --- a/app/View/Components/Forms/VisibilityToggle.php +++ b/app/View/Components/Forms/VisibilityToggle.php @@ -8,7 +8,7 @@ use Illuminate\View\Component; class VisibilityToggle extends Component { - public function __construct(private ?int $existingValue = null) + public function __construct(private ?int $existingValue = null, public string $inputClasses = '', public string $labelClasses = '') { } diff --git a/database/migrations/2022_06_23_112431_migrate_user_data.php b/database/migrations/2022_06_23_112431_migrate_user_data.php index 12ebd90c..2524d1c6 100644 --- a/database/migrations/2022_06_23_112431_migrate_user_data.php +++ b/database/migrations/2022_06_23_112431_migrate_user_data.php @@ -2,6 +2,7 @@ use App\Enums\ModelAttribute; use App\Models\LinkList; +use App\Models\Note; use App\Models\Tag; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; @@ -18,6 +19,7 @@ class MigrateUserData extends Migration $this->migrateLinkVisibility(); $this->migrateListVisibility(); $this->migrateTagVisibility(); + $this->migrateNoteVisibility(); } protected function migrateLinkVisibility(): void @@ -81,4 +83,24 @@ class MigrateUserData extends Migration $table->dropColumn(['is_private']); }); } + + protected function migrateNoteVisibility(): void + { + Schema::table('notes', function (Blueprint $table) { + $table->integer('visibility')->default(ModelAttribute::VISIBILITY_PRIVATE)->after('is_private'); + }); + + Note::withTrashed()->get()->each(function ($note) { + $note->visibility = match ((bool)$note->is_private) { + true => ModelAttribute::VISIBILITY_PRIVATE, + false => $this->guestAccessEnabled + ? ModelAttribute::VISIBILITY_PUBLIC : ModelAttribute::VISIBILITY_INTERNAL, + }; + $note->saveQuietly(); + }); + + Schema::table('notes', function (Blueprint $table) { + $table->dropColumn(['is_private']); + }); + } } diff --git a/lang/en_US/note.php b/lang/en_US/note.php index 7a6c8f36..e0318611 100644 --- a/lang/en_US/note.php +++ b/lang/en_US/note.php @@ -9,6 +9,8 @@ return [ 'update' => 'Update Note', 'delete' => 'Delete Note', + 'public' => 'Public Note', + 'internal' => 'Internal Note', 'private' => 'Private Note', 'note_content' => 'Note Content', diff --git a/resources/views/components/forms/visibility-toggle.blade.php b/resources/views/components/forms/visibility-toggle.blade.php index c0a59de7..dc1a7bf9 100644 --- a/resources/views/components/forms/visibility-toggle.blade.php +++ b/resources/views/components/forms/visibility-toggle.blade.php @@ -1,6 +1,6 @@