/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { if (!$options['disable_customer_datasource_types']) { return; } $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { /** @var Integration $data */ $data = $event->getData(); $form = $event->getForm(); if ($data === null) { return; } // Remove integration types that could be created only in scope of the channel // if type is already set, then keep choice list as is $sourceTypes = $this->settingsProvider->getSourceIntegrationTypes(); if (!in_array($data->getType(), $sourceTypes)) { $field = $form->get('type'); $config = $field->getConfig()->getOptions(); foreach ($sourceTypes as $sourceType) { unset($config['choices'][$sourceType]); } FormUtils::replaceField($form, 'type', ['choices' => $config['choices']], ['choice_list']); } }, 100); }
/** * @param FormEvent $event */ public function initChoicesByEntityName(FormEvent $event) { $valuefrompost = $this->requestStack->getCurrentRequest()->get('ibnab_pmanager_exportpdf'); if ($valuefrompost and isset($valuefrompost['entityClass'])) { $entityClass = $valuefrompost['entityClass']; } else { $entityClass = $this->requestStack->getCurrentRequest()->get('entityClass'); $entityClass = trim(str_replace("_", "\\", $entityClass)); } //$data = $event->getData(); if (null === $entityClass) { return; } //$entityClass = is_object($data) ? $data->getEntityClass() : $data['entityClass']; $form = $event->getForm(); /** @var UsernamePasswordOrganizationToken $token */ $token = $this->securityContext->getToken(); $organization = $token->getOrganizationContext(); FormUtils::replaceField($form, 'template', ['selectedEntity' => $entityClass, 'query_builder' => function (PDFTemplateRepository $templateRepository) use($entityClass, $organization) { return $templateRepository->getEntityTemplatesQueryBuilder($entityClass, $organization, true); }], ['choice_list', 'choices']); /* if ($this->securityContext->isGranted('EDIT', 'entity:Oro\Bundle\EmailBundle\Entity\EmailUser')) { FormUtils::replaceField( $form, 'contexts', [ 'read_only' => false, ] ); }*/ }
/** * Process form * * @param Task $entity * * @return bool True on successful processing, false otherwise */ public function process(Task $entity) { $action = $this->entityRoutingHelper->getAction($this->request); $targetEntityClass = $this->entityRoutingHelper->getEntityClassName($this->request); $targetEntityId = $this->entityRoutingHelper->getEntityId($this->request); if ($targetEntityClass && !$entity->getId() && $this->request->getMethod() === 'GET' && $action === 'assign' && is_a($targetEntityClass, 'Oro\\Bundle\\UserBundle\\Entity\\User', true)) { $entity->setOwner($this->entityRoutingHelper->getEntity($targetEntityClass, $targetEntityId)); FormUtils::replaceField($this->form, 'owner', ['read_only' => true]); } $this->form->setData($entity); if (in_array($this->request->getMethod(), array('POST', 'PUT'))) { $this->form->submit($this->request); if ($this->form->isValid()) { // TODO: should be refactored after finishing BAP-8722 // Contexts handling should be moved to common for activities form handler if ($this->form->has('contexts')) { $contexts = $this->form->get('contexts')->getData(); $this->activityManager->setActivityTargets($entity, $contexts); } elseif ($targetEntityClass && $action === 'activity') { // if we don't have "contexts" form field // we should save association between activity and target manually $this->activityManager->addActivityTarget($entity, $this->entityRoutingHelper->getEntityReference($targetEntityClass, $targetEntityId)); } $this->onSuccess($entity); return true; } } return false; }
/** * @dataProvider viewVariablesProvider * * @param array $vars * @param string|array $classToAppend * @param array $expectedVars */ public function testAddClass($vars, $classToAppend, $expectedVars) { $formView = new FormView(); $formView->vars = $vars; FormUtils::appendClass($formView, $classToAppend); $this->assertSame($expectedVars, $formView->vars); }
/** * Process form * * @param Issue $entity * * @return bool True on successful processing, false otherwise */ public function process(Issue $entity) { $action = $this->entityRoutingHelper->getAction($this->request); $targetEntityClass = $this->entityRoutingHelper->getEntityClassName($this->request); $targetEntityId = $this->entityRoutingHelper->getEntityId($this->request); if ($targetEntityClass && !$entity->getId() && $this->request->getMethod() === 'GET' && $action === 'assign' && is_a($targetEntityClass, 'Oro\\Bundle\\UserBundle\\Entity\\User', true)) { $entity->setAssignee($this->entityRoutingHelper->getEntity($targetEntityClass, $targetEntityId)); FormUtils::replaceField($this->form, 'assignee', ['read_only' => true]); } $this->form->setData($entity); if (in_array($this->request->getMethod(), array('POST', 'PUT'))) { $this->form->submit($this->request); if ($this->form->isValid()) { /* if ($targetEntityClass && $action === 'activity') { $this->activityManager->addActivityTarget( $entity, $this->entityRoutingHelper->getEntityReference($targetEntityClass, $targetEntityId) ); }*/ $this->onSuccess($entity); return true; } } return false; }
/** * @param FormEvent $event */ protected function updateDueDateFieldConstraints(FormEvent $event) { /** @var Task $data */ $data = $event->getData(); if ($data && $data->getCreatedAt()) { FormUtils::replaceField($event->getForm(), 'dueDate', ['constraints' => [$this->getDueDateValidationConstraint($data->getCreatedAt())]]); } }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { parent::buildView($view, $form, $options); FormUtils::appendClass($view, 'no-uniform'); $view->vars['translatable'] = $options['translatable']; $view->vars['allow_empty_color'] = $options['allow_empty_color']; $view->vars['empty_color'] = $options['empty_color']; }
/** * Replace email template field with new choices configuration * * @param string $entityName * @param string $fieldName * @param FormInterface $form */ protected function initChoicesByEntityName($entityName, $fieldName, FormInterface $form) { /** @var UsernamePasswordOrganizationToken $token */ $token = $this->securityContext->getToken(); $organization = $token->getOrganizationContext(); FormUtils::replaceField($form, $fieldName, ['selectedEntity' => $entityName, 'query_builder' => function (EmailTemplateRepository $templateRepository) use($entityName, $organization) { return $templateRepository->getEntityTemplatesQueryBuilder($entityName, $organization); }], ['choice_list', 'choices']); }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $modifier = function (FormEvent $event) { $form = $event->getForm(); if ($form->has('additional') && $form->get('additional')->has('branch')) { FormUtils::replaceField($form->get('additional'), 'branch', ['required' => true, 'constraints' => [new NotBlank()]]); } }; $builder->addEventListener(FormEvents::PRE_SET_DATA, $modifier); $builder->addEventListener(FormEvents::PRE_SUBMIT, $modifier); }
/** * Hide connectors for MailChimp channel * * @param FormEvent $event */ public function onPostSetData(FormEvent $event) { $data = $event->getData(); if (!$this->isApplicable($data)) { return; } $propertyAccessor = PropertyAccess::createPropertyAccessor(); $options = $event->getForm()['connectors']->getConfig()->getOptions(); $class = $propertyAccessor->getValue($options, self::CLASS_PATH); FormUtils::replaceField($event->getForm(), 'connectors', ['attr' => ['class' => implode(' ', [$class, 'hide'])]]); }
/** * POST_SET_DATA event handler * * @param FormEvent $event */ public function postSetData(FormEvent $event) { /** @var EmailApi $data */ $data = $event->getData(); if (!$data || $data->getEntity() && $data->getEntity()->getId()) { return; } $form = $event->getForm(); FormUtils::replaceField($form, 'folders', ['constraints' => [new Assert\NotBlank()]]); FormUtils::replaceField($form, 'messageId', ['constraints' => [new Assert\NotBlank()]]); FormUtils::replaceField($form, 'from', ['constraints' => [new Assert\NotBlank()]]); }
/** * @param FormEvent $event */ public function postSet(FormEvent $event) { $form = $event->getForm(); /** @var Channel $data */ $data = $event->getData(); if ($data === null) { return; } // disable modification of customer identity and channel type after save if ($data->getId()) { FormUtils::replaceField($form, 'channelType', ['required' => false, 'disabled' => true]); } }
/** * * @param type $organizations */ private function getModifierOrganizationsList($organizations) { return function (FormInterface $form) use($organizations) { if (empty($organizations)) { return; } $choices = []; foreach ($organizations as $organization) { $choices[$organization['organization_id']] = $organization['name']; } FormUtils::replaceField($form, 'organization_idvalue', ['choices' => $choices], ['choice_list']); }; }
/** * @dataProvider transformerProvider * * @param array $existingTransformers * @param string $type * @param DataTransformerInterface $toReplace * @param array $expected */ public function testReplaceTransformer(array $existingTransformers, $type, $toReplace, array $expected) { $builder = $this->getMock('Symfony\\Component\\Form\\Test\\FormBuilderInterface'); $model = 'model' === $type; $builder->expects($this->once())->method($model ? 'getModelTransformers' : 'getViewTransformers')->willReturn($existingTransformers); $builder->expects($this->once())->method($model ? 'resetModelTransformers' : 'resetViewTransformers'); $newTransformers = []; $builder->expects($this->any())->method($model ? 'addModelTransformer' : 'addViewTransformer')->willReturnCallback(function ($transformer) use(&$newTransformers) { $newTransformers[] = $transformer; }); FormUtils::replaceTransformer($builder, $toReplace, $type); $this->assertSame($expected, $newTransformers); }
/** * Builds form view in order to show on page, recreates form if update mode * due to validation errors should not be shown if submitted in 'update mode' * NOTE: it was impossible to use validation_groups because of structure of client validation framework * * @return FormView */ public function getFormView() { $isUpdateOnly = $this->request->get(IntegrationChannelHandler::UPDATE_MARKER, false); $form = $this->form; if ($isUpdateOnly) { $config = $form->getConfig(); $form = $config->getFormFactory()->createNamed($form->getName(), $config->getType()->getName(), $form->getData(), $this->options); } $view = $form->createView(); FormUtils::appendClass($view->children['connectors'], 'hide'); FormUtils::appendClass($view->children['type'], 'hide'); return $view; }
/** * PreSet event handler. * * Adds appropriate process field to form based on set value. * * @param FormEvent $event */ public function preSet(FormEvent $event) { /** @var Mailbox $data */ $data = $event->getData(); $form = $event->getForm(); if ($data === null) { return; } $processType = null; if (null !== ($processEntity = $data->getProcessSettings())) { $processType = $processEntity->getType(); } FormUtils::replaceField($form, 'processType', ['data' => $processType]); $this->addProcessField($form, $processType); }
/** * @param FormEvent $event */ public function initChoicesByEntityName(FormEvent $event) { /** @var Email|array $data */ $data = $event->getData(); if (null === $data || is_array($data) && empty($data['entityClass']) || is_object($data) && null === $data->getEntityClass()) { return; } $entityClass = is_object($data) ? $data->getEntityClass() : $data['entityClass']; $form = $event->getForm(); /** @var UsernamePasswordOrganizationToken $token */ $token = $this->securityContext->getToken(); $organization = $token->getOrganizationContext(); FormUtils::replaceField($form, 'template', ['selectedEntity' => $entityClass, 'query_builder' => function (EmailTemplateRepository $templateRepository) use($entityClass, $organization) { return $templateRepository->getEntityTemplatesQueryBuilder($entityClass, $organization, true); }], ['choice_list', 'choices']); }
/** * Disable fields that are not allowed to be modified since channel has at least one sync completed * * @param FormInterface $form */ protected function muteFields(FormInterface $form) { if (!$form->getParent()) { return; } /** @var Channel $channel */ $channel = $form->getParent()->getData(); // if channel is new if (!$channel || !$channel->getId()) { return; } if (IntegrationFormUtils::wasSyncedAtLeastOnce($channel)) { // disable start sync date FormUtils::replaceField($form, 'syncStartDate', ['disabled' => true]); // disable websites selector FormUtils::replaceField($form, 'websiteId', ['disabled' => true]); } }
/** * @param array $websites * * @return callable */ protected function getModifierWebsitesList($websites) { return function (FormInterface $form) use($websites) { if (empty($websites)) { return; } $choices = []; foreach ($websites as $website) { $choices[$website['id']] = $website['label']; } FormUtils::replaceField($form, 'websiteId', ['choices' => $choices], ['choice_list']); }; }
/** * @param FormInterface $form * @param string $childName */ protected function disableFieldChanges(FormInterface $form, $childName) { FormUtils::replaceField($form, $childName, ['disabled' => true]); }
/** * Disable fields that are not allowed to be modified since integration has at least one sync completed * * @param FormInterface $form * @param Integration $integration */ protected function muteFields(FormInterface $form, Integration $integration = null) { if (!($integration && $integration->getId())) { // do nothing if integration is new return; } if ($integration->getEditMode() !== Integration::EDIT_MODE_ALLOW) { // disable type field FormUtils::replaceField($form, 'type', ['disabled' => true]); } if (Integration::EDIT_MODE_DISALLOW === $integration->getEditMode()) { FormUtils::replaceField($form, 'connectors', ['disabled' => true, 'attr' => ['class' => 'hide']]); } }
/** * {@inheritdoc} */ public function finishView(FormView $view, FormInterface $form, array $options) { if (isset($view->children['owner'], $view->children['owner']->vars['choices']) && count($view->children['owner']->vars['choices']) < 2) { FormUtils::appendClass($view->children['owner'], 'hide'); } }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->remove('year'); FormUtils::replaceTransformer($builder, new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], ['month', 'day']), 'view'); FormUtils::replaceTransformer($builder, new ReversedTransformer(new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], ['month', 'day']))); }
/** * Configures user field so it searches only within mailboxes' organization. * * @param FormInterface $form * @param Mailbox $data */ protected function configureUserField(FormInterface $form, Mailbox $data) { if (!$data->getOrganization()) { return; } FormUtils::replaceField($form, 'authorizedUsers', ['configs' => ['route_name' => 'oro_email_mailbox_users_search', 'route_parameters' => ['organizationId' => $data->getOrganization()->getId()], 'multiple' => true, 'width' => '400px', 'placeholder' => 'oro.user.form.choose_user', 'allowClear' => true, 'result_template_twig' => 'OroUserBundle:User:Autocomplete/result.html.twig', 'selection_template_twig' => 'OroUserBundle:User:Autocomplete/selection.html.twig']]); }
/** * {@inheritdoc} */ public function finishView(FormView $view, FormInterface $form, array $options) { if (array_key_exists('label', $view->children)) { FormUtils::appendClass($view->children['label'], 'hide'); } }