Skip to content

Commit

Permalink
Merge pull request #51 from larriereguichet/dev
Browse files Browse the repository at this point in the history
v0.4.1
  • Loading branch information
johnkrovitch authored Sep 12, 2016
2 parents ceb2acc + 3e1fc7d commit bd4cbc2
Show file tree
Hide file tree
Showing 15 changed files with 544 additions and 220 deletions.
12 changes: 12 additions & 0 deletions Action/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LAG\AdminBundle\Action;

use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
use LAG\AdminBundle\Admin\AdminInterface;
use LAG\AdminBundle\Admin\Filter;
use LAG\AdminBundle\Field\Field;

Expand Down Expand Up @@ -151,4 +152,15 @@ public function getConfiguration()
{
return $this->configuration;
}

/**
* Return true if the pagination is required for this action. Only action with a "multiple" load strategy require
* pagination.
*
* @return bool
*/
public function isPaginationRequired()
{
return $this->configuration->getParameter('load_strategy') === AdminInterface::LOAD_STRATEGY_MULTIPLE;
}
}
7 changes: 7 additions & 0 deletions Action/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ public function setFields($fields);
* @param Field $field
*/
public function addField(Field $field);

/**
* Return true if the Action requires pagination.
*
* @return bool
*/
public function isPaginationRequired();
}
3 changes: 2 additions & 1 deletion Action/Configuration/ActionConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public function configureOptions(OptionsResolver $resolver)
'xls'
]);

// entity will be retrived with this order. It should be an array of field/order mapping
// retrieved entities are sorted according to this option it should be an array indexed by the field name
// with its order as a value
$resolver
->setDefault('order', [])
->setAllowedTypes('order', 'array');
Expand Down
59 changes: 43 additions & 16 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ public function create()
->entities
->add($entity);

// inform the user that the entity is created
$this
->messageHandler
->handleSuccess($this->generateMessageTranslationKey('created'));

return $entity;
}

