Skip to content

Commit

Permalink
Motion filters now also affect exports
Browse files Browse the repository at this point in the history
  • Loading branch information
CatoTH committed Jul 14, 2024
1 parent 6841aeb commit 5291a5e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 34 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 4.14.1 [not released yet]

- If a filter is set in the motion list, this will also filter the motions to be exported in the export row above (PDFs, ODTs, ODS etc.).
- Bugfix: The PDF export with included proposed procedures was sometimes broken.
- Bugfix: When multiple versions of a motion exist, the ODT / PDF export list showed all versions, instead of only the newest one.
- Bugfix: If a motion replaces one of a different consultation, editing as an admin removed the connection between these two motion versions.
Expand Down
9 changes: 7 additions & 2 deletions controllers/MotionExportTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,13 @@ private function getMotionsAndTemplate(string $motionTypeId, bool $inactive, boo
{
/** @var TexTemplate $texTemplate */
$texTemplate = null;
// @TODO Maybe support multiple motion types
$imotions = AdminMotionFilterForm::getMotionsForExport($this->consultation, (int) $motionTypeId, $inactive);

$search = AdminMotionFilterForm::getForConsultationFromRequest(
$this->consultation,
$this->consultation->motions,
$this->getRequestValue('Search')
);
$imotions = $search->getMotionsForExport($this->consultation, (int) $motionTypeId, $inactive);

if ($motionTypeId !== '' && $motionTypeId !== '0') {
$motionTypeIds = explode(',', $motionTypeId);
Expand Down
40 changes: 24 additions & 16 deletions controllers/admin/MotionListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ protected function actionListallProposalAmendments(): void
}
}

/**
* @param Motion[] $motions
*/
private function getSearchForm(array $motions): AdminMotionFilterForm
{
return AdminMotionFilterForm::getForConsultationFromRequest(
$this->consultation,
$motions,
$this->getRequestValue('Search')
);
}

public function actionIndex(?string $motionId = null): ResponseInterface
{
Expand Down Expand Up @@ -243,21 +254,12 @@ public function actionIndex(?string $motionId = null): ResponseInterface
$motions = $consultation->motions;
}

/** @var AdminMotionFilterForm $search */
$search = new $motionListClass($consultation, $motions, $privilegeScreening);
if ($this->isRequestSet('reset')) {
RequestContext::getSession()->set('motionListSearch' . $consultation->id, null);
return new RedirectResponse(UrlHelper::createUrl('/admin/motion-list/index'));
}
if ($this->getRequestValue('Search')) {
$attributes = $this->getRequestValue('Search');
RequestContext::getSession()->set('motionListSearch' . $consultation->id, $attributes);
$search->setAttributes($attributes);
} elseif (RequestContext::getSession()->get('motionListSearch' . $consultation->id)) {
$search->setAttributes(RequestContext::getSession()->get('motionListSearch' . $consultation->id));
}
$search = $this->getSearchForm($motions);

/** @var AdminMotionFilterForm $search */
return new HtmlResponse($this->render('list_all', [
'motionId' => $motionId,
'entries' => $search->getSorted(),
Expand Down Expand Up @@ -312,7 +314,8 @@ public function actionMotionOdslistall(bool $inactive): BinaryFileResponse

public function actionMotionOdslist(int $motionTypeId, bool $textCombined = false, int $inactive = 0): ResponseInterface
{
$imotions = AdminMotionFilterForm::getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));
$search = $this->getSearchForm($this->consultation->motions);
$imotions = $search->getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));
$motionType = $this->consultation->getMotionType($motionTypeId);

$filename = Tools::sanitizeFilename($motionType->titlePlural, false);
Expand All @@ -326,7 +329,8 @@ public function actionMotionOdslist(int $motionTypeId, bool $textCombined = fals

public function actionMotionOpenslides(int $motionTypeId, int $version = 1): ResponseInterface
{
$imotions = AdminMotionFilterForm::getMotionsForExport($this->consultation, $motionTypeId, false);
$search = $this->getSearchForm($this->consultation->motions);
$imotions = $search->getMotionsForExport($this->consultation, $motionTypeId, false);
$motionType = $this->consultation->getMotionType($motionTypeId);

$filename = Tools::sanitizeFilename($motionType->titlePlural, false);
Expand All @@ -345,7 +349,8 @@ public function actionMotionOpenslides(int $motionTypeId, int $version = 1): Res

public function actionMotionCommentsXlsx(int $motionTypeId, int $inactive = 0): ResponseInterface
{
$imotions = AdminMotionFilterForm::getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));
$search = $this->getSearchForm($this->consultation->motions);
$imotions = $search->getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));
$motionType = $this->consultation->getMotionType($motionTypeId);

$filename = Tools::sanitizeFilename(\Yii::t('export', 'Kommentare') . '-' . $motionType->titlePlural, false);
Expand All @@ -358,7 +363,8 @@ public function actionMotionCommentsXlsx(int $motionTypeId, int $inactive = 0):

public function actionMotionPdfziplist(int $motionTypeId = 0, int $inactive = 0): ResponseInterface
{
$imotions = AdminMotionFilterForm::getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));
$search = $this->getSearchForm($this->consultation->motions);
$imotions = $search->getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));

$zip = new ZipWriter();
foreach ($imotions as $imotion) {
Expand Down Expand Up @@ -394,7 +400,8 @@ public function actionMotionPdfziplist(int $motionTypeId = 0, int $inactive = 0)

public function actionMotionOdtziplist(int $motionTypeId = 0, int $inactive = 0): ResponseInterface
{
$imotions = AdminMotionFilterForm::getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));
$search = $this->getSearchForm($this->consultation->motions);
$imotions = $search->getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));

$zip = new ZipWriter();
foreach ($imotions as $imotion) {
Expand All @@ -415,7 +422,8 @@ public function actionMotionOdtziplist(int $motionTypeId = 0, int $inactive = 0)

public function actionMotionOdtall(int $motionTypeId = 0, int $inactive = 0): ResponseInterface
{
$imotions = AdminMotionFilterForm::getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));
$search = $this->getSearchForm($this->consultation->motions);
$imotions = $search->getMotionsForExport($this->consultation, $motionTypeId, ($inactive === 1));

$doc = $imotions[0]->getMyMotionType()->createOdtTextHandler();

Expand Down
56 changes: 43 additions & 13 deletions models/forms/AdminMotionFilterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use app\models\settings\{AntragsgruenApp, PrivilegeQueryContext, Privileges};
use app\models\exceptions\{ExceptionBase, ResponseException};
use app\models\http\HtmlErrorResponse;
use app\components\{IMotionStatusFilter, Tools, UrlHelper};
use app\components\{IMotionStatusFilter, RequestContext, Tools, UrlHelper};
use app\models\db\{Amendment, AmendmentSupporter, Consultation, ConsultationSettingsTag, IMotion, ISupporter, Motion, MotionSupporter, User};
use yii\helpers\Html;

Expand Down Expand Up @@ -63,6 +63,25 @@ public static function getClassToUse(): string
return AdminMotionFilterForm::class;
}

/**
* @param Motion[] $motions
*/
public static function getForConsultationFromRequest(Consultation $consultation, array $motions, ?array $searchParams): AdminMotionFilterForm
{
$motionListClass = AdminMotionFilterForm::getClassToUse();
$privilegeScreening = User::havePrivilege($consultation, Privileges::PRIVILEGE_SCREENING, PrivilegeQueryContext::anyRestriction());

$search = new $motionListClass($consultation, $motions, $privilegeScreening);
if ($searchParams) {
RequestContext::getSession()->set('motionListSearch' . $consultation->id, $searchParams);
$search->setAttributes($searchParams);
} elseif (RequestContext::getSession()->get('motionListSearch' . $consultation->id)) {
$search->setAttributes(RequestContext::getSession()->get('motionListSearch' . $consultation->id));
}

return $search;
}

/**
* @param Motion[] $allMotions
*/
Expand Down Expand Up @@ -177,13 +196,18 @@ public function setCurrentRoute(array $route): void
$this->route = $route;
}

public function getCurrentUrl(array $add = []): string
public function getSearchUrlParams(): array
{
$attributes = [];
foreach ($this->getAttributes() as $key => $val) {
$attributes['Search[' . $key . ']'] = $val;
}
return UrlHelper::createUrl(array_merge($this->route, $attributes, $add));
return $attributes;
}

