Problem with topic title and UTF-8 characters in notifications [SOLVED]

Discussion forum for Extension Writers regarding Extension Development.
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Problem with topic title and UTF-8 characters in notifications [SOLVED]

Post by caiocald »

Is it something with that part?

Code: Select all

    * Get the HTML formatted reference of the notification
     *
     * @return string
     */
    public function get_reference()
    {
        return censor_text($this->get_data('post_subject'));
    }
Last edited by caiocald on Sat Nov 07, 2020 10:32 am, edited 1 time in total.
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 27084
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Problem with topic title and UTF-8 characters in notifications

Post by Paul »

You will need to provide some more info, like for example what the actual problem is, and preferably some more code.
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Problem with topic title and UTF-8 characters in notifications

Post by caiocald »

Accented characters are in error in notification messages.

I am unable to make the topic title appear normally in website and email notifications.

Example: the "Ç" of "REDAÇÃO" is left with "????"
fdfdfdfdf.PNG
fdfdfdfdf.PNG (5.39 KiB) Viewed 792 times
User avatar
ViolaF
Registered User
Posts: 1546
Joined: Tue Aug 14, 2012 11:52 pm

Re: Problem with topic title and UTF-8 characters in notifications

Post by ViolaF »

Try:

Code: Select all

    * Get the HTML formatted reference of the notification
     *
     * @return string
     */
    public function get_reference()
    {
        return censor_text(utf8_normalize_nfc($this->get_data('post_subject')));
    }
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Problem with topic title and UTF-8 characters in notifications

Post by caiocald »

ViolaF wrote:
Thu Nov 05, 2020 8:58 pm
Try:

Code: Select all

    * Get the HTML formatted reference of the notification
     *
     * @return string
     */
    public function get_reference()
    {
        return censor_text(utf8_normalize_nfc($this->get_data('post_subject')));
    }
thanks for the help, but it didn't work
rxu
Extensions Development Team
Posts: 3453
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Problem with topic title and UTF-8 characters in notifications

Post by rxu »

What is the collation of notification_data field in phpbb_notifications table (phpbb_ prefix may vary) in the database?
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Problem with topic title and UTF-8 characters in notifications

Post by caiocald »

rxu wrote:
Fri Nov 06, 2020 2:31 am
What is the collation of notification_data field in phpbb_notifications table (phpbb_ prefix may vary) in the database?

Code: Select all

a:4:{s:12:"requester_id";i:36823;s:7:"user_id";i:28027;s:8:"topic_id";i:23473;s:12:"post_subject";s:45:"Desafios para a pr??tica da leitura no Brasil";}
It is?
As I understand it, the character error is happening before it reaches the database.
rxu
Extensions Development Team
Posts: 3453
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Problem with topic title and UTF-8 characters in notifications

Post by rxu »

The question was about database field collation. Example:
collation.png
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Problem with topic title and UTF-8 characters in notifications

Post by caiocald »

fdfsdfsdgg.PNG
rxu
Extensions Development Team
Posts: 3453
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Problem with topic title and UTF-8 characters in notifications

Post by rxu »

Looks correct. What is the actual extension code related to that?
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Problem with topic title and UTF-8 characters in notifications

Post by caiocald »

this is the notification file

Code: Select all

