Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification for Gallery instead of single Media file #100

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions activities/MediaUploaded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2022 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\gallery\activities;

use humhub\modules\activity\components\BaseActivity;
use humhub\modules\activity\interfaces\ConfigurableActivityInterface;
use humhub\modules\comment\models\Comment;
use humhub\modules\gallery\models\CustomGallery;
use Yii;

/**
* MediaUploaded activity
*/
class MediaUploaded extends BaseActivity implements ConfigurableActivityInterface
{

/**
* @inheritdoc
*/
public $moduleId = 'gallery';

/**
* @inheritdoc
*/
public $viewName = 'mediaUploaded';

/**
* @var CustomGallery
*/
public $source;

/**
* @inheritdoc
*/
public function getTitle()
{
return Yii::t('GalleryModule.base', 'Galleries');
}

/**
* @inheritdoc
*/
public function getDescription()
{
return Yii::t('CommentModule.base', 'Whenever new media files were uploaded.');
}

/**
* @inheritdoc
*/
public function getUrl()
{
return $this->source->url;
}

}
13 changes: 13 additions & 0 deletions activities/views/mediaUploaded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use humhub\modules\gallery\models\CustomGallery;
use humhub\modules\user\models\User;
use yii\helpers\Html;

/* @var $originator User */
/* @var $source CustomGallery */

echo Yii::t('GalleryModule.base', '{displayName} uploaded new media files into the Gallery "{galleryName}"', [
'displayName' => '<strong>' . Html::encode($originator->displayName) . '</strong>',
'galleryName' => Html::encode($source->title)
]);
9 changes: 8 additions & 1 deletion controllers/CustomGalleryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace humhub\modules\gallery\controllers;

use humhub\debug\RDebug;
use humhub\modules\file\libs\FileHelper;
use humhub\modules\gallery\helpers\Url;
use humhub\modules\gallery\models\BaseGallery;
Expand Down Expand Up @@ -165,10 +164,18 @@ public function actionUpload()

$errors = false;
$files = [];
$isNewFileUploaded = false;
foreach (UploadedFile::getInstancesByName('files') as $cFile) {
$result = $this->handleMediaUpload($gallery, $cFile);
$errors |= $result['error'];
$files[] = $result;
if (!$result['error']) {
$isNewFileUploaded = true;
}
}

if ($isNewFileUploaded) {
$gallery->notify();
}

return $this->asJson(['files' => $files]);
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
-----------------------
- Fix #89: Fix tests
- Fix #71: Improve wall entry styles for small screens
- Enh #36: Notification for Gallery instead of single Media file


1.2.2 (November 27, 2020)
Expand Down
1 change: 1 addition & 0 deletions models/BaseGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @property integer $sort_order
* @property integer $thumb_file_id
* @property integer $type
* @property-read string $url
*
* @package humhub.modules.gallery.models
* @since 1.0
Expand Down
23 changes: 20 additions & 3 deletions models/CustomGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

use humhub\modules\content\components\ActiveQueryContent;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\space\models\Space;
use Yii;
use humhub\modules\gallery\helpers\Url;
use humhub\modules\user\models\User;
use Yii;

/**
* This is the model class for a custom gallery.
Expand Down Expand Up @@ -103,4 +102,22 @@ public function isEmpty()
return $this->mediaListQuery()->one() === null;
}

/**
* Notify users about new uploaded media files
*/
public function notify()
{
/* @var User $user */
$user = Yii::$app->user->getIdentity();

\humhub\modules\gallery\notifications\MediaUploaded::instance()
->from($user)
->about($this)
->sendBulk($this->getFollowersWithNotificationQuery());

\humhub\modules\gallery\activities\MediaUploaded::instance()
->from($user)
->about($this)
->save();
}
}
24 changes: 11 additions & 13 deletions models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Media extends ContentActiveRecord implements Searchable
*/
public $wallEntryClass = "humhub\modules\gallery\widgets\WallEntryMedia";

