Commit 20c5c6cf authored by dj3c1t's avatar dj3c1t

import.previous.version : importAuthors

parent 53617406
......@@ -6,9 +6,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Filesystem\Filesystem;
use Dj3c1t\AppBundle\Entity\User;
use Dj3c1t\SourceMLBundle\Entity\Licence;
use Dj3c1t\SourceMLBundle\Entity\Author;
class ImportPreviousVersion {
......@@ -18,6 +23,7 @@ class ImportPreviousVersion {
protected $mw_data;
protected $mw_sgbd;
protected $logs;
protected $webDir;
public function __construct(Container $container) {
$this->container = $container;
......@@ -35,6 +41,9 @@ class ImportPreviousVersion {
$this->mw_env = $this->mw_app->env();
$this->mw_data = $this->mw_env->data();
$this->mw_sgbd = $this->mw_data->sgbd();
$this->webDir =
dirname($this->container->get('kernel')->getRootDir())
."/".$this->container->getParameter("web_dir");
}
// ----------------------------------------------------------------------
......@@ -52,10 +61,11 @@ class ImportPreviousVersion {
.dirname($this->container->get('kernel')->getRootDir())
);
$this->cleanTables();
$this->cleanMediasDir();
$this->importUsers();
$this->importLicences();
/*
$this->importAuthors();
/*
$this->importSources();
$this->importSourcesAuthors();
$this->importSourcesInfos();
......@@ -87,6 +97,12 @@ class ImportPreviousVersion {
}
}
public function cleanMediasDir() {
$this->log("clean new installation medias dir");
$fs = new Filesystem();
$fs->remove($this->webDir."/medias");
}
// ----------------------------------------------------------------------
// users
//
......@@ -182,10 +198,58 @@ class ImportPreviousVersion {
public function importAuthors() {
$this->log("import authors");
$em = $this->container->get('doctrine')->getManager();
$userRepo = $em->getRepository('Dj3c1tAppBundle:User');
if(($groupes = $this->mw_data->groupes()) === false) {
$this->error("can't load authors from mw_data");
}
$upload_manager = $this->container->get('dj3c1t_jq_file_upload.upload_manager');
foreach($groupes["list"] as $mw_groupe) {
$this->log("creating new author ".$mw_groupe["nom"]);
if(!($user = $userRepo->find($mw_groupe["id_user"]))) {
$this->log("can't load user ".$mw_groupe["id_user"]);
$this->log("WARNING skiping author : ".$mw_groupe["nom"]);
continue;
}
$this->loginAs($user);
$author = new Author();
$this->ignoreIdGenerator($author);
$author->setId($mw_groupe["id"]);
$author->setName($mw_groupe["nom"]);
$author->setUser($user);
$author->setDescription($mw_groupe["description"]);
$author->setEmail($mw_groupe["email"]);
$author->setHasContactForm($mw_groupe["contact_form"] == 1);
$author->setUseCaptcha($mw_groupe["captcha"] == 1);
$em->persist($author);
$this->log("persisting author to database");
$em->flush();
if($mw_groupe["image"]) {
$this->log("add author logo");
$upload_manager->init("sourceml_author_logo", $author->getId());
$media = $upload_manager->importMediaFromLocalFile(
$this->webDir."/".$this->mw_env->path("content")
."/uploads/".$mw_groupe["image"]
);
if(isset($media)) {
if($error = $media->getError()) {
$this->log("can't make media from author logo");
$this->log($error);
$this->log("WARNING skiping author logo for : ".$mw_groupe["nom"]);
}
else {
$em->persist($media);
$author->setImage($media);
$em->flush();
}
}
else {
$this->log("can't make media from author logo");
$this->log("WARNING skiping author logo for : ".$mw_groupe["nom"]);
}
}
// todo ...
}
}
// ----------------------------------------------------------------------
......@@ -318,4 +382,21 @@ class ImportPreviousVersion {
$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
}
public function loginAs(User $user) {
$token = new UsernamePasswordToken(
$user,
$user->getPassword(),
"secured_area",
$user->getRoles()
);
$this->container->get("security.token_storage")->setToken($token);
$this->container->get("event_dispatcher")->dispatch(
"security.interactive_login",
new InteractiveLoginEvent(
$this->container->get('request_stack')->getCurrentRequest(),
$token
)
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment