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.

63 lines
2.1 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. use Sonata\AdminBundle\Form\Type\CollectionType;
  11. /**
  12. * Class RuleAdminTrait
  13. * A trait that should be used in Sonata Admin classes for RuleEngine-aware entities.
  14. */
  15. trait PreconditionAdminTrait
  16. {
  17. /**
  18. * @param PreconditionInterface $subject
  19. * @return array
  20. */
  21. public function formatPrecondition(PreconditionInterface $subject)
  22. {
  23. $ruleManager = $this->getRuleManager($subject);
  24. $formatter = new RuleJsonConverter();
  25. if($subject->getPrecondition()===null){
  26. return $formatter->formatForDisplay('{}', $ruleManager);
  27. }
  28. return $formatter->formatForDisplay($subject->getPrecondition()->getJson(), $ruleManager);
  29. }
  30. /**
  31. * @param FormMapper $formMapper
  32. * @param array $options
  33. */
  34. protected function addPreconditionFormElement(FormMapper $formMapper, array $options = [])
  35. {
  36. $ruleManager = $this->getRuleManager();
  37. $options = $options + ['label' => false];
  38. $formMapper->add('precondition', AdminType::class, $options, ['rule_manager' => $ruleManager,'admin_code' => 'precondition.admin']);
  39. }
  40. /**
  41. * @param ListMapper $list
  42. */
  43. protected function addPreconditionListColumns(ListMapper $list)
  44. {
  45. $list->add('precondition.name')
  46. ->add('precondition.active', 'boolean', ['editable' => true])
  47. ->add('precondition', null, ['template' => 'ZitecRuleEngineBundle:Admin:json_precondition_field.html.twig']);
  48. // In dev environment, display the expression too.
  49. if ($this->getConfigurationPool()->getContainer()->has('kernel.debug')) {
  50. $list->add('precondition.expression', null, ['header_style' => 'width: 20%; text-align: center']);
  51. }
  52. }
  53. }