From f94c80b190dccfc5b579f75aa538ca9636e2b1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ho=CC=88=C3=9Fl?= Date: Sun, 14 Jul 2024 13:54:08 +0200 Subject: [PATCH] Motion filters now also affect amendments --- controllers/AmendmentController.php | 27 +++++++-------- controllers/admin/AmendmentController.php | 42 +++++++++++++---------- models/forms/AdminMotionFilterForm.php | 33 ++++++++++++++++++ views/admin/amendment/ods_list.php | 23 ++++++------- views/admin/amendment/ods_list_short.php | 18 ++++------ views/admin/amendment/pdf_list.php | 27 +++++++-------- views/admin/amendment/xlsx_list.php | 18 +++++----- 7 files changed, 107 insertions(+), 81 deletions(-) diff --git a/controllers/AmendmentController.php b/controllers/AmendmentController.php index e344ee40a..5d13e785f 100644 --- a/controllers/AmendmentController.php +++ b/controllers/AmendmentController.php @@ -17,7 +17,7 @@ use app\models\db\{Amendment, AmendmentAdminComment, AmendmentSupporter, ConsultationLog, ISupporter, Motion, User}; use app\models\events\AmendmentEvent; use app\models\exceptions\{FormError, MailNotSent, ResponseException}; -use app\models\forms\{AmendmentEditForm, ProposedChangeForm}; +use app\models\forms\{AdminMotionFilterForm, AmendmentEditForm, ProposedChangeForm}; use app\models\notifications\AmendmentProposedProcedure; use app\models\sectionTypes\ISectionType; use app\views\amendment\LayoutHelper; @@ -67,29 +67,26 @@ public function actionPdf(string $motionSlug, int $amendmentId): ResponseInterfa public function actionPdfcollection(int $inactive = 0): ResponseInterface { - $filter = IMotionStatusFilter::adminExport($this->consultation, ($inactive === 1)); - $motions = $filter->getFilteredConsultationIMotionsSorted(); - if (count($motions) === 0) { + $search = AdminMotionFilterForm::getForConsultationFromRequest($this->consultation, $this->consultation->motions, $this->getRequestValue('Search')); + $amendments = $search->getAmendmentsForExport($this->consultation, ($inactive === 1)); + + if (count($amendments) === 0) { return new HtmlErrorResponse(404, \Yii::t('motion', 'none_yet')); } - $amendments = []; - $motion = null; $texTemplate = null; - foreach ($motions as $motion) { - if (!is_a($motion, Motion::class) || $motion->getMyMotionType()->amendmentsOnly) { + $toShowAmendments = []; + foreach ($amendments as $amendmentGroups) { + if ($amendmentGroups['motion']->getMyMotionType()->amendmentsOnly) { continue; } // If we have multiple motion types, we just take the template from the first one. if ($texTemplate === null) { - $texTemplate = $motion->getMyMotionType()->texTemplate; + $texTemplate = $amendmentGroups['motion']->getMyMotionType()->texTemplate; } - $amendments = array_merge($amendments, $motion->getFilteredAmendments($filter)); - } - if (count($amendments) === 0) { - return new HtmlErrorResponse(404, \Yii::t('amend', 'none_yet')); + $toShowAmendments = array_merge($toShowAmendments, $amendmentGroups['amendments']); } - $selectedPdfLayout = IPDFLayout::getPdfLayoutForMotionType($motion->getMyMotionType()); + $selectedPdfLayout = IPDFLayout::getPdfLayoutForMotionType($amendments[0]['motion']->getMyMotionType()); $hasLaTeX = ($this->getParams()->xelatexPath || $this->getParams()->lualatexPath); if (!($hasLaTeX && $selectedPdfLayout->latexId !== null) && $selectedPdfLayout->id === null) { @@ -100,7 +97,7 @@ public function actionPdfcollection(int $inactive = 0): ResponseInterface $pdf = $this->renderPartial('pdf_collection_html2pdf', ['amendments' => $amendments]); } elseif ($selectedPdfLayout->latexId !== null) { $pdf = $this->renderPartial('pdf_collection_tex', [ - 'amendments' => $amendments, + 'amendments' => $toShowAmendments, 'texTemplate' => $texTemplate, ]); } else { diff --git a/controllers/admin/AmendmentController.php b/controllers/admin/AmendmentController.php index 04e9fa4cb..6803da54c 100644 --- a/controllers/admin/AmendmentController.php +++ b/controllers/admin/AmendmentController.php @@ -3,6 +3,7 @@ namespace app\controllers\admin; use app\models\consultationLog\ProposedProcedureChange; +use app\models\forms\AdminMotionFilterForm; use app\views\amendment\LayoutHelper as AmendmentLayoutHelper; use app\views\pdfLayouts\IPDFLayout; use app\models\http\{BinaryFileResponse, HtmlErrorResponse, HtmlResponse, RedirectResponse, ResponseInterface}; @@ -24,12 +25,12 @@ class AmendmentController extends AdminBase public function actionOdslist(bool $textCombined = false, int $inactive = 0): BinaryFileResponse { - $filter = IMotionStatusFilter::adminExport($this->consultation, ($inactive === 1)); + $search = AdminMotionFilterForm::getForConsultationFromRequest($this->consultation, $this->consultation->motions, $this->getRequestValue('Search')); + $amendments = $search->getAmendmentsForExport($this->consultation, ($inactive === 1)); $ods = $this->renderPartial('ods_list', [ - 'motions' => $filter->getFilteredConsultationIMotionsSorted(), + 'amendments' => $amendments, 'textCombined' => $textCombined, - 'filter' => $filter, ]); return new BinaryFileResponse(BinaryFileResponse::TYPE_ODS, $ods, true,'amendments'); @@ -37,12 +38,12 @@ public function actionOdslist(bool $textCombined = false, int $inactive = 0): Bi public function actionXlsxList(bool $textCombined = false, int $inactive = 0): BinaryFileResponse { - $filter = IMotionStatusFilter::adminExport($this->consultation, ($inactive === 1)); + $search = AdminMotionFilterForm::getForConsultationFromRequest($this->consultation, $this->consultation->motions, $this->getRequestValue('Search')); + $amendments = $search->getAmendmentsForExport($this->consultation, ($inactive === 1)); $ods = $this->renderPartial('xlsx_list', [ - 'motions' => $filter->getFilteredConsultationIMotionsSorted(), + 'amendments' => $amendments, 'textCombined' => $textCombined, - 'filter' => $filter, ]); return new BinaryFileResponse(BinaryFileResponse::TYPE_XLSX, $ods, true,'amendments'); @@ -50,36 +51,38 @@ public function actionXlsxList(bool $textCombined = false, int $inactive = 0): B public function actionOdslistShort(int $textCombined = 0, int $inactive = 0, int $maxLen = 2000): BinaryFileResponse { - $filter = IMotionStatusFilter::adminExport($this->consultation, ($inactive === 1)); + $search = AdminMotionFilterForm::getForConsultationFromRequest($this->consultation, $this->consultation->motions, $this->getRequestValue('Search')); + $amendments = $search->getAmendmentsForExport($this->consultation, ($inactive === 1)); $ods = $this->renderPartial('ods_list_short', [ - 'motions' => $filter->getFilteredConsultationIMotionsSorted(), + 'amendments' => $amendments, 'textCombined' => ($textCombined === 1), 'maxLen' => $maxLen, - 'filter' => $filter, ]); return new BinaryFileResponse(BinaryFileResponse::TYPE_ODS, $ods, true, 'amendments'); } public function actionPdflist(int $inactive = 0): HtmlResponse { - $filter = IMotionStatusFilter::adminExport($this->consultation, ($inactive === 1)); + $search = AdminMotionFilterForm::getForConsultationFromRequest($this->consultation, $this->consultation->motions, $this->getRequestValue('Search')); + $amendments = $search->getAmendmentsForExport($this->consultation, ($inactive === 1)); return new HtmlResponse( - $this->render('pdf_list', ['consultation' => $this->consultation, 'filter' => $filter]) + $this->render('pdf_list', ['consultation' => $this->consultation, 'amendments' => $amendments]) ); } public function actionPdfziplist(int $inactive = 0): BinaryFileResponse { - $filter = IMotionStatusFilter::adminExport($this->consultation, ($inactive === 1)); + $search = AdminMotionFilterForm::getForConsultationFromRequest($this->consultation, $this->consultation->motions, $this->getRequestValue('Search')); + $amendments = $search->getAmendmentsForExport($this->consultation, ($inactive === 1)); $zip = new ZipWriter(); - foreach ($filter->getFilteredConsultationMotionsSorted() as $motion) { - if ($motion->getMyMotionType()->amendmentsOnly || !$motion->getMyMotionType()->hasPdfLayout()) { + foreach ($amendments as $amendmentGroup) { + if ($amendmentGroup['motion']->getMyMotionType()->amendmentsOnly || !$amendmentGroup['motion']->getMyMotionType()->hasPdfLayout()) { continue; } - foreach ($motion->getFilteredAmendments($filter) as $amendment) { + foreach ($amendmentGroup['amendments'] as $amendment) { $selectedPdfLayout = IPDFLayout::getPdfLayoutForMotionType($amendment->getMyMotionType()); if ($selectedPdfLayout->id === IPDFLayout::LAYOUT_WEASYPRINT_DEFAULT) { $file = AmendmentLayoutHelper::createPdfFromHtml($amendment); @@ -97,14 +100,15 @@ public function actionPdfziplist(int $inactive = 0): BinaryFileResponse public function actionOdtziplist(int $inactive = 0): BinaryFileResponse { - $filter = IMotionStatusFilter::adminExport($this->consultation, ($inactive === 1)); + $search = AdminMotionFilterForm::getForConsultationFromRequest($this->consultation, $this->consultation->motions, $this->getRequestValue('Search')); + $amendments = $search->getAmendmentsForExport($this->consultation, ($inactive === 1)); $zip = new ZipWriter(); - foreach ($filter->getFilteredConsultationMotionsSorted() as $motion) { - if ($motion->getMyMotionType()->amendmentsOnly) { + foreach ($amendments as $amendmentGroup) { + if ($amendmentGroup['motion']->getMyMotionType()->amendmentsOnly) { continue; } - foreach ($motion->getFilteredAmendments($filter) as $amendment) { + foreach ($amendmentGroup['amendments'] as $amendment) { $doc = $amendment->getMyMotionType()->createOdtTextHandler(); LayoutHelper::printAmendmentToOdt($amendment, $doc); $zip->addFile($amendment->getFilenameBase(false) . '.odt', $doc->finishAndGetDocument()); diff --git a/models/forms/AdminMotionFilterForm.php b/models/forms/AdminMotionFilterForm.php index 47b01a916..5985a7658 100644 --- a/models/forms/AdminMotionFilterForm.php +++ b/models/forms/AdminMotionFilterForm.php @@ -1356,4 +1356,37 @@ public function getMotionsForExport(Consultation $consultation, ?int $motionType return $imotions; } + + /** + * Returns amendments + * If a filter is set via the motion filter, then this will return exactly what the motion list will show (only filtered by type). + * Otherwise, the inactive-flag will be considered. + * + * @return array + */ + public function getAmendmentsForExport(Consultation $consultation, bool $inactive): array + { + if ($this->isDefaultSettings()) { + $imotions = $this->getSorted(); + $filter = IMotionStatusFilter::adminExport($consultation, $inactive); + + $amendments = $filter->filterAmendments($imotions); + } else { + $allIMotions = $this->getSorted(); + $amendments = array_filter($allIMotions, fn(IMotion $IMotion) => is_a($IMotion, Amendment::class)); + } + + $filtered = []; + foreach ($amendments as $amendment) { + if (!isset($filtered[$amendment->motionId])) { + $filtered[$amendment->motionId] = [ + 'motion' => $amendment->getMyMotion(), + 'amendments' => [], + ]; + } + $filtered[$amendment->motionId]['amendments'][] = $amendment; + } + + return array_values($filtered); + } } diff --git a/views/admin/amendment/ods_list.php b/views/admin/amendment/ods_list.php index 8f3b0be20..bae4ca637 100644 --- a/views/admin/amendment/ods_list.php +++ b/views/admin/amendment/ods_list.php @@ -1,15 +1,14 @@ $amendments */ /** @var \app\controllers\Base $controller */ @@ -30,14 +29,14 @@ $hasAgendaItems = false; $hasResponsibilities = false; -foreach ($motions as $motion) { - if ($motion->getMyMotionType()->amendmentsOnly) { +foreach ($amendments as $amendmentGroup) { + if ($amendmentGroup['motion']->getMyMotionType()->amendmentsOnly) { continue; } - if ($motion->agendaItem) { + if ($amendmentGroup['motion']->agendaItem) { $hasAgendaItems = true; } - if ($motion->responsibilityId || $motion->responsibilityComment) { + if ($amendmentGroup['motion']->responsibilityId || $amendmentGroup['motion']->responsibilityComment) { $hasResponsibilities = true; } } @@ -113,7 +112,8 @@ $row = 3; -foreach ($motions as $motion) { +foreach ($amendments as $amendmentGroup) { + $motion = $amendmentGroup['motion']; if ($motion->getMyMotionType()->amendmentsOnly) { continue; } @@ -148,8 +148,7 @@ $doc->setCell($row, $COL_RESPONSIBILITY, Spreadsheet::TYPE_TEXT, implode(', ', $responsibility)); } - $amendments = $motion->getFilteredAndSortedAmendments($filter); - foreach ($amendments as $amendment) { + foreach ($amendmentGroup['amendments'] as $amendment) { $row++; $initiatorNames = []; diff --git a/views/admin/amendment/ods_list_short.php b/views/admin/amendment/ods_list_short.php index 6cd62b30c..2910e69e1 100644 --- a/views/admin/amendment/ods_list_short.php +++ b/views/admin/amendment/ods_list_short.php @@ -1,16 +1,15 @@ $amendments */ /** @var \app\controllers\Base $controller */ @@ -19,12 +18,9 @@ $DEBUG = false; -/** @var \app\models\settings\AntragsgruenApp $params */ -$params = Yii::$app->params; - /** @noinspection PhpUnhandledExceptionInspection */ $doc = new Spreadsheet([ - 'tmpPath' => $params->getTmpDir(), + 'tmpPath' => \app\models\settings\AntragsgruenApp::getInstance()->getTmpDir(), 'trustHtml' => true, ]); @@ -82,7 +78,8 @@ $row = 3; -foreach ($motions as $motion) { +foreach ($amendments as $amendmentGroup) { + $motion = $amendmentGroup['motion']; if ($motion->getMyMotionType()->amendmentsOnly) { continue; } @@ -102,8 +99,7 @@ $title = HTMLTools::correctHtmlErrors($title); $doc->setCell($row, $COL_PREFIX, Spreadsheet::TYPE_HTML, $title, null, ['fo:wrap-option' => 'no-wrap']); - $amendments = $motion->getFilteredAndSortedAmendments($filter); - foreach ($amendments as $amendment) { + foreach ($amendmentGroup['amendments'] as $amendment) { $change = ''; if ($amendment->changeEditorial !== '') { $change .= '

' . Yii::t('amend', 'editorial_hint') . '

'; diff --git a/views/admin/amendment/pdf_list.php b/views/admin/amendment/pdf_list.php index 20e78ce0b..26f2f5af5 100644 --- a/views/admin/amendment/pdf_list.php +++ b/views/admin/amendment/pdf_list.php @@ -2,11 +2,11 @@ /** * @var yii\web\View $this - * @var \app\models\db\Consultation $consultation - * @var IMotionStatusFilter $filter + * @var array $amendments */ -use app\components\{IMotionStatusFilter, UrlHelper}; +use app\models\db\{Amendment, Motion}; +use app\components\UrlHelper; use yii\helpers\Html; /** @var \app\controllers\Base $controller */ @@ -21,19 +21,16 @@ echo '

' . Yii::t('admin', 'amend_pdf_list') . '

'; -foreach ($filter->getFilteredConsultationMotionsSorted() as $motion) { - $amendments = $motion->getFilteredAmendments($filter); - if (count($amendments) > 0) { - echo '

' . Html::encode($motion->getTitleWithPrefix()) . '

'; - echo '
    '; - foreach ($amendments as $amendment) { - echo '
  • '; - $url = UrlHelper::createAmendmentUrl($amendment, 'pdf'); - echo Html::a($amendment->titlePrefix, $url, ['class' => 'amendment' . $amendment->id]); - echo '
  • '; - } - echo '
'; +foreach ($amendments as $data) { + echo '

' . Html::encode($data['motion']->getTitleWithPrefix()) . '

'; + echo '
    '; + foreach ($data['amendments'] as $amendment) { + echo '
  • '; + $url = UrlHelper::createAmendmentUrl($amendment, 'pdf'); + echo Html::a($amendment->titlePrefix, $url, ['class' => 'amendment' . $amendment->id]); + echo '
  • '; } + echo '
'; } echo '
'; diff --git a/views/admin/amendment/xlsx_list.php b/views/admin/amendment/xlsx_list.php index 8bca11817..d9d0eb988 100644 --- a/views/admin/amendment/xlsx_list.php +++ b/views/admin/amendment/xlsx_list.php @@ -1,7 +1,7 @@ $amendments */ /** @var \app\controllers\Base $controller */ @@ -35,7 +34,8 @@ $hasResponsibilities = false; $hasLikes = false; $hasDislikes = false; -foreach ($motions as $motion) { +foreach ($amendments as $amendmentGroup) { + $motion = $amendmentGroup['motion']; if ($motion->getMyMotionType()->amendmentsOnly) { continue; } @@ -150,7 +150,8 @@ $row = 3; $htmlHelper = new PhpOffice\PhpSpreadsheet\Helper\Html(); -foreach ($motions as $motion) { +foreach ($amendments as $amendmentGroup) { + $motion = $amendmentGroup['motion']; if ($motion->getMyMotionType()->amendmentsOnly) { continue; } @@ -184,8 +185,7 @@ $sheet->setCellValue($COL_RESPONSIBILITY . $row, implode(', ', $responsibility)); } - $amendments = $motion->getFilteredAndSortedAmendments($filter); - foreach ($amendments as $amendment) { + foreach ($amendmentGroup['amendments'] as $amendment) { $row++; // $sheet->getRowDimension($row)->setRowHeight(5, 'cm');