Edit Profile Field after core.user_add_after

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
JustinBack
Registered User
Posts: 5
Joined: Sat Oct 06, 2018 7:05 pm

Edit Profile Field after core.user_add_after

Post by JustinBack » Sat Oct 06, 2018 7:14 pm

Hello,

I am new to extension development and I finally figured out how to properly use events, however I am stuck with editing profile field data of a user.


Event Listener:

Code: Select all

<?php

namespace justinback\jblicense\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class register_event implements EventSubscriberInterface
{
    
    protected $config;
    protected $helper;
    protected $template;
    protected $user;
    protected $db;
    
    public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $helper, \phpbb\template\template $template, \phpbb\user $user)
{
    $this->config = $config;
    $this->helper = $helper;
    $this->template = $template;
    $this->user = $user;
}
    
    /**
     * Assign functions defined in this class to event listeners in the core
     *
     * @return array
     */
    static public function getSubscribedEvents()
    {
        return array(
            'core.user_add_after' => 'JBLicense_new_customer',
        );
    }

    public function JBLicense_new_customer($event)
    {
                $aOptions = array(
                    'http' => array(
                        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                        'method'  => 'POST',
                        'content' => http_build_query(array("name" => $event["user_row"]['username'], "email" => $event["user_row"]['user_email'])),
                        'ignore_errors' => true
                    )
                );
		
                $this->user = $event["user_row"];
                
                $cContext  = stream_context_create($aOptions);
                $fgcNewCustomer = file_get_contents("https://api.justinback.com/ILicense/NewCustomer/v1/?key=".$this->config["jblicense_apikey"], false, $cContext);
                $oNewCustomer = json_decode($fgcNewCustomer);
                
                echo $oNewCustomer->response->result->userinfo->Customer_ID;
                $event['cp_data'] = array_merge($event['cp_data'], array(
                    'jblicense_user_id'	=> $oNewCustomer->response->result->userinfo->Customer_ID
                ));
                
    }
}
cp_data is empty eventhough the Event List mentions that the array is being sent, the profile field gets properly created in the migration file and is present in the database, not populated however!


Migration:

Code: Select all

<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace justinback\jblicense\migrations;
class v_200 extends \phpbb\db\migration\profilefield_base_migration
{
    static public function depends_on()
    {
        return array(
            '\phpbb\db\migration\data\v310\profilefield_types',
            '\phpbb\db\migration\data\v310\profilefield_on_memberlist',
        );
    }
    protected $profilefield_name = 'jblicense_user_id';
    protected $profilefield_database_type = array('VCHAR', '');
    protected $profilefield_data = array(
        'field_name'            => 'jblicense_user_id',
        'field_type'            => 'profilefields.type.string',
        'field_ident'            => 'jblicense_user_id',
        'field_length'            => '80',
        'field_minlen'            => '0',
        'field_maxlen'            => '80',
        'field_novalue'            => '',
        'field_default_value'    => '0',
        'field_validation'        => '[0-9]+',
        'field_required'        => 1,
        'field_show_novalue'    => 0,
        'field_show_on_reg'        => 0,
        'field_show_on_pm'        => 0,
        'field_show_on_vt'        => 0,
        'field_show_profile'    => 0,
        'field_hide'            => 1,
        'field_no_view'            => 1,
        'field_active'            => 1,
    );
    public function update_data()
    {
        return array(
            array('custom', array(array($this, 'create_custom_field'))),
        );
    }
}
As the docs are lacking a bit of information I'd be glad to receive some information here :)

Thanks,
Justin Back

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3296
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Edit Profile Field after core.user_add_after

Post by kasimi » Sat Oct 06, 2018 7:33 pm

The cp_data in core.user_add_after is read only. See here, there's nothing happening with it afterwards.

Use core.user_add_modify_data for changes to cp_data to be saved to the database.

JustinBack
Registered User
Posts: 5
Joined: Sat Oct 06, 2018 7:05 pm

Re: Edit Profile Field after core.user_add_after

Post by JustinBack » Sat Oct 06, 2018 8:13 pm

Awesome thank you, it kinda works. It says the column jblicense_user_id doesn't exist eventhough its present in phpbb_profile_fields.

Image

does it have to be somewhere else? If it has to be in the phpbb_users table as a new column how would I achieve adding that column in the migration process?

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3296
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Edit Profile Field after core.user_add_after

Post by kasimi » Sat Oct 06, 2018 8:35 pm

I'm not sure if this is what's causing your problem but in the migration you posted you have defined an update_data() method which overwrites the one in the parent class causing the convert_user_field_to_custom_field() method that initializes the profile field data in the database not to be called. Try removing your update_data() method and running the migration again by disabling the extension, deleting extension data and then enabling it again.

JustinBack
Registered User
Posts: 5
Joined: Sat Oct 06, 2018 7:05 pm

