public function createArticleAction(Request $request)
{
$user = $this->container->get('user')->getCurrentUser();
if (!$user->hasPermission('AddArticle')) {
throw new AccessDeniedException('You do not have the right to add articles.');
}
$form = $this->createForm(new ArticleType(), array());
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->container->get('em');
$articleService = $this->container->get('newscoop_newscoop.article_service');
$attributes = $form->getData();
$language = $em->getRepository('Newscoop\\Entity\\Language')->findOneBy(array('id' => $attributes['language']));
if (!$language) {
throw new EntityNotFoundException('Language was not found');
}
$articleType = $em->getRepository('Newscoop\\Entity\\ArticleType')->findOneBy(array('name' => $attributes['type']));
if (!$articleType) {
throw new EntityNotFoundException('Article type was not found');
}
$publication = $em->getRepository('Newscoop\\Entity\\Publication')->findOneBy(array('id' => $attributes['publication']));
if (!$publication) {
throw new EntityNotFoundException('Publication was not found');
}
$issue = $em->getRepository('Newscoop\\Entity\\Issue')->findOneBy(array('publication' => $publication, 'id' => $attributes['issue']));
$section = $em->getRepository('Newscoop\\Entity\\Section')->findOneBy(array('publication' => $publication, 'issue' => $issue, 'id' => $attributes['section']));
$article = $articleService->createArticle($articleType, $language, $user, $publication, $attributes, $issue, $section);
if (!$user->getAuthor()) {
$author = new \Newscoop\Entity\Author($user->getFirstName(), $user->getLastName());
$em->persist($author);
$user->setAuthor($author);
}
$authorType = $em->getRepository('Newscoop\\Entity\\AuthorType')->findOneBy(array('type' => 'Journalist'));
if (!$authorType) {
$authorType = new \Newscoop\Entity\AuthorType('Journalist');
$em->persist($authorType);
}
$em->flush();
$authorService = $this->container->get('author');
$authorService->addAuthorToArticle($article, $user->getAuthor(), $authorType);
$this->postAddUpdate($article);
$view = FOSView\View::create($article, 201);
$view->setHeader('X-Location', $this->generateUrl('newscoop_gimme_articles_getarticle_language', array('number' => $article->getId(), 'language' => $article->getLanguageCode()), true));
return $view;
}
return $form;
}