Modifying notification_data doesn't work if I don't exit();

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
Toxyy
Registered User
Posts: 594
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Modifying notification_data doesn't work if I don't exit();

Post by Toxyy » Fri Nov 16, 2018 10:19 am

So I wrote this bit of code to modify usernames in the notification data for toggling anonymous posts when posts are updated. It works fine..... if I include that exit() at the end. It seems that somehow it's being affected by the update post sql query, but there aren't any suitable events to modify notification data, and I didn't expect it to be updated/canceled out. It's pretty late and it would take me a few hours going down the rabbit hole trying to find where notification data is updated. Can anyone point me in the right direction?

Code: Select all

// fetch unserialize notification_data, modify it, reserialize it and update it into the db. supports multiple notifications
public function modify_notification_username($generic_id, $username, $toggle_anonymous, $user_id = NULL)
{
        // fetch and unserialize notification_data for the item_id
        $notification_query = 'SELECT notification_data, user_id
                                FROM ' . NOTIFICATIONS_TABLE . '
                                WHERE item_id = ' . $generic_id . '
                                ORDER BY user_id, notification_time ASC';

        $result = $notification_data_ary = $user_id_ary = array();
        $result = $this->db->sql_query($notification_query);

        while($row = $this->db->sql_fetchrow($result))
        {
                $notification_data_ary[] = unserialize($row['notification_data']);
                $user_id_ary[] = $row['user_id'];
        }

        $num_rows = $result->num_rows;

        $this->db->sql_freeresult($result);
        unset($result);

        // if there's anything more to do
        if($num_rows > 0)
        {
                // change poster_id and post_username according to toggle_anonymous, then serialize the row
                foreach($notification_data_ary as &$row)
                {
                        $row['poster_id'] = $toggle_anonymous ? 1 : $user_id;
                        $row['post_username'] = $username;

                        $row = serialize($row);
                }

                // unset $row as it was referenced
                unset($row);

                // build each notification data case in order
                $individual_update_data = '';
                foreach($user_id_ary as $index => $value)
                        $individual_update_data .= 'WHEN (item_id = ' . $generic_id . ' AND user_id = ' . $value . ') THEN \'' . $notification_data_ary[$index] . '\' ELSE \'\' ';

                // serialize each notification_data for each user_id (usually just 1 user)
                $update_notifications_table = 'UPDATE ' . NOTIFICATIONS_TABLE . '
                                                SET notification_data = CASE ' .
                                                $individual_update_data . '
                                                END';

                $result = array();
                $result = $this->db->sql_query($update_notifications_table);

                unset($result);
                exit();
        }
}
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][RC] Show User Activity

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

Re: Modifying notification_data doesn't work if I don't exit();

Post by rxu » Sat Nov 17, 2018 4:14 am

Where do you put the code to?

User avatar
Toxyy
Registered User
Posts: 594
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Modifying notification_data doesn't work if I don't exit();

Post by Toxyy » Sat Nov 17, 2018 4:47 am

rxu wrote:
Sat Nov 17, 2018 4:14 am
Where do you put the code to?
core.submit_post_modify_sql_data

I handle all my post mode cases here
https://github.com/toxyy/anonymousposts ... r.php#L382

in the version on my computer, I have the function in the OP called after case ['edit', true, false] (toggling on anonymous) and case ['edit', false, true] (toggling off anonymous)

I've also since modified the function in the OP to use transactions instead of trying to stuff all the updating into one query.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][RC] Show User Activity

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

Re: Modifying notification_data doesn't work if I don't exit();

Post by rxu » Sat Nov 17, 2018 6:09 am

This is probably because notifications got sent after the core.submit_post_modify_sql_data got triggered, so the notifiction data is getting overrided.
This is solved in phpBB 3.2.4 where the core.modify_submit_notification_data event was added (by your request ;) ).

User avatar
Toxyy
Registered User
Posts: 594
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Modifying notification_data doesn't work if I don't exit();

Post by Toxyy » Sat Nov 17, 2018 6:26 am

rxu wrote:
Sat Nov 17, 2018 6:09 am
This is probably because notifications got sent after the core.submit_post_modify_sql_data got triggered, so the notifiction data is getting overrided.
This is solved in phpBB 3.2.4 where the core.modify_submit_notification_data event was added (by your request ;) ).
I didn't think that notifications were submitted again during post edits, when I wrote the function I was under the assumption that was only for posting/replying/quoting. I guess I'll mess around with it.

Edit: you were right rxu, notifications are able to be edited with the event I added. Much simpler than the function in the OP.

Thanks!
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][RC] Show User Activity

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 7 guests