public function getCurrentUrl(array $add = []): string
{
return UrlHelper::createUrl(array_merge($this->route, $this->getSearchUrlParams(), $add));
}

private ?array $versionNames = null;
Expand Down Expand Up @@ -1281,10 +1305,12 @@ public function showListActions(): string

/**
* Returns motions and statute 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 IMotion[]
*/
public static function getMotionsForExport(Consultation $consultation, ?int $motionTypeId, bool $inactive): array
public function getMotionsForExport(Consultation $consultation, ?int $motionTypeId, bool $inactive): array
{
if ($motionTypeId) {
try {
Expand All @@ -1294,19 +1320,23 @@ public static function getMotionsForExport(Consultation $consultation, ?int $mot
}
}

$privilegeScreening = User::havePrivilege($consultation, Privileges::PRIVILEGE_SCREENING, PrivilegeQueryContext::anyRestriction());
if ($this->isDefaultSettings()) {
if ($motionTypeId > 0) {
$this->motionType = $motionTypeId;
}

$motionListClass = AdminMotionFilterForm::getClassToUse();
$search = new $motionListClass($consultation, $consultation->motions, $privilegeScreening);
if ($motionTypeId > 0) {
$search->motionType = $motionTypeId;
}
$imotions = $this->getSorted();
$filter = IMotionStatusFilter::adminExport($consultation, $inactive);
$allIMotions = $filter->filterIMotions($imotions);
} else {
if ($motionTypeId > 0) {
$this->motionType = $motionTypeId;
}

$imotions = $search->getSorted();
$allIMotions = $this->getSorted();
}

try {
$filter = IMotionStatusFilter::adminExport($consultation, $inactive);
$allIMotions = $filter->filterIMotions($imotions);
if (count($allIMotions) === 0) {
throw new ResponseException(new HtmlErrorResponse(404, \Yii::t('motion', 'none_yet')));
}
Expand Down
9 changes: 7 additions & 2 deletions views/admin/motion-list/_list_all_export.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
/**
* @var \app\controllers\Base $controller
* @var Yii\web\View $this
* @var AdminMotionFilterForm $search
* @var bool $hasProposedProcedures
* @var bool $hasResponsibilities
*/

use app\models\forms\AdminMotionFilterForm;
use app\models\settings\Privileges;
use app\components\{HTMLTools, UrlHelper};
use app\models\settings\AntragsgruenApp;
use yii\helpers\Html;

$controller = $this->context;
Expand All @@ -19,9 +20,13 @@
$hasOpenslides = $consultation->getSettings()->openslidesExportEnabled;
$hasInactiveFunctionality = (!$hasResponsibilities || !$hasProposedProcedures || !$hasOpenslides);

$getExportLinkLi = function ($title, $route, $motionTypeId, $cssClass) {
$getExportLinkLi = function ($title, $route, $motionTypeId, $cssClass) use ($search) {
$params = array_merge($route, ['motionTypeId' => $motionTypeId, 'inactive' => '0']);
$paramsTmpl = array_merge($route, ['motionTypeId' => $motionTypeId, 'inactive' => 'INACTIVE']);
if (!$search->isDefaultSettings()) {
$params = array_merge($params, $search->getSearchUrlParams());
$paramsTmpl = array_merge($paramsTmpl, $search->getSearchUrlParams());
}
if ($route[0] === 'amendment/pdfcollection') {
$params['filename'] = Yii::t('con', 'feed_amendments') . '.pdf';
$paramsTmpl['filename'] = Yii::t('con', 'feed_amendments') . '.pdf';
Expand Down
3 changes: 2 additions & 1 deletion views/admin/motion-list/list_all.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@

echo $this->render('_list_all_export', [
'hasProposedProcedures' => $hasProposedProcedures,
'hasResponsibilities' => $hasResponsibilities,
'hasResponsibilities' => $hasResponsibilities,
'search' => $search,
]);

echo '<div class="content" data-antragsgruen-widget="backend/MotionList">';
Expand Down

0 comments on commit 5291a5e

Please sign in to comment.