Skip to content

Commit

Permalink
Add media importer
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbager committed Jul 12, 2018
1 parent 45be14e commit 8dcf8e8
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 8 deletions.
2 changes: 1 addition & 1 deletion features/admin/managing_pages.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ Feature: Managing cms pages
And I fill "Code, Content" fields
And I upload the "aston_martin_db_11.jpg" image
And I update it
Then I should be notified that the page was updated
Then I should be notified that the page was updated
18 changes: 14 additions & 4 deletions src/Entity/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ public function setMimeType(?string $mimeType): void
$this->mimeType = $mimeType;
}

public function getName(): ?string
{
return $this->getMediaTranslation()->getName();
}

public function setName(?string $name): void
{
$this->getMediaTranslation()->setName($name);
}

public function getDescription(): ?string
{
return $this->getMediaTranslation()->getDescription();
Expand All @@ -138,14 +148,14 @@ public function setDescription(?string $description): void
$this->getMediaTranslation()->setDescription($description);
}

public function getName(): ?string
public function getAlt(): ?string
{
return $this->getMediaTranslation()->getName();
return $this->getMediaTranslation()->getAlt();
}

public function setName(?string $name): void
public function setAlt(?string $alt): void
{
$this->getMediaTranslation()->setName($name);
$this->getMediaTranslation()->setAlt($alt);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Entity/MediaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ public function getMimeType(): ?string;

public function setMimeType(?string $mimeType): void;

public function getName(): ?string;

public function setName(?string $name): void;

public function getDescription(): ?string;

public function setDescription(?string $description): void;

public function getName(): ?string;
public function getAlt(): ?string;

public function setName(?string $name): void;
public function setAlt(?string $alt): void;
}
92 changes: 92 additions & 0 deletions src/Importer/MediaImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Importer;

use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterSectionsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ResourceResolverInterface;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Webmozart\Assert\Assert;

final class MediaImporter extends AbstractImporter implements MediaImporterInterface
{
/** @var ResourceResolverInterface */
private $mediaResourceResolver;

/** @var LocaleContextInterface */
private $localeContext;

/** @var ImporterSectionsResolverInterface */
private $importerSectionsResolver;

/** @var ImporterProductsResolverInterface */
private $importerProductsResolver;

/** @var EntityManagerInterface */
private $entityManager;

public function __construct(
ResourceResolverInterface $mediaResourceResolver,
LocaleContextInterface $localeContext,
ImporterSectionsResolverInterface $importerSectionsResolver,
ImporterProductsResolverInterface $importerProductsResolver,
EntityManagerInterface $entityManager
) {
$this->mediaResourceResolver = $mediaResourceResolver;
$this->localeContext = $localeContext;
$this->importerSectionsResolver = $importerSectionsResolver;
$this->importerProductsResolver = $importerProductsResolver;
$this->entityManager = $entityManager;
}

public function import(array $row): void
{
/** @var string $code */
$code = $this->getColumnValue(self::CODE_COLUMN, $row);
Assert::notNull($code);
/** @var MediaInterface $media */
$media = $this->mediaResourceResolver->getResource($code);

$media->setCode($code);
$media->setFallbackLocale($this->localeContext->getLocaleCode());

foreach ($this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)) as $locale) {
$media->setCurrentLocale($locale);
$media->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row));
$media->setDescription($this->getTranslatableColumnValue(self::DESCRIPTION_COLUMN, $locale, $row));
$media->setAlt($this->getTranslatableColumnValue(self::ALT_COLUMN, $locale, $row));
}

$this->importerSectionsResolver->resolve($media, $this->getColumnValue(self::SECTIONS_COLUMN, $row));
$this->importerProductsResolver->resolve($media, $this->getColumnValue(self::PRODUCTS_COLUMN, $row));

$media->getId() ?: $this->entityManager->persist($media);
$this->entityManager->flush();
}

public function getResourceCode(): string
{
return 'media';
}

private function getTranslatableColumns(): array
{
return [
self::NAME_COLUMN,
self::DESCRIPTION_COLUMN,
self::ALT_COLUMN,
];
}
}
24 changes: 24 additions & 0 deletions src/Importer/MediaImporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Importer;

interface MediaImporterInterface extends ImporterInterface
{
const CODE_COLUMN = 'code';
const SECTIONS_COLUMN = 'sections';
const CHANNELS_COLUMN = 'channels';
const PRODUCTS_COLUMN = 'products';
const NAME_COLUMN = 'name__locale__';
const DESCRIPTION_COLUMN = 'description__locale__';
const ALT_COLUMN = 'alt__locale__';
}
4 changes: 4 additions & 0 deletions src/Resources/config/grids/admin/media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ sylius_grid:
fields: [code, type]
actions:
main:
import:
type: import
options:
resourceCode: media
create:
type: create
item:
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/routing/admin/media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ bitbag_sylius_cms_plugin_admin_media:
resource: |
alias: bitbag_sylius_cms_plugin.media
section: admin
templates: SyliusAdminBundle:Crud
templates: BitBagSyliusCmsPlugin:CrudUi
redirect: update
grid: bitbag_sylius_cms_plugin_admin_media
except: ['show']
Expand Down
11 changes: 11 additions & 0 deletions src/Resources/config/services/importer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ services:
- "@doctrine.orm.entity_manager"
tags:
- { name: bitbag.importer }

