Skip to content

Instantly share code, notes, and snippets.

@cash
Created December 1, 2011 12:45
Show Gist options
  • Save cash/1416461 to your computer and use it in GitHub Desktop.
Save cash/1416461 to your computer and use it in GitHub Desktop.
Elgg 1.9 Notifications API
<?php
/**
* Notification event
*/
class ElggNotificationEvent {
/**
* @var string The name of the action/event
*/
protected $event;
/**
* @var int The data type of the notification
*/
protected $data_type;
/**
* @var int The GUID of the user who triggered the event
*/
protected $actor;
/**
* @var int The identifier of the data (GUID for entity, id for relationship or extender)
*/
protected $id;
/**
* Constructor
*/
public function __construct($data_type, $actor, $data_id, $action) {
}
public function getType() {
}
}
<?php
/**
* This class has to figure out who receives notifications based on subscriptions, create the
* notification messages, and then send them out.
*/
class ElggNotificationManager {
/**
* Send notifications until stop time is reached
*
* @param int $stopTime The Unix time to stop sending notifications
*/
public function run($stopTime) {
// grab mutex
while (time() < $stopTime) {
// dequeue notification event
// determine who gets notifications
// send notifications
}
// release mutex
}
protected function getUsers() {
}
}
<?php
/**
* FIFO queue for notifications
*/
class ElggNotificationQueue {
/**
* Add a notification event to the queue
*
* @param ElggNotificationEvent $event
* @return bool
*/
public function enqueue($event) {
}
/**
* Remove a notification event from the queue
*
* @return ElggNotificationEvent
*/
public function dequeue() {
}
}
<?php
/**
* Register a notification event
*
* Elgg sends notifications for the items that have been registered with this function. For example, if you want
* notifications to be sent when a bookmark has been created, call the function like this:
* elgg_register_notify_event(ENTITY, 'object', 'bookmarks', array('create'));
* If you want notifications sent when a friend relationship is created:
* elgg_register_notify_event(RELATIONSHIP, 'friend');
*
* @param int $data_type ENTITY, ANNOTATION, RELATIONSHIP, or METADATA
* @param string $type The type of the entity, annotation, relationship, or metadata
* @param string $subtype The subtype of the entity or null for all subtypes. Null for non-entity data types.
* @param array $events Array of events or empty array for the create event. An event is described
* by the first string passed to elgg_trigger_event(). Examples include
* 'create', 'update', and 'publish'.
* @return bool
*/
function elgg_register_notify_event($data_type, $type, $subtype = null, array $events = array()) {
}
/**
* Unregister a notification event
*
* @param int $data_type ENTITY, ANNOTATION, RELATIONSHIP, or METADATA
* @param string $type The type of the entity, annotation, relationship, or metadata
* @param string $subtype The subtype of the entity or null for all subtypes. Null for non-entity data types.
* @return bool
*/
function elgg_unregister_notify_event($data_type, $type, $subtype = null) {
}
/**
* Queue a notification event for later handling
*
* This function checks to see if this event has been registered for notifications. If so, it adds
* the event to a notification queue.
*
* This function triggers the 'queue', 'notification' hook.
*
* @param string $event The name of the event
* @param string $object The object of the event
* @return void
* @private
*/
function elgg_enqueue_notification_event($event, $type, $object) {
}
/**
* Subscribe a user for notifications to a set of notification events
*
* @param int $user_guid The GUID of the user
* @param int $data_type ENTITY, ANNOTATION, RELATIONSHIP, or METADATA
* @param array $params An array of parameters that define the set of events for this subscription.
* The parameters supported depend on the $data_type.
* ENTITY parameters:
* 'actor' => The GUID of the user performing the action on the entity
* 'owner' => The GUID of the entity that owns the entity
* 'container' => the GUID of the container entity for the entity
* 'type' => the type of the entity
* 'subtype' => the subtype of the entity
* 'event' => the action being performed on the entity ('create', 'update', 'publish')
*
* RELATIONSHIP parameters:
* 'actor' => The GUID of the user creating the relationship
* 'subject' => The GUID of the subject of the relationship
* 'object' => The GUID of the object of the relationship
* 'type' => The type of the relationship
*
* ANNOTATION/METADATA parameters:
* 'actor' => The GUID of the user creating the extender
* 'object' => The GUID of the subject of the extender
* 'container' => The GUID of the subject of the extender
* 'type' => The type of the extender
*
* @param array $methods An array of delivery methods strings: array('email', 'site')
*
* @return bool
*
* @see elgg_unsubscribe()
* @see elgg_register_notification_method()
*/
function elgg_subscribe($user_guid, $data_type, array $params = array()) {
}
/**
* Unsubscribe a user for a set of notification events
*
* @return bool
*
* @see elgg_subscribe()
*/
function elgg_unsubscribe($user_guid, $data_type, array $params = array()) {
}
/**
* Register a delivery method for notifications
*
* @param string $name
* @param function $callback
* @return bool
*
* @see elgg_unregister_notification_method()
*/
function elgg_register_notification_method($name, $callback) {
}
/**
* Unregister a delivery method for notifications
*
* @param string $name
* @param function $callback
* @return bool
*
* @see elgg_register_notification_method()
*/
function elgg_unregister_notification_method($name, $callback) {
}
/**
*
*/
function elgg_notification_handler() {
// calculate when we should stop
$stop_time = time() + 60;
$manager = new ElggNotificationManager();
$manager->run($stop_time);
}
/**
* Notifications initialization
* @access private
*/
function elgg_notifications_init() {
elgg_register_plugin_hook_handler('cron', 'minute', 'elgg_notification_handler');
}
elgg_register_event_handler('init', 'system', 'elgg_notifications_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment