-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
411 additions
and
361 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\FaqBundle\Picker; | ||
|
||
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; | ||
use Contao\FaqCategoryModel; | ||
use Contao\FaqModel; | ||
|
||
/** | ||
* Provides the faq picker. | ||
* | ||
* @author Andreas Schempp <https://github.com/aschempp> | ||
*/ | ||
class FaqPickerProvider extends AbstractPickerProvider implements DcaPickerProviderInterface, FrameworkAwareInterface | ||
{ | ||
use FrameworkAwareTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'faqPicker'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsContext($context) | ||
{ | ||
return 'link' === $context && $this->getUser()->hasAccess('faq', 'modules'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsValue(PickerConfig $config) | ||
{ | ||
return false !== strpos($config->getValue(), '{{faq_url::'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDcaTable() | ||
{ | ||
return 'tl_faq'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDcaAttributes(PickerConfig $config) | ||
{ | ||
$attributes = ['fieldType' => 'radio']; | ||
|
||
if ($this->supportsValue($config)) { | ||
$attributes['value'] = str_replace(['{{faq_url::', '}}'], '', $config->getValue()); | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertDcaValue(PickerConfig $config, $value) | ||
{ | ||
return '{{faq_url::'.$value.'}}'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getLinkClass() | ||
{ | ||
return 'faq'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getRouteParameters(PickerConfig $config) | ||
{ | ||
$params = ['do' => 'faq']; | ||
|
||
if ($config->getValue() && false !== strpos($config->getValue(), '{{faq_url::')) { | ||
$value = str_replace(['{{faq_url::', '}}'], '', $config->getValue()); | ||
|
||
if (null !== ($faqId = $this->getFaqCategoryId($value))) { | ||
$params['table'] = 'tl_faq'; | ||
$params['id'] = $faqId; | ||
} | ||
} | ||
|
||
return $params; | ||
} | ||
|
||
/** | ||
* Returns the FAQ category ID. | ||
* | ||
* @param int $id | ||
* | ||
* @return int|null | ||
*/ | ||
private function getFaqCategoryId($id) | ||
{ | ||
/** @var FaqModel $faqAdapter */ | ||
$faqAdapter = $this->framework->getAdapter(FaqModel::class); | ||
|
||
if (!(($faqModel = $faqAdapter->findById($id)) instanceof FaqModel)) { | ||
return null; | ||
} | ||
|
||
if (!(($faqCategory = $faqModel->getRelated('pid')) instanceof FaqCategoryModel)) { | ||
return null; | ||
} | ||
|
||
return $faqCategory->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.