Skip to content

Commit

Permalink
Пользовательские поля
Browse files Browse the repository at this point in the history
  • Loading branch information
visavi committed Aug 25, 2021
1 parent b3507cf commit 9270a99
Show file tree
Hide file tree
Showing 28 changed files with 179 additions and 108 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/MailController.php
Expand Up @@ -39,15 +39,15 @@ public function index(Request $request, Validator $validator)
->email($email, ['email' => __('validator.email')]);

if ($validator->isValid()) {
$subject = __('mails.email_from_site', ['sitename' => setting('title')], defaultSetting('language'));
$subject = __('mails.email_from_site', ['sitename' => setting('title')], setting('language'));

$message = str_replace(
'/uploads/stickers',
config('app.url') . '/uploads/stickers',
bbCode($message)->toHtml()
);

$message .= '<br><br>Email: ' . $name . ' &lt;' . $email . '&gt;<br>IP: ' . getIp() . '<br>Browser: ' . getBrowser() . '<br>' . __('main.sent_out', [], defaultSetting('language')) . ': ' . dateFixed(SITETIME, 'd.m.y / H:i');
$message .= '<br><br>Email: ' . $name . ' &lt;' . $email . '&gt;<br>IP: ' . getIp() . '<br>Browser: ' . getBrowser() . '<br>' . __('main.sent_out', [], setting('language')) . ': ' . dateFixed(SITETIME, 'd.m.y / H:i');
$data = [
'to' => config('app.email'),
'subject' => $subject,
Expand Down
53 changes: 47 additions & 6 deletions app/Http/Controllers/User/UserController.php
Expand Up @@ -11,8 +11,10 @@
use App\Models\Flood;
use App\Models\Invite;
use App\Models\User;
use App\Models\UserField;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
Expand All @@ -35,12 +37,22 @@ public function index(string $login): View
abort(404, __('validator.user'));
}

$invite = Invite::query()->where('invite_user_id', $user->id)->first();
$user->load('lastBan', 'data.field');

$user->load('lastBan');
$adminGroups = User::ADMIN_GROUPS;

return view('users/user', compact('user', 'invite', 'adminGroups'));
$invite = Invite::query()->where('invite_user_id', $user->id)->first();

$fields = UserField::query()
->select('uf.*', 'ud.value')
->from('user_fields as uf')
->leftJoin('user_data as ud', static function (JoinClause $join) use ($user) {
$join->on('uf.id', 'ud.field_id')
->where('ud.user_id', $user->id);
})
->whereNotNull('ud.value')
->orderBy('uf.sort')
->get();

return view('users/user', compact('user', 'invite', 'adminGroups', 'fields'));
}

/**
Expand Down Expand Up @@ -343,6 +355,16 @@ public function profile(Request $request, Validator $validator)
abort(403, __('main.not_authorized'));
}

$fields = UserField::query()
->select('uf.*', 'ud.value')
->from('user_fields as uf')
->leftJoin('user_data as ud', static function (JoinClause $join) use ($user) {
$join->on('uf.id', 'ud.field_id')
->where('ud.user_id', $user->id);
})
->orderBy('uf.sort')
->get();

if ($request->isMethod('post')) {
$info = $request->input('info');
$name = $request->input('name');
Expand All @@ -360,6 +382,16 @@ public function profile(Request $request, Validator $validator)
->length($info, 0, 1000, ['info' => __('users.info_yourself_long')])
->length($name, 3, 20, ['name' => __('users.name_short_or_long')], false);

foreach ($fields as $field) {
$validator->length(
$request->input('field' . $field->id),
$field->min,
$field->max,
['field' . $field->id => 'Ошибка'],
$field->required
);
}

if ($validator->isValid()) {
$country = utfSubstr($country, 0, 30);
$city = utfSubstr($city, 0, 50);
Expand All @@ -375,6 +407,15 @@ public function profile(Request $request, Validator $validator)
'info' => $info,
]);

foreach ($fields as $field) {
$user->data()
->updateOrCreate([
'field_id' => $field->id,
], [
'value' => $request->input('field' . $field->id),
]);
}

setFlash('success', __('users.profile_success_changed'));

return redirect('profile');
Expand All @@ -384,7 +425,7 @@ public function profile(Request $request, Validator $validator)
setFlash('danger', $validator->getErrors());
}

return view('users/profile', compact('user'));
return view('users/profile', compact('user', 'fields'));
}

/**
Expand Down
18 changes: 9 additions & 9 deletions app/Http/Middleware/AuthenticateCookie.php
Expand Up @@ -39,6 +39,11 @@ public function handle(Request $request, Closure $next)

$user->updatePrivate();
$user->gettingBonus();

/* Установка сессионных переменных */
if ($request->session()->missing('hits')) {
$request->session()->put('hits', 0);
}
}

