Skip to content

Commit

Permalink
#1226 Refactoring MultiSubForm
Browse files Browse the repository at this point in the history
  • Loading branch information
j3nsch committed Jun 7, 2024
1 parent 8edba7e commit 587eec2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 25 deletions.
55 changes: 55 additions & 0 deletions modules/admin/forms/Document/GndSubjects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the Licence, or any later version.
* OPUS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2024, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

use Opus\Common\DocumentInterface;

/**
* Form for adding GND subjects to document metadata.
*/
class Admin_Form_Document_GndSubjects extends Admin_Form_Document_SubjectType
{
public function __construct()
{
parent::__construct(
'swd',
[
'columns' => [
[],
['label' => 'Opus_Subject_Value'],
['label' => 'ExternalKey'],
],
]
);

$this->addElement('textarea', 'keywords');
}

}
35 changes: 24 additions & 11 deletions modules/admin/forms/Document/MultiSubForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class Admin_Form_Document_MultiSubForm extends Admin_Form_AbstractDocumentSubFor
/** @var array */
private $columns;

/** @var Zend_Form_SubForm */
private $valuesSubform;

/** @var Zend_Form_SubForm */
private $headerSubform;

/** @var Zend_Form_SubForm */
private $footerSubform;

/**
* Konstruiert Instanz von Fomular.
*
Expand Down Expand Up @@ -138,6 +147,10 @@ public function init()
} else {
$this->getDecorator('FieldsetWithButtons')->setLegendButtons(self::ELEMENT_ADD);
}

$this->valuesSubform = new Zend_Form_SubForm();
$this->valuesSubform->setDecorators(['FormElements']);
$this->addSubForm($this->valuesSubform, 'Values');
}

protected function initButton()
Expand Down Expand Up @@ -344,7 +357,7 @@ protected function addSubFormAndFixOrder($position)
$subForm->setOrder($position);

$this->setOddEven($subForm);
$this->addSubForm($subForm, $this->getSubFormBaseName() . $position);
$this->valuesSubform->addSubForm($subForm, $this->getSubFormBaseName() . $position);

return $subForm;
}
Expand All @@ -355,7 +368,7 @@ protected function addSubFormAndFixOrder($position)
*/
public function removeSubForm($name)
{
$result = parent::removeSubForm($name);
$result = $this->valuesSubform->removeSubForm($name);
$this->removeGapsInSubFormOrder();
return $result;
}
Expand Down Expand Up @@ -510,9 +523,9 @@ public function createNewSubFormInstance()
*/
protected function removeSubFormAndFixOrder($name)
{
$order = $this->getSubForm($name)->getOrder();
$order = $this->valuesSubform->getSubForm($name)->getOrder();

$this->removeSubForm($name);
$this->valuesSubform->removeSubForm($name);
$this->removeGapsInSubFormOrder();

return $order;
Expand All @@ -531,7 +544,7 @@ protected function removeSubFormAndFixOrder($name)
*/
protected function removeGapsInSubFormOrder()
{
$subforms = $this->getSubForms();
$subforms = $this->valuesSubform->getSubForms();

$renamedSubforms = [];

Expand All @@ -545,7 +558,7 @@ protected function removeGapsInSubFormOrder()
$pos++;
}

$this->setSubForms($renamedSubforms);
$this->valuesSubform->setSubForms($renamedSubforms);
}

/**
Expand All @@ -555,7 +568,7 @@ protected function removeGapsInSubFormOrder()
*/
public function appendSubForm()
{
$subforms = $this->getSubForms();
$subforms = $this->valuesSubform->getSubForms();

return $this->addSubFormAndFixOrder(count($subforms));
}
Expand All @@ -572,18 +585,18 @@ public function appendSubForm()
*/
public function determineSubFormForAnchor($removedPosition)
{
$subforms = $this->getSubForms();
$subforms = $this->valuesSubform->getSubForms();

$subformCount = count($subforms);

if ($subformCount === 0) {
return $this;
} elseif ($removedPosition < $subformCount) {
$keys = array_keys($subforms);
return $this->getSubForm($keys[$removedPosition]);
return $this->valuesSubform->getSubForm($keys[$removedPosition]);
} else {
$keys = array_keys($subforms);
return $this->getSubForm($keys[$subformCount - 1]);
return $this->valuesSubform->getSubForm($keys[$subformCount - 1]);
}
}

Expand Down Expand Up @@ -637,7 +650,7 @@ public function isValid($data, $context = null)
*/
public function isEmpty()
{
return count($this->getSubForms()) === 0;
return count($this->valuesSubform->getSubForms()) === 0;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion modules/admin/forms/Document/PersonRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ protected function insertSubForm($subForm, $position)
*/
public function sortSubFormsBySortOrder()
{
$subforms = $this->getSubForms();
if ($this->valuesSubform !== null) {
$subforms = $this->valuesSubform->getSubForms();
} else {
$subforms = [];
}

$digitsOrder = strlen(count($subforms));
$maxSortOrder = $this->getMaxSortOrder($subforms);
Expand Down
14 changes: 1 addition & 13 deletions modules/admin/forms/Document/Subjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,7 @@ public function init()
{
parent::init();

$this->addSubForm(
new Admin_Form_Document_SubjectType(
'swd',
[
'columns' => [
[],
['label' => 'Opus_Subject_Value'],
['label' => 'ExternalKey'],
],
]
),
'Swd'
);
$this->addSubForm(new Admin_Form_Document_GndSubjects(), 'Swd');

$this->addSubForm(
new Admin_Form_Document_SubjectType(
Expand Down

0 comments on commit 587eec2

Please sign in to comment.