Skip to content

Commit

Permalink
Now using the Elgg 1.9 notifications system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juho Jaakkola committed Apr 4, 2014
1 parent fa6b26d commit bfa356c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 67 deletions.
2 changes: 0 additions & 2 deletions actions/plugins/create_project.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

elgg_make_sticky_form('community_plugins');


// Get variables
$title = strip_tags(get_input("title"));
$description = plugins_strip_tags(get_input("description"));
Expand Down Expand Up @@ -127,7 +126,6 @@
'object_guid' => $plugin_project->getGUID(),
));

plugins_send_notifications($plugin_project);
system_message(elgg_echo("plugins:project:saved"));

elgg_clear_sticky_form('community_plugins');
Expand Down
1 change: 0 additions & 1 deletion actions/plugins/create_release.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
'object_guid' => $release->guid,
));

plugins_send_notifications($release);
system_message(elgg_echo("plugins:release:saved"));

elgg_clear_sticky_form('community_plugins');
Expand Down
26 changes: 14 additions & 12 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
'plugins:admin:contributors:help' => "Adding users as contributors <b>does not</b> give them any special privileges with regard to the plugin page, it does however list them as contributing members.
It is a way of recognizing community members for their collaborative work of reporting bugs, and submitting patches for bugfixes and enhancements.
Begin typing the name of the user who has contributed to the plugin. You can select as many users as necessary.",


/**
* Object views
*/
Expand All @@ -41,8 +41,8 @@
'plugins:project:title:version' => "%s for Elgg %s",
'plugins:author:byline' => "by %s",
'plugins:last:updated' => "Last updated %s",


/**
* Menu items and titles
*/
Expand Down Expand Up @@ -75,8 +75,8 @@
'plugins:listing:newest' => 'Newest',
'plugins:listing:popular' => 'Most downloads',
'plugins:listing:dugg' => 'Most recommended',


'plugins:upload:new' => "Upload a new plugin",


Expand Down Expand Up @@ -191,6 +191,8 @@
'plugins:plugin_release:notify:subject' => '%s has released a new version of the plugin %s',
'plugins:plugin_project:notify:body' => "%s has uploaded a new plugin called %s \n\n %s \n\n %s",
'plugins:plugin_release:notify:body' => "%s has released a new version of the plugin %s \n\n %s \n\n %s",
'plugins:plugin_project:notify:summary' => 'New plugin project called %s',
'plugins:plugin_project:notify:summary' => 'New release of the plugin %s',

/**
* Licenses
Expand Down Expand Up @@ -341,13 +343,13 @@
'plugins:settings:save:failure' => "Could not save plugin search settings: unrecognized parameters received.",

'plugins:filters:or' => '...or ',


'river:comment:object:plugin_release' => '%s commented on the plugin %s',
'river:comment:object:plugin_project' => '%s commented on the plugin %s',
'river:create:object:plugin_project' => "%s uploaded a new plugin: %s",
'river:create:object:plugin_release' => "%s released a new version of the plugin %s",

/* Edit Form */
'plugins:edit:helptext' => "You are editing the plugin project information for %s. To upload a new release click %s.",
'plugins:add:helptext' => "You are creating a new plugin project. If you want to release a new
Expand Down Expand Up @@ -381,15 +383,15 @@
'plugins:edit:label:recommended' => "Set as the recommended release",
'plugins:edit:help:recommended' => "Recommend all users of this plugin use this release?",
'plugins:link:here' => 'here',

/**
* Misc
*/
'plugins:warning:page:all:bookmark' => 'Please update your bookmark or report this link to the site owner as this page has moved.',
'plugins:error:invalid_release' => "We could not find the release you specified.",
'plugins:forward:recommended_release' => "Forwarded to recommended release.",
'plugins:error:unrecognized_plugin' => "We did not recognize that plugin",

/**
* Actions
*/
Expand All @@ -408,7 +410,7 @@
'plugins:action:invalid_user' => "Invalid user",
'plugins:action:delete_contributor:success' => 'User has been removed from the contributors list',
'plugins:action:invalid_access' => 'Unknown or insufficient access to release',

);