Re: Edit Profile Field after core.user_add_after

Post by JustinBack » Sat Oct 06, 2018 8:47 pm

Sadly that results in the same error, I found a way to edit the schema however its not adding the new field either, I am seriously missing something here.

Code: Select all

<?php

/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace justinback\jblicense\migrations;

class v_200 extends \phpbb\db\migration\profilefield_base_migration {

    static public function depends_on() {
        return array(
            '\phpbb\db\migration\data\v310\profilefield_types',
            '\phpbb\db\migration\data\v310\profilefield_on_memberlist',
        );
    }

    protected $profilefield_name = 'jblicense_user_id';
    protected $profilefield_database_type = array('VCHAR', '');
    protected $profilefield_data = array(
        'field_name' => 'jblicense_user_id',
        'field_type' => 'profilefields.type.string',
        'field_ident' => 'jblicense_user_id',
        'field_length' => '80',
        'field_minlen' => '0',
        'field_maxlen' => '80',
        'field_novalue' => '',
        'field_default_value' => '0',
        'field_validation' => '[0-9]+',
        'field_required' => 0,
        'field_show_novalue' => 0,
        'field_show_on_reg' => 0,
        'field_show_on_pm' => 0,
        'field_show_on_vt' => 0,
        'field_show_profile' => 0,
        'field_hide' => 1,
        'field_no_view' => 1,
        'field_active' => 1,
    );

    public function update_data() {
        return array(
            array('custom', array(array($this, 'create_custom_field'))),
        );
    }

    public function update_schema() {
        return array(
            'add_columns' => array(
                $this->table_prefix . 'users' => array(
                    'jblicense_user_id' => array('UINT', 0),
                ),
            ),
        );
    }

    public function revert_schema() {
        return array(
            'drop_columns' => array(
                $this->table_prefix . 'users' => array(
                    'jblicense_user_id',
                ),
            ),
        );
    }

}
Edit: Nevermind, just figured out it has to be 2 seperate files. However it still prints out

Image

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3296
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Edit Profile Field after core.user_add_after

Post by kasimi » Sat Oct 06, 2018 9:02 pm

After having another look at the class you inherit from, I believe your migration as you posted in your first post is fine. The additional convert_user_field_to_custom_field() method is only used for core profile fields and shouldn't be called from your migration.

I also noticed that the column name in the phpbb_profile_fields_data table is actually prefixed with pf_, see here: https://github.com/phpbb/phpbb/blob/rel ... on.php#L53

JustinBack
Registered User
Posts: 5
Joined: Sat Oct 06, 2018 7:05 pm

Re: Edit Profile Field after core.user_add_after

Post by JustinBack » Sat Oct 06, 2018 9:07 pm

kasimi wrote:
Sat Oct 06, 2018 9:02 pm
After having another look at the class you inherit from, I believe your migration as you posted in your first post is fine. The additional convert_user_field_to_custom_field() method is only used for core profile fields and shouldn't be called from your migration.

I also noticed that the column name in the phpbb_profile_fields_data table is actually prefixed with pf_, see here: https://github.com/phpbb/phpbb/blob/rel ... on.php#L53
The Prefix was the key, thank you very much it works now!
Image

Just a quick question: Is the user_row field read only too?

Code: Select all

$event['user_row']['jblicense_user_id'] = $oNewCustomer->response->result->userinfo->Customer_ID;
is not setting anything.


Also: Is there a way to "revert" the registration? Just in case if the license server doesn't create the customer and no ID gets assigned.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3296
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Edit Profile Field after core.user_add_after

Post by kasimi » Sat Oct 06, 2018 9:18 pm

Yes, the user_row is read only too, but you can add your data to the sql_ary array:

Code: Select all

$event['sql_ary'] = array_merge($event['sql_ary'], [
    'jblicense_user_id' => ...,
]);
At this point it's too late to stop the registration. I'd recommend doing all checks that could result in the registration to be denied in the core.ucp_register_data_after event. Adding error messages to that event's error array stops the registration and displays the messages to the user.

JustinBack
Registered User
Posts: 5
Joined: Sat Oct 06, 2018 7:05 pm

Re: Edit Profile Field after core.user_add_after

Post by JustinBack » Sat Oct 06, 2018 9:21 pm

kasimi wrote:
Sat Oct 06, 2018 9:18 pm
Yes, the user_row is read only too, but you can add your data to the sql_ary array:

Code: Select all

$event['sql_ary'] = array_merge($event['sql_ary'], [
    'jblicense_user_id' => ...,
]);
At this point it's too late to stop the registration. I'd recommend doing all checks that could result in the registration to be denied in the core.ucp_register_data_after event. Adding error messages to that event's error array stops the registration the messages to the user.
Awesome, thank you very much for your help. Thats a huge step forward for me!

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 20 guests