| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Netgen\Bundle\EzFormsBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use eZ\Bundle\EzPublishCoreBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Netgen\Bundle\EzFormsBundle\Form\Payload\InformationCollectionStruct; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Netgen\Bundle\EzFormsBundle\Form\Type\CreateContentType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Netgen\Bundle\EzFormsBundle\Form\Type\CreateUserType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Netgen\Bundle\EzFormsBundle\Form\Type\InformationCollectionType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Netgen\Bundle\EzFormsBundle\Form\Type\UpdateContentType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Netgen\Bundle\EzFormsBundle\Form\Type\UpdateUserType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Form\FormError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class DemoController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function demoCreateContentAction(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $repository = $this->getRepository(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $contentService = $repository->getContentService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $locationService = $repository->getLocationService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         // @todo for demo purpose, user should have necessary permissions by itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $repository->getPermissionResolver()->setCurrentUserReference( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $repository->getUserService()->loadUserByLogin('admin') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('test_type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $data = new DataWrapper($contentCreateStruct, $contentCreateStruct->contentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // No method to create named builder in framework controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         /** @var \Symfony\Component\Form\FormBuilderInterface $formBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $formBuilder = $this->container->get('form.factory')->createBuilder(CreateContentType::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // Adding controls as EzFormsBundle does not do that by itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $formBuilder->add('save', SubmitType::class, ['label' => 'Publish']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $form = $formBuilder->getForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 49 |  | View Code Duplication |         if ($form->isSubmitted() && $form->isValid()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $rootLocation = $locationService->loadLocation(2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $repository->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $contentDraft = $contentService->createContent( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     $data->payload, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         $locationService->newLocationCreateStruct($rootLocation->id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $content = $contentService->publishVersion($contentDraft->versionInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $repository->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $repository->rollback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 // @todo do something else if needed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return $this->redirect( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $this->generateUrl( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     UrlAliasRouter::URL_ALIAS_ROUTE_NAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         'location' => $locationService->loadLocation( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                             $content->contentInfo->mainLocationId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                         ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             '@NetgenEzForms/demo_form.html.twig', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 'form' => $form->createView(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function demoUpdateContentAction(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $repository = $this->getRepository(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $contentService = $repository->getContentService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $repository->getPermissionResolver()->setCurrentUserReference( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $repository->getUserService()->loadUserByLogin('admin') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $content = $contentService->loadContent(137); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $contentType = $repository->getContentTypeService()->loadContentType($content->contentInfo->contentTypeId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $contentUpdateStruct = $contentService->newContentUpdateStruct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $contentUpdateStruct->initialLanguageCode = 'eng-GB'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $data = new DataWrapper($contentUpdateStruct, $contentType, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // No method to create named builder in framework controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         /** @var \Symfony\Component\Form\FormBuilderInterface $formBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $formBuilder = $this->container->get('form.factory')->createBuilder(UpdateContentType::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         // Adding controls as EzFormsBundle does not do that by itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $formBuilder->add('save', SubmitType::class, ['label' => 'Update']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $form = $formBuilder->getForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         //$form = $this->createForm(UpdateContentType::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 115 |  | View Code Duplication |         if ($form->isSubmitted() && $form->isValid()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $repository->beginTransaction(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $contentDraft = $contentService->createContentDraft($content->contentInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $contentDraft = $contentService->updateContent( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     $contentDraft->versionInfo, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     $data->payload | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $content = $contentService->publishVersion($contentDraft->versionInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 $repository->commit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $repository->rollback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 // @todo do something else if needed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             return $this->redirect( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 $this->generateUrl( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     UrlAliasRouter::URL_ALIAS_ROUTE_NAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                         'location' => $this->getRepository()->getLocationService()->loadLocation( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                             $content->contentInfo->mainLocationId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                         ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             '@NetgenEzForms/demo_form.html.twig', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 'form' => $form->createView(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public function demoCreateUserAction(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         /** @todo check that user really is anonymous, otherwise it does not make sense to allow registration */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $repository = $this->getRepository(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $userService = $repository->getUserService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $repository->getPermissionResolver()->setCurrentUserReference( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             // @todo anonymous requires additional permissions to create new user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             $userService->loadUserByLogin('admin') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $userCreateStruct = $userService->newUserCreateStruct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             'eng-GB', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             $contentType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         // Setting manually as it is not controlled through form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $userCreateStruct->enabled = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $data = new DataWrapper($userCreateStruct, $userCreateStruct->contentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         // No method to create named builder in framework controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         /** @var \Symfony\Component\Form\FormBuilderInterface $formBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $formBuilder = $this->container->get('form.factory')->createBuilder(CreateUserType::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         // Adding controls as EzFormsBundle does not do that by itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $formBuilder->add('save', SubmitType::class, ['label' => 'Publish']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         $form = $formBuilder->getForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         if ($form->isSubmitted() && $form->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             /** @todo ensure that user can create 'user' type under required UserGroup Location */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             $userGroup = $userService->loadUserGroup(13); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                 $user = $userService->createUser( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                     $data->payload, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                     [$userGroup] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 // @todo send confirmation email and redirect to proper location (enter confirmation code or something) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |                 return $this->redirect( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |                     $this->generateUrl( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |                         UrlAliasRouter::URL_ALIAS_ROUTE_NAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |                         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |                             'location' => $this->getRepository()->getLocationService()->loadLocation( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                                 $user->contentInfo->mainLocationId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |                             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |             } catch (InvalidArgumentException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |                 // There is no better way to do this ATM... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |                 $existingUsernameMessage = "Argument 'userCreateStruct' is invalid: User with provided login already exists"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |                 if ($e->getMessage() === $existingUsernameMessage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |                     // Search for the first ezuser field type in content type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |                     foreach ($userCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |                         if ($fieldDefinition->fieldTypeIdentifier === 'ezuser') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |                             $userFieldDefinition = $fieldDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |                     // UserService validates for this, but it happens AFTER existing username validation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |                     if (!isset($userFieldDefinition)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |                         throw new RuntimeException("Could not find 'ezuser' field."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |                     $form->get($userFieldDefinition->identifier)->addError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |                         new FormError('User with provided username already exists.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |                     // @todo do something else if needed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |                     throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             '@NetgenEzForms/demo_form.html.twig', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |                 'form' => $form->createView(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 242 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 243 |  |  |     public function demoUpdateUserAction(Request $request): Response | 
            
                                                                        
                            
            
                                    
            
            
                | 244 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 245 |  |  |         $repository = $this->getRepository(); | 
            
                                                                        
                            
            
                                    
            
            
                | 246 |  |  |         $userService = $repository->getUserService(); | 
            
                                                                        
                            
            
                                    
            
            
                | 247 |  |  |         $contentService = $repository->getContentService(); | 
            
                                                                        
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  |         // @todo check that user is really logged in, it should have permissions to self edit | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  |         $repository->getPermissionResolver()->setCurrentUserReference( | 
            
                                                                        
                            
            
                                    
            
            
                | 251 |  |  |             $repository->getUserService()->loadUserByLogin('admin') | 
            
                                                                        
                            
            
                                    
            
            
                | 252 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 254 |  |  |         /** @todo load current user */ | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  |         $user = $userService->loadUser(142); | 
            
                                                                        
                            
            
                                    
            
            
                | 256 |  |  |         $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('user'); | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  |         $contentUpdateStruct = $contentService->newContentUpdateStruct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 258 |  |  |         $contentUpdateStruct->initialLanguageCode = 'eng-GB'; | 
            
                                                                        
                            
            
                                    
            
            
                | 259 |  |  |         $userUpdateStruct = $userService->newUserUpdateStruct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 260 |  |  |         $userUpdateStruct->contentUpdateStruct = $contentUpdateStruct; | 
            
                                                                        
                            
            
                                    
            
            
                | 261 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 262 |  |  |         $data = new DataWrapper($userUpdateStruct, $contentType, $user); | 
            
                                                                        
                            
            
                                    
            
            
                | 263 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 264 |  |  |         // No method to create named builder in framework controller | 
            
                                                                        
                            
            
                                    
            
            
                | 265 |  |  |         /** @var \Symfony\Component\Form\FormBuilderInterface $formBuilder */ | 
            
                                                                        
                            
            
                                    
            
            
                | 266 |  |  |         $formBuilder = $this->container->get('form.factory')->createBuilder(UpdateUserType::class, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 267 |  |  |         // Adding controls as EzFormsBundle does not do that by itself | 
            
                                                                        
                            
            
                                    
            
            
                | 268 |  |  |         $formBuilder->add('save', SubmitType::class, ['label' => 'Update']); | 
            
                                                                        
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 270 |  |  |         $form = $formBuilder->getForm(); | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |         $form->handleRequest($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 273 |  |  |         if ($form->isSubmitted() && $form->isValid()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 274 |  |  |             $user = $userService->updateUser($user, $userUpdateStruct); | 
            
                                                                        
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 276 |  |  |             return $this->redirect( | 
            
                                                                        
                            
            
                                    
            
            
                | 277 |  |  |                 $this->generateUrl( | 
            
                                                                        
                            
            
                                    
            
            
                | 278 |  |  |                     UrlAliasRouter::URL_ALIAS_ROUTE_NAME, | 
            
                                                                        
                            
            
                                    
            
            
                | 279 |  |  |                     [ | 
            
                                                                        
                            
            
                                    
            
            
                | 280 |  |  |                         'location' => $this->getRepository()->getLocationService()->loadLocation( | 
            
                                                                        
                            
            
                                    
            
            
                | 281 |  |  |                             $user->contentInfo->mainLocationId | 
            
                                                                        
                            
            
                                    
            
            
                | 282 |  |  |                         ), | 
            
                                                                        
                            
            
                                    
            
            
                | 283 |  |  |                     ] | 
            
                                                                        
                            
            
                                    
            
            
                | 284 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 285 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 286 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 288 |  |  |         return $this->render( | 
            
                                                                        
                            
            
                                    
            
            
                | 289 |  |  |             '@NetgenEzForms/demo_form.html.twig', | 
            
                                                                        
                            
            
                                    
            
            
                | 290 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 291 |  |  |                 'form' => $form->createView(), | 
            
                                                                        
                            
            
                                    
            
            
                | 292 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 293 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 294 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |     public function demoInformationCollectionAction(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         $repository = $this->getRepository(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |         $contentService = $repository->getContentService(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |         // @todo for demo purpose, user should have necessary permissions by itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |         $repository->getPermissionResolver()->setCurrentUserReference( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |             $repository->getUserService()->loadUserByLogin('admin') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |         $content = $contentService->loadContent(126); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         $contentTypeId = $content->versionInfo->contentInfo->contentTypeId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |         $contentType = $repository->getContentTypeService()->loadContentType($contentTypeId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         $informationCollection = new InformationCollectionStruct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |         $data = new DataWrapper($informationCollection, $contentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |         // No method to create named builder in framework controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |         /** @var \Symfony\Component\Form\FormBuilderInterface $formBuilder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |         $formBuilder = $this->container->get('form.factory')->createBuilder(InformationCollectionType::class, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |         // Adding controls as EzFormsBundle does not do that by itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |         $formBuilder->add('save', SubmitType::class, ['label' => 'Publish']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |         $form = $formBuilder->getForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |         $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         if ($form->isSubmitted() && $form->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |             /** @var InformationCollectionStruct $data */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |             $data = $form->getData()->payload; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             // save data to database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |             // or something else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |             // this is left for end developer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |         return $this->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |             '@NetgenEzForms/demo_form.html.twig', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |                 'form' => $form->createView(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 337 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 338 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.