Fill user_row object

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Pixma
Registered User
Posts: 5
Joined: Sat Jul 13, 2019 12:47 am

Fill user_row object

Post by Pixma » Sat Jul 13, 2019 12:56 am

Hello,

I want to access the structure of the people who ware online.
I found that all users are saved in the Variable "user_row".
Now I tried to put these users who are online in an array.

main_listener.php

Code: Select all

public function load_language_on_setup($event)
{
	...
	$template_row = array();
	$this->template->retrieve_block_vars('user_row', $template_row);
	echo sizeof ($template_row);
	...
}
My problem is that the array has a size of 0, but I am logged in with my administrator account.
So it should print 1.
I assume that I have to use a command to tell phpbb to retrieve all users which are actually online.
But I don't know which command I have to use?

Thanks for your help.

Pixma :)

User avatar
3Di
Former Team Member
Posts: 13769
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Fill user_row object

Post by 3Di » Sat Jul 13, 2019 3:54 pm

Explain please what you wanto to do.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity ΒΊ PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
πŸ‘¨β€πŸ« | Take a tour to | The Studio | πŸ‘¨β€πŸ«

Pixma
Registered User
Posts: 5
Joined: Sat Jul 13, 2019 12:47 am

Re: Fill user_row object

Post by Pixma » Sat Jul 13, 2019 4:23 pm

hi,
I want to customize the who is online list (viewonline_body.html).
The user shall see next to the username the avatar of the user, because actually it is only shown the username, current activity and last update.
I know this functionality is done in the file viewonline.php.
But in this file, the avatarname is not requested from the database.
So I want to fill the $template_row with the 'user_row', like in the previous post.
Then I want to filter the user table for the avatarname and use it to find the correct avatar, which shall be printed for each user.

User avatar
3Di
Former Team Member
Posts: 13769
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Fill user_row object

Post by 3Di » Sat Jul 13, 2019 7:28 pm

So you want to do something like this?

Image

You should do it in the PHP side of things, where the online list is generated

The event: core.obtain_users_online_string_modify

The function in my case is this one:
https://github.com/3D-I/IP-Country-Flag ... r.php#L258

I am using flags there, you should call the avatar instead.
Hope this helps.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity ΒΊ PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
πŸ‘¨β€πŸ« | Take a tour to | The Studio | πŸ‘¨β€πŸ«

Pixma
Registered User
Posts: 5
Joined: Sat Jul 13, 2019 12:47 am

Re: Fill user_row object

Post by Pixma » Sun Jul 14, 2019 4:06 am

I don't know If we both mean the same.
I want this what you show in the Menu Who is online, when you double click on the link in your picture.
In my picture, 1 shall be the position of the avatar.
2. are then the username, Forum Location and Last updated.
forumThread.png
ok, I will try this event.
So is this corrent, that I create an event handler in the getSubscribedEvents?:

Code: Select all

static public function getSubscribedEvents()
	{
		return array(
			'core.user_setup'	=> 'load_language_on_setup',
			'core.page_header'	=> 'add_page_header_link',
			'core.obtain_users_online_string_modify' => 'myNewEventHandler',
		);
	}

Code: Select all

public function myNewEventHandler($event)
{
	....
}

User avatar
3Di
Former Team Member
Posts: 13769
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Fill user_row object

Post by 3Di » Sun Jul 14, 2019 8:30 am

Pixma wrote: ↑
Sun Jul 14, 2019 4:06 am
I don't know If we both mean the same.
Re-reading your post, the correct event to use is
core.viewonline_modify_sql to add the avatar fileds to the $sql_ary array
and this one core.viewonline_modify_user_row to retrieve those added fields and add to the $template_row array.

Finally, as of 3.2.4-RC1 there are also template events in viewonline_body.html
<!-- EVENT viewonline_body_username_prepend -->
and
<!-- EVENT viewonline_body_username_append -->
where you will add the user_row's new template vars you above created.
Pixma wrote: ↑
Sun Jul 14, 2019 4:06 am
So is this corrent, that I create an event handler in the getSubscribedEvents?:
Yes.

Please have a read to the documentation about writing extension
https://area51.phpbb.com/docs/dev/3.2.x ... index.html
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity ΒΊ PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
πŸ‘¨β€πŸ« | Take a tour to | The Studio | πŸ‘¨β€πŸ«

Pixma
Registered User
Posts: 5
Joined: Sat Jul 13, 2019 12:47 am

Re: Fill user_row object

Post by Pixma » Sun Jul 14, 2019 6:50 pm

Thank you :)
Now it works, almost.
When I am logged in with two user accounts, both are showen with the same avatar.
But both have different avatars. Could the reason be that I have a template variable 'AVATAR' and not 'user_row.AVATAR'?
I tried to get it in the form of 'user_row.AVATAR', but I don't got a runnable solution.

