Skip to content

Commit

Permalink
Rewrite the DCA picker (see contao/core-bundle#950).
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp authored and leofeyer committed Jul 24, 2017
1 parent eb4bde4 commit f0c1e89
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 358 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Contao calendar bundle change log

### DEV

* Rewrite the DCA picker (see contao/core-bundle#950).

### 4.4.1 (2017-07-12)

* Always show the "show from" and "show until" fields (see contao/core-bundle#908).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^5.6|^7.0",
"symfony/framework-bundle": "^3.3",
"contao/core-bundle": "^4.4"
"contao/core-bundle": "dev-hotfix/4.4.2"
},
"require-dev": {
"contao/manager-plugin": "^2.0",
Expand Down
115 changes: 0 additions & 115 deletions src/Menu/EventPickerProvider.php

This file was deleted.

133 changes: 133 additions & 0 deletions src/Picker/EventPickerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\CalendarBundle\Picker;

use Contao\CalendarEventsModel;
use Contao\CalendarModel;
use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use Contao\CoreBundle\Framework\FrameworkAwareTrait;
use Contao\CoreBundle\Picker\AbstractPickerProvider;
use Contao\CoreBundle\Picker\DcaPickerProviderInterface;
use Contao\CoreBundle\Picker\PickerConfig;

/**
* Provides the event picker.
*
* @author Andreas Schempp <https://github.com/aschempp>
*/
class EventPickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface, FrameworkAwareInterface
{
use FrameworkAwareTrait;

/**
* {@inheritdoc}
*/
public function getName()
{
return 'eventPicker';
}

/**
* {@inheritdoc}
*/
public function supportsContext($context)
{
return 'link' === $context && $this->getUser()->hasAccess('calendar', 'modules');
}

/**
* {@inheritdoc}
*/
public function supportsValue(PickerConfig $config)
{
return false !== strpos($config->getValue(), '{{event_url::');
}

/**
* {@inheritdoc}
*/
public function getDcaTable()
{
return 'tl_calendar_events';
}

/**
* {@inheritdoc}
*/
public function getDcaAttributes(PickerConfig $config)
{
$attributes = ['fieldType' => 'radio'];

if ($this->supportsValue($config)) {
$attributes['value'] = str_replace(['{{event_url::', '}}'], '', $config->getValue());
}

return $attributes;
}

/**
* {@inheritdoc}
*/
public function convertDcaValue(PickerConfig $config, $value)
{
return '{{event_url::'.$value.'}}';
}

/**
* {@inheritdoc}
*/
protected function getLinkClass()
{
return 'event';
}

/**
* {@inheritdoc}
*/
protected function getRouteParameters(PickerConfig $config)
{
$params = ['do' => 'calendar'];

if ($config->getValue() && false !== strpos($config->getValue(), '{{event_url::')) {
$value = str_replace(['{{event_url::', '}}'], '', $config->getValue());

if (null !== ($calendarId = $this->getCalendarId($value))) {
$params['table'] = 'tl_calendar_events';
$params['id'] = $calendarId;
}
}

return $params;
}

/**
* Returns the calendar ID.
*
* @param int $id
*
* @return int|null
*/
private function getCalendarId($id)
{
/** @var CalendarEventsModel $eventAdapter */
$eventAdapter = $this->framework->getAdapter(CalendarEventsModel::class);

if (!(($calendarEventsModel = $eventAdapter->findById($id)) instanceof CalendarEventsModel)) {
return null;
}

if (!(($calendar = $calendarEventsModel->getRelated('pid')) instanceof CalendarModel)) {
return null;
}

return $calendar->id;
}
}
12 changes: 6 additions & 6 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ services:
calls:
- ["setFramework", ["@contao.framework"]]

contao_calendar.listener.event_picker_provider:
class: Contao\CalendarBundle\Menu\EventPickerProvider
contao_calendar.picker.event_provider:
class: Contao\CalendarBundle\Picker\EventPickerProvider
public: false
arguments:
- "@router"
- "@request_stack"
- "@security.token_storage"
- "@knp_menu.factory"
calls:
- [setTokenStorage, ["@security.token_storage"]]
tags:
- { name: contao.picker_menu_provider, priority: 96 }
- { name: contao.picker_provider, priority: 96 }
23 changes: 12 additions & 11 deletions tests/DependencyInjection/ContaoCalendarExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
use Contao\CalendarBundle\EventListener\InsertTagsListener;
use Contao\CalendarBundle\EventListener\PreviewUrlConvertListener;
use Contao\CalendarBundle\EventListener\PreviewUrlCreateListener;
use Contao\CalendarBundle\Menu\EventPickerProvider;
use Contao\CalendarBundle\Picker\EventPickerProvider;
use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

Expand Down Expand Up @@ -123,32 +124,32 @@ public function testPreviewUrlConvertListener()
}

/**
* Tests the contao_calendar.listener.event_picker_provider service.
* Tests the contao_calendar.picker.event_provider service.
*/
public function testEventPickerProvider()
{
$this->assertTrue($this->container->has('contao_calendar.listener.event_picker_provider'));
$this->assertTrue($this->container->has('contao_calendar.picker.event_provider'));

$definition = $this->container->getDefinition('contao_calendar.listener.event_picker_provider');
$definition = $this->container->getDefinition('contao_calendar.picker.event_provider');

$this->assertSame(EventPickerProvider::class, $definition->getClass());
$this->assertFalse($definition->isPublic());
$this->assertSame('router', (string) $definition->getArgument(0));
$this->assertSame('request_stack', (string) $definition->getArgument(1));
$this->assertSame('security.token_storage', (string) $definition->getArgument(2));
$this->assertSame('knp_menu.factory', (string) $definition->getArgument(0));

$conditionals = $definition->getInstanceofConditionals();

$this->assertArrayHasKey(FrameworkAwareInterface::class, $conditionals);

/** @var ChildDefinition $childDefinition */
$childDefinition = $conditionals[FrameworkAwareInterface::class];
$methodCalls = $childDefinition->getMethodCalls();

$this->assertSame('setFramework', $methodCalls[0][0]);
$this->assertSame('setFramework', $childDefinition->getMethodCalls()[0][0]);

$tags = $definition->getTags();

$this->assertArrayHasKey('contao.picker_menu_provider', $tags);
$this->assertSame(96, $tags['contao.picker_menu_provider'][0]['priority']);
$this->assertSame('setTokenStorage', $definition->getMethodCalls()[0][0]);

$this->assertArrayHasKey('contao.picker_provider', $tags);
$this->assertSame(96, $tags['contao.picker_provider'][0]['priority']);
}
}
Loading

0 comments on commit f0c1e89

Please sign in to comment.