Dependency filters can be kept during a session

This commit is contained in:
Emmanuel BENOîT 2016-01-17 00:23:13 +01:00
parent 525497e1f4
commit fa45d59ea8
2 changed files with 48 additions and 4 deletions

View file

@ -531,6 +531,8 @@ class Ctrl_DependencyAddFiltering
extends Controller extends Controller
implements FormAware implements FormAware
{ {
private static $fields = array( 'text' , 'state' , 'items' , 'item-children' , 'keep' );
private $filtering; private $filtering;
private $selector; private $selector;
private $task; private $task;
@ -553,6 +555,11 @@ class Ctrl_DependencyAddFiltering
$this->filterTaskDependencies( ); $this->filterTaskDependencies( );
$this->addDependencySelector( ); $this->addDependencySelector( );
$this->copyFiltersToSelector( ); $this->copyFiltersToSelector( );
if ( $this->getField( 'keep' ) ) {
$this->saveToSession( );
} elseif ( array_key_exists( 'add-dep-filters' , $_SESSION ) ) {
unset( $_SESSION[ 'add-dep-filters' ] );
}
return null; return null;
} }
@ -679,8 +686,7 @@ class Ctrl_DependencyAddFiltering
private function copyFiltersToSelector( ) private function copyFiltersToSelector( )
{ {
$fields = array( 'text' , 'state' , 'items' , 'item-children' ); foreach ( Ctrl_DependencyAddFiltering::$fields as $f ) {
foreach ( $fields as $f ) {
$v = $this->getField( $f ); $v = $this->getField( $f );
$this->selector->addField( $this->selector->addField(
Loader::Create( 'Field' , 'filters-' . $f , 'hidden' ) Loader::Create( 'Field' , 'filters-' . $f , 'hidden' )
@ -689,10 +695,25 @@ class Ctrl_DependencyAddFiltering
} }
} }
private function saveToSession( )
{
if ( array_key_exists( 'add-dep-filters' , $_SESSION ) ) {
$values = $_SESSION[ 'add-dep-filters' ];
} else {
$values = array( );
}
foreach ( Ctrl_DependencyAddFiltering::$fields as $f ) {
$fld = $this->filtering->field( $f );
if ( $fld !== null ) {
$values[ $f ] = $fld->value( );
}
}
$_SESSION[ 'add-dep-filters' ] = $values;
}
public function getFiltersFromSelector( ) public function getFiltersFromSelector( )
{ {
$fields = array( 'text' , 'state' , 'items' , 'item-children' ); foreach ( Ctrl_DependencyAddFiltering::$fields as $f ) {
foreach ( $fields as $f ) {
$field = $this->filtering->field( $f ); $field = $this->filtering->field( $f );
if ( $field !== null ) { if ( $field !== null ) {
$fv = $this->getParameter( 'filters-' . $f , 'POST' ); $fv = $this->getParameter( 'filters-' . $f , 'POST' );
@ -701,6 +722,22 @@ class Ctrl_DependencyAddFiltering
} }
} }
public function getFiltersFromSession( )
{
if ( !array_key_exists( 'add-dep-filters' , $_SESSION ) ) {
return;
}
$values = $_SESSION[ 'add-dep-filters' ];
foreach ( Ctrl_DependencyAddFiltering::$fields as $f ) {
if ( array_key_exists( $f , $values ) ) {
$field = $this->filtering->field( $f );
if ( $field != null ) {
$field->setFormValue( $values[ $f ] );
}
}
}
}
private function getField( $name ) private function getField( $name )
{ {
$fld = $this->filtering->field( $name ); $fld = $this->filtering->field( $name );

View file

@ -488,6 +488,8 @@ class Ctrl_DependencyAddForm
} }
if ( $submitted !== null ) { if ( $submitted !== null ) {
$fCtrl->getFiltersFromSelector( ); $fCtrl->getFiltersFromSelector( );
} else {
$fCtrl->getFiltersFromSession( );
} }
// Fake handling the form // Fake handling the form
@ -527,6 +529,11 @@ class Ctrl_DependencyAddForm
->addOption( '1' , 'Yes' ) ->addOption( '1' , 'Yes' )
->addOption( '0' , 'No' ) ); ->addOption( '0' , 'No' ) );
} }
$filters->addField( Loader::Create( 'Field' , 'keep' , 'select' )
->setMandatory( false )
->setDescription( 'Keep these filters for next time' )
->addOption( '0' , 'No' )
->addOption( '1' , 'Yes' ) );
return $filters; return $filters;
} }