$this->setSetting($user, $request);
Expand Down Expand Up @@ -85,27 +90,22 @@ private function cookieAuth(Request $request): void
*/
private function setSetting($user, Request $request): void
{
$language = $user->language ?? defaultSetting('language');
$theme = $user->themes ?? defaultSetting('themes');
$language = $user->language ?? setting('language');
$theme = $user->themes ?? setting('themes');

if ($request->session()->has('language')) {
$language = $request->session()->get('language');
}

if (! file_exists(resource_path('lang/' . $language))) {
$language = defaultSetting('language');
$language = setting('language');
}

if (! file_exists(public_path('themes/' . $theme))) {
$theme = defaultSetting('themes');
$theme = setting('themes');
}

App::setLocale($language);
View::addLocation(public_path('themes/' . $theme . '/views'));

/* Установка сессионных переменных */
if ($request->session()->missing('hits')) {
$request->session()->put('hits', 0);
}
}
}
12 changes: 3 additions & 9 deletions app/Http/Requests/StoreUserFieldRequest.php
Expand Up @@ -17,16 +17,10 @@ public function rules()
return [
'_token' => 'in:' . csrf_token(),
'type' => 'in:' . implode(',', UserField::TYPES),
'name' => 'required',
'length' => 'required',
'name' => 'required|max:50',
'min' => 'required',
'max' => 'required',
'required' => 'boolean'
];
}

/* public function messages()
{
return [
'name.required' => 'Название обязательно',
];
}*/
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Expand Up @@ -74,7 +74,7 @@
* @property int updated_at
* @property int created_at
*
* @property Collection data
* @property Collection<UserData> data
*/
class User extends BaseModel implements
AuthenticatableContract,
Expand Down
9 changes: 6 additions & 3 deletions app/Models/UserData.php
Expand Up @@ -27,11 +27,14 @@ class UserData extends BaseModel
public $timestamps = false;

/**
* The attributes that aren't mass assignable.
* The attributes that are mass assignable.
*
* @var array
* @var string[]
*/
protected $guarded = [];
protected $fillable = [
'value',
'field_id',
];

/**
* Return field
Expand Down
21 changes: 15 additions & 6 deletions app/Models/UserField.php
Expand Up @@ -14,7 +14,8 @@
* @property int sort
* @property string type
* @property string name
* @property int length
* @property int min
* @property int max
* @property bool required
*
* @property Collection<UserData> data
Expand All @@ -35,6 +36,15 @@ class UserField extends BaseModel
self::TEXTAREA,
];

/**
* The attributes that should be cast to native types.
*
* @var string[]
*/
protected $casts = [
'required' => 'bool',
];

/**
* Indicates if the model should be timestamped.
*
Expand All @@ -43,17 +53,16 @@ class UserField extends BaseModel
public $timestamps = false;

/**
* The attributes that aren't mass assignable.
* The attributes that are mass assignable.
*
* @var array
* @var string[]
*/
/* protected $guarded = [];*/

protected $fillable = [
'sort',
'type',
'name',
'length',
'min',
'max',
'required',
];

Expand Down
36 changes: 6 additions & 30 deletions app/helpers.php
Expand Up @@ -1660,33 +1660,28 @@ function setFlash(string $status, $message)
*/
function setInput(array $data)
{
session()->put('input', json_encode($data));
session()->flash('input', json_encode($data));
}