bitbag_sylius_cms_plugin.importer.media:
class: BitBag\SyliusCmsPlugin\Importer\MediaImporter
arguments:
- "@bitbag_sylius_cms_plugin.resolver.resource.media"
- "@sylius.context.locale"
- "@bitbag_sylius_cms_plugin.resolver.importer_sections"
- "@bitbag_sylius_cms_plugin.resolver.importer_products"
- "@doctrine.orm.entity_manager"
tags:
- { name: bitbag.importer }
7 changes: 7 additions & 0 deletions src/Resources/config/services/resolver.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ services:
- "@bitbag_sylius_cms_plugin.factory.block"
- "code"

bitbag_sylius_cms_plugin.resolver.resource.media:
class: BitBag\SyliusCmsPlugin\Resolver\ResourceResolver
arguments:
- "@bitbag_sylius_cms_plugin.repository.media"
- "@bitbag_sylius_cms_plugin.factory.media"
- "code"

bitbag_sylius_cms_plugin.resolver.block_resource:
class: BitBag\SyliusCmsPlugin\Resolver\BlockResourceResolver
arguments:
Expand Down
30 changes: 30 additions & 0 deletions src/Resources/views/CrudUi/create.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends 'BitBagSyliusCmsPlugin::layout.html.twig' %}

{% import 'SyliusUiBundle:Macro:headers.html.twig' as headers %}

{% set header = configuration.vars.header|default(metadata.applicationName~'.ui.new_'~metadata.name) %}
{% set event_prefix = metadata.applicationName ~ '.admin.' ~ metadata.name ~ '.create' %}

{% block title %}{{ header|trans }} {{ parent() }}{% endblock %}

{% form_theme form '@SyliusAdmin/Form/theme.html.twig' %}

{% block content %}
{{ sonata_block_render_event(event_prefix ~ '.before_header', {'resource': resource}) }}

{% include '@SyliusAdmin/Crud/Create/_header.html.twig' %}

{{ sonata_block_render_event(event_prefix ~ '.after_header', {'resource': resource}) }}

{% include '@SyliusAdmin/Crud/Create/_content.html.twig' %}

{{ sonata_block_render_event(event_prefix ~ '.after_content', {'resource': resource}) }}
{% endblock %}

{% block stylesheets %}
{{ parent() }}

<link rel="stylesheet" href="{{ asset('bundles/bitbagsyliuscmsplugin/css/style.css') }}">

{{ sonata_block_render_event(event_prefix ~ '.stylesheets') }}
{% endblock %}
35 changes: 35 additions & 0 deletions src/Resources/views/CrudUi/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends 'BitBagSyliusCmsPlugin::layout.html.twig' %}

{% import 'SyliusUiBundle:Macro:headers.html.twig' as headers %}

{% set definition = resources.definition %}
{% set data = resources.data %}
{% set event_prefix = metadata.applicationName ~ '.admin.' ~ metadata.name ~ '.index' %}

{% set header = configuration.vars.header|default(metadata.applicationName~'.ui.'~metadata.pluralName) %}

{% block title %}{{ header|trans }} {{ parent() }}{% endblock %}

{% block content %}
{{ sonata_block_render_event(event_prefix ~ '.before_header', {'resources': resources}) }}

{% include '@SyliusAdmin/Crud/Index/_header.html.twig' %}

{{ sonata_block_render_event(event_prefix ~ '.after_header', {'resources': resources}) }}

{% include '@SyliusAdmin/Crud/Index/_content.html.twig' %}

{{ sonata_block_render_event(event_prefix ~ '.after_content', {'resources': resources}) }}
{% endblock %}

{% block stylesheets %}
{{ parent() }}

{{ sonata_block_render_event(event_prefix ~ '.stylesheets') }}
{% endblock %}

{% block javascripts %}
{{ parent() }}

{{ sonata_block_render_event(event_prefix ~ '.javascripts') }}
{% endblock %}
31 changes: 31 additions & 0 deletions src/Resources/views/CrudUi/update.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends 'BitBagSyliusCmsPlugin::layout.html.twig' %}

{% import 'SyliusUiBundle:Macro:headers.html.twig' as headers %}
{% import 'SyliusUiBundle:Macro:buttons.html.twig' as buttons %}

{% set header = configuration.vars.header|default(metadata.applicationName~'.ui.edit_'~metadata.name) %}
{% set event_prefix = metadata.applicationName ~ '.admin.' ~ metadata.name ~ '.update' %}

{% block title %}{{ header|trans }} {{ parent() }}{% endblock %}

{% form_theme form '@SyliusAdmin/Form/theme.html.twig' %}

{% block content %}
{{ sonata_block_render_event(event_prefix ~ '.before_header', {'resource': resource}) }}

{% include '@SyliusAdmin/Crud/Update/_header.html.twig' %}

{{ sonata_block_render_event(event_prefix ~ '.after_header', {'resource': resource}) }}

{% include '@SyliusAdmin/Crud/Update/_content.html.twig' %}

{{ sonata_block_render_event(event_prefix ~ '.after_content', {'resource': resource}) }}
{% endblock %}

{% block stylesheets %}
{{ parent() }}

<link rel="stylesheet" href="{{ asset('bundles/bitbagsyliuscmsplugin/css/style.css') }}">

{{ sonata_block_render_event(event_prefix ~ '.stylesheets') }}
{% endblock %}

0 comments on commit 8dcf8e8

Please sign in to comment.