Commit ab4640f1 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Système d'initialisation des données (Import/Export)

parent 6683a9fb
Loading
Loading
Loading
Loading
Loading
+229 −0
Original line number Diff line number Diff line
{
  "activity_type": [
    {
      "label": "ANR",
      "description": "",
      "lft": 3,
      "rgt": 4,
      "nature": "0",
      "status": 1
    },
    {
      "label": "PIA",
      "description": "",
      "lft": 5,
      "rgt": 6,
      "nature": "0",
      "status": 1
    },
    {
      "label": "Subvention nationale",
      "description": "",
      "lft": 2,
      "rgt": 9,
      "nature": "0",
      "status": 1
    },
    {
      "label": "BPI",
      "description": "",
      "lft": 7,
      "rgt": 8,
      "nature": "0",
      "status": 1
    },
    {
      "label": "CPER",
      "description": "",
      "lft": 11,
      "rgt": 12,
      "nature": "0",
      "status": 1
    },
    {
      "label": "Subvention r\u00e9gionale",
      "description": "",
      "lft": 10,
      "rgt": 15,
      "nature": "0",
      "status": 1
    },
    {
      "label": "CPIER",
      "description": "",
      "lft": 13,
      "rgt": 14,
      "nature": "0",
      "status": 1
    },
    {
      "label": "EUREKA",
      "description": "",
      "lft": 17,
      "rgt": 18,
      "nature": "0",
      "status": 1
    },
    {
      "label": "FP6",
      "description": "",
      "lft": 19,
      "rgt": 20,
      "nature": "0",
      "status": 1
    },
    {
      "label": "FP7",
      "description": "",
      "lft": 21,
      "rgt": 22,
      "nature": "0",
      "status": 1
    },
    {
      "label": "H2020",
      "description": "",
      "lft": 23,
      "rgt": 24,
      "nature": "0",
      "status": 1
    },
    {
      "label": "ROOT",
      "description": "",
      "lft": 1,
      "rgt": 28,
      "nature": "Recherche et valorisation",
      "status": 1
    },
    {
      "label": "Subvention europ\u00e9enne",
      "description": "",
      "lft": 16,
      "rgt": 27,
      "nature": "0",
      "status": 1
    },
    {
      "label": "INTERREG",
      "description": "",
      "lft": 25,
      "rgt": 26,
      "nature": "0",
      "status": 1
    }
  ],
  "currency": [
    {
      "label": "Euro",
      "rate": 1,
      "symbol": "\u20ac"
    },
    {
      "label": "Livre",
      "rate": 0.7133,
      "symbol": "\u00a3"
    },
    {
      "label": "Yens",
      "rate": 132.651,
      "symbol": "\u00a5"
    },
    {
      "label": "Dollars",
      "rate": 1.096,
      "symbol": "$"
    }
  ],
  "tva": [
    {
      "label": "Exon\u00e9r\u00e9",
      "rate": 0,
      "active": true,
      "status": 1
    },
    {
      "label": "Taux r\u00e9duit (5,5%)",
      "rate": 5.5,
      "active": true,
      "status": 1
    },
    {
      "label": "Taux normal (19,6%)",
      "rate": 19.6,
      "active": true,
      "status": 1
    },
    {
      "label": "Taux DOM-TOM",
      "rate": 8.5,
      "active": true,
      "status": 1
    },
    {
      "label": "Taux r\u00e9duit 7%",
      "rate": 7,
      "active": true,
      "status": 1
    },
    {
      "label": "Taux normal 20%",
      "rate": 20,
      "active": true,
      "status": 1
    },
    {
      "label": "Taux r\u00e9duit 10%",
      "rate": 10,
      "active": true,
      "status": 1
    }
  ],
  "organization_type": [
    {
      "label": "Collectivit\u00e9",
      "description": "",
      "root": null
    },
    {
      "label": "Laboratoire",
      "description": "",
      "root": null
    },
    {
      "label": "Composante",
      "description": "",
      "root": null
    },
    {
      "label": "Institution",
      "description": "",
      "root": null
    },
    {
      "label": "Priv\u00e9",
      "description": "",
      "root": null
    },
    {
      "label": "Soci\u00e9t\u00e9",
      "description": "",
      "root": "Priv\u00e9"
    },
    {
      "label": "Fondation",
      "description": "",
      "root": "Priv\u00e9"
    },
    {
      "label": "Publique",
      "description": "",
      "root": null
    },
    {
      "label": "Association",
      "description": "",
      "root": "Priv\u00e9"
    }
  ]
}
 No newline at end of file
+13 −7
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ class OscarFixturesExportCommand extends OscarCommandAbstract
            ->addOption(
                'keys',
                'k',
                InputOption::VALUE_NONE,
                InputOption::VALUE_OPTIONAL,
                'Clefs à exporter'
            );
    }
@@ -46,14 +46,15 @@ class OscarFixturesExportCommand extends OscarCommandAbstract

        $io = new SymfonyStyle($input, $output);

        $io->title("Importation des données");


        $replace = $input->getOption('replace');
        $keys = $input->getOption('keys');
        $keysInput = $input->getOption('keys');
        if ($keysInput) {
            $keys = explode(',', $keysInput);
        } else {
            $keys = array_keys(FixtureService::getAllKeys());
        }
        $file = $input->getArgument('file');


        if ($file != null && file_exists($file)) {
            if (!$replace) {
                $io->error("Fichier '$file' existe déjà.'");
@@ -66,10 +67,15 @@ class OscarFixturesExportCommand extends OscarCommandAbstract
        /** @var FixtureService $fixtureService */
        $fixtureService = $this->getServicemanager()->get(FixtureService::class);

        $datas = $fixtureService->export(['tva']);
        $datas = $fixtureService->export($keys);

        $out = json_encode($datas, JSON_PRETTY_PRINT);

        if ($file === null) {
            echo $out;
        } else {
            $io->title("Importation des données dans '$file'");
            file_put_contents($file, $out);
        }


+42 −0
Original line number Diff line number Diff line
<?php

namespace Oscar\Command;

use Doctrine\ORM\EntityManager;
use Oscar\Entity\Privilege;
use Oscar\Entity\Role;
use Oscar\Service\Fixture\FixtureService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class OscarInitDataCoreCommand extends OscarCommandAbstract
{
    protected static $defaultName = 'initdata:core';

    protected function configure()
    {
        $this
            ->setDescription("Ajoute les données initiales de base (depuis le fichier install/init-data-core.json");
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->addOutputStyle($output);

        $io = new SymfonyStyle($input, $output);

        $io->title("Initialisation des rôles");


        /** @var FixtureService $fixtureService */
        $fixtureService = $this->getServicemanager()->get(FixtureService::class);

        $json = file_get_contents('install/init-data-core.json');

        $fixtureService->import(json_decode($json, true));

        return Command::SUCCESS;
    }
}
+5 −5
Original line number Diff line number Diff line
@@ -11,9 +11,9 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class OscarRolesInitCommand extends OscarCommandAbstract
class OscarInitDataRolesCommand extends OscarCommandAbstract
{
    protected static $defaultName = 'roles:init';
    protected static $defaultName = 'initdata:roles';

    protected function configure()
    {
@@ -37,7 +37,7 @@ class OscarRolesInitCommand extends OscarCommandAbstract
    { "user_role": [
        {
          "parent_id": null,
          "role_id": "Utilisateur",
          "roleId": "Utilisateur",
          "is_default": true,
          "ldap_filter": "",
          "spot": 4,
@@ -48,7 +48,7 @@ class OscarRolesInitCommand extends OscarCommandAbstract
        },
        {
          "parent_id": null,
          "role_id": "Administrateur",
          "roleId": "Administrateur",
          "is_default": false,
          "ldap_filter": null,
          "spot": 4,
@@ -61,7 +61,7 @@ class OscarRolesInitCommand extends OscarCommandAbstract
    }
EOF;

        $fixtureService->import(json_decode($json, true));
        $fixtureService->import(json_decode($json, true), [FixtureService::USER_ROLE]);

        /** @var EntityManager $entityManager */
        $entityManager = $this->getServicemanager()->get(EntityManager::class);
+33 −32
Original line number Diff line number Diff line
@@ -2,7 +2,13 @@

namespace Oscar\Service\Fixture;

use Oscar\Service\Fixture\io\AbstractFixtureIO;
use Oscar\Service\Fixture\io\ActivityTypeIO;
use Oscar\Service\Fixture\io\CategoryPrivilegeIO;
use Oscar\Service\Fixture\io\CurrencyIO;
use Oscar\Service\Fixture\io\OrganizationRoleIO;
use Oscar\Service\Fixture\io\OrganizationTypeIO;
use Oscar\Service\Fixture\io\PrivilegeIO;
use Oscar\Service\Fixture\io\TvaIO;
use Oscar\Service\Fixture\io\UserRoleIO;
use Oscar\Traits\UseEntityManager;
@@ -18,49 +24,62 @@ class FixtureService implements UseEntityManager, UseLoggerService, UseOscarConf

    const ALL = 'all';
    const CURRENCY = 'currency';
    const ORGANIZATION_TYPE = 'organization_type';
    const ORGANIZATION_ROLE = 'organization_role';

    const TVA = 'tva';
    const USER_ROLE = 'user_role';
    const PRIVILEGE = 'privilege';
    const CATEGORY_PRIVILEGE = 'category_privilege';
    const ACTIVITY_TYPE = 'activity_type';

    private array $keys;

    public function __construct()

    public static function getAllKeys(): array
    {
        $this->keys = [
        return [
            self::ACTIVITY_TYPE => ActivityTypeIO::class,
            self::CURRENCY => CurrencyIO::class,
            self::TVA => TvaIO::class,
            self::USER_ROLE => UserRoleIO::class,
            self::ORGANIZATION_TYPE => OrganizationTypeIO::class,
            self::ORGANIZATION_ROLE => OrganizationRoleIO::class,
//            self::CATEGORY_PRIVILEGE => CategoryPrivilegeIO::class,
//            self::PRIVILEGE => PrivilegeIO::class,
        ];
    }

    public function import(array $fixtures, ?array $keys = null): void
    {
        if ($keys === null) {
            $keys = $this->keys;
            $keys = array_keys(self::getAllKeys());
        }
        foreach ($fixtures as $key => $fixture) {
            if (array_key_exists($key, $keys)) {
                $ioClass = $this->keys[$key];
                $io = new $ioClass($this->getEntityManager(), $this->getLoggerService());

        $allowedKeys = self::getAllKeys();

        foreach ($allowedKeys as $key => $class) {
            if (array_key_exists($key, $fixtures) && in_array($key, $keys)) {
                $fixture = $fixtures[$key];
                /** @var AbstractFixtureIO $io */
                $io = new $class($this->getEntityManager(), $this->getLoggerService());
                $io->syncAll($fixture);
            } else {
                $this->getLoggerService()->info("Clef $key ignorée");
            }
        }
    }

    public function export(array $keys): array
    public function export(?array $keys = null): array
    {
        $exported = [];
        if ($keys === null) {
            $keys = $this->keys;
            $keys = self::getAllKeys();
        }

        var_dump($keys);
        $allowedKeys = self::getAllKeys();

        foreach ($keys as $key) {
            if (in_array($key, $keys)) {
                $ioClass = $this->keys[$key];
            if (array_key_exists($key, $allowedKeys)) {
                $ioClass = $allowedKeys[$key];
                $io = new $ioClass($this->getEntityManager(), $this->getLoggerService());
                $exported[$key] = $io->export();
            } else {
@@ -70,22 +89,4 @@ class FixtureService implements UseEntityManager, UseLoggerService, UseOscarConf

        return $exported;
    }

    private function importUserRole(mixed $fixture)
    {
        $fixtureIO = new UserRoleIO($this->getEntityManager(), $this->getLoggerService());
        $fixtureIO->syncAll($fixture);
    }

    private function importCurrency(mixed $fixture)
    {
        $fixtureIO = new CurrencyIO($this->getEntityManager(), $this->getLoggerService());
        $fixtureIO->syncAll($fixture);
    }

    private function importTva(mixed $fixture)
    {
        $fixtureIO = new TvaIO($this->getEntityManager(), $this->getLoggerService());
        $fixtureIO->syncAll($fixture);
    }
}
 No newline at end of file
Loading