container = $_container; $this->entityManager = $_container->get('doctrine.orm.entity_manager'); $this->class = $_class; $this->form_forge_warehouse = $_form_forge_warehouse; $this->session = $this->container->get('session'); $this->templating = $this->container->get('twig'); $this->box = $this->container->get('net15.box.manager')->getByName("form_forge_bundle", "systemBox"); } /** * @param string $csrf * @param FormForgeReasonInterface $reason * @param Collection $formForgeField * @param array $dataResponse * @param string $locale * @param CustomUserInterface|null $creator * @return bool|FormForgeReasonInterface|FormForgeReasonResponseInterface|null * @throws \Exception */ public function createFieldResponse(string $csrf, FormForgeReasonInterface $reason, Collection $formForgeField, array $dataResponse, string $locale, ?CustomUserInterface $creator = null){ // if(is_null($creator)) { // $creator = $this->storage->getToken()->getUser(); // // if(!$creator instanceof CustomUserInterface) // return false; // } // Create reason response $reasonResponse = $this->container->get('net15.form.forge.reasons.response.manager')->createReasonResponse($reason, $locale, $creator); if (!$reasonResponse instanceof FormForgeReasonResponse) return null; $index = 0; // Create directory $directory = $this->container->get('net15.directory.manager')->createSystemBoxDirectory('Reason Response #'.$reason->getId(), null, $this->box ); $reasonResponse->setDirectory($directory); // Add response data foreach ($dataResponse as $data){ $formForgeFieldResponse = new FormForgeFieldResponse($reasonResponse); $formForgeFieldResponse->setOrder(count($reasonResponse->getFieldResponse())); if ($data != ""){ $formForgeFieldResponse->setValue($data); if($formForgeField[$index]->getFormForgeType()->getParticularTemplate()) { $template = $formForgeField[$index]->getFormForgeType()->getCustomTemplate(); /** * @var FormForgeTemplate $template */ $html = $this->templating->render($template->getTwigName(),['response_value'=>$data]); $formForgeFieldResponse->setAdditionalValue($html); } // Check if field type is file type if($formForgeField[$index]->getFormForgeType()->getName() == 'form.forge.file') { // Recuperate csrf for file field type $fileUploadCSRF= $this->session->get('_csrf/'.$formForgeField[$index]->getId()); $filesData = json_decode($data); foreach ($filesData as $key => $value) { // Create FileItem and move files $fileItem = $this->container->get('net15.file.item.manager')->create( $this->form_forge_warehouse.DIRECTORY_SEPARATOR.$key, $value ); // Add fileItem to Directory $directory->addFile($fileItem); // Add file to Field response $formForgeFieldResponse->addFile($fileItem); // empty the session $this->session->set('files_'.$fileUploadCSRF, null); } $this->container->get('net15.directory.manager')->quickUpdate($directory); } }else{ $formForgeFieldResponse->setValue("----"); } $formForgeFieldResponse->setFormForgeField($formForgeField[$index]); $this->quickUpdateFieldResponse($formForgeFieldResponse); $index++; // Adding the answer to the "FieldsResponse()" collection $reasonResponse->addFieldResponse($formForgeFieldResponse); $this->quickUpdateReasonResponse($reasonResponse); } $this->container->get('net15.form.forge.reasons.response.manager')->callEvents($reasonResponse); return $reasonResponse; } public function quickUpdateFieldResponse(FormForgeFieldResponseInterface $element){ if(empty($element)) return false; try { $this->entityManager->persist($element); $this->entityManager->flush(); } catch (ORMException $e) { return false; } return true; } public function quickUpdateReasonResponse(FormForgeReasonResponseInterface $element){ if(empty($element)) return false; try { $this->entityManager->persist($element); $this->entityManager->flush(); } catch (ORMException $e) { return false; } return true; } }