/**
* Возвращает значение из POST данных
*
* @param string $name Имя поля
* @param string $key Имя поля
* @param mixed $default
*
* @return mixed Сохраненное значение
*
* @deprecated since 10.1 - Use old('field', 'default');
*/
function getInput(string $name, $default = null)
function getInput(string $key, $default = null)
{
if (session()->missing('input')) {
return $default;
}

$session = json_decode(session()->get('input'), true);
$input = Arr::pull($session, $name);
$input = json_decode(session()->get('input', []), true);

if ($input !== null) {
session()->put('input', json_encode($session));
}

return $input ?? $default;
return Arr::get($input, $key, $default);
}

/**
Expand Down Expand Up @@ -2105,7 +2100,7 @@ function ipBan(bool $clear = false): array
}

/**
* Возвращает пользовательские настройки сайта по ключу
* Возвращает настройки сайта по ключу
*
* @param string|null $key Ключ массива
* @param mixed $default Значение по умолчанию
Expand All @@ -2123,25 +2118,6 @@ function setting(?string $key = null, $default = null)
return $key ? ($settings[$key] ?? $default) : $settings;
}

/**
* Возвращает дефолтные настройки сайта по ключу
*
* @param string|null $key Ключ массива
* @param string|null $default Значение по умолчанию
*
* @return array|string|null Данные
*/
function defaultSetting(?string $key = null, ?string $default = null)
{
static $settings;

if (! $settings) {
$settings = Setting::getSettings();
}

return $key ? ($settings[$key] ?? $default) : $settings;
}

/**
* Возвращает имя сайта из ссылки
*
Expand Down
Expand Up @@ -20,7 +20,8 @@ public function up(): void
$table->integer('sort');
$table->enum('type', [UserField::INPUT, UserField::TEXTAREA]);
$table->string('name', 50);
$table->integer('length');
$table->integer('min');
$table->integer('max');
$table->boolean('required')->default(false);
});
}
Expand Down
Expand Up @@ -18,9 +18,9 @@ public function up(): void
$table->increments('id');
$table->integer('user_id');
$table->integer('field_id');
$table->text('value');
$table->text('value')->nullable();

$table->index(['user_id', 'field_id']);
$table->unique(['user_id', 'field_id']);
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions public/assets/css/main.css
Expand Up @@ -326,6 +326,11 @@ textarea.form-control {
max-height: 340px;
}

.form-required .form-label:after {
content:"*";
color:red;
}

/* Fix close button bootbox 5.5.2 */
.bootbox-close-button {
margin: -0.8rem -0.5rem -0.5rem auto;
Expand Down
2 changes: 1 addition & 1 deletion public/themes/default/views/theme.blade.php
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{ setting('language', 'ru') }}">
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta name="theme-color" content="#2e8cc2">
Expand Down
2 changes: 1 addition & 1 deletion public/themes/mobile/views/theme.blade.php
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{ setting('language', 'ru') }}">
<html lang="{{ app()->getLocale() }}">
<head>
<title>@yield('title') - {{ setting('title') }}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Expand Down
2 changes: 1 addition & 1 deletion public/themes/motor/views/theme.blade.php
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{ setting('language', 'ru') }}">
<html lang="{{ app()->getLocale() }}">
<head>
<title>@yield('title') - {{ setting('title') }}</title>
<meta charset="utf-8">
Expand Down
9 changes: 9 additions & 0 deletions resources/lang/en/admin.php
Expand Up @@ -291,4 +291,13 @@
'exists_install' => '
Attention! It is necessary to delete the file app/Http/Controllers/InstallController.php<br>
The presence of this file may compromise the security of the site. Remove it now!',

'user_fields' => [
'required' => 'Required',
'edit_field' => 'Edit field',
'create_field' => 'Create field',
'empty_fields' => 'No user fields yet!',
'input' => 'Оne-line field',
'textarea' => 'Multiline field',
],
];

0 comments on commit 9270a99

Please sign in to comment.