Visman %!s(int64=7) %!d(string=hai) anos
pai
achega
326582ff78
Modificáronse 4 ficheiros con 87 adicións e 38 borrados
  1. 1 1
      app/Core/Validator.php
  2. 12 10
      app/Models/DataModel.php
  3. 72 25
      app/Models/Pages/Profile.php
  4. 2 2
      app/Models/Rules/Profile.php

+ 1 - 1
app/Core/Validator.php

@@ -414,7 +414,7 @@ class Validator
      */
      */
     public function getData()
     public function getData()
     {
     {
-        if (empty($this->result)) {
+        if (empty($this->status)) {
             throw new RuntimeException('Data not found');
             throw new RuntimeException('Data not found');
         }
         }
         return $this->result;
         return $this->result;

+ 12 - 10
app/Models/DataModel.php

@@ -16,6 +16,7 @@ class DataModel extends Model
 
 
     /**
     /**
      * Устанавливает значения для свойств
      * Устанавливает значения для свойств
+     * Флаги модификации свойст сброшены
      *
      *
      * @param array $attrs
      * @param array $attrs
      *
      *
@@ -30,23 +31,24 @@ class DataModel extends Model
     }
     }
 
 
     /**
     /**
-     * Перезапись свойст модели
+     * Перезаписывает свойства модели
+     * Флаги модификации свойств сбрасываются/устанавливаются в зависимости от второго параметра
      *
      *
      * @param array $attrs
      * @param array $attrs
+     * @param bool $setFlags
      *
      *
      * @return DataModel
      * @return DataModel
      */
      */
-    public function replAttrs(array $attrs)
+    public function replAttrs(array $attrs, $setFlags = false)
     {
     {
-        foreach ($attrs as $key => $val) {
-            $this->{'__' . $key} = $val; //????
-//            unset($this->aCalc['key']);
-        }
+        foreach ($attrs as $name => $value) {
+            $this->__set($name, $value);
 
 
-        $modified = \array_diff(\array_keys($this->modified), \array_keys($attrs));
-        $this->modified = [];
-        foreach ($modified as $key) {
-            $this->modified[$key] = true;
+            if (! $setFlags) {
+//                $this->modified[$name] = true;
+//            } else {
+                unset($this->modified[$name]);
+            }
         }
         }
 
 
         return $this;
         return $this;

+ 72 - 25
app/Models/Pages/Profile.php

@@ -91,6 +91,42 @@ class Profile extends Page
         $this->c->Lang->load('profile');
         $this->c->Lang->load('profile');
 
 
         if ($isEdit && 'POST' === $method) {
         if ($isEdit && 'POST' === $method) {
+            if ($rules->rename) {
+                $ruleUsername = 'required|string:trim,spaces|min:2|max:25|login|check_username';
+            } else {
+                $ruleUsername = 'absent';
+            }
+
+            if ($rules->setTitle) {
+                $ruleTitle = 'string:trim|max:50|no_url';
+            } else {
+                $ruleTitle = 'absent';
+            }
+
+            if ($rules->useAvatar) {
+                $ruleAvatar = "image|max:{$this->c->Files->maxImgSize('K')}";
+            } else {
+                $ruleAvatar = 'absent';
+            }
+
+            if ($this->user->isAdmMod) {
+                $ruleAdminNote = 'string:trim|max:30';
+            } else {
+                $ruleAdminNote = 'absent';
+            }
+
+            if ($rules->editWebsite) {
+                $ruleWebsite = 'string:trim|max:100'; // ???? валидация url?
+            } else {
+                $ruleWebsite = 'absent';
+            }
+
+            if ($rules->useSignature) {
+                $ruleSignature = "string:trim|max:{$this->c->config->p_sig_length}|check_signature";
+            } else {
+                $ruleSignature = 'absent';
+            }
+
             $v = $this->c->Validator->reset()
             $v = $this->c->Validator->reset()
                 ->addValidators([
                 ->addValidators([
                     'no_url'          => [$this->c->Validators, 'vNoURL'],
                     'no_url'          => [$this->c->Validators, 'vNoURL'],
@@ -98,24 +134,38 @@ class Profile extends Page
                     'check_signature' => [$this, 'vCheckSignature'],
                     'check_signature' => [$this, 'vCheckSignature'],
                 ])->addRules([
                 ])->addRules([
                     'token'         => 'token:EditUserProfile',
                     'token'         => 'token:EditUserProfile',
-                    'username'      => $rules->rename? 'required|string:trim,spaces|min:2|max:25|login|check_username' : 'absent',
-                    'title'         => $rules->setTitle ? 'string:trim|max:50|no_url' : 'absent',
-                    'upload_avatar' => $rules->useAvatar ? "image|max:{$this->c->Files->maxImgSize('K')}" : 'absent',
-                    'admin_note'    => $this->user->isAdmMod ? 'string:trim|max:30' : 'absent',
+                    'username'      => $ruleUsername,
+                    'title'         => $ruleTitle,
+                    'upload_avatar' => $ruleAvatar,
+                    'admin_note'    => $ruleAdminNote,
                     'realname'      => 'string:trim|max:40|no_url',
                     'realname'      => 'string:trim|max:40|no_url',
                     'gender'        => 'required|integer|in:0,1,2',
                     'gender'        => 'required|integer|in:0,1,2',
                     'location'      => 'string:trim|max:30|no_url',
                     'location'      => 'string:trim|max:30|no_url',
                     'email_setting' => 'required|integer|in:0,1,2',
                     'email_setting' => 'required|integer|in:0,1,2',
-                    'url'           => $rules->editLinks ? 'string:trim|max:100' : 'absent',
-                    'signature'     => $rules->useSignature ? "string:trim|max:{$this->c->config->p_sig_length}|check_signature" : 'absent',
+                    'url'           => $ruleWebsite,
+                    'signature'     => $ruleSignature,
                 ])->addAliases([
                 ])->addAliases([
+                    'username'      => 'Username',
+                    'title'         => 'Title',
+                    'upload_avatar' => 'New avatar',
+                    'admin_note'    => 'Admin note',
+                    'realname'      => 'Realname',
+                    'gender'        => 'Gender',
+                    'location'      => 'Location',
+                    'email_setting' => 'Email settings label',
+                    'url'           => 'Website',
+                    'signature'     => 'Signature',
                 ])->addArguments([
                 ])->addArguments([
                     'token'                   => ['id' => $curUser->id],
                     'token'                   => ['id' => $curUser->id],
                     'username.check_username' => $curUser,
                     'username.check_username' => $curUser,
                 ])->addMessages([
                 ])->addMessages([
                 ]);
                 ]);
 
 
-            if ($v->validation($_FILES + $_POST)) {
+            $valid = $v->validation($_FILES + $_POST);
+            $data  = $v->getData();
+            unset($data['token'], $data['upload_avatar']);
+
+            if ($valid) {
                 if ($v->upload_avatar instanceof Image) {
                 if ($v->upload_avatar instanceof Image) {
                     $curUser->deleteAvatar();
                     $curUser->deleteAvatar();
                     $v->upload_avatar
                     $v->upload_avatar
@@ -125,19 +175,16 @@ class Profile extends Page
                         ->toFile($this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$curUser->id}.(jpg|png|gif)");
                         ->toFile($this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$curUser->id}.(jpg|png|gif)");
                 }
                 }
 
 
-                $data = $v->getData();
-                unset($data['token'], $data['upload_avatar']);
-
-                foreach ($data as $attr => $value) {
-                    $curUser->$attr = $value;
-                }
+                $curUser->replAttrs($data, true);
 
 
                 $this->c->users->update($curUser);
                 $this->c->users->update($curUser);
 
 
                 return $this->c->Redirect->page('EditUserProfile',  ['id' => $curUser->id])->message('Profile redirect');
                 return $this->c->Redirect->page('EditUserProfile',  ['id' => $curUser->id])->message('Profile redirect');
-            }
+            } else {
+                $this->fIswev = $v->getErrors();
 
 
-            $this->fIswev = $v->getErrors();
+                $curUser->replAttrs($data);
+            }
         }
         }
 
 
         $clSuffix = $isEdit ? '-edit' : '';
         $clSuffix = $isEdit ? '-edit' : '';
@@ -175,7 +222,7 @@ class Profile extends Page
                 'caption'   => \ForkBB\__('Username'),
                 'caption'   => \ForkBB\__('Username'),
                 'required'  => true,
                 'required'  => true,
                 'pattern'   => '^.{2,25}$',
                 'pattern'   => '^.{2,25}$',
-                'value'     => isset($v->username) ? $v->username : $curUser->username,
+                'value'     => $curUser->username,
             ];
             ];
         } else {
         } else {
             $fields['username'] = [
             $fields['username'] = [
@@ -192,7 +239,7 @@ class Profile extends Page
                 'type'      => 'text',
                 'type'      => 'text',
                 'maxlength' => 50,
                 'maxlength' => 50,
                 'caption'   => \ForkBB\__('Title'),
                 'caption'   => \ForkBB\__('Title'),
-                'value'     => isset($v->title) ? $v->title : $curUser->title,
+                'value'     => $curUser->title,
                 'info'      => \ForkBB\__('Leave blank'),
                 'info'      => \ForkBB\__('Leave blank'),
             ];
             ];
         } else {
         } else {
@@ -257,7 +304,7 @@ class Profile extends Page
                     'type'      => 'text',
                     'type'      => 'text',
                     'maxlength' => 30,
                     'maxlength' => 30,
                     'caption'   => \ForkBB\__('Admin note'),
                     'caption'   => \ForkBB\__('Admin note'),
-                    'value'     => isset($v->admin_note) ? $v->admin_note : $curUser->admin_note,
+                    'value'     => $curUser->admin_note,
                 ];
                 ];
             } elseif ('' != $curUser->admin_note) {
             } elseif ('' != $curUser->admin_note) {
                 $fields['admin_note'] = [
                 $fields['admin_note'] = [
@@ -286,7 +333,7 @@ class Profile extends Page
                 'type'      => 'text',
                 'type'      => 'text',
                 'maxlength' => 40,
                 'maxlength' => 40,
                 'caption'   => \ForkBB\__('Realname'),
                 'caption'   => \ForkBB\__('Realname'),
-                'value'     => isset($v->realname) ? $v->realname : $curUser->realname,
+                'value'     => $curUser->realname,
             ];
             ];
         } elseif ('' != $curUser->realname) {
         } elseif ('' != $curUser->realname) {
             $fields['realname'] = [
             $fields['realname'] = [
@@ -307,7 +354,7 @@ class Profile extends Page
                 'id'      => 'gender',
                 'id'      => 'gender',
                 'class'   => 'block',
                 'class'   => 'block',
                 'type'    => 'radio',
                 'type'    => 'radio',
-                'value'   => isset($v->gender) ? $v->gender : $curUser->gender,
+                'value'   => $curUser->gender,
                 'values'  => $genders,
                 'values'  => $genders,
                 'caption' => \ForkBB\__('Gender'),
                 'caption' => \ForkBB\__('Gender'),
             ];
             ];
@@ -326,7 +373,7 @@ class Profile extends Page
                 'type'      => 'text',
                 'type'      => 'text',
                 'maxlength' => 30,
                 'maxlength' => 30,
                 'caption'   => \ForkBB\__('Location'),
                 'caption'   => \ForkBB\__('Location'),
-                'value'     => isset($v->location) ? $v->location : $curUser->location,
+                'value'     => $curUser->location,
             ];
             ];
         } elseif ('' != $curUser->location) {
         } elseif ('' != $curUser->location) {
             $fields['location'] = [
             $fields['location'] = [
@@ -384,7 +431,7 @@ class Profile extends Page
                 'id'      => 'email_setting',
                 'id'      => 'email_setting',
                 'class'   => 'block',
                 'class'   => 'block',
                 'type'    => 'radio',
                 'type'    => 'radio',
-                'value'   => isset($v->email_setting) ? $v->email_setting : $curUser->email_setting,
+                'value'   => $curUser->email_setting,
                 'values'  => [
                 'values'  => [
                     0 => \ForkBB\__('Display e-mail label'),
                     0 => \ForkBB\__('Display e-mail label'),
                     1 => \ForkBB\__('Hide allow form label'),
                     1 => \ForkBB\__('Hide allow form label'),
@@ -393,7 +440,7 @@ class Profile extends Page
                 'caption' => \ForkBB\__('Email settings label'),
                 'caption' => \ForkBB\__('Email settings label'),
             ];
             ];
         }
         }
-        if ($rules->editLinks && $isEdit) {
+        if ($rules->editWebsite && $isEdit) {
             $fields['url'] = [
             $fields['url'] = [
                 'id'        => 'website',
                 'id'        => 'website',
                 'type'      => 'text',
                 'type'      => 'text',
@@ -401,7 +448,7 @@ class Profile extends Page
                 'caption'   => \ForkBB\__('Website'),
                 'caption'   => \ForkBB\__('Website'),
                 'value'     => isset($v->url) ? $v->url : $curUser->url,
                 'value'     => isset($v->url) ? $v->url : $curUser->url,
             ];
             ];
-        } elseif ($rules->viewLinks && $curUser->url) {
+        } elseif ($rules->viewWebsite && $curUser->url) {
             $fields['url'] = [
             $fields['url'] = [
                 'id'      => 'website',
                 'id'      => 'website',
                 'class'   => 'pline',
                 'class'   => 'pline',
@@ -427,7 +474,7 @@ class Profile extends Page
                 $fields['signature'] = [
                 $fields['signature'] = [
                     'id'      => 'signature',
                     'id'      => 'signature',
                     'type'    => 'textarea',
                     'type'    => 'textarea',
-                    'value'   => isset($v->signature) ? $v->signature : $curUser->signature,
+                    'value'   => $curUser->signature,
                     'caption' => \ForkBB\__('Signature'),
                     'caption' => \ForkBB\__('Signature'),
                     'info'    => \ForkBB\__('Sig max size', \ForkBB\num($this->c->config->p_sig_length), \ForkBB\num($this->c->config->p_sig_lines)),
                     'info'    => \ForkBB\__('Sig max size', \ForkBB\num($this->c->config->p_sig_length), \ForkBB\num($this->c->config->p_sig_lines)),
                 ];
                 ];

+ 2 - 2
app/Models/Rules/Profile.php

@@ -88,12 +88,12 @@ class Profile extends Rules
         return '1' == $this->c->config->o_signatures;
         return '1' == $this->c->config->o_signatures;
     }
     }
 
 
-    protected function getviewLinks()
+    protected function getviewWebsite()
     {
     {
         return $this->user->isAdmMod || '1' == $this->curUser->g_post_links;
         return $this->user->isAdmMod || '1' == $this->curUser->g_post_links;
     }
     }
 
 
-    protected function geteditLinks()
+    protected function geteditWebsite()
     {
     {
         return $this->admin || (($this->moderator || $this->my) && '1' == $this->user->g_post_links);
         return $this->admin || (($this->moderator || $this->my) && '1' == $this->user->g_post_links);
     }
     }