Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbarkowsky committed Nov 16, 2015
1 parent fa045ec commit a7e3954
Showing 1 changed file with 148 additions and 140 deletions.
288 changes: 148 additions & 140 deletions forms/FormProtectedSelectMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,149 +11,157 @@
* @license http://opensource.org/licenses/lgpl-3.0.html
*/


namespace Contao;


class FormProtectedSelectMenu extends \FormSelectMenu
{

/**
* Add specific attributes
*/
public function __set($strKey, $varValue)
{
parent::__set($strKey, $varValue);
}


/**
* Add specific attributes
*/
public function __get($strKey)
{
switch( $strKey )
{
case 'value':
$this->arrOptions = deserialize($this->protectedOptions, true);

if (is_array($this->varValue))
{
$arrValues = array();
foreach( $this->varValue as $k => $value )
{
foreach( $this->arrOptions as $option )
{
if ($option['reference'] == $value)
{
$arrValues[$k] = $option['value'];
continue(2);
}
}
}
return $arrValues;
}

foreach( $this->arrOptions as $option )
{
if ($option['reference'] == $this->varValue)
{
return $option['value'];
}
}
break;

default:
return parent::__get($strKey);
}
}


/**
* Check for a valid option
*/
/**/
public function validate()
{
$mandatory = $this->mandatory;
$options = $this->getPost($this->strName);

// Check if there is at least one value
if ($mandatory && is_array($options))
{
foreach ($options as $option)
{
if (strlen($option))
{
$this->mandatory = false;
break;
}
}
}

$varInput = $this->validator($options);

// Check for a valid option (see #4383)
if (!empty($varInput) && !$this->isValidOption($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalid'], (is_array($varInput) ? implode(', ', $varInput) : $varInput)));
}

// Add class "error"
if ($this->hasErrors())
{
$this->class = 'error';
}
else
{
$this->varValue = $varInput;
}

// Reset the property
if ($mandatory)
{
$this->mandatory = true;
}
}


protected function isValidOption($varInput)
{
$protectedOptions = deserialize($this->protectedOptions, true);

foreach($protectedOptions as $k => $option)
{
if($varInput == $option['reference'])
{
return true;
}
}

return false;
}


/**
* Generate the widget and return it as string
*/
public function generate()
{
$this->arrOptions = deserialize($this->protectedOptions, true);

if (!is_array($this->varValue) && !strlen($this->varValue) && isset($_GET[$this->strName])) {
$this->varValue = \Input::get($this->strName);
}

$arrOptions = $this->arrOptions;

foreach( $this->arrOptions as $k => $option ) {
$this->arrOptions[$k]['value'] = $option['reference'];
}

$strBuffer = parent::generate();

$this->arrOptions = $arrOptions;

return $strBuffer;
}

/**
* Template
*
* @var string
*/
protected $strTemplate = 'form_widget';


/**
* Add specific attributes
*/
public function __set($strKey, $varValue)
{
parent::__set($strKey, $varValue);
}


/**
* Add specific attributes
*/
public function __get($strKey)
{
switch( $strKey )
{
case 'value':
$this->arrOptions = deserialize($this->protectedOptions, true);

if (is_array($this->varValue))
{
$arrValues = array();
foreach( $this->varValue as $k => $value )
{
foreach( $this->arrOptions as $option )
{
if ($option['reference'] == $value)
{
$arrValues[$k] = $option['value'];
continue(2);
}
}
}
return $arrValues;
}

foreach( $this->arrOptions as $option )
{
if ($option['reference'] == $this->varValue)
{
return $option['value'];
}
}
break;

default:
return parent::__get($strKey);
}
}


/**
* Check for a valid option
*/
/**/
public function validate()
{
$mandatory = $this->mandatory;
$options = $this->getPost($this->strName);

// Check if there is at least one value
if ($mandatory && is_array($options))
{
foreach ($options as $option)
{
if (strlen($option))
{
$this->mandatory = false;
break;
}
}
}

$varInput = $this->validator($options);

// Check for a valid option (see #4383)
if (!empty($varInput) && !$this->isValidOption($varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalid'], (is_array($varInput) ? implode(', ', $varInput) : $varInput)));
}

// Add class "error"
if ($this->hasErrors())
{
$this->class = 'error';
}
else
{
$this->varValue = $varInput;
}

// Reset the property
if ($mandatory)
{
$this->mandatory = true;
}
}


protected function isValidOption($varInput)
{
$protectedOptions = deserialize($this->protectedOptions, true);

foreach($protectedOptions as $k => $option)
{
if($varInput == $option['reference'])
{
return true;
}
}

return false;
}


/**
* Generate the widget and return it as string
*/
public function generate()
{
$this->arrOptions = deserialize($this->protectedOptions, true);

if (!is_array($this->varValue) && !strlen($this->varValue) && isset($_GET[$this->strName])) {
$this->varValue = \Input::get($this->strName);
}

$arrOptions = $this->arrOptions;

foreach( $this->arrOptions as $k => $option ) {
$this->arrOptions[$k]['value'] = $option['reference'];
}

$strBuffer = parent::generate();

$this->arrOptions = $arrOptions;

return $strBuffer;
}
}

0 comments on commit a7e3954

Please sign in to comment.