Created
October 12, 2012 15:09
-
-
Save havvg/3879667 to your computer and use it in GitHub Desktop.
Propel ModelCriteriaSubscriber
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 | |
namespace Ormigo\Component\Pager; | |
interface FieldWhiteListAwareInterface | |
{ | |
/** | |
* Returns a list of columns valid for sorting. | |
* | |
* @return string[] | |
*/ | |
public function getFieldWhitelist(); | |
} |
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 | |
namespace Ormigo\Component\Pager\Event\Subscriber; | |
use BasePeer; | |
use Knp\Component\Pager\Event\ItemsEvent; | |
use Ormigo\Component\Pager\FieldWhiteListAwareInterface; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
class ModelCriteriaSubscriber implements EventSubscriberInterface | |
{ | |
const OPTION_FIELD_NAME_TYPE = 'propel.field_name_type'; | |
const DEFAULT_FIELD_NAME_TYPE = 'fieldName'; | |
protected $request; | |
public function __construct(Request $request) | |
{ | |
$this->request = $request; | |
} | |
public function items(ItemsEvent $event) | |
{ | |
if (!$event->target instanceof \ModelCriteria) { | |
return; | |
} | |
if (!$columnName = $this->request->get($event->options['sortFieldParameterName'])) { | |
return; | |
} | |
if ($event->target->getOrderByColumns()) { | |
return; | |
} | |
$fieldWhiteList = array(); | |
// The query knows its whitelist? | |
if ($event->target instanceof FieldWhiteListAwareInterface) { | |
$fieldWhiteList = $event->target->getFieldWhitelist(); | |
} | |
// The options passed by the user overwrite the general white list. | |
if (isset($event->options['sortFieldWhitelist'])) { | |
$fieldWhiteList = $event->options['sortFieldWhitelist']; | |
} | |
if (!empty($fieldWhiteList) and !in_array($columnName, $fieldWhiteList)) { | |
throw new \UnexpectedValueException("Cannot sort by: [$columnName] this field is not in whitelist"); | |
} | |
$fieldNameType = empty($event->options[static::OPTION_FIELD_NAME_TYPE]) | |
? static::DEFAULT_FIELD_NAME_TYPE | |
: $event->options[static::OPTION_FIELD_NAME_TYPE] | |
; | |
// Translate the column name, so it can be used by "orderBy". | |
if (BasePeer::TYPE_PHPNAME !== $fieldNameType) { | |
$columnName = call_user_func_array(array($event->target->getModelPeerName(), 'translateFieldName'), array($columnName, $fieldNameType, BasePeer::TYPE_PHPNAME)); | |
} | |
$order = strtoupper($this->request->get($event->options['sortDirectionParameterName'])) === 'ASC' ? 'ASC' : 'DESC'; | |
$event->target->orderBy($columnName, $order); | |
} | |
public static function getSubscribedEvents() | |
{ | |
return array( | |
'knp_pager.items' => array('items', 5), | |
); | |
} | |
} |
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
services: | |
pager.subscriber.model_criteria: | |
class: 'Ormigo\Component\Pager\Event\Subscriber\ModelCriteriaSubscriber' | |
scope: 'request' | |
arguments: | |
- '@request' | |
tags: | |
- { name: 'kernel.event_subscriber' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment