private function processForm($request, $attachment = null)
{
$em = $this->container->get('em');
$attachmentService = $this->container->get('attachment');
if (!$attachment) {
$statusCode = 201;
} else {
$statusCode = 200;
$attachment = $em->getRepository('Newscoop\\Entity\\Attachment')->findOneById($attachment);
if (!$attachment) {
throw new EntityNotFoundException('Result was not found.');
}
}
$form = $this->createForm(new AttachmentType(), array());
$form->handleRequest($request);
if ($form->isValid()) {
$file = $form['attachment']->getData();
$attributes = $form->getData();
$user = $this->getUser();
if ($user) {
$attributes['user'] = $user;
}
$language = $em->getRepository('Newscoop\\Entity\\Language')->findOneById($attributes['language']);
unset($attributes['language']);
$attachment = $attachmentService->upload($file, $attributes['description'], $language, $attributes, $attachment);
$response = new Response();
$response->setStatusCode($statusCode);
$response->headers->set('X-Location', $this->generateUrl('newscoop_gimme_attachments_getattachment', array('number' => $attachment->getId()), true));
return $response;
}
return $form;
}