diff --git a/Events.php b/Events.php index 6b920356..e1519bc0 100644 --- a/Events.php +++ b/Events.php @@ -8,18 +8,20 @@ namespace humhub\modules\mail; -use humhub\modules\mail\models\Message; -use humhub\modules\mail\models\UserMessageTag; -use humhub\modules\mail\permissions\StartConversation; -use humhub\modules\user\models\User; -use Yii; use humhub\modules\mail\helpers\Url; +use humhub\modules\mail\models\Config; use humhub\modules\mail\models\MessageEntry; +use humhub\modules\mail\models\Message; +use humhub\modules\mail\models\UserMessageTag; use humhub\modules\mail\models\UserMessage; +use humhub\modules\mail\permissions\SendMail; +use humhub\modules\mail\permissions\StartConversation; +use humhub\modules\mail\search\SearchProvider; use humhub\modules\mail\widgets\NewMessageButton; use humhub\modules\mail\widgets\NotificationInbox; -use humhub\modules\mail\permissions\SendMail; -use humhub\modules\mail\models\Config; +use humhub\widgets\MetaSearchWidget; +use humhub\modules\user\models\User; +use Yii; /** * Description of Events @@ -28,7 +30,6 @@ */ class Events { - /** * @param $event */ @@ -218,4 +219,16 @@ public static function onRestApiAddRules() ], 'mail'); } + public static function onMetaSearchWidgetInit($event) + { + if (Yii::$app->user->isGuest) { + return; + } + + /* @var MetaSearchWidget $widget */ + $widget = $event->sender; + + $widget->addProvider(SearchProvider::class); + } + } diff --git a/config.php b/config.php index fd6798f8..33a7415f 100644 --- a/config.php +++ b/config.php @@ -17,5 +17,6 @@ ['class' => ProfileHeaderControls::class, 'event' => ProfileHeaderControls::EVENT_INIT, 'callback' => ['humhub\modules\mail\Events', 'onProfileHeaderControlsInit']], ['class' => IntegrityController::class, 'event' => IntegrityController::EVENT_ON_RUN, 'callback' => ['humhub\modules\mail\Events', 'onIntegrityCheck']], ['class' => 'humhub\modules\rest\Module', 'event' => 'restApiAddRules', 'callback' => ['humhub\modules\mail\Events', 'onRestApiAddRules']], + ['class' => 'humhub\widgets\MetaSearchWidget', 'event' => 'init', 'callback' => ['humhub\modules\mail\Events', 'onMetaSearchWidgetInit']], ], -]; \ No newline at end of file +]; diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index abf7f9b5..8f9c51b4 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ Changelog ========= +3.2.2 (Unreleased) +---------------------- +- Enh #382: Implement provider for meta searching + 3.2.1 (April 15, 2024) ---------------------- - Enh #370: Remove message entries on disable module diff --git a/models/forms/InboxFilterForm.php b/models/forms/InboxFilterForm.php index 02c695d4..6776cb84 100644 --- a/models/forms/InboxFilterForm.php +++ b/models/forms/InboxFilterForm.php @@ -1,13 +1,10 @@ autoLoad === self::AUTO_LOAD_ALL || $this->autoLoad === self::AUTO_LOAD_GET) { + $keyword = Yii::$app->request->get('keyword'); + if ($keyword !== null) { + $this->term = $keyword; + } + } + $this->query = UserMessage::findByUser(); } @@ -95,7 +98,7 @@ public function apply() $this->query->andWhere(new OrCondition([ new ExistsCondition('EXISTS', $messageEntryContentSubQuery), - $this->createTermLikeCondition('message.title') + $this->createTermLikeCondition('message.title'), ])); } @@ -163,4 +166,9 @@ public function formName() { return ''; } -} \ No newline at end of file + + public function isFiltered(): bool + { + return !empty($this->term) || !empty($this->participants) || !empty($this->tags); + } +} diff --git a/module.json b/module.json index 58b6a157..ad941cb2 100644 --- a/module.json +++ b/module.json @@ -8,9 +8,9 @@ "messenger", "communication" ], - "version": "3.2.1", + "version": "3.2.2", "humhub": { - "minVersion": "1.15" + "minVersion": "1.16" }, "homepage": "https://github.com/humhub/mail", "authors": [ diff --git a/search/SearchProvider.php b/search/SearchProvider.php new file mode 100644 index 00000000..5f4c1a3c --- /dev/null +++ b/search/SearchProvider.php @@ -0,0 +1,106 @@ +route; + } + + /** + * @inheritdoc + */ + public function getAllResultsText(): string + { + return $this->getService()->hasResults() + ? Yii::t('base', 'Show all results') + : Yii::t('MailModule.base', 'Advanced Messages Search'); + } + + /** + * @inheritdoc + */ + public function getIsHiddenWhenEmpty(): bool + { + return true; + } + + /** + * @inheritdoc + */ + public function getResults(int $maxResults): array + { + $filter = new InboxFilterForm(['term' => $this->getKeyword()]); + $filter->apply(); + $totalCount = $filter->query->count(); + $resultsQuery = $filter->query->limit($maxResults); + + $results = []; + foreach ($resultsQuery->all() as $userMessage) { + $results[] = new SearchRecord($userMessage); + } + + return [ + 'totalCount' => $totalCount, + 'results' => $results, + ]; + } + + /** + * @inheritdoc + */ + public function getService(): MetaSearchService + { + if ($this->service === null) { + $this->service = new MetaSearchService($this); + } + + return $this->service; + } + + /** + * @inheritdoc + */ + public function getKeyword(): ?string + { + return $this->keyword; + } +} diff --git a/search/SearchRecord.php b/search/SearchRecord.php new file mode 100644 index 00000000..46ecf9a9 --- /dev/null +++ b/search/SearchRecord.php @@ -0,0 +1,72 @@ +preview = new InboxMessagePreview(['userMessage' => $userMessage]); + } + + /** + * @inheritdoc + */ + public function getImage(): string + { + $lastParticipant = $this->preview->lastParticipant(); + + if ($lastParticipant instanceof User) { + return Image::widget([ + 'user' => $lastParticipant, + 'width' => 36, + 'link' => false, + 'hideOnlineStatus' => true, + ]); + } + + return Icon::get('envelope'); + } + + /** + * @inheritdoc + */ + public function getTitle(): string + { + return $this->preview->getMessage()->title; + } + + /** + * @inheritdoc + */ + public function getDescription(): string + { + return $this->preview->getMessagePreview(); + } + + /** + * @inheritdoc + */ + public function getUrl(): string + { + return Url::toMessenger($this->preview->getMessage()); + } +} diff --git a/widgets/InboxMessagePreview.php b/widgets/InboxMessagePreview.php index 2ae85908..c0cd6ff2 100644 --- a/widgets/InboxMessagePreview.php +++ b/widgets/InboxMessagePreview.php @@ -1,9 +1,7 @@ $this->getMessagePreview(), 'messageTime' => $this->getMessageTime(), 'lastParticipant' => $this->lastParticipant(), - 'options' => $this->getOptions() + 'options' => $this->getOptions(), ]); } @@ -48,11 +46,11 @@ private function getOptions(): array 'message-id' => $message->id, 'action-click' => 'mail.notification.loadMessage', 'action-url' => Url::toMessenger($message), - ] + ], ]; } - private function getMessage(): Message + public function getMessage(): Message { if ($this->_message === null) { $this->_message = $this->userMessage->message; @@ -61,7 +59,7 @@ private function getMessage(): Message return $this->_message; } - private function lastParticipant(): ?User + public function lastParticipant(): ?User { return $this->isGroupChat() ? $this->getLastEntry()->user @@ -88,7 +86,7 @@ private function getMessageTitle(): string { if ($this->isGroupChat()) { $suffix = ', ' . Yii::t('MailModule.base', '{n,plural,=1{# other} other{# others}}', [ - 'n' => $this->getMessage()->getUsersCount() - 2 + 'n' => $this->getMessage()->getUsersCount() - 2, ]); } else { $suffix = ''; @@ -97,7 +95,7 @@ private function getMessageTitle(): string return $this->getUsername() . $suffix; } - private function getMessagePreview(): string + public function getMessagePreview(): string { switch ($this->getLastEntry()->type) { case AbstractMessageEntry::TYPE_USER_JOINED: @@ -131,7 +129,7 @@ private function getMessageTime(): string if ($datetime->format('Y-m-d') === date('Y-m-d')) { // Show time for today return Yii::$app->formatter->asTime($datetime, 'short'); - } else if (time() - $datetime->getTimestamp() < 3600 * 24 * 7) { + } elseif (time() - $datetime->getTimestamp() < 3600 * 24 * 7) { // Show week day for week ago messages switch ($datetime->format('w')) { case 0: return Yii::t('MailModule.base', 'Sunday'); diff --git a/widgets/views/inboxFilter.php b/widgets/views/inboxFilter.php index cbc6bd33..b58d1a2e 100644 --- a/widgets/views/inboxFilter.php +++ b/widgets/views/inboxFilter.php @@ -1,8 +1,6 @@ = Html::beginTag('div', $options) ?> @@ -24,14 +21,22 @@ ->id('conversation-filter-link') ->href('#mail-filter-menu') ->icon('filter') - ->options(['data-toggle' => "collapse"]) + ->options(['data-toggle' => 'collapse']) ->sm() ?> -