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.

59 lines
2.0 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 PostconditionAdminTrait
  15. {
  16. /**
  17. * @param RuleInterface $subject
  18. * @return array
  19. */
  20. public function formatPostcondition(PostconditionInterface $subject)
  21. {
  22. $ruleManager = $this->getRuleManager($subject);
  23. $formatter = new RuleJsonConverter();
  24. if($subject->getPostcondition()===null){
  25. return $formatter->formatForDisplay('{}', $ruleManager);
  26. }
  27. return $formatter->formatForDisplay($subject->getPostcondition()->getJson(), $ruleManager);
  28. }
  29. /**
  30. * @param FormMapper $formMapper
  31. * @param array $options
  32. */
  33. protected function addPostconditionFormElement(FormMapper $formMapper, array $options = [])
  34. {
  35. $ruleManager = $this->getRuleManager();
  36. $options = $options + ['label' => false];
  37. $formMapper->add('postcondition', AdminType::class, $options, ['rule_manager' => $ruleManager,'admin_code' => 'postcondition.admin']);
  38. }
  39. protected function addPostconditionListColumns(ListMapper $list)
  40. {
  41. $list->add('postcondition.name')
  42. ->add('postcondition.active', 'boolean', ['editable' => true])
  43. ->add('postcondition', null, ['template' => 'ZitecRuleEngineBundle:Admin:json_postcondition_field.html.twig']);
  44. // In dev environment, display the expression too.
  45. if ($this->getConfigurationPool()->getContainer()->has('kernel.debug')) {
  46. $list->add('postcondition.expression', null, ['header_style' => 'width: 20%; text-align: center']);
  47. }
  48. }
  49. }