e107inc / e107

e107 Bootstrap CMS (Content Management System) v2 with PHP, MySQL, HTML5, jQuery and Twitter Bootstrap. Issue Discussion Room: https://gitter.im/e107inc/e107
https://e107.org
GNU General Public License v3.0
321 stars 213 forks source link

Right way to update User Extended fields from code #3879

Open nesjett opened 5 years ago

nesjett commented 5 years ago

I've been trying to update extended fields from withing the code and It looks really complex, looking into usersettings.php looks like the proper way of doing so, but I think it could be done in just 2 methods, one for sanitizing and the other for actually inserting the data.

Actually, the way I found to do it, based on usersettings.php is like this:

`$d = ['user_gender'=> $genderize->gender];

            $euVals = $ue->sanitizeAll($d);
            //$euVals = $ue->userExtendedValidateAll($euVals, []);      // Validate the extended user fields
            $eufVals['data']['user_gender'] = $tp->toDB($euVals['user_gender']);

            if (!empty($eufVals['data']))
            {
                $ue->addFieldTypes($eufVals);               // Add in the data types for storage
                $eufVals['_DUPLICATE_KEY_UPDATE'] = true; // update record if key found, otherwise INSERT.
                $eufVals['data']['user_extended_id'] = $data['user_id'];

                if (false === $sql->insert('user_extended', $eufVals))
                {
                    $log->add('error setting gender', $eufVals, 'E_LOG_INFORMATIVE', 'OAUTH');
                }

            }

`

Althought, this line: //$euVals = $ue->userExtendedValidateAll($euVals, []); // Validate the extended user fields Is used in usersettings.php but I can't get it to work so I had to put results by hand, without other checks with this line: $eufVals['data']['user_gender'] = $tp->toDB($euVals['user_gender']);

Really there is no other way of setting user extended fields?

nesjett commented 5 years ago

A detailed look into userExtendedValidateAll() shows that this line is returning false, so the function does not do its job:

if (($isSignup && (int) $defs['user_extended_struct_applicable'] === (int) e_UC_MEMBER && (int) $defs['user_extended_struct_write'] === (int) e_UC_MEMBER) || (check_class($defs['user_extended_struct_applicable']) && check_class($defs['user_extended_struct_write'])))

I still dont understand it at all, but I guess it's because I'm calling it with the user logged out

Moc commented 5 years ago

There are lots of functionalities related to the EUF in core already so I doubt that you'll need to change the code.

Can you explain the end result that you are trying to achieve?

nesjett commented 5 years ago

I just want to modify the value of an user extended field, taking into account its params, for example if its a radio button, it should only accept one of it's configured params.

** EDIT: but I want to do so by the system, without the need of a logged in user

Moc commented 5 years ago

Not sure if I understand you correctly still, but can you not edit that by editing the user in the admin area?

nesjett commented 5 years ago

No, I need to edit values programatically, for example, I'm updating the extended user field "gender" when the event user_xup_signup is fired.

This would be perfect:

e107::user($id)->setExtendedFront('gender', 'M => UE_LAN_MALE');

But it checks for the right permissions of the user, this means this cannot be fired when the user is "anonimous"

Jimmi08 commented 5 years ago

@nesjett sorry, I didn't read it in detail (it's too complicated) but we used the birthday as mandatory field on signup page and it worked for anonymous too.

nesjett commented 5 years ago

Thank you @Jimmi08 , what I'm asking is just for the best way to modify a extended user field WITHOUT an input element.

I'll check the way signup does it anyway, thank you!

Jimmi08 commented 2 years ago

I needed this now and this works:

 if($result) {
     $userThreadId = $result['threadid'];
         $ue = new e107_user_extended;
        $ue->user_extended_setvalue($aposUserId, 'user_plugin_apos_forum_thread', $userThreadId)ô
}