Skip to content

Commit

Permalink
Enh humhub-contrib#51: add "Posted links" section
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-farre committed Sep 19, 2022
1 parent f266d8c commit 24cbcf6
Show file tree
Hide file tree
Showing 12 changed files with 603 additions and 109 deletions.
193 changes: 193 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\linklist;

use humhub\modules\comment\models\Comment;
use humhub\modules\linklist\models\StreamLink;
use humhub\modules\post\models\Post;
use Yii;
use yii\db\AfterSaveEvent;

class Events
{
/**
* Defines what to do if a spaces sidebar is initialzed.
*
* @param $event
*/
public static function onSpaceSidebarInit($event)
{

$space = $event->sender->space;
if ($space->isModuleEnabled('linklist')) {
$event->sender->addWidget(widgets\Sidebar::className(), array('contentContainer' => $space), array(
'sortOrder' => 200,
));
}
}

/**
* On build of a Space Navigation, check if this module is enabled.
* When enabled add a menu item
*
* @param $event
*/
public static function onSpaceMenuInit($event)
{

$space = $event->sender->space;
if ($space->isModuleEnabled('linklist') && $space->isMember()) {
$event->sender->addItem(array(
'label' => Yii::t('LinklistModule.base', 'Linklist'),
'group' => 'modules',
'url' => $space->createUrl('/linklist/linklist'),
'icon' => '<i class="fa fa-link"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'linklist')
));
}
}

/**
* On build of a Profile Navigation, check if this module is enabled.
* When enabled add a menu item
*
* @param $event
*/
public static function onProfileMenuInit($event)
{
$user = $event->sender->user;

// Is Module enabled on this workspace?
if ($user->isModuleEnabled('linklist') && !Yii::$app->user->isGuest && $user->id == Yii::$app->user->id) {
$event->sender->addItem(array(
'label' => Yii::t('LinklistModule.base', 'Linklist'),
'url' => $user->createUrl('/linklist/linklist'),
'icon' => '<i class="fa fa-link"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'linklist'),
));
}
}

/**
* Defines what to do if a spaces sidebar is initialzed.
*
* @param $event
*/
public static function onProfileSidebarInit($event)
{
$user = $event->sender->user;

if ($user->isModuleEnabled('linklist')) {
$event->sender->addWidget(widgets\Sidebar::className(), array('contentContainer' => $user), array(
'sortOrder' => 200,
));
}
}

/**
* @param AfterSaveEvent $event
*/
public static function onPostAfterInsert($event)
{
if (!isset($event->sender)) {
return;
}

/** @var Post $post */
$post = $event->sender;

if (!empty($post->content->id) && !empty($post->message)) {
StreamLink::updateLinks($post->message, $post->content->id, Post::class, $post->id);
}
}

/**
* @param AfterSaveEvent $event
*/
public static function onCommentAfterInsert($event)
{
if (!isset($event->sender)) {
return;
}

/** @var Comment $comment */
$comment = $event->sender;

if ($comment->object_model === Post::class && !empty($comment->content->id) && !empty($comment->message)) {
StreamLink::updateLinks($comment->message, $comment->content->id, Comment::class, $comment->id);
}
}

/**
* @param AfterSaveEvent $event
*/
public static function onPostAfterUpdate($event)
{
if (!isset($event->sender, $event->changedAttributes)) {
return;
}

/** @var Post $post */
$post = $event->sender;

if (!empty($post->content->id) && !empty($post->message)) {
StreamLink::updateLinks($post->message, $post->content->id, Post::class, $post->id);
}
}

/**
* @param AfterSaveEvent $event
*/
public static function onCommentAfterUpdate($event)
{
if (!isset($event->sender, $event->changedAttributes)) {
return;
}

/** @var Comment $comment */
$comment = $event->sender;

if ($comment->object_model === Post::class && !empty($comment->content->id) && !empty($comment->message)) {
StreamLink::updateLinks($comment->message, $comment->content->id, Comment::class, $comment->id);
}
}

/**
* @param AfterSaveEvent $event
*/
public static function onPostAfterDelete($event)
{
if (!isset($event->sender)) {
return;
}

/** @var Post $post */
$post = $event->sender;

if ($post) {
StreamLink::deleteAll(['object_model' => Post::class, 'object_id' => $post->id]);
}
}

/**
* @param AfterSaveEvent $event
*/
public static function onCommentAfterDelete($event)
{
if (!isset($event->sender)) {
return;
}

/** @var Comment $comment */
$comment = $event->sender;

if ($comment) {
StreamLink::deleteAll(['object_model' => Comment::class, 'object_id' => $comment->id]);
}
}
}
116 changes: 36 additions & 80 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace humhub\modules\linklist;

