Externally Insert Notifications

Discussion forum for Extension Writers regarding Extension Development.
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Externally Insert Notifications

Post by Geed »

Hello all,

I am currently working on a project which is using phpBB 3.1 mainly for the user registration/login purposes (and the forum :D). There is a pretty major application type thing that is basically separate from phpBB (as in not an extension). However, I'd like to utilize the new notification feature. How can I externally insert a notification into the phpBB notifications database table? I'm having trouble understanding what exactly goes into which field when I insert into the table.

Here is my understanding of each of the fields:

notification_id => clearly just the primary key
notification_type_id => from the phpbb_notification_types table
item_id => ?
item_parent_id => ?
user_id => the id of the recipient?
notification_read => 0 or 1 unread or read
notification_time => a timestamp?
notification_data => I can kind of understand this field but not 100% either...

Is there some sort of method that I can use to insert a notification without using it within an extension that would make this process easier?
DG Kim | Princeton '18
Electrical Engineering
User avatar
John P
Registered User
Posts: 1237
Joined: Mon Jan 21, 2008 3:55 pm
Location: Netherlands
Name: John
Contact:

Re: Externally Insert Notifications

Post by John P »

Look at the files in phpbb/notification/type/

item_id is a following number, if the number already exists no new noti is saved.
item_parent_id for hierarchy of noti
notification_data serialized data for noti

That's is how I understand the fields.
Image
Webhosting, Custom MODs, Technical management, MOD installation and Webdesign
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Re: Externally Insert Notifications

Post by Geed »

Thank you for that info...

Now would it be easier(/better) to create my own notification class or to try to simply insert rows into the notification table?

I'm feeling like creating my own notification class would be better, but I'm not sure how to use the class once it's created (sorry if I'm asking an extremely easy question :oops:). Could someone give me a quick example of how notifications are used in general, once a class is written?
DG Kim | Princeton '18
Electrical Engineering
User avatar
nickvergessen
Former Team Member
Posts: 4397
Joined: Mon Apr 30, 2007 5:33 pm
Location: Stuttgart, Germany
Name: Joas Schilling
Contact:

Re: Externally Insert Notifications

Post by nickvergessen »

item_id is e.g. the post id
in this case item_parent_id would be the topic_id
No Support via PM
User avatar
John P
Registered User
Posts: 1237
Joined: Mon Jan 21, 2008 3:55 pm
Location: Netherlands
Name: John
Contact:

Re: Externally Insert Notifications

Post by John P »

Example:
Used in ucp as a member starts a new advertisement campaign

Code: Select all

$phpbb_notifications = $phpbb_container->get('notification_manager');
$phpbb_notifications->add_notifications('campaign', array(
    'log_id'        => $db->sql_nextid(),
    'log_type'        => 2,
    'log_site_id'    => $sit,
    'log_start'        => $startday,
    'log_end'        => $endday
));
Image
Webhosting, Custom MODs, Technical management, MOD installation and Webdesign
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Re: Externally Insert Notifications

Post by Geed »

Thanks for the answers... now for example in the post notification thing I see the following code:

Code: Select all

    public static function get_item_id($post)
    {
        return (int) $post['post_id'];
    }
How do I pass the $post variable to the notification class?
DG Kim | Princeton '18
Electrical Engineering
User avatar
tbackoff
Former Team Member
Posts: 7068
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Externally Insert Notifications

Post by tbackoff »

You can make $post whatever you want. Have a look at this example function in a controller and this example function in a notification file. In these, I used $notification_data as the array.
Flying is the second best thrill to cheerleaders; being caught is the first.
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Re: Externally Insert Notifications

Post by Geed »

Thank you for the response!

I still have one more question though...

How can I use $this->get_data('example'); and how does it differ from using the above? Do they have completely different purposes?

For example:

Code: Select all

    public function get_avatar()
    {
        return $this->user_loader->get_avatar($this->get_data('poster_id'));
    }
EDIT:

Here is what I came up with, but for some reason no notifications are made?

admin.php

Code: Select all

$phpbb_notifications = $phpbb_container->get('notification_manager');

$mc_id = $submission_data['submission_id'];
$user_id = $submission_data['user_id'];
               
// all of these fields work
$notification_data = array(
    'badge_subject'            => 'Your recent FRQ has been graded.',
    'graded_user_id'           => $user->data['user_id'],
    'mc_submission_id'       => $mc_id,
    'user_id'                       => $user_id                
);
                
if($earn == true){
    $phpbb_notifications->add_notifications('badge_earn', $notification_data);
}
forum/phpbb/notification/type/badge_earn.php

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 dgkim\calcmerit\notification;

/**
* Merit badge notifications class
* This class handles notifications for merit badges
*/

class badge_earn extends \phpbb\notification\type\base
{
    /**
    * Get notification type name
    *
    * @return string
    */
    public function get_type()
    {
        return 'badge_earn';
    }

    protected $language_key = 'NOTIFICATION_BADGE_EARN';

    /**
    * 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(
        'id'    => 'badge_earn',
        'lang'    => 'NOTIFICATION_TYPE_BADGE_EARN',
        'group' => 'NOTIFICATION_GROUP_MISCELLANEOUS'
    );

    /**
    * Is available
    */
    public function is_available()
    {
        return true;
    }

    /**
    * Get the id of the item
    *
    * @param array $submission The data from the submission
    */
    public static function get_item_id($submission)
    {
        return (int) $submission['mc_submission_id'];
    }

    /**
    * Get the id of the parent
    *
    * @param array $submission The data from the submission
    */
    public static function get_item_parent_id($submission)
    {
        // No parent
        return 0;
    }

    /**
    * Find the users who want to receive notifications
    *
    * @param array $submission The data from the submission
    *
    * @return array
    */
    public function find_users_for_notification($submission, $options = array())
    {
        $user_array = array(
            $submission['user_id'] => array('')
        );
                
        return $user_array;
    }

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

    /**
    * Get the HTML formatted title of this notification
    *
    * @return string
    */
    public function get_title()
    {
        return $this->user->lang('NOTIFICATION_BADGE_EARN');
    }

    /**
    * Get the HTML formatted reference of the notification
    *
    * @return string
    */
    public function get_reference()
    {
        return $this->user->lang(
            'NOTIFICATION_REFERENCE',
            $this->get_data('badge_subject')
        );
    }

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

    /**
    * Get email template variables
    *
    * @return array
    */
    public function get_email_template_variables()
    {
        return array();
    }

    /**
    * Get the url to this item
    *
    * @return string URL
    */
    public function get_url()
    {
        return append_sid('../merit.' . $this->php_ext, "?&mode=progress&p={$this->item_id}");
    }

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

    /**
    * Function for preparing the data for insertion in an SQL query
    * (The service handles insertion)
    *
    * @param array $submission Data from grading
    * @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($submission, $pre_create_data = array())
    {
        $this->set_data('badge_subject', $submission['badge_subject']);
        
        $this->set_data('graded_user_id', $submission['graded_user_id']);
        
        $this->set_data('mc_submission_id', $submission['mc_submission_id']);
        
        $this->set_data('user_id', $submission['user_id']);

        return parent::create_insert_array($submission, $pre_create_data);
        
        $this->notification_time = $submission['notification_time'] = time();
        
        return $submission;
    }
}
 
Last edited by Geed on Thu Jul 17, 2014 9:02 pm, edited 1 time in total.
DG Kim | Princeton '18
Electrical Engineering
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Re: Externally Insert Notifications

Post by Geed »

Any help would be appreciated!
DG Kim | Princeton '18
Electrical Engineering
User avatar
tbackoff
Former Team Member
Posts: 7068
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Externally Insert Notifications

Post by tbackoff »

:oops: Completely forgot about this. Can you wait until tonight and I'll see if I can work something up? Not in front of my development server at the moment.
Flying is the second best thrill to cheerleaders; being caught is the first.
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Re: Externally Insert Notifications

Post by Geed »

tmbackoff - Of course! Any and all help is appreciated, and you're taking your own time out anyways. 8-)
DG Kim | Princeton '18
Electrical Engineering
User avatar
tbackoff
Former Team Member
Posts: 7068
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: Externally Insert Notifications

Post by tbackoff »

Code: Select all

namespace dgkim\calcmerit\notification; 
This is wrong. Since you are placing this in the actual folder phpBB uses from notifications, your namespace should look like this:

Code: Select all

namespace phpbb\notification\type; 
------------------------------------------------------------------------------------------------------------------------------

Code: Select all

public static function get_item_id($submission)
{
    return (int) $submission['mc_submission_id'];
}
Replace each instance of $submission with $notification_data.

------------------------------------------------------------------------------------------------------------------------------

Code: Select all

public static function get_item_parent_id($submission)
{
    // No parent
    return 0;
}
Same here...

------------------------------------------------------------------------------------------------------------------------------

Code: Select all

public function find_users_for_notification($submission, $options = array())
{
    $user_array = array(
        $submission['user_id'] => array('')
    );

    return $user_array;
}
... and here...

------------------------------------------------------------------------------------------------------------------------------

Code: Select all

public function create_insert_array($submission, $pre_create_data = array())
{
    $this->set_data('badge_subject', $submission['badge_subject']);

    $this->set_data('graded_user_id', $submission['graded_user_id']);

    $this->set_data('mc_submission_id', $submission['mc_submission_id']);

    $this->set_data('user_id', $submission['user_id']);

    return parent::create_insert_array($submission, $pre_create_data);

    $this->notification_time = $submission['notification_time'] = time();

    return $submission;
} 
... and here. Also, change return parent::create_insert_array($submission, $pre_create_data); to $notification_data = parent::create_insert_array($notification_data, $pre_create_data);.

Obviously, this is me just looking at it and not actually testing, so no guarantees this will work.
Flying is the second best thrill to cheerleaders; being caught is the first.
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Re: Externally Insert Notifications

Post by Geed »

Thank you for that, but it still doesn't work...

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 phpbb\notification\type;

/**
* Merit badge notifications class
* This class handles notifications for merit badges
*/

class badge_earn extends \phpbb\notification\type\base
{
    /**
    * Get notification type name
    *
    * @return string
    */
    public function get_type()
    {
        return 'badge_earn';
    }