Here my new code:
main_listener.php

Code: Select all

static public function getSubscribedEvents()
{
	return array(
		'core.viewonline_modify_sql' => 'modify_view_only_sql',
		'core.viewonline_modify_user_row' => 'modify_view_online_user_row_dataset'
	);
}


public function modify_view_only_sql($event)
{
	// append sql_ary with avatar data
	$sql_ary = $event['sql_ary'];
	$sql_ary['SELECT'] .= ', u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height';
     	$event['sql_ary'] = $sql_ary;
	  
	// set S_IN_USERONLINELIST
	$this->template->assign_var('S_IN_USERONLINELIST', true);
}
	
public function modify_view_online_user_row_dataset($event)
{
	//echo var_dump($event["template"]);
	$template_row = (array)$event['template_row']; // template row				
	$forum_data = (array)$event['row']; // sql_ary dataset
	$avatar = get_user_avatar($forum_data['user_avatar'], $forum_data['user_avatar_type'], $forum_data['user_avatar_width'], 	$forum_data['user_avatar_height']);
	$this->template->assign_var('AVATAR', $avatar);
}
viewonline_body_username_prepend.html

Code: Select all

<!-- IF AVATAR -->
	<div class="user_avatar">{AVATAR}</div>
<!-- ELSE -->
	<div class="user_avatar "><img src="{T_THEME_PATH}/images/no_avatar.gif" alt="Avatar" /></div>	
<!-- ENDIF -->

Pixma
Registered User
Posts: 5
Joined: Sat Jul 13, 2019 12:47 am

Re: Fill user_row object

Post by Pixma » Mon Jul 15, 2019 12:53 am

Now it runs.
I have fixed the bug.
The problem was the template variable "AVATAR".
I found the way to create a template Variable "user_row.AVATAR". :)
This is my solution:

Code: Select all

public function modify_view_online_user_row_dataset($event)
{
	$temp = $event["template_row"];
				
	$forum_data = (array)$event['row']; // sql_ary dataset
	$avatar = get_user_avatar($forum_data['user_avatar'], $forum_data['user_avatar_type'], $forum_data['user_avatar_width'], $forum_data['user_avatar_height']);
	if($forum_data['user_avatar'] == null)
	{
		$temp["AVATAR"] = null;
	}
	else
	{
		$temp["AVATAR"] = $avatar;
	}
	
	$event["template_row"] = $temp;
}
and

Code: Select all

<!-- IF user_row.AVATAR -->
	<div class="user_avatar">{user_row.AVATAR}</div>
<!-- ELSE -->
	<div class="user_avatar "><img src="{T_THEME_PATH}/images/no_avatar.gif" alt="Avatar" /></div>	
<!-- ENDIF -->

User avatar
3Di
Former Team Member
Posts: 13769
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Fill user_row object

Post by 3Di » Mon Jul 15, 2019 11:46 am

I'd have done like that (.. on the fly, not tested)

Listener

Code: Select all

static public function getSubscribedEvents()
{
	return array(
		'core.viewonline_modify_sql' => 'modify_view_only_sql',
		'core.viewonline_modify_user_row' => 'modify_view_online_user_row_dataset'
	);
}

public function modify_view_only_sql($event)
{
	$sql_ary = $event['sql_ary'];
	$sql_ary['SELECT'] .= ', u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height';
	$event['sql_ary'] = $sql_ary;
}

public function modify_view_online_user_row_dataset($event)
{
	$row = $event['row'];

	// set S_IN_USERONLINELIST
	$this->template->assign_var('S_IN_USERONLINELIST', true);

	/* Map arguments for phpbb_get_avatar() */
	$row_avatar = [
		'avatar'		=> $row['user_avatar'],
		'avatar_type'	=> $row['user_avatar_type'],
		'avatar_height'	=> $row['user_avatar_height'],
		'avatar_width'	=> $row['user_avatar_width'],
	];

	$no_avatar = '<img src="' . generate_board_url() . '/images/no_avatar.gif" />';
	$avatar = (!empty($row['user_avatar'])) ? phpbb_get_avatar($row_avatar, '') : $no_avatar;

	$event['template_row'] = array_merge($event['template_row'], ['AVATAR'	=>	$avatar]);
}
Template event

Code: Select all

{% if S_IN_USERONLINELIST %}
	<div class="user_avatar">{{ user_row.AVATAR }}</div>
{% endif %}
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity ΒΊ PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
πŸ‘¨β€πŸ« | Take a tour to | The Studio | πŸ‘¨β€πŸ«

Post Reply

Return to β€œExtension Writers Discussion”