From bc3fbc5f58b44efbd9f6a58014ef9eb36288b95f Mon Sep 17 00:00:00 2001 From: "M. Vondano" Date: Thu, 27 Jun 2019 18:05:51 +0200 Subject: [PATCH] review CE: allow to gather all products vs. selected ones only synchronize products that aren't obsolete --- src/Resources/contao/dca/tl_content.php | 17 +++++++-- .../contao/languages/de/tl_content.php | 1 + .../contao/languages/en/tl_content.php | 1 + src/Synchronizer.php | 38 ++++++++++++++----- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/Resources/contao/dca/tl_content.php b/src/Resources/contao/dca/tl_content.php index 331a5c8..d4842b2 100644 --- a/src/Resources/contao/dca/tl_content.php +++ b/src/Resources/contao/dca/tl_content.php @@ -3,8 +3,9 @@ /* * Regiondo Bundle for Contao Open Source CMS. * - * @copyright Copyright (c) 2018, derhaeuptling + * @copyright Copyright (c) 2019, derhaeuptling * @author Codefog + * @author Moritz V. * @license MIT */ @@ -18,9 +19,11 @@ /* * Add palettes */ +$GLOBALS['TL_DCA']['tl_content']['palettes']['__selector__'][] = 'regiondo_filterProducts'; $GLOBALS['TL_DCA']['tl_content']['palettes']['regiondo_event_booking_iframe'] = '{type_legend},type;{include_legend},regiondo_calendar,regiondo_iframeWidth;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID;{invisible_legend:hide},invisible,start,stop'; -$GLOBALS['TL_DCA']['tl_content']['palettes']['regiondo_reviews'] = '{type_legend},type;{include_legend},regiondo_products,regiondo_reviewsLimit,regiondo_syncReviews;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID;{invisible_legend:hide},invisible,start,stop'; +$GLOBALS['TL_DCA']['tl_content']['palettes']['regiondo_reviews'] = '{type_legend},type;{include_legend},regiondo_filterProducts,regiondo_reviewsLimit,regiondo_syncReviews;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID;{invisible_legend:hide},invisible,start,stop'; $GLOBALS['TL_DCA']['tl_content']['palettes']['regiondo_voucher'] = '{type_legend},type;{include_legend},regiondo_voucher,regiondo_iframeWidth;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID;{invisible_legend:hide},invisible,start,stop'; +$GLOBALS['TL_DCA']['tl_content']['subpalettes']['regiondo_filterProducts'] = 'regiondo_products'; /* * Add fields @@ -65,7 +68,7 @@ 'label' => &$GLOBALS['TL_LANG']['tl_content']['regiondo_reviewsLimit'], 'exclude' => true, 'inputType' => 'text', - 'eval' => ['mandatory' => true, 'rgxp' => 'natural', 'tl_class' => 'w50'], + 'eval' => ['mandatory' => true, 'rgxp' => 'natural', 'tl_class' => 'w50 clr'], 'sql' => ['type' => 'smallint', 'unsigned' => true, 'default' => 0], ]; @@ -85,3 +88,11 @@ $GLOBALS['TL_DCA']['tl_content']['fields']['regiondo_reviews'] = [ 'sql' => ['type' => 'blob'], ]; + +$GLOBALS['TL_DCA']['tl_content']['fields']['regiondo_filterProducts'] = [ + 'label' => &$GLOBALS['TL_LANG']['tl_content']['regiondo_filterProducts'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => ['submitOnChange' => true], + 'sql' => ['type' => 'string', 'default' => '1', 'length' => 1, 'options' => ['fixed' => true]], +]; diff --git a/src/Resources/contao/languages/de/tl_content.php b/src/Resources/contao/languages/de/tl_content.php index bf66ed6..8b4d7ce 100644 --- a/src/Resources/contao/languages/de/tl_content.php +++ b/src/Resources/contao/languages/de/tl_content.php @@ -9,6 +9,7 @@ */ $GLOBALS['TL_LANG']['tl_content']['regiondo_calendar'] = ['Regiondo calendar', 'Please choose the Regiondo calendar.']; +$GLOBALS['TL_LANG']['tl_content']['regiondo_filterProducts'] = ['Filter Regiondo products', 'Enable to select a range of products. If disabled all available products will be used.']; $GLOBALS['TL_LANG']['tl_content']['regiondo_products'] = ['Regiondo products', 'Please choose one or more Regiondo products to get the reviews from.']; $GLOBALS['TL_LANG']['tl_content']['regiondo_voucher'] = ['Regiondo voucher', 'Please choose the Regiondo voucher.']; $GLOBALS['TL_LANG']['tl_content']['regiondo_eventTemplate'] = ['Regiondo event template', 'Here you can choose the Regiondo event template.']; diff --git a/src/Resources/contao/languages/en/tl_content.php b/src/Resources/contao/languages/en/tl_content.php index bf66ed6..8b4d7ce 100644 --- a/src/Resources/contao/languages/en/tl_content.php +++ b/src/Resources/contao/languages/en/tl_content.php @@ -9,6 +9,7 @@ */ $GLOBALS['TL_LANG']['tl_content']['regiondo_calendar'] = ['Regiondo calendar', 'Please choose the Regiondo calendar.']; +$GLOBALS['TL_LANG']['tl_content']['regiondo_filterProducts'] = ['Filter Regiondo products', 'Enable to select a range of products. If disabled all available products will be used.']; $GLOBALS['TL_LANG']['tl_content']['regiondo_products'] = ['Regiondo products', 'Please choose one or more Regiondo products to get the reviews from.']; $GLOBALS['TL_LANG']['tl_content']['regiondo_voucher'] = ['Regiondo voucher', 'Please choose the Regiondo voucher.']; $GLOBALS['TL_LANG']['tl_content']['regiondo_eventTemplate'] = ['Regiondo event template', 'Here you can choose the Regiondo event template.']; diff --git a/src/Synchronizer.php b/src/Synchronizer.php index be0fb20..76d14bf 100644 --- a/src/Synchronizer.php +++ b/src/Synchronizer.php @@ -132,10 +132,18 @@ public function synchronizeReviews(int $contentElementId): array throw new SynchronizerException(\sprintf('Content element ID %s does not exist', $contentElementId)); } - $productIds = $this->getProductIds($contentElement['regiondo_products']); + if($contentElement['regiondo_filterProducts']) { + $productIds = $this->getProductIds($contentElement['regiondo_products'], false); - if (0 === \count($productIds)) { - throw new SynchronizerException(\sprintf('Content element ID %s has no Regiondo products', $contentElementId)); + if (0 === \count($productIds)) { + throw new SynchronizerException(\sprintf('Content element ID %s has no Regiondo products', $contentElementId)); + } + } else { + $productIds = $this->getProductIds(null, false); + + if(0 === \count($productIds)) { + return []; + } } $reviews = []; @@ -570,20 +578,32 @@ private function storeImage(Client $client, int $calendarId, string $url, string /** * Get the product IDs. * - * @param string|array $recordIds + * @param string|array|null $recordIds Record ids to filter for. + * @param bool $includeObsolete Whether to include products marked as 'obsolete'. * * @return array */ - private function getProductIds($recordIds): array + private function getProductIds($recordIds = null, $includeObsolete = true): array { - $recordIds = StringUtil::deserialize($recordIds, true); + $filterConditions = []; - if (0 === \count($recordIds)) { - return []; + if(null !== $recordIds) { + $recordIds = StringUtil::deserialize($recordIds, true); + + if (0 === \count($recordIds)) { + return []; + } + + $filterConditions[] = 'id IN ('.\implode(',', $recordIds).')'; + } + + if(!$includeObsolete) { + $filterConditions[] = 'obsolete = 0'; } $productIds = []; - $records = $this->db->fetchAll('SELECT product FROM tl_regiondo_product WHERE id IN ('.\implode(',', $recordIds).')'); + $filterCondition = \count($filterConditions) > 0 ? ' WHERE ' . implode(' AND ', $filterConditions) : ''; + $records = $this->db->fetchAll('SELECT product FROM tl_regiondo_product' . $filterCondition); foreach ($records as $record) { $productIds[] = (int) $record['product'];