    protected $language_key = 'NOTIFICATION_BADGE_EARN';

    /**
    * 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(
        'id'    => 'badge_earn',
        'lang'    => 'NOTIFICATION_TYPE_BADGE_EARN',
        'group' => 'NOTIFICATION_GROUP_MISCELLANEOUS'
    );

    /**
    * Is available
    */
    public function is_available()
    {
        return true;
    }

    /**
    * Get the id of the item
    *
    * @param array $notification_data The data from the submission
    */
    public static function get_item_id($notification_data)
    {
        return (int) $notification_data['mc_submission_id'];
    }

    /**
    * Get the id of the parent
    *
    * @param array $notification_data The data from the submission
    */
    public static function get_item_parent_id($notification_data)
    {
        // No parent
        return 0;
    }

    /**
    * Find the users who want to receive notifications
    *
    * @param array $notification_data The data from the submission
    *
    * @return array
    */
    public function find_users_for_notification($notification_data, $options = array())
    {
        $user_array = array(
            $notification_data['user_id'] => array('')
        );
                
        return $user_array;
    }

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

    /**
    * Get the HTML formatted title of this notification
    *
    * @return string
    */
    public function get_title()
    {
        return $this->user->lang('NOTIFICATION_BADGE_EARN');
    }

    /**
    * Get the HTML formatted reference of the notification
    *
    * @return string
    */
    public function get_reference()
    {
        return $this->user->lang(
            'NOTIFICATION_REFERENCE',
            $this->get_data('badge_subject')
        );
    }

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

    /**
    * Get email template variables
    *
    * @return array
    */
    public function get_email_template_variables()
    {
        return array();
    }

    /**
    * Get the url to this item
    *
    * @return string URL
    */
    public function get_url()
    {
        return append_sid('../merit.' . $this->php_ext, "?&mode=progress&id={$this->item_id}");
    }

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

    /**
    * Function for preparing the data for insertion in an SQL query
    * (The service handles insertion)
    *
    * @param array $notification_data Data from grading
    * @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($notification_data, $pre_create_data = array())
    {
        $this->set_data('badge_subject', $notification_data['badge_subject']);
        
        $this->set_data('graded_user_id', $notification_data['graded_user_id']);
        
        $this->set_data('mc_submission_id', $notification_data['mc_submission_id']);
        
        $this->set_data('user_id', $notification_data['user_id']);
        
        $this->notification_time = $notification_data['notification_time'] = time();

        return parent::create_insert_array($notification_data, $pre_create_data);
    }
}
DG Kim | Princeton '18
Electrical Engineering
User avatar
John P
Registered User
Posts: 1237
Joined: Mon Jan 21, 2008 3:55 pm
Location: Netherlands
Name: John
Contact:

Re: Externally Insert Notifications

Post by John P »

Did you register the service in notifications.yml in config folder?
Image
Webhosting, Custom MODs, Technical management, MOD installation and Webdesign
Geed
Registered User
Posts: 197
Joined: Sat Sep 20, 2008 1:02 am
Location: Princeton, NJ
Name: DG Kim
Contact:

Re: Externally Insert Notifications

Post by Geed »

Thank you for that... but unfortunately I still didn't get it to work. Here's what I've added to notifications.yml

Code: Select all

    notification.type.badge_earn:
        class: phpbb\notification\type\badge_earn
        scope: prototype # scope MUST be prototype for this to work!
        arguments:
            - @user_loader
            - @dbal.conn
            - @cache.driver
            - @user
            - @auth
            - @config
            - %core.root_path%
            - %core.php_ext%
            - %tables.notification_types%
            - %tables.notifications%
            - %tables.user_notifications%
        tags:
            - { name: notification.type }	
DG Kim | Princeton '18
Electrical Engineering
Post Reply

Return to “Extension Writers Discussion”