Skip to content

Commit

Permalink
changed: create wire notification is now group tool dependent
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalsem committed Feb 14, 2022
1 parent c09c969 commit 37b41e9
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 102 deletions.
2 changes: 0 additions & 2 deletions classes/ColdTrick/TheWireTools/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ protected function registerHooks() {
$hooks->registerHandler('export:counters', 'elasticsearch', __NAMESPACE__ . '\Elasticsearch::exportCounter');
$hooks->registerHandler('group_tool_widgets', 'widget_manager', __NAMESPACE__ . '\Widgets::groupToolBasedWidgets');
$hooks->registerHandler('handlers', 'widgets', __NAMESPACE__ . '\Widgets::registerHandlers');
$hooks->registerHandler('prepare', 'notification:create:object:thewire', __NAMESPACE__ . '\Notifications::prepareNotificationSubject', 600);
$hooks->registerHandler('prepare', 'notification:create:object:thewire', __NAMESPACE__ . '\Notifications::prepareReshareNotification', 700);
$hooks->registerHandler('register', 'menu:entity', __NAMESPACE__ . '\Menus::entityRegisterImprove', 501);
$hooks->registerHandler('register', 'menu:social', __NAMESPACE__ . '\Menus::entityRegisterReshare');
$hooks->registerHandler('register', 'menu:entity', __NAMESPACE__ . '\Menus::entityRegisterFeature');
Expand Down
100 changes: 0 additions & 100 deletions classes/ColdTrick/TheWireTools/Notifications.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace ColdTrick\TheWireTools\Notifications;

use Elgg\TheWire\Notifications\CreateTheWireEventHandler as CoreTheWireEventHandler;

/**
* Notification Event Handler for 'object' 'thewire' 'create' action
*/
class CreateTheWireEventHandler extends CoreTheWireEventHandler {

/**
* @var \ElggEntity
*/
protected $reshare_entity;

/**
* {@inheritDoc}
*/
protected function getNotificationSubject(\ElggUser $recipient, string $method): string {
$shared_entity = $this->getReshareEntity();
if (empty($shared_entity)) {
return parent::getNotificationSummary($recipient, $method);
}

if (elgg_language_key_exists("item:{$shared_entity->getType()}:{$shared_entity->getSubtype()}")) {
$type = elgg_echo("item:{$shared_entity->getType()}:{$shared_entity->getSubtype()}", [], $recipient->getLanguage());
} else {
$type = elgg_echo('unknown', [], $recipient->getLanguage());
}

$title = $shared_entity->getDisplayName() ?: $this->event->getObject()->description;
$title = elgg_get_excerpt($title, 25);

return elgg_echo('thewire_tools:notify:reshare:subject', [
$this->event->getActor()->getDisplayName(),
$type,
$title,
], $recipient->getLanguage());
}

/**
* {@inheritDoc}
*/
protected function getNotificationSummary(\ElggUser $recipient, string $method): string {
$shared_entity = $this->getReshareEntity();
if (empty($shared_entity)) {
return parent::getNotificationSummary($recipient, $method);
}

if (elgg_language_key_exists("item:{$shared_entity->getType()}:{$shared_entity->getSubtype()}")) {
$type = elgg_echo("item:{$shared_entity->getType()}:{$shared_entity->getSubtype()}", [], $recipient->getLanguage());
} else {
$type = elgg_echo('unknown', [], $recipient->getLanguage());
}

$title = $shared_entity->getDisplayName() ?: $this->event->getObject()->description;
$title = elgg_get_excerpt($title, 25);

return elgg_echo('thewire_tools:notify:reshare:summary', [
$this->event->getActor()->getDisplayName(),
$type,
$title,
], $recipient->getLanguage());
}

/**
* {@inheritDoc}
*/
protected function getNotificationBody(\ElggUser $recipient, string $method): string {
$shared_entity = $this->getReshareEntity();
if (empty($shared_entity)) {
return parent::getNotificationBody($recipient, $method);
}

/* @var $entity \ElggWire */
$entity = $this->event->getObject();

return elgg_echo('thewire_tools:notify:reshare:body', [
$this->event->getActor()->getDisplayName(),
$entity->description,
$shared_entity->getURL(),
$entity->getURL(),
], $recipient->getLanguage());
}

/**
* Returns the related reshared entity
*
* @return \ElggEntity|null
*/
protected function getReshareEntity(): ?\ElggEntity {
if (!isset($this->reshare_entity)) {
$reshare_entities = $this->event->getObject()->getEntitiesFromRelationship([
'limit' => 1,
'relationship' => 'reshare',
]);

if (empty($reshare_entities)) {
$this->reshare_entity = false;
} else {
$this->reshare_entity = $reshare_entities[0];
}
}

if (!$this->reshare_entity instanceof \ElggEntity) {
return null;
}

return $this->reshare_entity;
}

/**
* {@inheritDoc}
*/
protected static function isConfigurableForGroup(\ElggGroup $group): bool {
return $group->isToolEnabled('thewire');
}
}
8 changes: 8 additions & 0 deletions elgg-plugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use ColdTrick\TheWireTools\Bootstrap;
use ColdTrick\TheWireTools\Notifications\CreateTheWireEventHandler;

require_once(dirname(__FILE__) . '/lib/functions.php');

Expand Down Expand Up @@ -62,4 +63,11 @@
'thewire_tools/reshare_list' => ['ajax' => true],
'thewire_tools/thread' => ['ajax' => true],
],
'notifications' => [
'object' => [
'thewire' => [
'create' => CreateTheWireEventHandler::class,
],
],
],
];

0 comments on commit 37b41e9

Please sign in to comment.