Skip to content

Commit

Permalink
Implements the newscoop_gimme_articles_createarticle endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
terwey committed Jul 25, 2014
1 parent 56d24c3 commit 14da710
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,55 +35,45 @@ class ArticlesController extends FOSRestController
* 201="Returned when Article is created"
* },
* parameters={
* {"name"="language", "dataType"="string", "required"=true, "description"="Language code"}
* {"name"="issue", "dataType"="integer", "required"=true, "description"="Issue number"}
* {"name"="section", "dataType"="integer", "required"=true, "description"="Section number"}
* {"name"="articleType", "dataType"="integer", "required"=true, "description"="Article Type number"}
* {"name"="language", "dataType"="string", "required"=true, "description"="Language code"},
* {"name"="publication", "dataType"="integer", "required"=true, "description"="Publication ID"},
* {"name"="issue", "dataType"="integer", "required"=true, "description"="Issue number"},
* {"name"="section", "dataType"="integer", "required"=true, "description"="Section number"},
* {"name"="articleType", "dataType"="string", "required"=true, "description"="Article Type name"},
* {"name"="articleTitle", "dataType"="string", "required"=true, "description"="Article name"},
* }
* )
*
* @Route("/articles/create/{language}/{issue}/{section}/{articleType}.{_format}", defaults={"_format"="json", "language"="en"}, options={"expose"=true}, name="newscoop_gimme_articles_createarticle")
* @Route("/articles/create/{language}/{publication}/{issue}/{section}/{articleType}/{articleTitle}.{_format}", defaults={"_format"="json", "language"="en"}, options={"expose"=true}, name="newscoop_gimme_articles_createarticle")
* @Method("POST")
*/
public function createArticleAction(Request $request, $language, $issue, $section, $articleType)
public function createArticleAction(Request $request, $language, $publication, $issue, $section, $articleType, $articleTitle)
{
$content = $this->get("request")->getContent();
if (!empty($content)) {
$params = json_decode($content, true); // 2nd param to get as array
}

if (!array_key_exists('title', $params)) {
throw new \Exception('Article title is missing');
}

// Create article
$em = $this->container->get('em');
$languageObject = $em->getRepository('Newscoop\Entity\Language')->findOneByCode($language);

// Fetch article
$articleObj = new \Article($languageObject->getId());

// the following is mostly a copy from admin-files/articles/do_add.php
// This is here if we decide to later use default Publication anyway
//$publication = $this->get('newscoop_newscoop.publication_service')->getPublication()->getId();

$publication = $this->get('newscoop_newscoop.publication_service')->getPublication()->getId();

$conflictingArticles = Article::GetByName($params['title'], $publication, $issue, $section, null, true);
$conflictingArticles = \Article::GetByName($articleTitle, $publication, $issue, $section, null, true);
if (count($conflictingArticles) > 0) {
$conflictingArticle = array_pop($conflictingArticles);
$conflictingArticleLink = camp_html_article_url($conflictingArticle, $conflictingArticle->getLanguageId(), 'edit.php');
throw new \Exception($translator->trans("You cannot have two articles in the same section with the same name. The article name you specified is already in use by the article $1.",
array('$1' => $conflictingArticle->getNumber() ." ".$conflictingArticle->getName()."</a>"), 'articles'));
throw new \Exception("You cannot have two articles in the same section with the same name. The article name you specified is already in use by the article ".$conflictingArticle->getArticleNumber() ." '".$conflictingArticle->getName()."'");
} else {
$articleObj->create($articleType, $params['title'], $publication, $issue, $section);
$articleObj->create($articleType, $articleTitle, $publication, $issue, $section);
}

if ($articleObj->exists()) {
$em = $this->_helper->service('em');
$currentUser = $this->getUser();
$author = $currentUser->getAuthorId();
$articleObj->setCreatorId($currentUser->getUserId());
if (empty($author)) {
$authorObj = new Author($currentUser->getRealName());
$authorObj = new \Author($currentUser->getRealName());
if (!$authorObj->exists()) {
$authorData = Author::ReadName($currentUser->getRealName());
$authorData['email'] = $currentUser->getEmail();
Expand All @@ -96,7 +86,7 @@ public function createArticleAction(Request $request, $language, $issue, $sectio
$em->flush();
}
} else {
$authorObj = new Author($author);
$authorObj = new \Author($author);
}

if ($authorObj->exists()) {
Expand All @@ -109,12 +99,20 @@ public function createArticleAction(Request $request, $language, $issue, $sectio
$articleObj->setCommentsEnabled($commentDefault);
}

//camp_html_add_msg($translator->trans("Article created.", array(), 'articles'), "ok");
//camp_html_goto_page(camp_html_article_url($articleObj, $languageObject->getId(), "edit.php"), false);
ArticleIndex::RunIndexer(3, 10, true);
//exit();
$response = new Response();
$response->setStatusCode(201);

$response->headers->set(
'X-Location',
$this->generateUrl('newscoop_gimme_articles_getarticle', array(
'number' => $articleObj->getArticleNumber(),
), true)
);

\ArticleIndex::RunIndexer(3, 10, true);
return $response;
} else {
throw new \Exception($translator->trans("Could not create article.", array(), 'articles'));
throw new \Exception("Could not create article.");
}
}

Expand Down

0 comments on commit 14da710

Please sign in to comment.