Commit 657f81be authored by Jean-Baptiste Oellers's avatar Jean-Baptiste Oellers
Browse files

Ajout remplacer dans fichier

parent f1151386
Loading
Loading
Loading
Loading
+98 −24
Original line number Diff line number Diff line
@@ -131,40 +131,114 @@ class ConventionModeleEtapeAdministrationController extends AbstractOscarControl
            $documentPath = $documentLocation . DIRECTORY_SEPARATOR . $conventionModeleEtape->fichier;


            $conventionModeleFileContent = file_get_contents('zip://' . $documentPath . "#content.xml");

            $reader = new \XMLReader();
            $reader->open('zip://' . $documentPath . "#content.xml");
            $odt_meta = array();
            while ($reader->read()) {
                if ($reader->nodeType == \XMLREADER::ELEMENT) {
                    $elm = $reader->name;
                } else {
                    if ($reader->nodeType == \XMLREADER::END_ELEMENT && $reader->name == 'office:meta') {
            $conventionModeleFileContentReplaced = replace_in_xml($conventionModeleFileContent, "MES_CONVENTIONS_REMPLACER[Coucou]", "OKJB");

            $slugify = new \Cocur\Slugify\Slugify();
            $tmp_location = sprintf("/tmp/oscar-conventions-%s-%s",
                $slugify->slugify((new \DateTime())->format(\DateTime::ATOM)),
                $slugify->slugify($conventionModeleEtape->fichier)
            );

            file_put_contents($tmp_location, $conventionModeleFileContentReplaced);

            $documentPath2 = $documentLocation . DIRECTORY_SEPARATOR . $conventionModeleEtape->fichier . 'v2';
            copy($documentPath, $documentPath2);

            // copy($tmp_location, $documentPath2);

            $za = new \ZipArchive();
            $za->open($documentPath2);
            $i = 0;
            for ($i=0; $i<$za->numFiles;$i++) {
                if ($za->statIndex($i)['name'] == "content.xml") {
                    $za->replaceFile($tmp_location, $i);
                    break;
                }
                    if (!trim($reader->value)) {
                        continue;
            }
                    $odt_meta[$elm] = $reader->value;
            $za->close();

        }

        return $this->redirect()->toRoute('administration/conventionModeles/configurer/etapes/configurer', ['id' => $conventionModele->id, 'idEtape' => $conventionModeleEtape->id]);
    }
}

function replace_in_xml(string $xmlString, string $lookingFor, string $replaceWith) : string {
    $xmlStringSplitPerCharacters = mb_str_split($xmlString, 1, "UTF-8");
    $xmlStringSplitPerCharactersLength = count($xmlStringSplitPerCharacters);
    if ($xmlStringSplitPerCharactersLength == 0) {
        return $xmlString;
    }
    $lookingForSplitPerCharacters = mb_str_split($lookingFor, 1, "UTF-8");
    $lookingForSplitPerCharactersLength = count($lookingForSplitPerCharacters);
    if ($lookingForSplitPerCharactersLength == 0) {
        return $xmlString;
    }



    $lookingForCurrentCharacterIndex = 0;

            // $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($documentPath);
            // //$templateProcessor->setValue('name', 'myvar');
            // //$templateProcessor->saveAs($documentPath . '2');
    $resultString = "";
    $notYetTotallyFoundBuffer = "";
    $currentPosition = 0;
    $startIgnoringTagPositionFirstOfAll = -1;
    $startIgnoringTagPosition = -1;
    for ($currentPosition = 0; $currentPosition < $xmlStringSplitPerCharactersLength; $currentPosition++) {

            // \PhpOffice\PhpWord\Settings::setPdfRendererName(\PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF);
            // \PhpOffice\PhpWord\Settings::setPdfRendererPath(__DIR__ . '/../../../../../vendor/dompdf/dompdf');
            // $phpWord = \PhpOffice\PhpWord\IOFactory::load($documentPath);
            // $pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
            // $pdfWriter->save($documentPath . '.pdf');
        // We are ignoring xml tag
        if ($startIgnoringTagPosition != -1) {
            if ($xmlStringSplitPerCharacters[$currentPosition] == '>') {
                $startIgnoringTagPosition = -1;
            }

        // We found the character we are looking for
        } else if ($xmlStringSplitPerCharacters[$currentPosition] == $lookingForSplitPerCharacters[$lookingForCurrentCharacterIndex]) {

            $lookingForCurrentCharacterIndex++;

            // Whole match found
            if ($lookingForCurrentCharacterIndex == $lookingForSplitPerCharactersLength) {
                $resultString .= $replaceWith;
                $notYetTotallyFoundBuffer = "";
                $lookingForCurrentCharacterIndex = 0;

                $startIgnoringTagPositionFirstOfAll = -1;
                $startIgnoringTagPosition = -1;
            } else {
                // Partial match
                $notYetTotallyFoundBuffer .= $xmlStringSplitPerCharacters[$currentPosition];
            }

        return $this->redirect()->toRoute('administration/conventionModeles/configurer/etapes/configurer', ['id' => $conventionModele->id, 'idEtape' => $conventionModeleEtape->id]);
        } else if ($lookingForCurrentCharacterIndex != 0 && $xmlStringSplitPerCharacters[$currentPosition] == '<') {
            if ($startIgnoringTagPositionFirstOfAll == -1) {
                $startIgnoringTagPositionFirstOfAll = $currentPosition;
            }
            $startIgnoringTagPosition = $currentPosition;

        // We didn't find the character we were expecting
        } else if ($startIgnoringTagPositionFirstOfAll != -1) {
            $currentPosition = $startIgnoringTagPositionFirstOfAll;
            $startIgnoringTagPositionFirstOfAll = -1;
            $startIgnoringTagPosition = -1;
            $notYetTotallyFoundBuffer = "";
            $lookingForCurrentCharacterIndex = 0;
            
        } else if ($lookingForCurrentCharacterIndex != 0) {

            // If we were in the middle of a potential match, restore what was already read
            $resultString .= $notYetTotallyFoundBuffer;
            $notYetTotallyFoundBuffer = "";
            $lookingForCurrentCharacterIndex = 0;
            $currentPosition--; // Reconsidere current character, because it could be the start of the match

        } else {
            // Just add the character to the result string
            $resultString .= $xmlStringSplitPerCharacters[$currentPosition];
        }
    }

    return $resultString;
}
+76 −3
Original line number Diff line number Diff line
@@ -2,8 +2,9 @@

namespace Oscar\Controller;

class ConventionModeleInstanceController extends AbstractOscarController
class ConventionModeleInstanceController extends AbstractOscarController implements \Oscar\Traits\UseOscarConfigurationService
{
    use \Oscar\Traits\UseOscarConfigurationServiceTrait;

    /** @var \Oscar\Service\ConventionModeleInstanceService */
    public $conventionModeleInstanceService;
@@ -23,7 +24,7 @@ class ConventionModeleInstanceController extends AbstractOscarController
        if ($this->params()->fromPost('action') == "enregistrer_reponse") {
            $this->conventionModeleInstanceService->enregitrerRéponse($conventionModeleInstance, $this->params()->fromQuery('question', ''), $this->params()->fromQuery('reponse', ''), $this->getCurrentPerson());

            $this->créerActivité();
            $this->créerActivité($conventionModeleInstance->conventionModeleEtapeEnCours);

            $conventionModeleInstance->conventionModeleEtapeEnCours = NULL;
            $conventionModeleInstance->updatedBy = $this->getCurrentPerson();
@@ -34,13 +35,85 @@ class ConventionModeleInstanceController extends AbstractOscarController
        return $this->redirect()->toRoute('conventionModeleInstances/afficher', ['id' => $conventionModeleInstance->id]);
    }

    private function créerActivité() {
    private function créerActivité(\Oscar\Entity\ConventionModeleEtape $conventionModeleEtape) {
        $newActivity = new \Oscar\Entity\Activity();
        $newActivity->setCurrency($this->getEntityManager()->getRepository(\Oscar\Entity\Currency::class)->findAll()[0]);
        $newActivity->setPcruValidPoleCompetitivite(false);
        $newActivity->setDisciplines([]);
        $newActivity->setMotscles([]);
        $this->getEntityManager()->persist($newActivity);

        if (mb_strlen(trim($conventionModeleEtape->fichier)) > 0) {

            // Copie du fichier
            $destination = $this->getOscarConfigurationService()->getDocumentDropLocation();
            $documentPath = $destination . DIRECTORY_SEPARATOR . $conventionModeleEtape->fichier;


            $nextId = $this->getEntityManager()->getRepository(\Oscar\Entity\ContractDocument::class)->getLastDocumentId();
            $originalFilename = "convention.odt";
            $renameOriginal = "$nextId-" . $originalFilename;
            $slugify = new \Cocur\Slugify\Slugify();
            $renamed = sprintf(
                "oscar-%s-%s-%s-%s",
                $newActivity->getId(),
                1,
                $nextId,
                $slugify->slugify($originalFilename)
            ) . ".odt";

            $newDocumentPath = $destination . DIRECTORY_SEPARATOR . $renamed;
            copy($documentPath, $newDocumentPath);
            $fileSize = filesize($newDocumentPath);

            $tabDocuments = $this->getEntityManager()->getRepository(\Oscar\Entity\TabDocument::class)->findBy([], ['label' => 'ASC']);
            $tabDocument = NULL;
            foreach ($tabDocuments as $t) {
                if ($t->isDefault()) {
                    $tabDocument = $t;
                    break;
                }
            }
            if ($tabDocument == NULL && count($tabDocuments) > 0) {
                $tabDocument = $tabDocuments[0];
            }

            $typesDocumentsDatas = $this->getEntityManager()->getRepository(\Oscar\Entity\TypeDocument::class)->getTypes();
            $typeDocument = NULL;
            foreach ($typesDocumentsDatas as $t) {
                if ($t->isDefault()) {
                    $typeDocument = $t;
                    break;
                }
            }
            if ($typeDocument == NULL && count($typesDocumentsDatas) > 0) {
                $typeDocument = $typesDocumentsDatas[0];
            }

            // Création du document
            $document = new \Oscar\Entity\ContractDocument();
            $this->getEntityManager()->persist($document);

            $document
                ->setVersion(1)
                ->setDateUpdoad(new \DateTime())
                ->setFileName($renameOriginal)
                ->setPath($renamed)
                ->setLocation(\Oscar\Entity\ContractDocument::LOCATION_LOCAL_FILE)
                ->setFileTypeMime("application/vnd.oasis.opendocument.text")
                ->setFileSize($fileSize)
                //->setPerson($sender)
                ->setTabDocument($tabDocument)
                ->setTypeDocument($typeDocument)
                ->setGrant($newActivity);
                //->setInformation($description)
                //->setDateDeposit($dateDeposit)
                //->setDateSend($dateSend);

        }
        
        

        $this->getEntityManager()->flush();
    }

+1 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ class ConventionModeleInstanceControllerFactory implements FactoryInterface
        $c = new ConventionModeleInstanceController();
        $c->setEntityManager($container->get(EntityManager::class));
        $c->setOscarUserContextService($container->get(OscarUserContext::class));
        $c->setOscarConfigurationService($container->get(\Oscar\Service\OscarConfigurationService::class));
        $c->conventionModeleInstanceService = $container->get(\Oscar\Service\ConventionModeleInstanceService::class);
        return $c;
    }