/**
* @inheritdoc
*/
public $silentContentCreation = true;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -210,20 +215,13 @@ public function handleUpload(UploadedFile $file)
{
$mediaUpload = new MediaUpload();
$mediaUpload->setUploadedFile($file);
$valid = $mediaUpload->validate();

if ($valid) {
if ($mediaUpload->validate()) {
$this->title = $mediaUpload->file_name;

$valid = $this->validate();

if ($valid) {
if ($this->save()) {
$mediaUpload->object_model = self::class;
$mediaUpload->object_id = $this->id;
$mediaUpload->show_in_stream = false;
$mediaUpload->save();
}
if ($this->validate() && $this->save()) {
$mediaUpload->object_model = self::class;
$mediaUpload->object_id = $this->id;
$mediaUpload->show_in_stream = false;
$mediaUpload->save();
}
}

Expand Down
39 changes: 39 additions & 0 deletions notifications/GalleryNotificationCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2022 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\gallery\notifications;

use humhub\modules\notification\components\NotificationCategory;
use Yii;

/**
* GalleryNotificationCategory
*/
class GalleryNotificationCategory extends NotificationCategory
{

/**
* @inheritdoc
*/
public $id = 'gallery';

/**
* @inheritdoc
*/
public function getTitle()
{
return Yii::t('GalleryModule.base', 'Gallery');
}

/**
* @inheritdoc
*/
public function getDescription()
{
return Yii::t('GalleryModule.base', 'Receive Notifications for new uploaded media files.');
}
}
66 changes: 66 additions & 0 deletions notifications/MediaUploaded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2022 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\gallery\notifications;

use humhub\modules\gallery\models\CustomGallery;
use humhub\modules\notification\components\BaseNotification;
use Yii;
use yii\bootstrap\Html;

/**
* MediaUploaded notification for multiple files uploaded.
*/
class MediaUploaded extends BaseNotification
{

/**
* @inheritdoc
*/
public $viewName = 'mediaUploaded';

/**
* @inheritdoc
*/
public $moduleId = 'gallery';

/**
* @inheritdoc
* @var CustomGallery
*/
public $source;

/**
* @inheritdoc
*/
public function category()
{
return new GalleryNotificationCategory();
}

/**
* @inheritdoc
*/
public function html()
{
return Yii::t('GalleryModule.base', '{displayName} uploaded new files into {contentTitle}.', [
'displayName' => Html::tag('strong', Html::encode($this->originator->displayName)),
'contentTitle' => $this->getContentInfo($this->source)
]);
}

/**
* @inheritdoc
*/
public function getMailSubject()
{
return Yii::t('GalleryModule.base', '{originator} uploaded files into {contentTitle}', [
'originator' => $this->originator->displayName,
'contentTitle' => $this->getContentInfo($this->source)
]);
}
}
39 changes: 39 additions & 0 deletions notifications/views/mails/mediaUploaded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use humhub\modules\gallery\notifications\MediaUploaded;
use humhub\modules\notification\models\Notification;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use humhub\widgets\mails\MailButton;
use humhub\widgets\mails\MailButtonList;
use humhub\widgets\mails\MailContentEntry;
use yii\web\View;

/* @var $this View */
/* @var $viewable MediaUploaded */
/* @var $url string */
/* @var $date string */
/* @var $originator User */
/* @var $space Space */
/* @var $record Notification */
/* @var $_params_ array */
?>
<?php $this->beginContent('@notification/views/layouts/mail.php', $_params_); ?>

<?= MailContentEntry::widget([
'originator' => $originator,
'receiver' => $record->user,
'content' => Yii::t('GalleryModule.base', 'New files have been uploaded into the gallery "{galleryName}".', [
'galleryName' => $viewable->source->title
]),
'date' => $date,
'space' => $space
]) ?>

<?= MailButtonList::widget([
'buttons' => [
MailButton::widget(['url' => $url, 'text' => Yii::t('GalleryModule.base', 'View Online')])
]
]) ?>

<?php $this->endContent();