Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added contact documents upload and fixed profile without avatar #101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 3 additions & 40 deletions Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Sulu\Bundle\ContactBundle\Entity\AddressType;
use Sulu\Bundle\ContactBundle\Entity\ContactAddress;
use Sulu\Bundle\ContactBundle\Entity\Note;
use Sulu\Bundle\MediaBundle\Api\Media;
use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -27,6 +26,8 @@
*/
class ProfileController extends AbstractController
{
use SaveMediaTrait;

const TYPE = Configuration::TYPE_PROFILE;

/**
Expand Down Expand Up @@ -61,7 +62,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 +87,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
119 changes: 119 additions & 0 deletions Controller/SaveMediaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Controller;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion this is a big trait.
Maybe create an service here?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No that would call then always the service and if somebody did overwrite saveAvatar that function is never called.


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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mark this method as deprecated then?

{
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