/** * create a form with given widget. * * @param Widget $widget * @param View $view * @param string $businessEntityId * @param string $namespace * @param string $formMode * @param int $position * * @throws \Exception * * @return Form */ public function buildWidgetForm(Widget $widget, View $view, $businessEntityId = null, $namespace = null, $formMode = Widget::MODE_STATIC, $position = null, $parentWidgetMap = null, $slotId = null, $quantum = null) { $router = $this->container->get('router'); //test parameters if ($businessEntityId !== null) { if ($namespace === null) { throw new \Exception('The namespace is mandatory if the businessEntityId is given'); } if (in_array($formMode, [Widget::MODE_STATIC, null])) { throw new \Exception('The formMode cannot be null or static if the businessEntityId is given'); } } $container = $this->container; $formFactory = $container->get('form.factory'); //are we updating or creating the widget? if ($widget->getId() === null) { $viewReference = $view->getReference(); $actionParams = ['viewReference' => $viewReference->getId(), 'slot' => $slotId, 'type' => $widget->getType(), 'position' => $position, 'parentWidgetMap' => $parentWidgetMap, 'quantum' => $quantum]; $action = 'victoire_core_widget_create'; if ($businessEntityId) { $actionParams['businessEntityId'] = $businessEntityId; $actionParams['mode'] = $formMode; } else { $action = 'victoire_core_widget_create_static'; } $formUrl = $router->generate($action, $actionParams); } else { $viewReference = $this->container->get('victoire_core.current_view')->getCurrentView()->getReference(); $formUrl = $router->generate('victoire_core_widget_update', ['id' => $widget->getId(), 'viewReference' => $viewReference->getId(), 'businessEntityId' => $businessEntityId, 'mode' => $formMode, 'quantum' => $quantum]); } $widgetName = $this->container->get('victoire_widget.widget_helper')->getWidgetName($widget); $widgetFormTypeClass = ClassUtils::getClass($this->container->get(sprintf('victoire.widget.form.%s', strtolower($widgetName)))); $optionsContainer = new WidgetOptionsContainer(['businessEntityId' => $businessEntityId, 'namespace' => $namespace, 'mode' => $formMode, 'action' => $formUrl, 'method' => 'POST', 'dataSources' => $this->container->get('victoire_criteria.chain.data_source_chain')]); $event = new WidgetFormCreateEvent($optionsContainer, $widgetFormTypeClass); $this->container->get('event_dispatcher')->dispatch(WidgetFormEvents::PRE_CREATE, $event); $this->container->get('event_dispatcher')->dispatch(WidgetFormEvents::PRE_CREATE . '_' . strtoupper($widgetName), $event); /** @var Form $mockForm Get the base form to get the name */ $mockForm = $formFactory->create($widgetFormTypeClass, $widget, $optionsContainer->getOptions()); //Prefix base name with form mode to avoid to have unique form fields ids $form = $formFactory->createNamed(sprintf('%s_%s_%s_%s', $businessEntityId, $this->convertToString($quantum), $formMode, $mockForm->getName()), $widgetFormTypeClass, $widget, $optionsContainer->getOptions()); return $form; }
/** * create a form with given widget * * @param Widget $widget * @param View $view * @param string $entityName * @param string $namespace * @param string $formMode * @param integer $position * * @return $form * * @throws \Exception */ public function buildWidgetForm(Widget $widget, View $view, $entityName = null, $namespace = null, $formMode = Widget::MODE_STATIC, $position = 0) { $router = $this->container->get('router'); //test parameters if ($entityName !== null) { if ($namespace === null) { throw new \Exception('The namespace is mandatory if the entityName is given'); } if ($formMode === null) { throw new \Exception('The formMode is mandatory if the entityName is given'); } } $container = $this->container; $formFactory = $container->get('form.factory'); $formAlias = 'victoire_widget_form_' . strtolower($this->container->get('victoire_widget.widget_helper')->getWidgetName($widget)); $filters = array(); if ($this->container->has('victoire_core.filter_chain')) { $filters = $this->container->get('victoire_core.filter_chain')->getFilters(); } //are we updating or creating the widget? if ($widget->getId() === null) { $viewReference = $view->getReference(); $formUrl = $router->generate('victoire_core_widget_create', array('mode' => $formMode, 'viewReference' => $viewReference['id'], 'slot' => $widget->getSlot(), 'type' => $widget->getType(), 'entityName' => $entityName, 'positionReference' => $position)); } else { $viewReference = $widget->getCurrentView()->getReference(); $formUrl = $router->generate('victoire_core_widget_update', array('id' => $widget->getId(), 'viewReference' => $viewReference['id'], 'entityName' => $entityName)); } $params = array('entityName' => $entityName, 'namespace' => $namespace, 'mode' => $formMode, 'action' => $formUrl, 'method' => 'POST', 'filters' => $filters); /** @var Form $mockForm Get the base form to get the name */ $mockForm = $formFactory->create($formAlias, $widget, $params); //Prefix base name with form mode to avoid to have unique form fields ids $form = $formFactory->createNamed(sprintf("%s_%s_%s", $entityName, $formMode, $mockForm->getName()), $formAlias, $widget, $params); return $form; }