Skip to content

Commit

Permalink
Motion filters now also affect amendments
Browse files Browse the repository at this point in the history
  • Loading branch information
CatoTH committed Jul 14, 2024
1 parent 5291a5e commit f94c80b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 81 deletions.
27 changes: 12 additions & 15 deletions controllers/AmendmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
42 changes: 23 additions & 19 deletions controllers/admin/AmendmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -24,62 +25,64 @@ 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');
}

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');
}

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);
Expand All @@ -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());
Expand Down
33 changes: 33 additions & 0 deletions models/forms/AdminMotionFilterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{motion: Motion, amendments: Amendment[]}>
*/
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);

Check failure on line 1390 in models/forms/AdminMotionFilterForm.php

View workflow job for this annotation

GitHub Actions / evaluate-pr

Method app\models\forms\AdminMotionFilterForm::getAmendmentsForExport() should return array<array{motion: app\models\db\Motion, amendments: array<app\models\db\Amendment>}> but returns array<int, array{motion: app\models\db\Motion|null, amendments: non-empty-array<int<0, max>, app\models\db\Amendment>}>.
}
}
23 changes: 11 additions & 12 deletions views/admin/amendment/ods_list.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

use app\models\sectionTypes\TextSimpleCommon;
use app\components\{HTMLTools, IMotionStatusFilter};
use app\models\db\{AmendmentSection, Motion};
use app\components\HTMLTools;
use app\models\db\{Amendment, AmendmentSection, Motion};
use CatoTH\HTML2OpenDocument\Spreadsheet;
use yii\helpers\Html;

/**
* @var $this yii\web\View
* @var Motion[] $motions
* @var IMotionStatusFilter $filter
* @var yii\web\View $this
* @var array<array{motion: Motion, amendments: Amendment[]}> $amendments
*/

/** @var \app\controllers\Base $controller */
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -113,7 +112,8 @@

$row = 3;

foreach ($motions as $motion) {
foreach ($amendments as $amendmentGroup) {
$motion = $amendmentGroup['motion'];
if ($motion->getMyMotionType()->amendmentsOnly) {
continue;
}
Expand Down Expand Up @@ -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 = [];
Expand Down
18 changes: 7 additions & 11 deletions views/admin/amendment/ods_list_short.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

use app\components\{HTMLTools, IMotionStatusFilter};
use app\models\db\Motion;
use app\components\HTMLTools;
use app\models\db\{Amendment, Motion};
use CatoTH\HTML2OpenDocument\Spreadsheet;
use yii\helpers\Html;

/**
* @var yii\web\View $this
* @var Motion[] $motions
* @var IMotionStatusFilter $filter
* @var bool $textCombined
* @var int $maxLen
* @var array<array{motion: Motion, amendments: Amendment[]}> $amendments
*/

/** @var \app\controllers\Base $controller */
Expand All @@ -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,
]);

Expand Down Expand Up @@ -82,7 +78,8 @@

$row = 3;

foreach ($motions as $motion) {
foreach ($amendments as $amendmentGroup) {
$motion = $amendmentGroup['motion'];
if ($motion->getMyMotionType()->amendmentsOnly) {
continue;
}
Expand All @@ -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 .= '<h4>' . Yii::t('amend', 'editorial_hint') . '</h4>';
Expand Down
27 changes: 12 additions & 15 deletions views/admin/amendment/pdf_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

/**
* @var yii\web\View $this
* @var \app\models\db\Consultation $consultation
* @var IMotionStatusFilter $filter
* @var array<array{motion: Motion, amendments: Amendment[]}> $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 */
Expand All @@ -21,19 +21,16 @@
echo '<h1>' . Yii::t('admin', 'amend_pdf_list') . '</h1>
<div class="content">';

foreach ($filter->getFilteredConsultationMotionsSorted() as $motion) {
$amendments = $motion->getFilteredAmendments($filter);
if (count($amendments) > 0) {
echo '<h2>' . Html::encode($motion->getTitleWithPrefix()) . '</h2>';
echo '<ul>';
foreach ($amendments as $amendment) {
echo '<li>';
$url = UrlHelper::createAmendmentUrl($amendment, 'pdf');
echo Html::a($amendment->titlePrefix, $url, ['class' => 'amendment' . $amendment->id]);
echo '</li>';
}
echo '</ul>';
foreach ($amendments as $data) {
echo '<h2>' . Html::encode($data['motion']->getTitleWithPrefix()) . '</h2>';
echo '<ul>';
foreach ($data['amendments'] as $amendment) {
echo '<li>';
$url = UrlHelper::createAmendmentUrl($amendment, 'pdf');
echo Html::a($amendment->titlePrefix, $url, ['class' => 'amendment' . $amendment->id]);
echo '</li>';
}
echo '</ul>';
}

echo '</div>';
Loading

0 comments on commit f94c80b

Please sign in to comment.