add_translation("en", $english);
53 changes: 3 additions & 50 deletions lib/plugin_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Get the mimetype of the plugin archive
*
*
* @param string $name
* @return string/bool
*/
Expand Down Expand Up @@ -75,7 +75,7 @@ function plugins_get_downloads_histogram($guid = 0, $days = 30) {

/**
* Plugin project search hook
*
*
* @param string $hook
* @param string $type
* @param <type> $value
Expand Down Expand Up @@ -145,56 +145,9 @@ function plugins_search_hook($hook, $type, $value, $params) {
);
}

// need our own notification code because core Elgg code uses container for matches
function plugins_send_notifications($entity) {
global $CONFIG, $NOTIFICATION_HANDLERS;

$owner = $entity->getOwnerEntity();

// Get users interested in content from this person and notify them
foreach ($NOTIFICATION_HANDLERS as $method => $foo) {
$interested_users = elgg_get_entities_from_relationship(array(
'relationship' => 'notify' . $method,
'relationship_guid' => $owner->guid,
'inverse_relationship' => TRUE,
'types' => 'user',
'limit' => 99999
));

if ($interested_users && is_array($interested_users)) {
foreach ($interested_users as $user) {
if ($user instanceof ElggUser && !$user->isBanned()) {
if (($user->guid != elgg_get_logged_in_user_guid()) && has_access_to_entity($entity, $user)) {

$subtype = $entity->getSubtype();
if ($subtype == 'plugin_project') {
$text = $entity->description;
} else {
$text = $entity->release_notes;
}

$subject = sprintf(elgg_echo("plugins:$subtype:notify:subject"), $owner->name, $entity->title);
$body = sprintf(elgg_echo("plugins:$subtype:notify:body"), $owner->name, $entity->title, $text, $entity->getURL());

notify_user(
$user->guid,
$entity->owner_guid,
$subject,
$body,
NULL,
array($method)
);
}
}
}
}
}
}


/**
* Return the count of all downloads
*
*
* @return int
*/
function plugins_get_all_download_count() {
Expand Down
81 changes: 79 additions & 2 deletions start.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ function plugins_init() {
// Image handler
elgg_register_page_handler('plugins_image', 'plugins_image_page_handler');

// TODO: This looks like a bug. "plugins" is not a valid subtype
register_notification_object('object', 'plugins', elgg_echo('plugins:new'));
// Tell core to send notifications when new projects and releases are created
elgg_register_notification_event('object', 'plugin_project', array('create'));
elgg_register_notification_event('object', 'plugin_release', array('create'));

// The notifications for projects and releases are almost identical so we can use the same handler for both
elgg_register_plugin_hook_handler('prepare', 'notification:create:object:plugin_project', 'plugins_prepare_notification');
elgg_register_plugin_hook_handler('prepare', 'notification:create:object:plugin_release', 'plugins_prepare_notification');

// Releases are contained by a project so we need to get the notification subscriptions manually
elgg_register_plugin_hook_handler('get', 'subscriptions', 'plugins_get_release_subscriptions');

//register a widget
elgg_register_widget_type('plugins', elgg_echo('plugins'), elgg_echo('plugins'), array('profile'));
Expand Down Expand Up @@ -476,3 +484,72 @@ function plugins_update_download_counts() {
$count += 1200000;
elgg_set_plugin_setting('site_plugins_downloads', $count, 'community_plugins');
}

/**
* Prepare a notification message about a new plugin project or a new plugin release
*
* @param string $hook Hook name
* @param string $type Hook type
* @param Elgg_Notifications_Notification $notification The notification to prepare
* @param array $params Hook parameters
* @return Elgg_Notifications_Notification
*/
function plugins_prepare_notification($hook, $type, $notification, $params) {
$entity = $params['event']->getObject();
$owner = $params['event']->getActor();
$recipient = $params['recipient'];
$language = $params['language'];
$method = $params['method'];

$subtype = $entity->getSubtype();
if ($subtype == 'plugin_project') {
$text = $entity->description;
} else {
$text = $entity->release_notes;
}

// Notification title
$notification->subject = elgg_echo("plugins:{$subtype}:notify:subject", array($owner->name, $entity->title), $language);

// Notification body
$notification->body = elgg_echo("plugins:{$subtype}:notify:body", array(
$owner->name,
$entity->title,
$text,
$entity->getURL()
), $language);

// Notification summary
$notification->summary = elgg_echo("plugins:{$subtype}:notify:summary", array($entity->title), $language);

return $notification;
}

/**
* Get notification subscriptions for a new plugin release
*
* The Elgg 1.9 subscriptions system is based on containers. By default it is
* possible to subscribe only to users. The container of a release is however
* a project, so we need to get subscriptions for the project when notifying
* about a new release.
*
* @param string $hook Hook name
* @param string $type Hook type
* @param array $subscriptions Array of subscriptions
* @param array $params Hook parameters
* @return array $subscriptions
*/
function plugins_get_release_subscriptions($hook, $type, $subscriptions, $params) {
$entity = $params['event']->getObject();

if ($entity instanceof PluginRelease) {
$project = $entity->getContainerEntity();

// We skip the first release because we notify about the new plugin project instead
if ($project->getReleases(array('count' => true)) > 1) {
$subscriptions += elgg_get_subscriptions_for_container($project->container_guid);
}
}

return $subscriptions;
}

0 comments on commit bfa356c

Please sign in to comment.