fluxRepository = $fluxRepository; $this->projectRepository = $projectRepository; $this->elementTypeFieldProvider = $elementTypeFieldProvider; $this->draftQuery = $draftQuery; $this->elementValueProvider = $elementValueProvider; $this->exportQuery = $exportQuery; $this->exportRepository = $exportRepository; } public function save(int $projectId, array $flux) { if(!empty($flux) && isset($flux['exports'])){ foreach ($flux['exports'] as $exportFlux) { $fluxEntity = $this->fluxRepository->findByExportId($exportFlux['id']); if (empty($fluxEntity)) { $project = $this->projectRepository->findById($projectId); $export = $this->exportRepository->findById($exportFlux['id']); $fluxEntity = new Flux(); $fluxEntity->setProject($project); $fluxEntity->setExport($export); } else { $project = $fluxEntity->getProject(); } $colonnes = ['colonnes' => $exportFlux['colonnes']]; $fluxEntity->setFields(json_encode($colonnes)); $fluxEntity->setSeparator($exportFlux['separatorFlux']); $save = $this->fluxRepository->save($fluxEntity); } if ($save) { if ($project->getCurrentStep() < 5) { $project->setCurrentStep(5); $this->projectRepository->create($project); } $this->draftQuery->changeDraft($projectId); } return true; } return false; } public function readSeparator(int $exportId) { $fluxEntity = $this->fluxRepository->findByExportId($exportId); return !empty($fluxEntity) && !empty($fluxEntity->getSeparator()) ? $fluxEntity->getSeparator() : ";"; } public function getFluxByProject(int $projectId) { $project = $this->projectRepository->findById($projectId); $result = []; if(!empty($project) && !$project->getExports()->isEmpty()){ foreach ($project->getExports() as $export) { $exportType = $this->exportRepository::EXPORT_TYPE[$export->getExportType()]; if(!$export->getDeleted() && in_array($exportType, ["CSV", "EXCEL"])){ $result[$export->getId()] = $this->listArrayJson($projectId, $export->getId()); $result[$export->getId()]['name'] = $export->getName(); $result[$export->getId()]['type'] = $exportType; } } } return $result; } public function listArrayJson(int $projectId, int $exportId) { $result = [ "json" => [], "fields" => [], "elementTypes" => [], "separator" => ";" ]; $arrayField = $this->projectRepository->getArrayFieldByIdProject($projectId); $listFields = []; $listElementType = []; foreach ($arrayField as $fieldsByElementType) { if ($fieldsByElementType["elementTypeFieldsPIM"] !== null && $fieldsByElementType["elementTypePIM"] !== null) { $listFields = array_merge($listFields, $fieldsByElementType["elementTypeFieldsPIM"]); $listElementType[] = $fieldsByElementType["elementTypePIM"]; } } $allFields = $this->elementTypeFieldProvider->findByElementTypes(implode(",", $listElementType), implode(",", $listFields)); // $fluxEntity = $this->fluxRepository->findByProjectId($projectId); $fluxEntity = $this->fluxRepository->findByExportId($exportId); if (!empty($allFields)) { foreach ($allFields as $field) { if (!isset($result['fields'][$field['elementTypeName']])) { $result['fields'][$field['elementTypeName']] = []; $result['elementTypes'][] = [ "id" => $field['elementTypeId'], "name" => $field['elementTypeName'] ]; } $inputs = []; if (!empty($field['fieldsetInputs'])) { $inputs = array_filter($field['fieldsetInputs'], function ($input) { return !in_array($input['fieldType'], ["fieldset", "productLink"]); }); } $result['fields'][$field['elementTypeName']][] = [ "id" => $field['elementTypeFieldId'], "name" => $field['elementTypeFieldName'], "type" => $field['fieldType'], "protocol" => $field['protocol'], "source" => $field['sourceId'], "inputs" => array_values($inputs) ]; } } if (!empty($fluxEntity)) { $result['json'] = json_decode($fluxEntity->getFields()); } $result['separator'] = !empty($fluxEntity) && !empty($fluxEntity->getSeparator()) ? $fluxEntity->getSeparator() : ";"; return $result; } public function list(int $projectId) { $arrayField = $this->projectRepository->getArrayFieldByIdProject($projectId); $listFields = []; $listElementType = []; foreach ($arrayField as $fieldsByElementType) { if ($fieldsByElementType["elementTypeFieldsPIM"] !== null && $fieldsByElementType["elementTypePIM"] !== null) { $listFields = array_merge($listFields, $fieldsByElementType["elementTypeFieldsPIM"]); $listElementType[] = $fieldsByElementType["elementTypePIM"]; } } $allFields = $this->elementTypeFieldProvider->findByElementTypes(implode(",", $listElementType), implode(",", $listFields)); $fluxEntity = $this->fluxRepository->findByProjectId($projectId); $result = []; if ($fluxEntity && $fluxEntity->getFields()) { $fluxFields = json_decode($fluxEntity->getFields(), true); foreach ($fluxFields as $fieldFlux) { // Vérifier si le champ sauvegardé pour le flux existe toujours dans PIM, s'il n'existe pas, il ne doit pas être pris en compte $pos = $this->findField($allFields, $fieldFlux, 'protocol'); if ($pos >= 0) { $result[$fieldFlux['protocol']] = $fieldFlux; } } } // Vérifier s'il y a des mises à jour dans PIM foreach ($allFields as $field) { $protocol = $field['protocol']; if (!isset($result[$protocol]) || ($result[$protocol]['value'] === '' && !isset($result[$protocol]["inputs"]) && empty($result[$protocol]["inputs"]))) { $inputs = []; if ($field['fieldType'] === "fieldset" || $field['fieldType'] === "productLink") { foreach ($field['fieldsetInputs'] as $input) { $inputs[] = [ "id" => $input['id'], "name" => $input['name'], "protocol" => $input['protocol'], "valueInput" => "", ]; } } if (!isset($result[$protocol])) { $result[$protocol] = [ 'id' => $field['elementTypeFieldId'], 'name' => $field['elementTypeFieldName'], 'protocol' => $protocol, 'elementTypes' => [$field['elementTypeName']], 'commun' => false, ]; } $result[$protocol]['value'] = ''; $result[$protocol]['inputs'] = $inputs; if (!in_array($field['elementTypeName'], $result[$protocol]['elementTypes'], false)) { $result[$protocol]['elementTypes'][] = $field['elementTypeName']; } $result[$protocol]['commun'] = count($result[$protocol]['elementTypes']) > 1; } } return $result; } public function list_old(int $projectId) { $arrayField = $this->projectRepository->getArrayFieldByIdProject($projectId); $listFields = []; $listElementType = []; if (!empty($arrayField)) { foreach ($arrayField as $fieldsByElementType) { if ($fieldsByElementType["elementTypeFieldsPIM"] !== null && $fieldsByElementType["elementTypePIM"] !== null) { $listFields = array_merge($listFields, $fieldsByElementType["elementTypeFieldsPIM"]); $listElementType[] = $fieldsByElementType["elementTypePIM"]; } } } $allFields = $this->elementTypeFieldProvider->findByElementTypes(implode(",", $listElementType), implode(",", $listFields)); $fluxEntity = $this->fluxRepository->findByProjectId($projectId); $project = $this->projectRepository->findById($projectId); $fields = []; if (!empty($fluxEntity)) { $fields = json_decode($fluxEntity->getFields(), true); } $result = []; if (!empty($allFields)) { foreach ($allFields as $field) { $flux = array_filter($fields, (function ($fluxField) use ($field) { return $fluxField['protocol'] == $field['protocol']; })); if (!empty($flux)) { if (isset($result[$field['protocol']]) && $result[$field['protocol']]['id'] !== $field['elementTypeFieldId']) { if (isset($result[$field['protocol']]['elementTypes']) && !in_array($field['elementTypeName'], $result[$field['protocol']]['elementTypes'])) { $result[$field['protocol']]['elementTypes'][] = $field['elementTypeName']; } $result[$field['protocol']]['commun'] = true; } else { $result[$field['protocol']] = reset($flux); $result[$field['protocol']]['commun'] = false; $result[$field['protocol']]['elementTypes'] = [$field['elementTypeName']]; } } else { if (isset($result[$field['protocol']]) && $result[$field['protocol']]['id'] !== $field['elementTypeFieldId']) { if (isset($result[$field['protocol']]['elementTypes']) && !in_array($field['elementTypeName'], $result[$field['protocol']]['elementTypes'])) { $result[$field['protocol']]['elementTypes'][] = $field['elementTypeName']; } $result[$field['protocol']]['commun'] = true; } else { $inputs = []; if ($field['fieldType'] == "fieldset" || $field['fieldType'] == "productLink") { foreach ($field['fieldsetInputs'] as $input) { $inputs[] = [ "id" => $input['id'], "name" => $input['name'], "protocol" => $input['protocol'], "value" => "", ]; } } $result[$field['protocol']] = [ "id" => $field['elementTypeFieldId'], "name" => $field['elementTypeFieldName'], "protocol" => $field['protocol'], "value" => "", "inputs" => $inputs, "commun" => false, "elementTypes" => [$field['elementTypeName']] ]; } } } } return $result; } public function fluxCSV(int $exportId, ?int $indexElement) { $export = $this->exportQuery->getExport($exportId); $values = []; $fieldsetMaxCollection = []; $headers = []; $typeExport = ""; if (!empty($export)) { $projectId = $export->getProject()->getId(); $flux = $this->listArrayJson($projectId, $exportId); $typeExport = $this->exportRepository::EXPORT_TYPE[$export->getExportType()]; if (!empty($flux['fields'])) { $list = $this->listElementTypeField($flux['fields']); $elementsPIMExport = $this->exportRepository->getElementsById($exportId); $elements = []; $headers = []; if (!empty($elementsPIMExport)) { foreach ($elementsPIMExport as $elementPIM) { if ($elementPIM['elementsPIM'] != null) { $elements = array_merge($elementPIM['elementsPIM'], $elements); } } if (!empty($flux) && !empty($elements)) { $protocols = []; $fieldsCollection = []; $fieldType = ""; $fieldIdParent = []; foreach ($flux['json']->colonnes as $key => $colonne) { $isFirstElementType = true; $protoFields = []; $protoCol = []; $concat = ""; $type = $colonne->type; foreach ($colonne->elementTypes as $elementType) { foreach ($elementType->fields as $field) { if ($type == "input" && $fieldType == "fieldset") { $listFieldIds = array_column($list['inputs'], 'id'); $listFields = $list['inputs']; } else { $listFieldIds = array_column($list['fields'], 'id'); $listFields = $list['fields']; } $listFieldIds = array_column($listFields, 'id'); $index = array_search($field->id, $listFieldIds); $protocols[] = $listFields[$index]['protocol']; $fields[] = $listFields[$index]['id']; if (!empty($colonne->colonneLink)) { $filter = array_filter($flux['fields'][$elementType->name], function ($elementTypeField) use ($field) { return $elementTypeField['id'] == $field->id; }); if (!empty($filter)) { $fieldType = reset($filter)['type']; $fieldIdParent[$elementType->name] = reset($filter)['id']; } $fieldsCollection[$key][] = $listFields[$index]['id']; } else { if ($isFirstElementType) { $protoFields[] = $listFields[$index]['protocol']; $concat = $elementType->concat; } $protoCol[$elementType->name]['protocol'][] = $listFields[$index]['protocol']; if ($type == "input" && $fieldType == "fieldset" && empty($colonne->colonneLink) && isset($fieldIdParent[$elementType->name])) { $protoCol[$elementType->name]['fieldId'][] = $fieldIdParent[$elementType->name]; $protoCol[$elementType->name]['inputId'][] = $listFields[$index]['id']; } else if ($type == "input" && $fieldType == "productLink" && isset($fieldIdParent[$elementType->name])) { $protoCol[$elementType->name]['fieldId'][] = $fieldIdParent[$elementType->name]; $protoCol[$elementType->name]['inputId'][] = $listFields[$index]['id']; } else { $protoCol[$elementType->name]['fieldId'][] = $listFields[$index]['id']; } $protoCol[$elementType->name]['concat'] = $elementType->concat; } } $isFirstElementType = false; } $proto = $colonne->protocol; if (empty($proto)) { $proto = implode($concat, $protoFields); } $headers[$proto] = $protoCol; } if (!empty($fields)) { $elementIdChunk = array_chunk($elements, 100); $values = $this->elementValueProvider->readByProtocolAndElement($fields, $elements); $fieldsetMaxCollection = $this->findMaxCollection($values, $fieldsCollection); } } } } } $headers = $this->createHeader($headers, $fieldsetMaxCollection, $flux['json']->colonnes); return ['values' => $values, 'flux' => $flux['json']->colonnes, "headers" => $headers['headers'], "assoc" => $headers['assoc'], "separator" => $flux['separator'], "typeExport" => $typeExport]; } private function findMaxCollection($elements, $colonnes) { $result = []; $data = $elements; foreach ($colonnes as $key => $colonne) { $result[$key] = 0; foreach ($colonne as $fieldId) { foreach ($data as $element) { if (array_key_exists($fieldId, $element['fields']) && count($element['fields'][$fieldId]['collections']) > $result[$key]) { $result[$key] = count($element['fields'][$fieldId]['collections']); } } } } return $result; } private function findField($allFields, $filed, $byAttribute): int { foreach ($allFields as $key => $value) { if ($value[$byAttribute] === $filed[$byAttribute]) { return $key; } } return -1; } public function arrayToCsvArray($flux): array { $csvArray = []; foreach ($flux as $key => $value) { $csvArray['Nom Produit'][$key] = $value['name']; $csvArray['Modèle de donnée'][$key] = $value['elementType']; $csvArray[$key] = $value['fields']; // $this->getFieldToCSV($csvArray, $value['fields'], $key); } return $csvArray; } private function getFieldToCSV(&$csvArray, $fields, $key, $index = 1): void { foreach ($fields as $fieldName => $fieldValue) { if (isset($fieldValue['collections'])) { $index = 1; foreach ($fieldValue['collections'] as $value) { // $this->getFieldToCSV($csvArray, $value, $key, $index); // $index ++; $csvArray[$fieldName][$key][] = $this->prepareFieldset($value); } } else { if (is_array($fieldValue)) { $fieldValue = implode(',', $fieldValue); } $fieldName = str_replace('{X}', $index, $fieldName); $csvArray[$fieldName][$key] = $fieldValue; } } } // Fieldset in Fieldset no develop private function prepareFieldset($collection) { $result = []; foreach ($collection as $key => $input) { if (is_array($input)) { if(!isset($input['collections'])){ $input = implode(',', $input); } else { $input = ""; } } $result[$key] = $input; } return $result; } private function createHeader($headers, $fieldsetMaxCol, $flux): array { $result = []; $index = 0; $colToSkip = []; foreach ($headers as $key => $header) { if (!in_array($key, $colToSkip)) { $col = $flux[$index]; if (!array_key_exists($index, $fieldsetMaxCol)) { $result["headers"][] = $key; $result["assoc"][] = $header; } else { for ($i = 0; $i < $fieldsetMaxCol[$index]; $i++) { foreach ($col->colonneLink as $indexInput) { $input = $flux[$indexInput]; $proto = str_replace("{X}", $i + 1, $input->protocol); $result["headers"][] = $proto; $headerCollection = $headers[$input->protocol]; foreach ($headers[$input->protocol] as $keyInput => $dataModel) { for ($j = 0; $j < count($dataModel['protocol']); $j++) { $headerCollection[$keyInput]['protocol'][$j] = str_replace("{X}", $i + 1, $headers[$input->protocol][$keyInput]['protocol'][$j]); $headerCollection[$keyInput]['indexCol'] = $i; } } $result["assoc"][] = $headerCollection; $colToSkip[] = $input->protocol; } } } } $index++; } return $result; } private function listElementTypeField($elementTypes) { $result = []; foreach ($elementTypes as $elementType) { foreach ($elementType as $field) { if (!empty($field['inputs'])) { foreach ($field['inputs'] as $input) { if ($field['type'] == "fieldset") { $result['inputs'][] = [ "id" => $input['id'], "protocol" => $input['protocol'] ]; } else { $result['fields'][] = [ "id" => $input['id'], "protocol" => $input['protocol'] ]; } } } $result['fields'][] = [ "id" => $field['id'], "protocol" => $field['protocol'] ]; } } return $result; } }