diff --git a/controllers/ConsultationController.php b/controllers/ConsultationController.php index 4ad5130e9f..f25c999fda 100644 --- a/controllers/ConsultationController.php +++ b/controllers/ConsultationController.php @@ -312,20 +312,15 @@ public function actionIndex(): ResponseInterface $this->consultationSidebar($this->consultation); $myself = User::getCurrentUser(); - if ($myself) { - $myMotions = $myself->getMySupportedMotionsByConsultation($this->consultation); - $myAmendments = $myself->getMySupportedAmendmentsByConsultation($this->consultation); - } else { - $myMotions = null; - $myAmendments = null; - } return new HtmlResponse($this->render('index', [ 'cache' => $cache, 'consultation' => $this->consultation, 'myself' => $myself, - 'myMotions' => $myMotions, - 'myAmendments' => $myAmendments, + 'myMotions' => $myself?->getMySupportedMotionsByConsultation($this->consultation), + 'myAmendments' => $myself?->getMySupportedAmendmentsByConsultation($this->consultation), + 'myMotionComments' => MotionComment::getPrivatelyCommentedByConsultation($myself, $this->consultation), + 'myAmendmentComments' => AmendmentComment::getPrivatelyCommentedByConsultation($myself, $this->consultation), ])); } @@ -593,10 +588,14 @@ private function tagMotionResolutionList(int $tagId, bool $isResolutionList): Re return new HtmlErrorResponse(404, 'Tag not found'); } + $myself = User::getCurrentUser(); + return new HtmlResponse($this->render('tag_motion_list', [ 'tag' => $tag, 'cache' => LayoutHelper::getTagMotionListCache($this->consultation, $tag, $isResolutionList), 'isResolutionList' => $isResolutionList, + 'myMotionComments' => MotionComment::getPrivatelyCommentedByConsultation($myself, $this->consultation), + 'myAmendmentComments' => AmendmentComment::getPrivatelyCommentedByConsultation($myself, $this->consultation), ])); } diff --git a/models/db/AmendmentComment.php b/models/db/AmendmentComment.php index c25e6ff065..35d6a9c29f 100644 --- a/models/db/AmendmentComment.php +++ b/models/db/AmendmentComment.php @@ -121,43 +121,32 @@ public static function getNewestByConsultation(Consultation $consultation, int $ })); } - private static array $cachedComments = []; - /** - * @return AmendmentComment[][] + * @return AmendmentComment[] */ - public static function getAllForUserAndConsultationByMotion(Consultation $consultation, ?User $user, int $status): array + public static function getPrivatelyCommentedByConsultation(?User $user, Consultation $consultation): array { if (!$user) { return []; } - $key = $user->id . '.' . $status; - if (isset(self::$cachedComments[$key])) { - return self::$cachedComments[$key]; - } - - $invisibleStatuses = array_map('intval', $consultation->getStatuses()->getInvisibleMotionStatuses()); - /** @var AmendmentComment[] $allComments */ - $allComments = static::find()->joinWith('amendment', true)->joinWith('amendment.motionJoin', true) - ->where('amendmentComment.status = ' . intval($status)) - ->andWhere('amendmentComment.userId = ' . intval($user->id)) - ->andWhere('amendment.status NOT IN (' . implode(', ', $invisibleStatuses) . ')') - ->andWhere('motion.status NOT IN (' . implode(', ', $invisibleStatuses) . ')') - ->andWhere('motion.consultationId = ' . intval($consultation->id)) - ->orderBy('amendmentComment.paragraph') - ->all(); + $query = AmendmentComment::find(); + $query->innerJoin( + 'amendment', + 'amendmentComment.amendmentId = amendment.id' + ); + $query->innerJoin('motion', 'motion.id = amendment.motionId'); + $query->where('motion.status != ' . IntVal(Motion::STATUS_DELETED)); + $query->andWhere('amendment.status != ' . IntVal(Motion::STATUS_DELETED)); + $query->andWhere('motion.consultationId = ' . IntVal($consultation->id)); + $query->andWhere('amendmentComment.userId = ' . IntVal($user->id)); + $query->andWhere('amendmentComment.status = ' . AmendmentComment::STATUS_PRIVATE); + $query->orderBy('motion.titlePrefix ASC, amendment.titlePrefix ASC, amendment.dateCreation DESC, amendmentComment.paragraph ASC'); - $byAmendment = []; - foreach ($allComments as $comment) { - if (!isset($byAmendment[$comment->amendmentId])) { - $byAmendment[$comment->amendmentId] = []; - } - $byAmendment[$comment->amendmentId][] = $comment; - } + /** @var AmendmentComment[] $comments */ + $comments = $query->all(); - self::$cachedComments[$key] = $byAmendment; - return $byAmendment; + return $comments; } public function getMotionTitle(): string diff --git a/models/db/MotionComment.php b/models/db/MotionComment.php index 6e2ff9ad77..de03c3a71d 100644 --- a/models/db/MotionComment.php +++ b/models/db/MotionComment.php @@ -126,42 +126,30 @@ public static function getNewestByConsultation(Consultation $consultation, int $ })); } - private static array $cachedComments = []; - /** - * @return MotionComment[][] + * @return MotionComment[] */ - public static function getAllForUserAndConsultationByMotion(Consultation $consultation, ?User $user, int $status): array + public static function getPrivatelyCommentedByConsultation(?User $user, Consultation $consultation): array { if (!$user) { return []; } - $key = $user->id . '.' . $status; - if (isset(self::$cachedComments[$key])) { - return self::$cachedComments[$key]; - } - - $invisibleStatuses = array_map('intval', $consultation->getStatuses()->getInvisibleMotionStatuses()); - /** @var MotionComment[] $allComments */ - $allComments = static::find()->joinWith('motion', true) - ->where('motionComment.status = ' . intval($status)) - ->andWhere('motionComment.userId = ' . intval($user->id)) - ->andWhere('motion.status NOT IN (' . implode(', ', $invisibleStatuses) . ')') - ->andWhere('motion.consultationId = ' . intval($consultation->id)) - ->orderBy('motionComment.paragraph') - ->all(); + $query = MotionComment::find(); + $query->innerJoin( + 'motion', + 'motionComment.motionId = motion.id' + ); + $query->where('motion.status != ' . intval(Motion::STATUS_DELETED)); + $query->andWhere('motion.consultationId = ' . intval($consultation->id)); + $query->andWhere('motionComment.userId = ' . intval($user->id)); + $query->andWhere('motionComment.status = ' . MotionComment::STATUS_PRIVATE); + $query->orderBy('motion.titlePrefix ASC, motion.dateCreation DESC, motion.id DESC, motionComment.paragraph ASC'); - $byMotion = []; - foreach ($allComments as $comment) { - if (!isset($byMotion[$comment->motionId])) { - $byMotion[$comment->motionId] = []; - } - $byMotion[$comment->motionId][] = $comment; - } + /** @var MotionComment[] $comments */ + $comments = $query->all(); - self::$cachedComments[$key] = $byMotion; - return $byMotion; + return $comments; } public function getConsultation(): ?Consultation diff --git a/models/db/User.php b/models/db/User.php index d4542931fd..bf5d655f95 100644 --- a/models/db/User.php +++ b/models/db/User.php @@ -583,7 +583,6 @@ public function getMySupportedAmendmentsByConsultation(Consultation $consultatio return $supporters; } - public function getNotificationUnsubscribeCode(): string { return $this->id . '-' . $this->createConfirmationCode('unsubscribe'); diff --git a/views/consultation/LayoutHelper.php b/views/consultation/LayoutHelper.php index da5b26520b..ac8f90c98d 100644 --- a/views/consultation/LayoutHelper.php +++ b/views/consultation/LayoutHelper.php @@ -96,9 +96,7 @@ public static function flushViewCaches(Consultation $consultation): void private static function getMotionLineContent(Motion $motion, Consultation $consultation): string { $return = '
' . "\n";
-
- $privateMotionComments = MotionComment::getAllForUserAndConsultationByMotion($consultation, User::getCurrentUser(), MotionComment::STATUS_PRIVATE);
- $return .= LayoutHelper::getPrivateCommentIndicator($motion, $privateMotionComments, []);
+ $return .= '';
$motionUrl = UrlHelper::createMotionUrl($motion);
$return .= '';
@@ -157,8 +155,7 @@ private static function getAmendmentLineContent(Amendment $amendment): string
$return = '';
$consultation = $amendment->getMyConsultation();
- $privateAmendmentComments = AmendmentComment::getAllForUserAndConsultationByMotion($consultation, User::getCurrentUser(), AmendmentComment::STATUS_PRIVATE);
- $return .= LayoutHelper::getPrivateCommentIndicator($amendment, [], $privateAmendmentComments);
+ $return .= '';
$title = ($amendment->showTitlePrefix() ? $amendment->getFormattedTitlePrefix() : \Yii::t('amend', 'amendment'));
$return .= 'motionId])) {
+ $motionComments[$comment->motionId] = [];
+ }
+ if ($comment->paragraph === -1) {
+ $motionComments[$comment->motionId][] = $comment->text;
+ } else {
+ $motionComments[$comment->motionId][] = str_replace('%NO%', (string) $comment->paragraph, Yii::t('motion', 'private_notes_para')) .
+ ': ' . $comment->text;
+ }
+}
+
+$amendmentComments = [];
+foreach ($myAmendmentComments as $comment) {
+ if (!isset($amendmentComments[$comment->amendmentId])) {
+ $amendmentComments[$comment->amendmentId] = [];
+ }
+ if ($comment->paragraph === -1) {
+ $amendmentComments[$comment->amendmentId][] = $comment->text;
+ } else {
+ $amendmentComments[$comment->amendmentId][] = str_replace('%NO%', (string) $comment->paragraph, Yii::t('motion', 'private_notes_para')) .
+ ': ' . $comment->text;
+ }
+}
+
+echo '';
if (!$consultation->getSettings()->hideTitlePrefix) {
- echo ' ' . $privateComment . Html::encode($imotion->getFormattedTitlePrefix(LayoutHooks::CONTEXT_MOTION_LIST)) . ' ';
+ echo '' . Html::encode($imotion->getFormattedTitlePrefix(LayoutHooks::CONTEXT_MOTION_LIST)) . ' ';
}
echo '';
if ($consultation->getSettings()->hideTitlePrefix) {
- echo $privateComment;
+ echo '';
}
echo '