Профиль: телефон, сайт, должность, город и «о себе» — необязательные поля
This commit is contained in:
1 parent
6751452281
commit
156c8c40c4
5 files changed
+111
-14
No files matched your search
@@ -31,26 +31,32 @@ final class ProfileController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Имя и почта.
|
||||
* Имя, почта и необязательные поля сверх минимума ядра.
|
||||
*/
|
||||
public function update(Request $request, User $user): Response
|
||||
{
|
||||
$data = $this->validate($request, [
|
||||
'name' => 'nullable|max:191',
|
||||
'email' => 'nullable|email|max:191|unique:users,email,' . $user->id(),
|
||||
], ['name' => 'Имя', 'email' => 'Почта']);
|
||||
'name' => 'nullable|max:191',
|
||||
'email' => 'nullable|email|max:191|unique:users,email,' . $user->id(),
|
||||
'phone' => 'nullable|max:32',
|
||||
'website' => 'nullable|url|max:191',
|
||||
'position' => 'nullable|max:191',
|
||||
'location' => 'nullable|max:191',
|
||||
'bio' => 'nullable|max:500',
|
||||
], [
|
||||
'name' => 'Имя', 'email' => 'Почта', 'phone' => 'Телефон',
|
||||
'website' => 'Сайт', 'position' => 'Должность', 'location' => 'Город', 'bio' => 'О себе',
|
||||
]);
|
||||
|
||||
$before = ['name' => $user->name, 'email' => $user->email];
|
||||
$fields = ['name', 'email', 'phone', 'website', 'position', 'location', 'bio'];
|
||||
$before = array_intersect_key($user->toArray(), array_flip($fields));
|
||||
|
||||
$user->forceFill([
|
||||
'name' => (string) ($data['name'] ?? ''),
|
||||
'email' => (string) ($data['email'] ?? ''),
|
||||
])->save();
|
||||
$user->forceFill(array_map(static fn (mixed $value): string => (string) ($value ?? ''), $data))->save();
|
||||
|
||||
Audit::updated('user', $user->id(), 'изменён свой профиль', Audit::between($before, [
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
]));
|
||||
Audit::updated('user', $user->id(), 'изменён свой профиль', Audit::between(
|
||||
$before,
|
||||
array_intersect_key($user->toArray(), array_flip($fields))
|
||||
));
|
||||
|
||||
$this->flash('Профиль сохранён');
|
||||
|
||||
|
||||
@@ -21,7 +21,12 @@ final class User extends Model
|
||||
{
|
||||
protected static string $table = 'users';
|
||||
|
||||
protected array $fillable = ['login', 'email', 'name', 'role_id', 'active'];
|
||||
protected array $fillable = [
|
||||
'login', 'email', 'name', 'role_id', 'active',
|
||||
// Профиль сверх минимума ядра: не используются нигде, кроме самой
|
||||
// карточки — свободны для своих нужд в приложении поверх фреймворка
|
||||
'phone', 'website', 'position', 'location', 'bio',
|
||||
];
|
||||
|
||||
protected array $hidden = ['password_hash'];
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Migrations;
|
||||
|
||||
use Rsgrinko\Proton\Database\Migration;
|
||||
use Rsgrinko\Proton\Database\Schema\Blueprint;
|
||||
|
||||
/**
|
||||
* Необязательные поля профиля: ядру они не нужны, но почти любому приложению
|
||||
* на Proton рано или поздно понадобится телефон или подпись под именем —
|
||||
* заводить их тогда придётся той же миграцией, только на боевой базе.
|
||||
* Все поля пустые по умолчанию и ни на что не влияют, пока их не заполнили.
|
||||
*/
|
||||
final class AddUserProfileFields extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->table('users', function (Blueprint $table): void {
|
||||
$table->string('phone', 32)->default('');
|
||||
$table->string('website', 191)->default('');
|
||||
$table->string('position', 191)->default('');
|
||||
$table->string('location', 191)->default('');
|
||||
$table->string('bio', 500)->default('');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->table('users', function (Blueprint $table): void {
|
||||
$table->dropColumn('phone');
|
||||
$table->dropColumn('website');
|
||||
$table->dropColumn('position');
|
||||
$table->dropColumn('location');
|
||||
$table->dropColumn('bio');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,28 @@ $hint = $isNew ? 'пусто — придумаем сами' : 'пусто
|
||||
</div>
|
||||
|
||||
<?php if (!$isNew) { ?>
|
||||
<div class="card">
|
||||
<h2>Профиль</h2>
|
||||
<p class="muted small">Необязательные поля — заполняет сам человек в своём профиле.</p>
|
||||
|
||||
<dl class="props">
|
||||
<dt>Телефон</dt>
|
||||
<dd><?= $user->phone !== '' ? View::e((string) $user->phone) : '<span class="muted">—</span>' ?></dd>
|
||||
|
||||
<dt>Сайт</dt>
|
||||
<dd><?php if ($user->website !== '') { ?><a href="<?= View::e((string) $user->website) ?>" target="_blank" rel="noopener"><?= View::e((string) $user->website) ?></a><?php } else { ?><span class="muted">—</span><?php } ?></dd>
|
||||
|
||||
<dt>Должность</dt>
|
||||
<dd><?= $user->position !== '' ? View::e((string) $user->position) : '<span class="muted">—</span>' ?></dd>
|
||||
|
||||
<dt>Город</dt>
|
||||
<dd><?= $user->location !== '' ? View::e((string) $user->location) : '<span class="muted">—</span>' ?></dd>
|
||||
|
||||
<dt>О себе</dt>
|
||||
<dd><?= $user->bio !== '' ? nl2br(View::e((string) $user->bio)) : '<span class="muted">—</span>' ?></dd>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>Устройства</h2>
|
||||
|
||||
|
||||
@@ -36,6 +36,31 @@ use Rsgrinko\Proton\View\View;
|
||||
<input type="email" name="email" value="<?= View::e((string) $user->email) ?>">
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>Телефон</span>
|
||||
<input type="tel" name="phone" value="<?= View::e((string) $user->phone) ?>" placeholder="+7 900 000-00-00">
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>Сайт</span>
|
||||
<input type="url" name="website" value="<?= View::e((string) $user->website) ?>" placeholder="https://example.com">
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>Должность</span>
|
||||
<input type="text" name="position" value="<?= View::e((string) $user->position) ?>">
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>Город</span>
|
||||
<input type="text" name="location" value="<?= View::e((string) $user->location) ?>">
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>О себе</span>
|
||||
<textarea name="bio" style="min-height: 70px;"><?= View::e((string) $user->bio) ?></textarea>
|
||||
</label>
|
||||
|
||||
<button type="submit" class="primary">Сохранить</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user