Skip to content

Commit

Permalink
added contact documents upload and fixed profile without avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Aug 2, 2018
1 parent bbd106f commit 5e9905e
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 39 deletions.
4 changes: 4 additions & 0 deletions Controller/CompletionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class CompletionController extends AbstractController
{
use SaveMediaTrait;

const TYPE = Configuration::TYPE_COMPLETION;

/**
Expand Down Expand Up @@ -54,6 +56,8 @@ public function indexAction(Request $request)
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();

$this->saveMediaFields($form, $user, $request->getLocale());

// Completion User
$communityManager->completion($user);

Expand Down
42 changes: 3 additions & 39 deletions Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class ProfileController extends AbstractController
{
use SaveMediaTrait;

const TYPE = Configuration::TYPE_PROFILE;

/**
Expand Down Expand Up @@ -61,7 +63,7 @@ public function indexAction(Request $request)
$user->setLocale($request->getLocale());
}

$this->saveAvatar($form, $user, $request->getLocale());
$this->saveMediaFields($form, $user, $request->getLocale());

// Register User
$communityManager->saveProfile($user);
Expand All @@ -86,44 +88,6 @@ public function indexAction(Request $request)
);
}

/**
* Save media and set avatar on user.
*
* @param Form $form
* @param User $user
* @param string $locale
*
* @return Media|null
*/
protected function saveAvatar(Form $form, User $user, $locale)
{
$uploadedFile = $form->get('contact')->get('avatar')->getData();
if (null === $uploadedFile) {
return null;
}

$systemCollectionManager = $this->get('sulu_media.system_collections.manager');
$mediaManager = $this->get('sulu_media.media_manager');

$collection = $systemCollectionManager->getSystemCollection('sulu_contact.contact');
$avatar = $user->getContact()->getAvatar();

$apiMedia = $mediaManager->save(
$uploadedFile,
[
'id' => (null !== $avatar ? $avatar->getId() : null),
'locale' => $locale,
'title' => $user->getUsername(),
'collection' => $collection,
],
$user->getId()
);

$user->getContact()->setAvatar($apiMedia->getEntity());

return $apiMedia;
}

/**
* {@inheritdoc}
*
Expand Down
4 changes: 4 additions & 0 deletions Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class RegistrationController extends AbstractController
{
use SaveMediaTrait;

const TYPE = Configuration::TYPE_REGISTRATION;

/**
Expand Down Expand Up @@ -53,6 +55,8 @@ public function indexAction(Request $request)
$user->setLocale($request->getLocale());
}

$this->saveMediaFields($form, $user, $request->getLocale());

// Register User
$user = $communityManager->register($user);

Expand Down
111 changes: 111 additions & 0 deletions Controller/SaveMediaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Sulu\Bundle\CommunityBundle\Controller;


use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Bundle\SecurityBundle\Entity\User;
use Sulu\Component\Media\SystemCollections\SystemCollectionManagerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

trait SaveMediaTrait
{
private function saveMediaFields(FormInterface $form, User $user, $locale)
{
$this->saveAvatar($form, $user, $locale);
$this->saveDocuments($form, $user, $locale);
}

private function saveDocuments(FormInterface $form, User $user, $locale)
{
if (!$form->has('contact') || !$form->get('contact')->has('medias')) {
return;
}

$uploadedFiles = $form->get('contact')->get('medias')->getData();

if (empty($uploadedFiles)) {
return null;
}

if (!is_array($uploadedFiles)) {
$uploadedFiles = [$uploadedFiles];
}

$contact = $user->getContact();
$apiMedias = [];

foreach ($uploadedFiles as $uploadedFile) {
$apiMedia = $this->saveMedia($uploadedFile, null, $locale, $user->getId());
$contact->addMedia($apiMedia->getEntity());
$apiMedias[] = $apiMedia;
}

return $apiMedias;
}

protected function saveAvatar(FormInterface $form, User $user, $locale)
{
if (!$form->has('contact') || !$form->get('contact')->has('avatar')) {
return;
}

$uploadedFile = $form->get('contact')->get('avatar')->getData();
if (null === $uploadedFile) {
return null;
}

$avatar = $user->getContact()->getAvatar();

$apiMedia = $this->saveMedia($uploadedFile, (null !== $avatar ? $avatar->getId() : null), $locale, $user->getId());

$user->getContact()->setAvatar($apiMedia->getEntity());

return $apiMedia;
}

private function saveMedia(UploadedFile $uploadedFile, $id, $locale, $userId)
{
return $this->getMediaManager()->save(
$uploadedFile,
[
'id' => $id,
'locale' => $locale,
'title' => $uploadedFile->getClientOriginalName(),
'collection' => $this->getContactMediaCollection(),
],
$userId
);
}

/**
* Get system collection manager.
*
* @return SystemCollectionManagerInterface
*/
private function getSystemCollectionManager()
{
return $this->get('sulu_media.system_collections.manager');
}

/**
* Get media manager.
*
* @return MediaManagerInterface
*/
private function getMediaManager()
{
return $this->get('sulu_media.media_manager');
}

/**
* Get contact media collection.
*
* @return int
*/
private function getContactMediaCollection()
{
return $this->getSystemCollectionManager()->getSystemCollection('sulu_contact.contact');
}
}
Loading

0 comments on commit 5e9905e

Please sign in to comment.