Created
March 2, 2011 13:30
-
-
Save kriswallsmith/850931 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 | |
/* | |
* This file is part of the Assetic package. | |
* | |
* (c) Kris Wallsmith <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Kris\GridBundle\Controller; | |
use Kris\GridBundle\Grid\GridInterface; | |
use Symfony\Component\Form\Form; | |
use Symfony\Component\HttpFoundation\Request; | |
/** | |
* An abstract controller for grid views. | |
* | |
* @author Kris Wallsmith <[email protected]> | |
*/ | |
abstract class GridController | |
{ | |
protected $request; | |
protected $session; | |
protected $grid; | |
public function __construct(Request $request, GridInterface $grid) | |
{ | |
$this->request = $request; | |
$this->session = $request->getSession(); | |
$this->grid = $grid; | |
} | |
abstract protected function getSessionKey(); | |
protected function handle() | |
{ | |
$form = $this->grid->createForm('filter'); | |
if ($this->request->query->has('reset')) { | |
$this->session->remove($this->getSessionKey()); | |
} | |
if (is_array($data = $this->request->query->get('filter'))) { | |
// begin | |
$snapshot = clone $this->grid; | |
$this->bindForm($form, $data); | |
if ($form->isValid()) { | |
// commit | |
$this->session->set($this->getSessionKey(), $form->getDisplayedData()); | |
} else { | |
// rollback | |
$this->grid = $snapshot; | |
} | |
} else { | |
$this->bindForm($form); | |
} | |
$this->grid->load(); | |
} | |
private function bindForm(Form $form, $data = array()) | |
{ | |
$form->submit(array_merge( | |
$form->getDisplayedData(), | |
$this->session->get($this->getSessionKey(), array()), | |
$data | |
)); | |
$form->validate(); | |
} | |
} |
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 | |
/* | |
* This file is part of the Assetic package. | |
* | |
* (c) Kris Wallsmith <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Kris\GridBundle\Grid;; | |
use Symfony\Component\Validator\ValidatorInterface; | |
/** | |
* An admin grid. | |
* | |
* Implementation of the Countable interface should return the total number | |
* of items that are present when the looping through the iterator. If the | |
* iterator is a MongoCursor, this means passing true to its count() method. | |
*/ | |
interface GridInterface extends \IteratorAggregate, \Countable | |
{ | |
/** | |
* Creates a new filter form for the current grid. | |
* | |
* @param string $key The form key | |
* | |
* @return Symfony\Component\Form\Form The default filter form | |
*/ | |
function createForm($key); | |
/** | |
* Loads the current grid with data. | |
* | |
* This method should use the data bound to the grid using the form to | |
* craft a database query. Those data are then accessible via the iterator | |
* interface. | |
*/ | |
function load(); | |
} |
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
Copyright (c) Kris Wallsmith | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is furnished | |
to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment