Skip to content

Commit

Permalink
Allow unread
Browse files Browse the repository at this point in the history
  • Loading branch information
eluhr committed Jan 10, 2024
1 parent 45144ff commit 43916b6
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/controllers/InboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use yii\helpers\Html;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\helpers\VarDumper;
use yii\web\Response;

/**
Expand All @@ -41,6 +40,7 @@ public function behaviors()
'actions' => [
'index',
'preferences',
'unread',
'read',
'delete-inbox-message',
'sent',
Expand Down Expand Up @@ -82,8 +82,8 @@ public function behaviors()
/**
* @param Action $action
*
* @return bool
* @throws \yii\web\BadRequestHttpException
* @return bool
*/
public function beforeAction($action)
{
Expand All @@ -94,9 +94,9 @@ public function beforeAction($action)
/**
* @param $inboxMessageId
*
* @return \yii\web\Response
* @throws NotFoundHttpException
* @throws \yii\base\InvalidConfigException
* @return \yii\web\Response
*/
public function actionMarkInboxMessage($inboxMessageId)
{
Expand All @@ -118,9 +118,9 @@ public function actionMarkInboxMessage($inboxMessageId)
}

/**
* @return string
* @throws \yii\base\InvalidConfigException
* @throws \yii\base\Exception
* @return string
*/
public function actionIndex()
{
Expand All @@ -136,8 +136,8 @@ public function actionIndex()
}

/**
* @return string
* @throws \yii\base\InvalidConfigException
* @return string
*/
public function actionSent()
{
Expand All @@ -150,9 +150,9 @@ public function actionSent()
}

/**
* @return string
* @throws \yii\base\Exception
* @throws \yii\base\InvalidConfigException
* @return string
*/
public function actionUserGroup()
{
Expand All @@ -167,9 +167,24 @@ public function actionUserGroup()
/**
* @param $inboxMessageId
*
* @return string
* @throws \yii\base\InvalidConfigException
* @throws NotFoundHttpException
* @return Response
*/
public function actionUnread($inboxMessageId)
{
if ($this->setMessageRead($inboxMessageId, 0)) {
return $this->redirect(['index']);
}
throw new NotFoundHttpException(Yii::t('notification', 'Message not found.'));
}

/**
* @param $inboxMessageId
*
* @throws \yii\base\InvalidConfigException
* @throws NotFoundHttpException
* @return string
*/
public function actionRead($inboxMessageId)
{
Expand All @@ -183,9 +198,9 @@ public function actionRead($inboxMessageId)
/**
* @param $messageId
*
* @return string
* @throws \yii\base\InvalidConfigException
* @throws NotFoundHttpException
* @return string
*/
public function actionReadSent($messageId)
{
Expand All @@ -204,11 +219,11 @@ public function actionReadSent($messageId)
/**
* @param $inboxMessageId
*
* @return \yii\web\Response
* @throws NotFoundHttpException
* @throws \Throwable
* @throws \yii\base\InvalidConfigException
* @throws \yii\db\StaleObjectException
* @return \yii\web\Response
*/
public function actionDeleteInboxMessage($inboxMessageId)
{
Expand All @@ -219,11 +234,11 @@ public function actionDeleteInboxMessage($inboxMessageId)
/**
* @param $messageUserGroupId
*
* @return \yii\web\Response
* @throws NotFoundHttpException
* @throws \Throwable
* @throws \yii\base\InvalidConfigException
* @throws \yii\db\StaleObjectException
* @return \yii\web\Response
*/
public function actionDeleteUserGroup($messageUserGroupId)
{
Expand Down Expand Up @@ -282,9 +297,9 @@ public function actionCompose($messageId = null, $replyTo = null)
/**
* @param null|string $messageUserGroupId
*
* @return string
* @throws \yii\base\InvalidConfigException
* @throws NotFoundHttpException
* @return string
*/
public function actionUserGroupEdit($messageUserGroupId = null)
{
Expand Down Expand Up @@ -340,13 +355,13 @@ public function actionContextAction()
$post = \Yii::$app->request->post();
$messages = $post['checked'] ?? [];

if(!empty($messages)) {
if (!empty($messages)) {
if (\Yii::$app->request->post(Message::SUBMIT_TYPE_NAME) === Message::DELETE_MESSAGE) {
foreach ($messages as $messageId){
foreach ($messages as $messageId) {
$this->deleteInboxMessage($messageId);
}
} else if (\Yii::$app->request->post(Message::SUBMIT_TYPE_NAME) === Message::MARK_MESSAGE_AS_READ){
foreach ($messages as $messageId){
} else if (\Yii::$app->request->post(Message::SUBMIT_TYPE_NAME) === Message::MARK_MESSAGE_AS_READ) {
foreach ($messages as $messageId) {
$this->setMessageRead($messageId);
}
} /*
Expand All @@ -362,9 +377,10 @@ public function actionContextAction()

/**
* @param $inboxMessageId
*
* @return InboxMessage|null
*/
protected function setMessageRead($inboxMessageId, $readStatus = 1)
private function setMessageRead($inboxMessageId, $readStatus = 1)
{
/** @var InboxMessage|null $inboxMessageModel */
$inboxMessageModel = InboxMessage::find()->own()->andWhere(['id' => $inboxMessageId])->one();
Expand All @@ -373,18 +389,17 @@ protected function setMessageRead($inboxMessageId, $readStatus = 1)
throw new NotFoundHttpException(Yii::t('notification', 'Message not found.'));
}

if ($inboxMessageModel->read === 0) {
$inboxMessageModel->read = $readStatus;
if (!$inboxMessageModel->save()) {
Yii::$app->session->addFlash('info',
Yii::t('notification', 'Cannot update read status of this message.'));
}
$inboxMessageModel->read = $readStatus;
if (!$inboxMessageModel->save()) {
Yii::$app->session->addFlash('info',
Yii::t('notification', 'Cannot update read status of this message.'));
}
return $inboxMessageModel;
}

/**
* @param $inboxMessageId
*
* @return void
*/
private function deleteInboxMessage($inboxMessageId)
Expand All @@ -395,6 +410,7 @@ private function deleteInboxMessage($inboxMessageId)

/**
* @param $inboxMessageId
*
* @return void
*/
private function deleteSentMessage($inboxMessageId)
Expand All @@ -405,6 +421,7 @@ private function deleteSentMessage($inboxMessageId)

/**
* @param $messageModel
*
* @return void
*/
private function deleteMessage($messageModel)
Expand Down

0 comments on commit 43916b6

Please sign in to comment.