Skip to content

Commit

Permalink
Implements the Creation of SnippetTemplates through POST
Browse files Browse the repository at this point in the history
  • Loading branch information
terwey committed May 8, 2014
1 parent 0689f4c commit c5e63b1
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,27 @@ public function deleteSnippetTemplate($id, $force = false)
throw new \Exception('SnippetTemplate with ID: '.$id.' does not exist');
}
}

public function save(SnippetTemplate $snippetTemplate)
{
if (!$snippetTemplate->hasName()) {
throw new \InvalidArgumentException("SnippetTemplate name cannot be empty");
}

if (!$snippetTemplate->hasTemplateCode()) {
throw new \InvalidArgumentException("SnippetTemplate templateCode cannot be empty");
}

if (!$snippetTemplate->hasFields()) {
throw new \InvalidArgumentException("SnippetTemplate requires at least 1 SnippetTemplateField");
}
foreach($snippetTemplate->getFields()->toArray() as $field) {
$field->setTemplate($snippetTemplate);
}

$em = $this->getEntityManager();
$em->persist($snippetTemplate);
$em->flush();
}

}
54 changes: 34 additions & 20 deletions newscoop/library/Newscoop/Entity/Snippet/SnippetTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SnippetTemplate
* @ORM\Column(name="Enabled", type="boolean", nullable=false)
* @var boolean
*/
protected $enabled = 1;
protected $enabled = true;

/**
* @ORM\Column(name="IconInactive", type="text", nullable=true)
Expand Down Expand Up @@ -147,6 +147,11 @@ public function getName()
return $this->name;
}

public function hasName()
{
return !empty($this->name);
}

/**
* Setter for name
*
Expand All @@ -157,7 +162,7 @@ public function getName()
public function setName($name)
{
$this->name = $name;
$this->setUpdated();
$this->setModified();

return $this;
}
Expand All @@ -182,7 +187,7 @@ public function getController()
public function setController($controller)
{
$this->controller = $controller;
$this->setUpdated();
$this->setModified();

return $this;
}
Expand All @@ -194,7 +199,11 @@ public function setController($controller)
*/
public function hasFields()
{
return !is_null($this->fields);
if (count($this->fields) >= 1) {
return true;
}

return false;
}

/**
Expand All @@ -218,7 +227,7 @@ public function addField(SnippetTemplateField $field)
{
$field->setTemplate($this);
$this->fields->add($field);
$this->setUpdated();
$this->setModified();

return $this;
}
Expand Down Expand Up @@ -278,11 +287,16 @@ public function getTemplateCode()
public function setTemplateCode($templateCode)
{
$this->templateCode = $templateCode;
$this->setUpdated();
$this->setModified();

return $this;
}

public function hasTemplateCode()
{
return !empty($this->templateCode);
}

/**
* Getter for favourite
*
Expand All @@ -303,7 +317,7 @@ public function getFavourite()
public function setFavourite($favourite)
{
$this->favourite = $favourite;
$this->setUpdated();
$this->setModified();

return $this;
}
Expand All @@ -328,7 +342,7 @@ public function getEnabled()
public function setEnabled($enabled)
{
$this->enabled = $enabled;
$this->setUpdated();
$this->setModified();

return $this;
}
Expand All @@ -353,7 +367,7 @@ public function getIconInactive()
public function setIconInactive($iconInactive)
{
$this->iconInactive = $iconInactive;
$this->setUpdated();
$this->setModified();

return $this;
}
Expand All @@ -378,7 +392,7 @@ public function getIconActive()
public function setIconActive($iconActive)
{
$this->iconActive = $iconActive;
$this->setUpdated();
$this->setModified();

return $this;
}
Expand Down Expand Up @@ -407,35 +421,35 @@ public function setCreated($created = null)
}

$this->created = $created;
$this->setUpdated();
$this->setModified();

return $this;
}

/**
* Getter for updated
* Getter for modified
*
* @return mixed
*/
public function getUpdated()
public function getModified()
{
return $this->updated;
return $this->modified;
}

/**
* Setter for updated
* Setter for modified
*
* @param mixed $updated Value to set
* @param mixed $modified Value to set
*
* @return self
*/
public function setUpdated($updated = null)
public function setModified($modified = null)
{
if (!($updated instanceof \DateTime)) {
$updated = new \DateTime("now");
if (!($modified instanceof \DateTime)) {
$modified = new \DateTime("now");
}

$this->updated = $updated;
$this->modified = $modified;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SnippetTemplateField
{
const TYPE_INT = 'integer';
const TYPE_STRING = 'string';
const TYPE_BOOL = 'bool';
const TYPE_BOOL = 'boolean';
const SCOPE_FRONTEND = 'frontend';
const SCOPE_BACKEND = 'backend';

Expand Down Expand Up @@ -97,6 +97,11 @@ public function getName()
{
return $this->templateFieldName;
}

public function hasName()
{
return !empty($this->templateFieldName);
}

/**
* Setter for name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Doctrine\ORM\EntityNotFoundException;
use Newscoop\GimmeBundle\Form\Type\SnippetType;
use Newscoop\GimmeBundle\Form\Type\SnippetTemplateType;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Newscoop\Entity\Snippet;
use Newscoop\Entity\Snippet\SnippetTemplate;
Expand Down Expand Up @@ -128,4 +128,78 @@ public function deleteSnippetTemplateAction(Request $request, $id)
$articleSnippets = $em->getRepository('Newscoop\Entity\Snippet\SnippetTemplate')
->deleteSnippetTemplate($id, $force);
}

/**
* Create new SnippetTemplate
*
* @ApiDoc(
* statusCodes={
* 201="Returned when SnippetTemplate created succesfuly"
* },
* input="\Newscoop\GimmeBundle\Form\Type\SnippetTemplateType"
* )
*
* @Route("/snippetTemplates.{_format}", defaults={"_format"="json"})
* @Method("POST")
* @View()
*
* @return Form
*/
public function createSnippetTemplateAction(Request $request)
{
return $this->processForm($request, null);
}

/**
* Process SnippetTemplate form
*
* @param Request $request
* @param integer $snippetTemplateId
*
* @return Form
*/
private function processForm($request, $snippetTemplateId = null)
{
// XXX It breaks using PATCH
$em = $this->container->get('em');
$patch = false;
if (!is_null($snippetTemplateId) && !is_numeric($snippetTemplateId)) {
throw new InvalidArgumentException("Parameter 'template' is not numeric");
}

if (!$snippetTemplateId) {
$snippetTemplate = new SnippetTemplate();
$statusCode = 201;
} else {
$snippetTemplate = $em->getRepository('Newscoop\Entity\Snippet\SnippetTemplate')
->getTemplateById($templateId);
$statusCode = 200;
$patch = true;
if (is_null($snippetTemplate)) {
throw new InvalidArgumentException("Template with ID: '".$templateId."' does not exist.");
}
}

$form = $this->container->get('form.factory')->create(new SnippetTemplateType(array('patch'=>$patch)), $snippetTemplate);
$form->handleRequest($request);

if ($form->isValid()) {
$snippetTemplate = $form->getData();
$em->getRepository('Newscoop\Entity\Snippet\SnippetTemplate')
->save($snippetTemplate);
$response = new Response();
$response->setStatusCode($statusCode);

$response->headers->set(
'X-Location',
$this->generateUrl('newscoop_gimme_snippettemplates_getsinglesnippettemplate', array(
'id' => $snippetTemplate->getId(),
), true)
);

return $response;
}

return $form;
}
}

0 comments on commit c5e63b1

Please sign in to comment.