use Yii;

use humhub\modules\linklist\models\Link;
use humhub\modules\comment\models\Comment;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\components\ContentContainerModule;
use humhub\modules\linklist\models\Category;
use humhub\modules\linklist\models\Link;
use humhub\modules\linklist\models\StreamLink;
use humhub\modules\post\models\Post;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\components\ContentContainerModule;
use yii\db\StaleObjectException;

class Module extends ContentContainerModule
{

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -41,7 +42,7 @@ public function disable()
foreach (Category::find()->all() as $category) {
$category->delete();
}

parent::disable();
}

Expand All @@ -53,6 +54,25 @@ public function enableContentContainer(ContentContainerActiveRecord $container)
$container->setSetting('enableDeadLinkValidation', 0, 'linklist');
$container->setSetting('enableWidget', 0, 'linklist');
parent::enableContentContainer($container);

// Grep links from posts and related comments
$posts = Post::find()
->joinWith('content')
->where(['content.contentcontainer_id' => $container->contentcontainer_id])
->all();
$postIds = [];
foreach ($posts as $post) {
if (!empty($post->content->id) && !empty($post->message)) {
$postIds[] = $post->id;
StreamLink::updateLinks($post->message, $post->content->id, Post::class, $post->id);
}
}
$comments = Comment::findAll(['object_model' => Post::class, 'object_id' => $postIds]);
foreach ($comments as $comment) {
if (!empty($comment->content->id) && !empty($comment->message)) {
StreamLink::updateLinks($comment->message, $comment->content->id, Comment::class, $comment->id);
}
}
}

/**
Expand All @@ -68,80 +88,16 @@ public function disableContentContainer(ContentContainerActiveRecord $container)
foreach (Link::find()->contentContainer($container)->all() as $content) {
$content->delete();
}
}

/**
* Defines what to do if a spaces sidebar is initialzed.
*
* @param type $event
*/
public static function onSpaceSidebarInit($event)
{

$space = $event->sender->space;
if ($space->isModuleEnabled('linklist')) {
$event->sender->addWidget(widgets\Sidebar::className(), array('contentContainer' => $space), array(
'sortOrder' => 200,
));
$streamLinks = StreamLink::find()
->joinWith('content')
->where(['content.contentcontainer_id' => $container->contentcontainer_id])
->all();
foreach ($streamLinks as $streamLink) {
try {
$streamLink->delete();
} catch (StaleObjectException|\Exception $e) {
}
}
}

/**
* On build of a Space Navigation, check if this module is enabled.
* When enabled add a menu item
*
* @param type $event
*/
public static function onSpaceMenuInit($event)
{

$space = $event->sender->space;
if ($space->isModuleEnabled('linklist') && $space->isMember()) {
$event->sender->addItem(array(
'label' => Yii::t('LinklistModule.base', 'Linklist'),
'group' => 'modules',
'url' => $space->createUrl('/linklist/linklist'),
'icon' => '<i class="fa fa-link"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'linklist')
));
}
}

/**
* On build of a Profile Navigation, check if this module is enabled.
* When enabled add a menu item
*
* @param type $event
*/
public static function onProfileMenuInit($event)
{
$user = $event->sender->user;

// Is Module enabled on this workspace?
if ($user->isModuleEnabled('linklist') && !Yii::$app->user->isGuest && $user->id == Yii::$app->user->id) {
$event->sender->addItem(array(
'label' => Yii::t('LinklistModule.base', 'Linklist'),
'url' => $user->createUrl('/linklist/linklist'),
'icon' => '<i class="fa fa-link"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'linklist'),
));
}
}

/**
* Defines what to do if a spaces sidebar is initialzed.
*
* @param type $event
*/
public static function onProfileSidebarInit($event)
{
$user = $event->sender->user;

if ($user->isModuleEnabled('linklist')) {
$event->sender->addWidget(widgets\Sidebar::className(), array('contentContainer' => $user), array(
'sortOrder' => 200,
));
}
}

}
19 changes: 14 additions & 5 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
<?php

use humhub\modules\comment\models\Comment;
use humhub\modules\linklist\Events;
use humhub\modules\post\models\Post;
use humhub\modules\space\widgets\Menu;
use humhub\modules\space\widgets\Sidebar;
use humhub\modules\user\widgets\ProfileMenu;
use humhub\modules\user\widgets\ProfileSidebar;
use humhub\modules\space\widgets\Sidebar;

return [
'id' => 'linklist',
'class' => 'humhub\modules\linklist\Module',
'namespace' => 'humhub\modules\linklist',
'events' => [
array('class' => Menu::className(), 'event' => Menu::EVENT_INIT, 'callback' => array('humhub\modules\linklist\Module', 'onSpaceMenuInit')),
array('class' => ProfileMenu::className(), 'event' => ProfileMenu::EVENT_INIT, 'callback' => array('humhub\modules\linklist\Module', 'onProfileMenuInit')),
array('class' => Sidebar::className(), 'event' => Sidebar::EVENT_INIT, 'callback' => array('humhub\modules\linklist\Module', 'onSpaceSidebarInit')),
array('class' => ProfileSidebar::className(), 'event' => ProfileSidebar::EVENT_INIT, 'callback' => array('humhub\modules\linklist\Module', 'onProfileSidebarInit')),
['class' => Menu::className(), 'event' => Menu::EVENT_INIT, 'callback' => [Events::class, 'onSpaceMenuInit']],
['class' => ProfileMenu::className(), 'event' => ProfileMenu::EVENT_INIT, 'callback' => [Events::class, 'onProfileMenuInit']],
['class' => Sidebar::className(), 'event' => Sidebar::EVENT_INIT, 'callback' => [Events::class, 'onSpaceSidebarInit']],
['class' => ProfileSidebar::className(), 'event' => ProfileSidebar::EVENT_INIT, 'callback' => [Events::class, 'onProfileSidebarInit']],
['class' => Post::class, 'event' => Post::EVENT_AFTER_INSERT, 'callback' => [Events::class, 'onPostAfterInsert']],
['class' => Comment::class, 'event' => Comment::EVENT_AFTER_INSERT, 'callback' => [Events::class, 'onCommentAfterInsert']],
['class' => Post::class, 'event' => Post::EVENT_AFTER_UPDATE, 'callback' => [Events::class, 'onPostAfterUpdate']],
['class' => Comment::class, 'event' => Comment::EVENT_AFTER_UPDATE, 'callback' => [Events::class, 'onCommentAfterUpdate']],
['class' => Post::class, 'event' => Post::EVENT_AFTER_DELETE, 'callback' => [Events::class, 'onPostAfterDelete']],
['class' => Comment::class, 'event' => Comment::EVENT_AFTER_DELETE, 'callback' => [Events::class, 'onCommentAfterDelete']],
],
];
?>
Loading

0 comments on commit 24cbcf6

Please sign in to comment.