User values in extension

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
bzozoo
Registered User
Posts: 24
Joined: Mon Feb 11, 2019 6:33 am

User values in extension

Post by bzozoo » Sun Jun 16, 2019 4:22 pm

Hi All

My Config values problem is solved, but there is another problem right here. I'm trying to display the user variable with analogie from David63's help.

I have 3 template user vars in main_listener.php

Code: Select all

                         'CCCT'                 => $this->user->page['page_name'],
			 'CCCT2'                 => $this->user->data['username'],
			 'CCCT3'                 => $this->user->data['user_id']

But I get 3 error messages:

Code: Select all

[phpBB Debug] PHP Notice: in file [ROOT]/ext/bzozoodev/bzozoomod/event/main_listener.php on line 93: Undefined index: page_name
[phpBB Debug] PHP Notice: in file [ROOT]/ext/bzozoodev/bzozoomod/event/main_listener.php on line 94: Undefined index: username
[phpBB Debug] PHP Notice: in file [ROOT]/ext/bzozoodev/bzozoomod/event/main_listener.php on line 95: Undefined index: user_id
My /event/main_listener.php content is:

Code: Select all

<?php
/**
 *
 * bzozoo Mod. An extension for the phpBB Forum Software package.
 *
 * @copyright (c) 2019, bzozoo DEV
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 */

namespace bzozoodev\bzozoomod\event;

/**
 * @ignore
 */
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * bzozoo Mod Event listener.
 */
class main_listener implements EventSubscriberInterface
{
	public static function getSubscribedEvents()
	{
		return array(
			'core.user_setup'				=> 'load_language_on_setup',
			'core.common'     => 'global_calls',
		);
	}

	/* @var \phpbb\language\language */
	protected $request;
	protected $user;
	protected $auth;
	protected $db;
	protected $template;
	protected $root_path;
	protected $phpEx;
	protected $config;
	protected $language;


	/**
	 * Constructor
	 *
	 * @param \phpbb\language\language	$language	Language object
	 */
	public function __construct(
		\phpbb\request\request $request,
		\phpbb\user $user,
		\phpbb\auth\auth $auth,
		\phpbb\db\driver\factory $db,
		\phpbb\config\config $config,
		\phpbb\template\template $template,
		$root_path,
		$phpEx,
		\phpbb\language\language $language)
	{
		$this->request = $request;
		$this->user			= $user;
		$this->auth         = $auth;
		$this->db = $db;
		$this->config		= $config;
		$this->template		= $template;
		$this->root_path	= $root_path;
		$this->phpEx		= $phpEx;
		$this->language		= $language;
	}

	/**
	 * Load common language files during user setup
	 *
	 * @param \phpbb\event\data	$event	Event object
	 */
	public function load_language_on_setup($event)
	{
		$lang_set_ext = $event['lang_set_ext'];
		$lang_set_ext[] = array(
			'ext_name' => 'bzozoodev/bzozoomod',
			'lang_set' => 'common',
		);
		$event['lang_set_ext'] = $lang_set_ext;
		

	}
	
		public function global_calls($event)
	{
		// Assign global template vars.
		$this->template->assign_vars(array(
             'ACTUAL_BB_VERSION'	=> $this->config['version'],
			 'TOTAL_USERS_NUMBER'	=> $this->config['num_users'],
			 'CCCT'                 => $this->user->page['page_name'],
			 'CCCT2'                 => $this->user->data['username'],
			 'CCCT3'                 => $this->user->data['user_id']
		));
	}
}
My /config/services.yml is:

Code: Select all

services:
    bzozoodev.bzozoomod.listener:
        class: bzozoodev\bzozoomod\event\main_listener
        arguments:
            - '@request'
            - '@user'
            - '@auth'
            - '@dbal.conn'
            - '@config'
            - '@template'
            - '%core.root_path%'
            - '%core.php_ext%'
            - '@language'
        tags:
            - { name: event.listener }
What could be the problem? Why can't It find user variables? When I use $this->user->... and I use public function __construct(\phpbb\user $user,....... What do I need to fix the code?

Thanks for yopur help and sorry If my Englis words is poor, It is not my default language.

User avatar
david63
Registered User
Posts: 16718
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: User values in extension

Post by david63 » Sun Jun 16, 2019 8:41 pm

Did you read the note within that event
NOTE: At this point the global session ($user) and permissions ($auth)
* do NOT exist yet. If you need to use the user object
* (f.e. to include language files) or need to check permissions,
* please use the core.user_setup event instead!
That explains why your code will not work (the user object has not been created)

Not sure what it is that you are trying to do but that is not normally a good event to use.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
david63
Registered User
Posts: 16718
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: User values in extension

Post by david63 » Mon Jun 17, 2019 7:28 am

Thinking about this I do not think that the event core.common will actually handle templare variables. If you are wanting something to appear on every page then you will probably be better off using the core.page_header_after event
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

bzozoo
Registered User
Posts: 24
Joined: Mon Feb 11, 2019 6:33 am

Re: User values in extension

Post by bzozoo » Mon Jun 17, 2019 5:57 pm

Hi David63

In my previous question I used the core.common and It works.
But now I added to my code this: $this->user->session_begin(); and now It work :)

This is my function now:

Code: Select all

		public function global_calls($event)
	{
$this->user->session_begin();
		$testvalue = 'TEST VALUE';
		$user_nicknam = $this->user->data['username'];
		$web_proti = $this->config['server_protocol'];
		$web_path = $this->config['server_name'];
		$scrs_path = $this->config['script_path'];
		$total_domain_path = $web_proti .$web_path .$scrs_path;
		// Assign global template vars.
		$this->template->assign_vars(array(
             'ACTUAL_BB_VERSION'	=> $this->config['version'],
			 'TOTAL_USERS_NUMBER'	=> $this->config['num_users'],
			 'CCCT'                 => $this->user->page['page_name'],
			 'CCCT2'                 => $user_nicknam,
			 'CCCT3'                 => $this->user->data['user_id'],
			 'T_WEB_PATH'			=> $total_domain_path,
			 'T_WEB_PROTI'			=> $web_proti,
		'USER_FILTER_1'		    => (substr($user_nicknam, 0, 4) == 'John')  ? true : false,
	    'MEMBER_TEMPLATE_FILTER	    	=> (substr($this->user->page['page_name'], 0, 15) == 'memberlist.php/')  ? true : false,
		'FASTLINKS_TEMPLATE_FILTERS'	    => (substr($this->user->page['page_name'], 0, 22) == 'search.php/unreadposts' or substr($this->user->page['page_name'], 0, 19) == 'search.php/newposts' or substr($this->user->page['page_name'], 0, 21) == 'search.php/unanswered' or substr($this->user->page['page_name'], 0, 24) == 'search.php/active-topics'  or substr($this->user->page['page_name'], 0, 17) == 'search.php/member')  ? true : false,
		'VT_VF_FILTERS'	=> (substr($this->user->page['page_name'], 0, 13) == 'viewtopic.php' or substr($this->user->page['page_name'], 0, 13) == 'viewforum.php')  ? true : false,
			 'TEST_VALUE' => $testvalue
		));
	}


}

So I can now add a lot of filters and I can use it with an IF function in the template. Example:

This code welcomes all John users withouth full nicname. John00001. John0002 ... John0000N......

Code: Select all

<!-- IF USER_FILTER_1 -->
	Hi John!<br />
	<!-- ELSE -->
	Hi {CURRENT_USERNAME_SIMPLE}<br />
	<!-- ENDIF --> 

I need use a variable $user_nicknam = $this->user->data['username']; becouse If I use 'USER_FILTER_1' => (substr($this->user->data['username'], 0, 4) == 'John') ? true : false, then i get an error message.
That's interesting: I can use 'MEMBER_TEMPLATE_FILTER => (substr($this->user->page['page_name'], 0, 15) == 'memberlist.php/') ? true : false, without error mesagge and without variable, here I can use $this->user->page['page_name']

I also added the helper to the code, But I'm not sure this is necessary for this.

Code: Select all

	public function __construct(
		\phpbb\request\request $request,
		\phpbb\user $user,
		\phpbb\auth\auth $auth,
		\phpbb\db\driver\factory $db,
		\phpbb\config\config $config,
		\phpbb\controller\helper $helper,
		\phpbb\template\template $template,
		$root_path,
		$phpEx,
		\phpbb\language\language $language)
	{
		$this->request = $request;
		$this->user			= $user;
		$this->auth         = $auth;
		$this->db = $db;
		$this->config		= $config;
		$this->helper = $helper;
		$this->template		= $template;
		$this->root_path	= $root_path;
		$this->phpEx		= $phpEx;
		$this->language		= $language;
	}
And in to the Services.yml I added these arguments:

Code: Select all

        arguments:
            - '@request'
            - '@user'
            - '@auth'
            - '@dbal.conn'
            - '@config'
            - '@controller.helper'
            - '@template'
            - '%core.root_path%'
            - '%core.php_ext%'
            - '@language'
I don't really understand how they work, it's my first extension, just for my own use. I'm sure I'll expand the code and there will be new problems. :lol:
Thank you for your help and your time for my problem :)

Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 25466
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: User values in extension

Post by Paul » Mon Jun 17, 2019 6:13 pm

You really should not call session_begin within your extension, instead you should use a different event instead.
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

bzozoo
Registered User
Posts: 24
Joined: Mon Feb 11, 2019 6:33 am

Re: User values in extension

Post by bzozoo » Mon Jun 17, 2019 6:36 pm

Paul wrote:
Mon Jun 17, 2019 6:13 pm
use a different event instead.
You mean core.page_header_after?

Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 25466
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: User values in extension

Post by Paul » Mon Jun 17, 2019 7:38 pm

Well, that depends on what you want to do exactly.
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

bzozoo
Registered User
Posts: 24
Joined: Mon Feb 11, 2019 6:33 am

Re: User values in extension

Post by bzozoo » Tue Jun 18, 2019 8:41 pm

I want use these template vars in this extension , and and later expand more vars.

User avatar
david63
Registered User
Posts: 16718
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: User values in extension

Post by david63 » Tue Jun 18, 2019 8:54 pm

bzozoo wrote:
Tue Jun 18, 2019 8:41 pm
I want use these template vars in this extension
But where do you want to use them?

You need to find the most appropriate php event where you can load those template vars in conjunction with the template event that will display them.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

bzozoo
Registered User
Posts: 24
Joined: Mon Feb 11, 2019 6:33 am

Re: User values in extension

Post by bzozoo » Tue Jun 18, 2019 9:48 pm

I want use on all pages (index, viewtopic, viewforum) or filter templates on a filtered page, example memberlist.php ('MEMBERPAGE_FILTER' => (substr($this->user->page['page_name'], 0, 15) == 'memberlist.php/') ? true : false,) etc... With this, I can display different contents on different subpages
Now I use core.page_header_after and now It works without $this->user->session_begin(); and It works on all pages. :)

Post Reply

Return to “Extension Writers Discussion”