Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Admin;
  3. use Zitec\RuleEngineBundle\DoctrineBehaviors\PreconditionInterface;
  4. use Zitec\RuleEngineBundle\DoctrineBehaviors\RuleInterface;
  5. use Zitec\RuleEngineBundle\Service\RuleConditionsManager;
  6. use Zitec\RuleEngineBundle\Service\RuleJsonConverter;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Form\FormMapper;
  9. use Sonata\AdminBundle\Form\Type\AdminType;
  10. /**
  11. * Class RuleAdminTrait
  12. * A trait that should be used in Sonata Admin classes for RuleEngine-aware entities.
  13. */
  14. trait RuleAdminTrait
  15. {
  16. /**
  17. * @param RuleInterface $subject
  18. * @return array
  19. */
  20. public function formatRule(RuleInterface $subject)
  21. {
  22. $ruleManager = $this->getRuleManager($subject);
  23. $formatter = new RuleJsonConverter();
  24. if($subject->getRule()===null){
  25. return $formatter->formatForDisplay('{}', $ruleManager);
  26. }
  27. return $formatter->formatForDisplay($subject->getRule()->getJson(), $ruleManager);
  28. }
  29. /**
  30. * @param FormMapper $formMapper
  31. * @param array $options
  32. */
  33. protected function addRuleFormElement(FormMapper $formMapper, array $options = [])
  34. {
  35. $ruleManager = $this->getRuleManager();
  36. $options = $options + ['label' => false];
  37. $formMapper->add('rule', AdminType::class, $options, ['rule_manager' => $ruleManager]);
  38. }
  39. /**
  40. * @param ListMapper $list
  41. */
  42. protected function addRuleListColumns(ListMapper $list)
  43. {
  44. $list->add('rule.name')
  45. ->add('rule.active', 'boolean', ['editable' => true])
  46. ->add('rule', null, ['template' => 'ZitecRuleEngineBundle:Admin:json_field.html.twig']);
  47. // In dev environment, display the expression too.
  48. if ($this->getConfigurationPool()->getContainer()->has('kernel.debug')) {
  49. $list->add('rule.expression', null, ['header_style' => 'width: 20%; text-align: center']);
  50. }
  51. }
  52. }