Skip to content

Commit

Permalink
Fix checkbox fields with default active (could not be disabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Jun 8, 2018
1 parent 86601b7 commit 1d3ab2c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
32 changes: 14 additions & 18 deletions src/system/Papaya/Ui/Dialog/Field.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<?php
/**
* Superclass for dialog fields
*
* @copyright 2010 by papaya Software GmbH - All rights reserved.
* @link http://www.papaya-cms.com/
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
*
* You can redistribute and/or modify this script under the terms of the GNU General Public
* License (GPL) version 2, provided that the copyright and license notes, including these
* lines, remain unmodified. papaya 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.
*
* @package Papaya-Library
* @subpackage Ui
* @version $Id: Field.php 39126 2014-02-06 16:34:04Z weinert $
*/
* papaya CMS
*
* @copyright 2000-2018 by papayaCMS project - All rights reserved.
* @link http://www.papaya-cms.com/
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
*
* You can redistribute and/or modify this script under the terms of the GNU General Public
* License (GPL) version 2, provided that the copyright and license notes, including these
* lines, remain unmodified. papaya 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.
*/

/**
* Superclass for dialog fields
Expand Down Expand Up @@ -223,7 +219,7 @@ public function setDefaultValue($defaultValue) {
/**
* Get the default value for the field.
*
* @return string
* @return mixed
*/
public function getDefaultValue() {
return $this->_defaultValue;
Expand Down Expand Up @@ -466,4 +462,4 @@ protected function _getFieldClass($prefix = 'PapayaUi') {
}
return $class;
}
}
}
79 changes: 49 additions & 30 deletions src/system/Papaya/Ui/Dialog/Field/Input/Checkbox.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
<?php
/**
* A checkbox for an active/inactive value
*
* Creates a dialog field for time input.
*
* @copyright 2010 by papaya Software GmbH - All rights reserved.
* @link http://www.papaya-cms.com/
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
*
* You can redistribute and/or modify this script under the terms of the GNU General Public
* License (GPL) version 2, provided that the copyright and license notes, including these
* lines, remain unmodified. papaya 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.
*
* @package Papaya-Library
* @subpackage Ui
* @version $Id: Checkbox.php 39403 2014-02-27 14:25:16Z weinert $
*/
* papaya CMS
*
* @copyright 2000-2018 by papayaCMS project - All rights reserved.
* @link http://www.papaya-cms.com/
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
*
* You can redistribute and/or modify this script under the terms of the GNU General Public
* License (GPL) version 2, provided that the copyright and license notes, including these
* lines, remain unmodified. papaya 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.
*/

/**
* A checkbox for an active/inactive value
Expand All @@ -43,7 +37,7 @@ class PapayaUiDialogFieldInputCheckbox extends PapayaUiDialogFieldInput {
/**
* Field type, used in template
*
* @var string
* @var array
*/
protected $_values = array(
'active' => TRUE,
Expand Down Expand Up @@ -97,7 +91,7 @@ public function appendTo(PapayaXmlElement $parent) {
),
(string)$this->_values['active']
);
if ($currentValue == $this->_values['active']) {
if ((string)$currentValue === (string)$this->_values['active']) {
$input->setAttribute('checked', 'checked');
}
return $field;
Expand All @@ -116,7 +110,7 @@ public function setValues($active, $inactive) {
'The active value can not be empty.'
);
}
if ($active == $inactive) {
if ((string)$active === (string)$inactive) {
throw new InvalidArgumentException(
'The active value and the inactive value must be different.'
);
Expand All @@ -137,13 +131,39 @@ public function setValues($active, $inactive) {
* @return mixed
*/
public function getCurrentValue() {
$value = parent::getCurrentValue();
if (empty($value) || $value === $this->_values['inactive']) {
return $this->_values['inactive'];
} elseif ($value == $this->_values['active']) {
return $this->_values['active'];
$name = $this->getName();
if (!empty($name) && ($dialog = $this->getDialog())) {
if ($this->getDisabled()) {
return $this->getDefaultValue();
}
if ($dialog->isSubmitted()) {
if (
$dialog->parameters()->has($name) &&
$dialog->parameters()->get($name) === (string)$this->_values['active']
) {
$isActive = TRUE;
} else {
$isActive = FALSE;
}
} elseif ($dialog->data()->has($name)) {
$isActive = $dialog->data()->get($name) === (string)$this->_values['active'];
} else {
$isActive = $this->getDefaultValue() === (string)$this->_values['active'];
}
return $this->_values[$isActive ? 'active' : 'inactive'];
}
return $this->_values['inactive'];
return $this->getDefaultValue();
}

/**
* Get the default value for the field.
*
* @return string
*/
public function getDefaultValue() {
$value = parent::getDefaultValue();
$isActive = (string)$value === (string)$this->_values['active'];
return $this->_values[$isActive ? 'active' : 'inactive'];
}

/**
Expand All @@ -155,8 +175,7 @@ public function getCurrentValue() {
public function getFilter() {
if ($this->getMandatory()) {
return parent::getFilter();
} else {
return NULL;
}
return NULL;
}
}
}
6 changes: 3 additions & 3 deletions tests/system/Papaya/Ui/Dialog/Field/Input/CheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ public function testImplicitFilterExpectingFalse($value, $mandatory) {
* @coversPapayaUiDialogFieldInputCheckbox::getCurrentValue
* @dataProvider provideCheckboxValues
* @param mixed $expected
* @param mixed $current
* @param mixed $default
* @param mixed $active
* @param mixed $inactive
*/
public function testGetCurrentValue($expected, $current, $active, $inactive) {
$checkbox = new PapayaUiDialogFieldInputCheckbox('caption', 'name', $current);
public function testGetCurrentValue($expected, $default, $active, $inactive) {
$checkbox = new PapayaUiDialogFieldInputCheckbox('caption', 'name', $default);
$checkbox->setValues($active, $inactive);
$this->assertSame(
$expected, $checkbox->getCurrentValue()
Expand Down

0 comments on commit 1d3ab2c

Please sign in to comment.