<?php
    public function get_type()
    {
        return 'notification.type.pontuacao';
    }

    /**
     * Notification option data (for outputting to the user)
     *
     * @var bool|array False if the service should use it's default data
     * 					Array of data (including keys 'id', 'lang', and 'group')
     */
    public static $notification_option = array(
        'lang'	=> 'NOTIFICACAO_PAINEL_TITULO',
    );

    /** @var \phpbb\user_loader */
    protected $user_loader;

    /** @var \phpbb\config\config */
    protected $config;

    public function set_config(\phpbb\config\config $config)
    {
        $this->config = $config;
    }

    public function set_user_loader(\phpbb\user_loader $user_loader)
    {
        $this->user_loader = $user_loader;
    }

    /**
     * Is this type available to the current user (defines whether or not it will be shown in the UCP Edit notification options)
     *
     * @return bool True/False whether or not this is available to the user
     */
    public function is_available()
    {
        return true;
    }

    /**
     * Get the id of the liker
     *
     * @param array $data The data for the like
     * @return int
     */
    public static function get_item_id($data)
    {
        return $data['topic_id'];
    }

    /**
     * Get the id of the parent
     *
     * @param array $data The data for the like
     * @return int
     */
    public static function get_item_parent_id($data)
    {
        return isset($data['topic_id']) ? $data['topic_id'] : 0;
    }

    /**
     * Find the users who will receive notifications
     *
     * @param array $data The data for the like
     *
     * @return array
     */
    public function find_users_for_notification($data, $options = array())
    {
        $options = array_merge(array(
            'ignore_users'			=> array(),
        ), $options);
        $users = array(
            $data['user_id']	=> 0,
        );
        $this->user_loader->load_users(array_keys($users));

        return $this->check_user_notification_options(array_keys($users), $options);
    }

    /**
     * Get the user's avatar
     */
    public function get_avatar()
    {
        return $this->user_loader->get_avatar($this->get_data('requester_id'), false, true);
    }

    /**
     * Get the HTML formatted title of this notification
     *
     * @return string
     */
    public function get_title()
    {
        $username = $this->user_loader->get_username($this->get_data('requester_id'), 'no_profile');
        return $this->language->lang('NOTIFICACAO_PONTUACAO', $username);
    }

    /**
     * Get the HTML formatted reference of the notification
     *
     * @return string
     */
    public function get_reference()
    {
        return censor_text($this->get_data('post_subject'));
    }

    /**
     * Get email template
     *
     * @return string|bool
     */
    public function get_email_template()
    {
        return '@red1000_pontuacao/pontuacao_mail';
    }

    /**
     * Get email template variables
     *
     * @return array
     */
    public function get_email_template_variables()
    {
        return [
            'PONTUADOR_NAME'        => $this->user_loader->get_username($this->get_data('requester_id'), 'username'),
            'AUTOR_NAME'            => $this->user_loader->get_username($this->get_data('user_id'), 'username'),
            'TOPIC_TITLE'		    => $this->get_data('post_subject'),
            'U_LINK_TO_TOPIC'       => generate_board_url() . '/viewtopic.' . $this->php_ext . '?t=' . $this->get_data('topic_id'),
            'U_LINK_NOTIFICACOES'   => generate_board_url() . '/ucp.' . $this->php_ext . '?i=ucp_notifications&mode=notification_options'
        ];
    }

    /**
     * Get the url to this item
     *
     * @return string URL
     */
    public function get_url()
    {
        return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "t=". $this->get_data('topic_id'));
    }

    /**
     * Users needed to query before this notification can be displayed
     *
     * @return array Array of user_ids
     */
    public function users_to_query()
    {
        return array($this->get_data('requester_id'));
    }

    /**
     * Function for preparing the data for insertion in an SQL query
     * (The service handles insertion)
     *
     * @param array $data The data for the updated rules
     * @param array $pre_create_data Data from pre_create_insert_array()
     *
     * @return array Array of data ready to be inserted into the database
     */
    public function create_insert_array($data, $pre_create_data = array())
    {
        $this->set_data('requester_id', $data['requester_id']);
        $this->set_data('user_id', $data['user_id']);
        $this->set_data('topic_id', $data['topic_id']);
        $this->set_data('post_subject', $data['post_subject']);

        parent::create_insert_array($data, $pre_create_data);
    }
}

this is the notification controller

Code: Select all

class mainnotificacao
{
	/**
	* Constructor
	*
	* @param \phpbb\config\config $config                      Config object
	* @param \phpbb\db\driver\driver $db                       Database object
	* @param \phpbb\request\request $request                   Request object
	* @param \phpbb\template\template $template                Template object
	* @param \phpbb\user $user                                 User object
	* @param Container $phpbb_container
	* @param string $root_path                                 phpBB root path
	* @param string $php_ext                                   phpEx
	* @access public
	*/
	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, Container $phpbb_container, $root_path, $php_ext)
	{
		$this->config = $config;
		$this->db = $db;
		$this->request = $request;
		$this->template = $template;
		$this->user = $user;
		$this->phpbb_container = $phpbb_container;
		$this->root_path = $root_path;
		$this->php_ext = $php_ext;
	}

	/**
	* Main notification function
	* @param type			Type of notification (add/confirm)
	* @param post_id		Post ID
	* @param poster_user	User to notify
	* @param liker_user	User that trigered the action
	*/
	public function notify($type, $topic_id, $post_subject, $author_user_id, $pontuador_user)
	{
		$notification_data = array(
			'topic_id'	=> (int) $topic_id,
			'post_subject'	=>	$post_subject,
			'user_id'	=> (int) $author_user_id,
			'requester_id'	=> (int) $pontuador_user,
		);

		//$this->test($notification_data);
		$phpbb_notifications = $this->phpbb_container->get('notification_manager');
		if ($notification_data['requester_id'] != $notification_data['user_id'])
		{
			switch ($type)
			{
				case 'add':
					$phpbb_notifications->add_notifications('notification.type.pontuacao', $notification_data);
				break;
				case 'remove':
					$notifications = $phpbb_notifications->get_item_type_class('notification.type.pontuacao');
					$phpbb_notifications->delete_notifications('notification.type.pontuacao', $notifications->get_item_id($notification_data));
				break;
			}
		}
	}
}
rxu
Extensions Development Team
Posts: 3453
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Problem with topic title and UTF-8 characters in notifications

Post by rxu »

Okay, what's the codepublic function notify($type, $topic_id, $post_subject, $author_user_id, $pontuador_user) is being called from / its arguments are coming from? Especially the code where $post_subject is initialized.
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Problem with topic title and UTF-8 characters in notifications

Post by caiocald »

Code: Select all

$forum_subject = $this->request->variable('subject', 'Tópico');
Last edited by caiocald on Sat Nov 07, 2020 10:34 am, edited 1 time in total.
rxu
Extensions Development Team
Posts: 3453
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Problem with topic title and UTF-8 characters in notifications

Post by rxu »

Try adding 3rd argument of true.

Code: Select all

$forum_subject = $this->request->variable('subject', 'Tópico', true);
User avatar
caiocald
Registered User
Posts: 161
Joined: Mon Feb 26, 2018 9:32 pm
Name: B!

Re: Problem with topic title and UTF-8 characters in notifications

Post by caiocald »

It worked!!!
Can you explain the function of "true" to me?
I'm learning programming
Post Reply

Return to “Extension Writers Discussion”