/** * @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; }
/** * 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; }
/** * {@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 */ 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())]]); } }
/** * 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']); }; }
/** * 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']); }
/** * @dataProvider optionsProvider * * @param array $expectedOptions * @param array $modifyOptions * @param array $unsetOptions */ public function testReplaceField($expectedOptions = [], $modifyOptions = [], $unsetOptions = []) { $testFieldName = 'testField'; $testTypeName = 'testType'; $testOptions = ['required' => true, 'auto_initialize' => true]; $rootForm = $this->getMock('Symfony\\Component\\Form\\Test\\FormInterface'); $childForm = $this->getMock('Symfony\\Component\\Form\\Test\\FormInterface'); $formConfig = $this->getMock('Symfony\\Component\\Form\\FormConfigInterface'); $formType = $this->getMock('Symfony\\Component\\Form\\ResolvedFormTypeInterface'); $rootForm->expects($this->once())->method('get')->with($testFieldName)->will($this->returnValue($childForm)); $childForm->expects($this->once())->method('getConfig')->will($this->returnValue($formConfig)); $formConfig->expects($this->once())->method('getType')->will($this->returnValue($formType)); $formConfig->expects($this->once())->method('getOptions')->will($this->returnValue($testOptions)); $formType->expects($this->once())->method('getName')->will($this->returnValue($testTypeName)); $rootForm->expects($this->once())->method('add')->with($testFieldName, $testTypeName, $expectedOptions); FormUtils::replaceField($rootForm, $testFieldName, $modifyOptions, $unsetOptions); }
/** * 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']]); } }
/** * 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 FormInterface $form * @param string $childName */ protected function disableFieldChanges(FormInterface $form, $childName) { FormUtils::replaceField($form, $childName, ['disabled' => true]); }
/** * 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']]); }
/** * @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']); }; }