Expand All @@ -212,16 +217,16 @@ public function save()
->dataProvider
->save($entity);
}
// inform user everything went fine
// inform the user that the entity is saved
$this
->messageHandler
->handleSuccess('lag.admin.'.$this->name.'.saved');
->handleSuccess($this->generateMessageTranslationKey('saved'));
$success = true;
} catch (Exception $e) {
$this
->messageHandler
->handleError(
'lag.admin.saved_errors',
$this->generateMessageTranslationKey('lag.admin.saved_errors'),
"An error has occurred while saving an entity : {$e->getMessage()}, stackTrace: {$e->getTraceAsString()}"
);
$success = false;
Expand All @@ -242,16 +247,16 @@ public function remove()
->dataProvider
->remove($entity);
}
// inform user everything went fine
// inform the user that the entity is removed
$this
->messageHandler
->handleSuccess('lag.admin.'.$this->name.'.deleted');
->handleSuccess($this->generateMessageTranslationKey('deleted'));
$success = true;
} catch (Exception $e) {
$this
->messageHandler
->handleError(
'lag.admin.deleted_errors',
$this->generateMessageTranslationKey('lag.admin.deleted_errors'),
"An error has occurred while deleting an entity : {$e->getMessage()}, stackTrace: {$e->getTraceAsString()} "
);
$success = false;
Expand All @@ -272,10 +277,10 @@ public function generateRouteName($actionName)
{
if (!array_key_exists($actionName, $this->getConfiguration()->getParameter('actions'))) {
throw new Exception(
sprintf('Invalid action name %s for admin %s (available action are: %s)',
$actionName,
$this->getName(),
implode(', ', $this->getActionNames()))
sprintf('Invalid action name %s for admin %s (available action are: %s)',
$actionName,
$this->getName(),
implode(', ', $this->getActionNames()))
);
}
// get routing name pattern
Expand All @@ -298,13 +303,16 @@ public function generateRouteName($actionName)
*/
public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1)
{
$pager = $this
$actionConfiguration = $this
->getCurrentAction()
->getConfiguration();
$pager = $actionConfiguration->getParameter('pager');
$requirePagination = $this
->getCurrentAction()
->getConfiguration()
->getParameter('pager');
->isPaginationRequired();

if ($pager == 'pagerfanta') {
// adapter to pager fanta
if ($pager == 'pagerfanta' && $requirePagination) {
// adapter to pagerfanta
$adapter = new PagerFantaAdminAdapter($this->dataProvider, $criteria, $orderBy);
// create pager
$this->pager = new Pagerfanta($adapter);
Expand All @@ -315,6 +323,10 @@ public function load(array $criteria, $orderBy = [], $limit = 25, $offset = 1)
->pager
->getCurrentPageResults();
} else {
// if the current action should retrieve only one entity, the offset should be zero
if ($actionConfiguration->getParameter('load_strategy') !== AdminInterface::LOAD_STRATEGY_MULTIPLE) {
$offset = 0;
}
$entities = $this
->dataProvider
->findBy($criteria, $orderBy, $limit, $offset);
Expand Down Expand Up @@ -480,12 +492,27 @@ public function isCurrentActionDefined()
}

/**
* Return admin configuration object
* Return admin configuration object.
*
* @return AdminConfiguration
*/
public function getConfiguration()
{
return $this->configuration;
}

/**
* Return a translation key for a message according to the Admin's translation pattern.
*
* @param string $message
* @return string
*/
protected function generateMessageTranslationKey($message)
{
return $this->getTranslationKey(
$this->configuration->getParameter('translation_pattern'),
$message,
$this->name
);
}
}
1 change: 1 addition & 0 deletions Admin/Behaviors/AdminTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ trait AdminTrait
use EntityLabelTrait {
getEntityLabel as parentEntityLabel;
}
use TranslationKeyTrait;

/**
* @var Pagerfanta
Expand Down
54 changes: 46 additions & 8 deletions Application/Configuration/ApplicationConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function configureOptions(OptionsResolver $resolver)

// main base template
// as bundles are not loaded when reading the configuration, the kernel locateResources will always failed.
// So we must not check resource existance here.
// So we must not check resource existence here.
$resolver->setDefault('base_template', 'LAGAdminBundle::admin.layout.html.twig');
$resolver->setAllowedTypes('base_template', 'string');

Expand All @@ -64,13 +64,38 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('string_length_truncate', 'string');

// routing configuration (route name pattern and url name pattern)
$this->setRoutingOptions($resolver);

// translation configuration
$this->setTranslationOptions($resolver);

// maximum number of elements displayed
$resolver->setDefault('max_per_page', 25);
$resolver->setAllowedTypes('max_per_page', 'integer');

// admin field type mapping
$this->setFieldsOptions($resolver);
}

/**
* @param OptionsResolver $resolver
*/
protected function setRoutingOptions(OptionsResolver $resolver)
{
$resolver->setDefault('routing', [
'url_pattern' => '/{admin}/{action}',
'name_pattern' => 'lag.admin.{admin}',
'name_pattern' => 'lag.admin.{admin}.{action}',
]);
$resolver->setAllowedTypes('routing', 'array');
$resolver->setNormalizer('routing', function(Options $options, $value) {

if (!array_key_exists('url_pattern', $value)) {
$value['url_pattern'] = '/{admin}/{action}';
}
if (!array_key_exists('name_pattern', $value)) {
$value['name_pattern'] = 'lag.admin.{admin}.{action}';
}

// url pattern should contain {admin} and {action} token
$urlPattern = $value['url_pattern'];

Expand All @@ -87,18 +112,30 @@ public function configureOptions(OptionsResolver $resolver)
if (strstr($namePattern, '{admin}') === false) {
throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder');
}
if (strstr($namePattern, '{action}') === false) {
throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {action} placeholder');
}

return $value;
});
}

// translation configuration
/**
* @param OptionsResolver $resolver
*/
protected function setTranslationOptions(OptionsResolver $resolver)
{
$resolver->setDefault('translation', [
'enabled' => true,
'pattern' => 'lag.admin.{key}'
]);
$resolver->setAllowedTypes('translation', 'array');
$resolver->setNormalizer('translation', function(Options $options, $value) {

if (!array_key_exists('enabled', $value)) {
throw new InvalidOptionsException('Admin translation enabled parameter should be defined');
}

if (!is_bool($value['enabled'])) {
throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean');
}
Expand All @@ -113,12 +150,13 @@ public function configureOptions(OptionsResolver $resolver)

return $value;
});
}

// maximum number of elements displayed
$resolver->setDefault('max_per_page', 25);
$resolver->setAllowedTypes('max_per_page', 'integer');

// admin field type mapping
/**
* @param OptionsResolver $resolver
*/
protected function setFieldsOptions(OptionsResolver $resolver)
{
$defaultMapping = [
Field::TYPE_STRING => StringField::class,
Field::TYPE_ARRAY => Field\ArrayField::class,
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function getAdminsConfigurationNode()
$node = $builder->root('admins');

$node
// useAttributeAsKey() method will preserve keys when multiple configurations files are used and then avoid
// admin not found by configuration override
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('entity')->end()
Expand Down
6 changes: 5 additions & 1 deletion Form/Type/DateTimePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DateTimePickerType extends AbstractType
Expand All @@ -24,9 +25,12 @@ public function configureOptions(OptionsResolver $resolver)
]);
}

/**
* @return string
*/
public function getParent()
{
return 'datetime';
return DateTimeType::class;
}

public function getName()
Expand Down
Loading

0 comments on commit bd4cbc2

Please sign in to comment.