1: <?php
2:
3: namespace Mapbender\CoreBundle\Form\EventListener;
4:
5: use Symfony\Component\Form\Event\DataEvent;
6: use Symfony\Component\Form\FormFactoryInterface;
7: use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8: use Symfony\Component\Form\FormEvents;
9: use Mapbender\CoreBundle\Element\Type\PrintClientTemplateAdminType;
10: use Mapbender\CoreBundle\Element\Type\PrintClientQualityAdminType;
11:
12: 13: 14:
15: class PrintClientSubscriber implements EventSubscriberInterface
16: {
17:
18: 19: 20: 21: 22:
23: private $factory;
24:
25: 26: 27: 28: 29:
30: private $application;
31:
32: 33: 34: 35: 36:
37: public function __construct(FormFactoryInterface $factory, $application)
38: {
39: $this->factory = $factory;
40: $this->application = $application;
41: }
42:
43: 44: 45:
46: public static function getSubscribedEvents()
47: {
48: return array(
49: FormEvents::PRE_SET_DATA => 'preSetData',
50: FormEvents::PRE_BIND => 'preBind');
51: }
52:
53: 54: 55: 56: 57:
58: public function preBind(DataEvent $event)
59: {
60: $data = $event->getData();
61: $form = $event->getForm();
62: if(null === $data)
63: {
64: return;
65: }
66: if(key_exists("scales", $data) && is_string($data["scales"]))
67: {
68: $data["scales"] = preg_split("/\s?,\s?/", $data["scales"]);
69: $event->setData($data);
70: }
71:
72: if(key_exists("templates", $data) )
73: {
74: $form->add($this->factory->createNamed(
75: 'templates', "collection", null,
76: array(
77: 'property_path' => '[templates]',
78: 'type' => new PrintClientTemplateAdminType(),
79: 'options' => array(
80: ))));
81: }
82: if(key_exists("quality_levels", $data) )
83: {
84: $form->add($this->factory->createNamed(
85: 'quality_levels', "collection", null,
86: array(
87: 'property_path' => '[quality_levels]',
88: 'type' => new PrintClientQualityAdminType(),
89: 'options' => array(
90: ))));
91: }
92:
93: }
94:
95: 96: 97: 98: 99:
100: public function preSetData(DataEvent $event)
101: {
102: $data = $event->getData();
103: $form = $event->getForm();
104: if(null === $data)
105: {
106: return;
107: }
108:
109: if(key_exists("scales", $data) && is_array($data["scales"]))
110: {
111: $data["scales"] = implode(",", $data["scales"]);
112: $event->setData($data);
113: }
114:
115: if(key_exists("templates", $data) )
116: {
117: $form->add($this->factory->createNamed(
118: 'templates', "collection", null,
119: array(
120: 'property_path' => '[templates]',
121: 'type' => new PrintClientTemplateAdminType(),
122: 'options' => array(
123: ))));
124: }
125:
126: if(key_exists("quality_levels", $data) )
127: {
128: $form->add($this->factory->createNamed(
129: 'quality_levels', "collection", null,
130: array(
131: 'property_path' => '[quality_levels]',
132: 'type' => new PrintClientQualityAdminType(),
133: 'options' => array(
134: ))));
135: }
136:
137: }
138:
139: }