Created
July 9, 2013 11:10
-
-
Save Kedrigern/5956564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Homepage presenter. | |
*/ | |
class HomepagePresenter extends BasePresenter | |
{ | |
public function __construct(\Nette\Http\Session $session) | |
{ | |
parent::__construct(); | |
$this->session = $session; | |
$config = new PersistentModel($session); | |
$config->preparePresenter($this); | |
} | |
public function __destruct() | |
{ | |
$config = new PersistentModel($this->session); | |
$config->savePersistents($this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PersistentModel extends Nette\Object | |
{ | |
/** | |
* @var Nette\Http\Session | |
*/ | |
protected $session; | |
/** | |
* @var Nette\Http\SessionSection | |
*/ | |
protected $section; | |
/** | |
* @param Nette\Http\Session $session | |
*/ | |
public function __construct(\Nette\Http\Session $session) | |
{ | |
$this->session = $session; | |
$this->section = $this->session->getSection('defaults'); | |
} | |
/** | |
* @param Nette\Application\IPresenter $presenter | |
*/ | |
public function preparePresenter(\Nette\Application\UI\PresenterComponent $presenter) | |
{ | |
/** | |
* 1) Nedefaultní hodnota z url | |
* 2) Hodnota se session | |
* 3) Default hodnota presenteru | |
*/ | |
foreach($presenter->getPersistentParams() as $per) | |
{ | |
$def = $presenter->reflection->defaultProperties[$per]; | |
if($presenter->$per !== $def) continue; | |
$sesVal = $this->section->$per; | |
if(isset($sesVal)) { | |
$presenter->$per = $sesVal; | |
} | |
} | |
} | |
public function savePersistents(\Nette\Application\UI\PresenterComponent $presenter) | |
{ | |
foreach($presenter->getPersistentParams() as $per) | |
{ | |
$def = $presenter->reflection->defaultProperties[$per]; | |
if($presenter->$per !== $def) { | |
$this->section->$per = $presenter->$per; | |
} else { | |
unset($presenter->$per); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment