-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite the DCA picker (see contao/core-bundle#950).
- Loading branch information
Showing
8 changed files
with
412 additions
and
358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.