Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NDB_Page] Add default maxYear for date elements #9498

Merged
merged 12 commits into from
Dec 10, 2024
14 changes: 0 additions & 14 deletions php/libraries/NDB_BVL_Instrument.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ abstract class NDB_BVL_Instrument extends NDB_Page
*/
protected $jsonData = false;

/**
* The form object for this instrument.
*
* @var LorisForm
*/
public $form;

/**
* Variable defining the type of form in use
*
Expand All @@ -40,13 +33,6 @@ abstract class NDB_BVL_Instrument extends NDB_Page
*/
public $formType;

/**
* Array holding the date format
*
* @var array
*/
public $dateOptions;

/**
* Type of instrument. Can be 'normal' or 'DirectEntry'
*
Expand Down
14 changes: 7 additions & 7 deletions php/libraries/NDB_Page.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
* The default dateOptions to send to LorisForm to render elements
* on this page. This is usually set by subclasses of NDB_Page which
* need to modify the options to show on a date (ie. min year, max year,
* etc.)
* etc.). Default values can be added here, which can be overriden.
*
* @var array
*/
public $dateOptions;
public $dateOptions = [
'maxYear' => '9999'
];

/**
* Does the setup required for this page. By default, sets up elements
Expand Down Expand Up @@ -315,9 +317,7 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
'style' => 'max-width:33%; display:inline-block;',
]
) {
if ($options === [] && !empty($this->dateOptions)) {
$options = $this->dateOptions;
}
$options = ($options ?? []) + $this->dateOptions;
$this->form->addElement('date', $field, $label, $options, $attribs);
}

Expand Down Expand Up @@ -592,7 +592,7 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
function createDate(
$field,
$label,
$dateOptions=null,
$dateOptions=[],
$attribs=[
'class' => 'form-control input-sm',
'style' => 'max-width:33%; display:inline-block;',
Expand All @@ -602,7 +602,7 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
"date",
$field,
$label,
$dateOptions,
($dateOptions ?? []) + $this->dateOptions,
$attribs
);
}
Expand Down
5 changes: 4 additions & 1 deletion test/unittests/NDB_BVL_Instrument_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ function testAddCustomDateElement()
'type' => 'date',
'html' => $this->_instrument->form
->renderElement($groupEl['elements'][0]),
'options' => ['value' => 'Option']
'options' => [
'value' => 'Option',
'maxYear' => '9999'
]
],
[
'label' => null,
Expand Down
22 changes: 16 additions & 6 deletions test/unittests/NDB_PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function testAddBasicTextArea()
/**
* Test that addBasicDate calls addElement from LorisForm and properly adds
* an element to the page's form when dateOptions is not set. Since
* dateOptions is not set, the options array should remain empty.
* dateOptions is not set, the options array should have the default maxYear.
*
* @covers NDB_Page::addBasicDate
* @return void
Expand All @@ -256,7 +256,9 @@ public function testAddBasicDateWithNoDateOptions()
'label' => 'test_label',
'type' => 'date',
'class' => 'form-control input-sm',
'options' => []
'options' => [
'maxYear' => '9999'
]
],
$this->_page->form->form['test_name']
);
Expand All @@ -265,21 +267,27 @@ public function testAddBasicDateWithNoDateOptions()
/**
* Test that addBasicDate calls addElement from LorisForm and properly adds
* an element to the page's form when dateOptions is set. Since
* dateOptions is set, the options array should have information in it.
* dateOptions is set, the options array should have that information in it
*
* @covers NDB_Page::addBasicDate
* @return void
*/
public function testAddBasicDateWithDateOptionsSet()
{
$this->_page->dateOptions = ['someOption' => 'true'];
$this->_page->dateOptions = [
'someOption' => 'true',
'maxYear' => '2028'
];
$this->_page->addBasicDate("test_name", "test_label");
$this->assertEquals(
['name' => 'test_name',
'label' => 'test_label',
'type' => 'date',
'class' => 'form-control input-sm',
'options' => ['someOption' => 'true']
'options' => [
'someOption' => 'true',
'maxYear' => '2028'
]
],
$this->_page->form->form['test_name']
);
Expand Down Expand Up @@ -603,7 +611,9 @@ public function testCreateDate()
'label' => 'test_label',
'type' => 'date',
'class' => 'form-control input-sm',
'options' => null
'options' => [
'maxYear' => '9999'
]
],
$this->_page->createDate("test_field", "test_label")
);
Expand Down
Loading