Change email display logic

Logic moved from rules to user model.
This commit is contained in:
Visman 2023-07-29 21:04:08 +07:00
parent dde662fadc
commit edfae72916
5 changed files with 81 additions and 58 deletions

View file

@ -37,13 +37,8 @@ class Email extends Page
return $this->c->Message->message('Bad request'); return $this->c->Message->message('Bad request');
} }
$rules = $this->c->ProfileRules->setUser($this->curUser); if (empty($this->curUser->linkEmail)) {
$message = null === $this->curUser->linkEmail ? 'Form email disabled' : 'Bad request';
if (
! $rules->viewEmail
|| ! $rules->sendEmail
) {
$message = null === $rules->sendEmail ? 'Form email disabled' : 'Bad request';
return $this->c->Message->message($message); return $this->c->Message->message($message);
} }

View file

@ -72,6 +72,7 @@ class View extends Profile
$fields[] = [ $fields[] = [
'type' => 'endwrap', 'type' => 'endwrap',
]; ];
if ( if (
$this->rules->useAvatar $this->rules->useAvatar
&& $this->curUser->avatar && $this->curUser->avatar
@ -82,6 +83,7 @@ class View extends Profile
'value' => 'avatar', 'value' => 'avatar',
]; ];
} }
$form['sets']['header'] = [ $form['sets']['header'] = [
'class' => ['header'], 'class' => ['header'],
'legend' => 'Essentials', 'legend' => 'Essentials',
@ -109,6 +111,7 @@ class View extends Profile
// личное // личное
$fields = []; $fields = [];
if ('' != $this->curUser->realname) { if ('' != $this->curUser->realname) {
$fields['realname'] = [ $fields['realname'] = [
'class' => ['pline'], 'class' => ['pline'],
@ -117,10 +120,12 @@ class View extends Profile
'value' => $this->curUser->censorRealname, 'value' => $this->curUser->censorRealname,
]; ];
} }
$genders = [ $genders = [
FORK_GEN_MAN => __('Male'), FORK_GEN_MAN => __('Male'),
FORK_GEN_FEM => __('Female'), FORK_GEN_FEM => __('Female'),
]; ];
if (isset($genders[$this->curUser->gender])) { if (isset($genders[$this->curUser->gender])) {
$fields['gender'] = [ $fields['gender'] = [
'class' => ['pline'], 'class' => ['pline'],
@ -129,6 +134,7 @@ class View extends Profile
'caption' => 'Gender', 'caption' => 'Gender',
]; ];
} }
if ('' != $this->curUser->location) { if ('' != $this->curUser->location) {
$fields['location'] = [ $fields['location'] = [
'class' => ['pline'], 'class' => ['pline'],
@ -137,6 +143,7 @@ class View extends Profile
'value' => $this->curUser->censorLocation, 'value' => $this->curUser->censorLocation,
]; ];
} }
if (! empty($fields)) { if (! empty($fields)) {
$form['sets']['personal'] = [ $form['sets']['personal'] = [
'class' => ['data'], 'class' => ['data'],
@ -147,6 +154,7 @@ class View extends Profile
// контактная информация // контактная информация
$fields = []; $fields = [];
if ($this->rules->sendPM) { if ($this->rules->sendPM) {
$this->c->Csrf->setHashExpiration(3600); $this->c->Csrf->setHashExpiration(3600);
@ -166,27 +174,27 @@ class View extends Profile
'href' => $this->c->Router->link('PMAction', $pmArgs), 'href' => $this->c->Router->link('PMAction', $pmArgs),
]; ];
} }
if ($this->rules->viewEmail) {
if (0 === $this->curUser->email_setting) { if (! empty($this->curUser->linkEmail)) {
if (\str_starts_with($this->curUser->linkEmail, 'mailto:')) {
$fields['email'] = [ $fields['email'] = [
'class' => ['pline'], 'class' => ['pline'],
'type' => 'link', 'type' => 'link',
'caption' => 'Email info', 'caption' => 'Email info',
'value' => $this->curUser->censorEmail, 'value' => \substr($this->curUser->linkEmail, 7),
'href' => 'mailto:' . $this->curUser->censorEmail, 'href' => $this->curUser->linkEmail,
]; ];
} elseif ($this->rules->sendEmail) { } else {
$this->c->Csrf->setHashExpiration(3600);
$fields['email'] = [ $fields['email'] = [
'class' => ['pline'], 'class' => ['pline'],
'type' => 'link', 'type' => 'link',
'caption' => 'Email info', 'caption' => 'Email info',
'value' => __('Send email'), 'value' => __('Send email'),
'href' => $this->c->Router->link('SendEmail', ['id' => $this->curUser->id]), 'href' => $this->curUser->linkEmail,
]; ];
} }
} }
if ( if (
$this->rules->viewWebsite $this->rules->viewWebsite
&& $this->curUser->url && $this->curUser->url
@ -201,6 +209,7 @@ class View extends Profile
'rel' => 'ugc', 'rel' => 'ugc',
]; ];
} }
if (! empty($fields)) { if (! empty($fields)) {
$form['sets']['contacts'] = [ $form['sets']['contacts'] = [
'class' => ['data'], 'class' => ['data'],
@ -212,6 +221,7 @@ class View extends Profile
// подпись // подпись
if ($this->rules->useSignature) { if ($this->rules->useSignature) {
$fields = []; $fields = [];
if ($this->curUser->isSignature) { if ($this->curUser->isSignature) {
$fields['signature'] = [ $fields['signature'] = [
'type' => 'yield', 'type' => 'yield',
@ -221,6 +231,7 @@ class View extends Profile
$this->signatureSection = true; $this->signatureSection = true;
} }
if (! empty($fields)) { if (! empty($fields)) {
$form['sets']['signature'] = [ $form['sets']['signature'] = [
'class' => ['data'], 'class' => ['data'],
@ -244,6 +255,7 @@ class View extends Profile
'value' => dt($this->curUser->last_post, true), 'value' => dt($this->curUser->last_post, true),
'caption' => 'Last post info', 'caption' => 'Last post info',
]; ];
if ($this->curUser->last_post > 0) { if ($this->curUser->last_post > 0) {
if (1 === $this->user->g_search) { if (1 === $this->user->g_search) {
$fields['posts'] = [ $fields['posts'] = [
@ -289,10 +301,12 @@ class View extends Profile
]; ];
} }
} }
if ($this->rules->viewSubscription) { if ($this->rules->viewSubscription) {
$subscr = $this->c->subscriptions; $subscr = $this->c->subscriptions;
$subscrInfo = $subscr->info($this->curUser); $subscrInfo = $subscr->info($this->curUser);
$isLink = 1 === $this->user->g_search; $isLink = 1 === $this->user->g_search;
if (! empty($subscrInfo[$subscr::FORUMS_DATA])) { if (! empty($subscrInfo[$subscr::FORUMS_DATA])) {
$fields['forums_subscr'] = [ $fields['forums_subscr'] = [
'class' => ['pline'], 'class' => ['pline'],
@ -309,6 +323,7 @@ class View extends Profile
'title' => __('Show forums subscriptions'), 'title' => __('Show forums subscriptions'),
]; ];
} }
if (! empty($subscrInfo[$subscr::TOPICS_DATA])) { if (! empty($subscrInfo[$subscr::TOPICS_DATA])) {
$fields['topics_subscr'] = [ $fields['topics_subscr'] = [
'class' => ['pline'], 'class' => ['pline'],
@ -326,6 +341,7 @@ class View extends Profile
]; ];
} }
} }
$form['sets']['activity'] = [ $form['sets']['activity'] = [
'class' => ['data'], 'class' => ['data'],
'legend' => 'User activity', 'legend' => 'User activity',
@ -334,6 +350,7 @@ class View extends Profile
// приватная информация // приватная информация
$fields = []; $fields = [];
if ($this->rules->viewLastVisit) { if ($this->rules->viewLastVisit) {
$fields['lastvisit'] = [ $fields['lastvisit'] = [
'class' => ['pline'], 'class' => ['pline'],
@ -344,6 +361,7 @@ class View extends Profile
'caption' => 'Last visit info', 'caption' => 'Last visit info',
]; ];
} }
if ($this->rules->viewOEmail) { if ($this->rules->viewOEmail) {
$fields['open-email'] = [ $fields['open-email'] = [
'class' => $this->curUser->email_confirmed ? ['pline', 'confirmed'] : ['pline', 'unconfirmed'], 'class' => $this->curUser->email_confirmed ? ['pline', 'confirmed'] : ['pline', 'unconfirmed'],
@ -353,6 +371,7 @@ class View extends Profile
'href' => 'mailto:' . $this->curUser->censorEmail, 'href' => 'mailto:' . $this->curUser->censorEmail,
]; ];
} }
if ( if (
$this->rules->viewIP $this->rules->viewIP
&& false !== \filter_var($this->curUser->registration_ip, \FILTER_VALIDATE_IP) && false !== \filter_var($this->curUser->registration_ip, \FILTER_VALIDATE_IP)
@ -371,13 +390,13 @@ class View extends Profile
'title' => __('IP title'), 'title' => __('IP title'),
]; ];
} }
$form['sets']['private'] = [ $form['sets']['private'] = [
'class' => ['data'], 'class' => ['data'],
'legend' => 'Private information', 'legend' => 'Private information',
'fields' => $fields, 'fields' => $fields,
]; ];
return $form; return $form;
} }
} }

View file

@ -62,20 +62,6 @@ class Profile extends Rules
return $this->my || $this->user->isAdmMod; return $this->my || $this->user->isAdmMod;
} }
protected function getviewEmail(): bool
{
return ! $this->my
&& (
$this->user->isAdmMod // ???? модераторы у админов должны видеть email?
|| (
! $this->user->isGuest
&& ! $this->user->isAdmMod
&& 1 === $this->user->g_send_email
&& $this->curUser->email_setting < 2
)
);
}
protected function geteditEmail(): bool protected function geteditEmail(): bool
{ {
return $this->my || $this->admin; return $this->my || $this->admin;
@ -86,21 +72,6 @@ class Profile extends Rules
return $this->my && ! $this->curUser->email_confirmed; return $this->my && ! $this->curUser->email_confirmed;
} }
protected function getsendEmail(): ?bool // ???? проверка на подтвержденный email?
{
if ($this->viewEmail) {
if ($this->user->isAdmMod) {
return true;
} elseif (1 === $this->curUser->email_setting) {
return true;
}
} elseif (2 === $this->curUser->email_setting) {
return null;
}
return false;
}
protected function getsendPM(): bool protected function getsendPM(): bool
{ {
return ! $this->my return ! $this->my

View file

@ -30,13 +30,14 @@ class User extends DataModel
parent::__construct($container); parent::__construct($container);
$this->zDepend = [ $this->zDepend = [
'group_id' => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'isBanByName', 'link', 'usePM'], 'group_id' => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'isBanByName', 'link', 'usePM', 'linkEmail'],
'id' => ['isGuest', 'link', 'online'], 'id' => ['isGuest', 'link', 'online', 'linkEmail'],
'last_visit' => ['currentVisit'], 'last_visit' => ['currentVisit'],
'signature' => ['isSignature'], 'signature' => ['isSignature'],
'email' => ['email_normal'], 'email' => ['email_normal', 'linkEmail'],
'username' => ['username_normal'], 'username' => ['username_normal'],
'g_pm' => ['usePM'], 'g_pm' => ['usePM'],
'email_setting' => ['linkEmail'],
]; ];
} }
@ -366,4 +367,41 @@ class User extends DataModel
|| $this->isAdmin || $this->isAdmin
); );
} }
/**
* Формирует ссылку на отправку письма от $this->c->user к $this
* ???? Результат используется как условие для вариантов отображения
*/
protected function getlinkEmail(): ?string
{
if (
$this->c->user->isGuest
|| $this->id === $this->c->user->id
|| empty($this->email)
) {
return '';
} elseif (2 === $this->email_setting) {
return null;
} elseif (
0 === $this->email_setting
|| (
$this->isGuest
&& $this->c->user->isAdmMod
)
) {
return 'mailto:' . $this->censorEmail;
} elseif (
1 === $this->email_setting
&& (
1 === $this->c->user->g_send_email
|| $this->c->user->isAdmin
)
) {
$this->c->Csrf->setHashExpiration(3600);
return $this->c->Router->link('SendEmail', ['id' => $this->id]);
} else {
return '';
}
}
} }

View file

@ -115,8 +115,8 @@
@if ($post->user->url) @if ($post->user->url)
<li class="f-user-contacts f-website"><a href="{{ $post->user->censorUrl }}" title="{{ __('Website title') }}" rel="ugc"><span class="f-psfont">{!! __('Website') !!}</span></a></li> <li class="f-user-contacts f-website"><a href="{{ $post->user->censorUrl }}" title="{{ __('Website title') }}" rel="ugc"><span class="f-psfont">{!! __('Website') !!}</span></a></li>
@endif @endif
@if (($post->user->isGuest && $post->user->email && $p->user->isAdmMod) || 0 === $post->user->email_setting) @if ($post->user->linkEmail)
<li class="f-user-contacts f-email"><a href="mailto:{{ $post->user->censorEmail }}" title="{{ __('Email title') }}" rel="ugc"><span class="f-psfont">{!! __('Email ') !!}</span></a></li> <li class="f-user-contacts f-email"><a href="{{ $post->user->linkEmail }}" title="{{ __('Email title') }}" rel="ugc"><span class="f-psfont">{!! __('Email ') !!}</span></a></li>
@endif @endif
</ul> </ul>
@endif @endif