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
));
}
}
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'))),
);
}
}
Thanks,
Justin Back