From 7f56cf62252dea9b331f61505290568fdc16223c Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Thu, 6 Jul 2023 14:19:36 +0100 Subject: [PATCH 1/4] entrify feeds that match what was just entrified --- src/Plugin.php | 19 +++++++++++++++++++ src/services/Feeds.php | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/Plugin.php b/src/Plugin.php index 7925e88b..ee6da8ec 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -4,6 +4,8 @@ use Craft; use craft\base\Model; +use craft\console\controllers\EntrifyController; +use craft\events\EntrifyEvent; use craft\events\RegisterUrlRulesEvent; use craft\feedme\base\PluginTrait; use craft\feedme\models\Settings; @@ -86,6 +88,7 @@ public function init(): void $this->_registerCpRoutes(); $this->_registerTwigExtensions(); $this->_registerVariables(); + $this->_listenToEvents(); } /** @@ -169,4 +172,20 @@ private function _registerVariables(): void $event->sender->set('feedme', FeedMeVariable::class); }); } + + /** + * Listen to events + * + * @return void + */ + private function _listenToEvents(): void + { + Event::on( + EntrifyController::class, + EntrifyController::EVENT_AFTER_ENTRIFY, + function (EntrifyEvent $event) { + self::$plugin->feeds->entrifyFeeds($event->elementType, $event->elementGroup); + } + ); + } } diff --git a/src/services/Feeds.php b/src/services/Feeds.php index b6e5dfef..271c173e 100644 --- a/src/services/Feeds.php +++ b/src/services/Feeds.php @@ -7,6 +7,8 @@ use craft\base\Component; use craft\db\ActiveQuery; use craft\db\Query; +use craft\elements\Entry; +use craft\elements\GlobalSet; use craft\feedme\errors\FeedException; use craft\feedme\events\FeedEvent; use craft\feedme\models\FeedModel; @@ -237,6 +239,44 @@ public function reorderFeeds(array $feedIds): bool return true; } + /** + * Adjust the feeds after entrification. + * + * @param string $elementType Element being entrified + * @param array $elementGroup From and To IDs; e.g. from would be a groupId, to would be sectionId and typeId + * @return void + */ + public function entrifyFeeds(string $elementType, array $elementGroup): void + { + $allFeeds = $this->getFeeds(); + + foreach ($allFeeds as $feed) { + if ($feed->elementType === $elementType) { + // if, in the elementGroup, the key matches $elementType + // and the value for that key matches $elementGroup[from] + // (in case of GlobalSet, the value would be ['globalSet' => ]) + if (isset($feed->elementGroup[$elementType]) && + ( + $feed->elementGroup[$elementType] == $elementGroup['from'] || + ($elementType === GlobalSet::class && $feed->elementGroup[$elementType] == ['globalSet' => $elementGroup['from']]) + ) + ) { + // change the elementType + $feed->elementType = Entry::class; + // in the elementGroup change the value for the $elementType key to an empty string + $feed->elementGroup[$elementType] = ""; + // in the elementGroup change the value for the craft\elements\Entry key + $feed->elementGroup[Entry::class] = [ + 'section' => $elementGroup['to']['sectionId'], + 'entryType' => $elementGroup['to']['typeId'], + ]; + + $this->saveFeed($feed); + } + } + } + } + // Private Methods // ========================================================================= From 649274e1923a0984f538390d79a2542020d7ecab Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Fri, 7 Jul 2023 11:29:51 +0100 Subject: [PATCH 2/4] ecs --- src/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index ee6da8ec..d0a8264d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -183,7 +183,7 @@ private function _listenToEvents(): void Event::on( EntrifyController::class, EntrifyController::EVENT_AFTER_ENTRIFY, - function (EntrifyEvent $event) { + function(EntrifyEvent $event) { self::$plugin->feeds->entrifyFeeds($event->elementType, $event->elementGroup); } ); From f9dd632db36e6f6a0e14c05fee83781a65c381e3 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Mon, 10 Jul 2023 14:39:45 +0100 Subject: [PATCH 3/4] adjust after event data changed --- src/services/Feeds.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/services/Feeds.php b/src/services/Feeds.php index 271c173e..3f64b6aa 100644 --- a/src/services/Feeds.php +++ b/src/services/Feeds.php @@ -250,6 +250,14 @@ public function entrifyFeeds(string $elementType, array $elementGroup): void { $allFeeds = $this->getFeeds(); + $sectionsService = Craft::$app->getSections(); + $section = $sectionsService->getSectionByUid($elementGroup['to']['section']); + $entryType = $sectionsService->getEntryTypeByUid($elementGroup['to']['entryType']); + + if (!$section || !$entryType) { + return; + } + foreach ($allFeeds as $feed) { if ($feed->elementType === $elementType) { // if, in the elementGroup, the key matches $elementType @@ -257,8 +265,8 @@ public function entrifyFeeds(string $elementType, array $elementGroup): void // (in case of GlobalSet, the value would be ['globalSet' => ]) if (isset($feed->elementGroup[$elementType]) && ( - $feed->elementGroup[$elementType] == $elementGroup['from'] || - ($elementType === GlobalSet::class && $feed->elementGroup[$elementType] == ['globalSet' => $elementGroup['from']]) + $feed->elementGroup[$elementType] == $elementGroup['from']['id'] || + ($elementType === GlobalSet::class && $feed->elementGroup[$elementType] == ['globalSet' => $elementGroup['from']['id']]) ) ) { // change the elementType @@ -267,8 +275,8 @@ public function entrifyFeeds(string $elementType, array $elementGroup): void $feed->elementGroup[$elementType] = ""; // in the elementGroup change the value for the craft\elements\Entry key $feed->elementGroup[Entry::class] = [ - 'section' => $elementGroup['to']['sectionId'], - 'entryType' => $elementGroup['to']['typeId'], + 'section' => $section->id, + 'entryType' => $entryType->id, ]; $this->saveFeed($feed); From e9753bc9d49f34a02bfef43900a4e5e9ad4a67b7 Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Mon, 17 Jul 2023 10:12:57 +0100 Subject: [PATCH 4/4] adjust after separating events --- src/Plugin.php | 26 ++++++++-- src/services/Feeds.php | 109 +++++++++++++++++++++++++++-------------- 2 files changed, 94 insertions(+), 41 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index d0a8264d..037e1018 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -5,7 +5,9 @@ use Craft; use craft\base\Model; use craft\console\controllers\EntrifyController; -use craft\events\EntrifyEvent; +use craft\events\EntrifyCategoriesEvent; +use craft\events\EntrifyGlobalSetEvent; +use craft\events\EntrifyTagsEvent; use craft\events\RegisterUrlRulesEvent; use craft\feedme\base\PluginTrait; use craft\feedme\models\Settings; @@ -182,9 +184,25 @@ private function _listenToEvents(): void { Event::on( EntrifyController::class, - EntrifyController::EVENT_AFTER_ENTRIFY, - function(EntrifyEvent $event) { - self::$plugin->feeds->entrifyFeeds($event->elementType, $event->elementGroup); + EntrifyController::EVENT_ENTRIFY_CATEGORIES, + function(EntrifyCategoriesEvent $event) { + self::$plugin->feeds->entrifyCategoryFeeds($event); + } + ); + + Event::on( + EntrifyController::class, + EntrifyController::EVENT_ENTRIFY_TAGS, + function(EntrifyTagsEvent $event) { + self::$plugin->feeds->entrifyTagFeeds($event); + } + ); + + Event::on( + EntrifyController::class, + EntrifyController::EVENT_ENTRIFY_GLOBAL_SET, + function(EntrifyGlobalSetEvent $event) { + self::$plugin->feeds->entrifyGlobalSetFeeds($event); } ); } diff --git a/src/services/Feeds.php b/src/services/Feeds.php index 3f64b6aa..d8c1dcba 100644 --- a/src/services/Feeds.php +++ b/src/services/Feeds.php @@ -7,13 +7,20 @@ use craft\base\Component; use craft\db\ActiveQuery; use craft\db\Query; +use craft\elements\Category; use craft\elements\Entry; use craft\elements\GlobalSet; +use craft\elements\Tag; +use craft\events\EntrifyCategoriesEvent; +use craft\events\EntrifyGlobalSetEvent; +use craft\events\EntrifyTagsEvent; use craft\feedme\errors\FeedException; use craft\feedme\events\FeedEvent; use craft\feedme\models\FeedModel; use craft\feedme\records\FeedRecord; use craft\helpers\Json; +use craft\models\EntryType; +use craft\models\Section; use Exception; use Throwable; @@ -240,49 +247,36 @@ public function reorderFeeds(array $feedIds): bool } /** - * Adjust the feeds after entrification. + * Adjust Category Feeds after entrification * - * @param string $elementType Element being entrified - * @param array $elementGroup From and To IDs; e.g. from would be a groupId, to would be sectionId and typeId + * @param EntrifyCategoriesEvent $event * @return void */ - public function entrifyFeeds(string $elementType, array $elementGroup): void + public function entrifyCategoryFeeds(EntrifyCategoriesEvent $event): void { - $allFeeds = $this->getFeeds(); - - $sectionsService = Craft::$app->getSections(); - $section = $sectionsService->getSectionByUid($elementGroup['to']['section']); - $entryType = $sectionsService->getEntryTypeByUid($elementGroup['to']['entryType']); - - if (!$section || !$entryType) { - return; - } + $this->_entrifyFeeds(Category::class, $event->section, $event->entryType, $event->categoryGroup->id); + } - foreach ($allFeeds as $feed) { - if ($feed->elementType === $elementType) { - // if, in the elementGroup, the key matches $elementType - // and the value for that key matches $elementGroup[from] - // (in case of GlobalSet, the value would be ['globalSet' => ]) - if (isset($feed->elementGroup[$elementType]) && - ( - $feed->elementGroup[$elementType] == $elementGroup['from']['id'] || - ($elementType === GlobalSet::class && $feed->elementGroup[$elementType] == ['globalSet' => $elementGroup['from']['id']]) - ) - ) { - // change the elementType - $feed->elementType = Entry::class; - // in the elementGroup change the value for the $elementType key to an empty string - $feed->elementGroup[$elementType] = ""; - // in the elementGroup change the value for the craft\elements\Entry key - $feed->elementGroup[Entry::class] = [ - 'section' => $section->id, - 'entryType' => $entryType->id, - ]; + /** + * Adjust Tag Feeds after entrification + * + * @param EntrifyTagsEvent $event + * @return void + */ + public function entrifyTagFeeds(EntrifyTagsEvent $event): void + { + $this->_entrifyFeeds(Tag::class, $event->section, $event->entryType, $event->tagGroup->id); + } - $this->saveFeed($feed); - } - } - } + /** + * Adjust Global Set Feeds after entrification + * + * @param EntrifyGlobalSetEvent $event + * @return void + */ + public function entrifyGlobalSetFeeds(EntrifyGlobalSetEvent $event): void + { + $this->_entrifyFeeds(GlobalSet::class, $event->section, $event->entryType, ['globalSet' => $event->globalSet->id]); } @@ -368,4 +362,45 @@ private function _getFeedRecordById(int $feedId = null): FeedRecord return $feedRecord; } + + /** + * Adjust the feeds after entrification. + * + * @param string $entrifiedElementClass class name of the entrified elements + * @param Section $section section to entrify to + * @param EntryType $entryType element type to entrify to + * @param int|array $entrifiedModelId id of the entrified model (category group, tag group or global set) + * @return void + */ + private function _entrifyFeeds( + string $entrifiedElementClass, + Section $section, + EntryType $entryType, + int|array $entrifiedModelId + ): void + { + $allFeeds = $this->getFeeds(); + + foreach ($allFeeds as $feed) { + if ($feed->elementType === $entrifiedElementClass) { + // if, in the elementGroup, the key matches $elementType + // and the value for that key matches $elementGroup[from] + // (in case of GlobalSet, the value would be ['globalSet' => ]) + if (isset($feed->elementGroup[$entrifiedElementClass]) && + $feed->elementGroup[$entrifiedElementClass] == $entrifiedModelId) { + // change the elementType + $feed->elementType = Entry::class; + // in the elementGroup change the value for the $elementType key to an empty string + $feed->elementGroup[$entrifiedElementClass] = ""; + // in the elementGroup change the value for the craft\elements\Entry key + $feed->elementGroup[Entry::class] = [ + 'section' => $section->id, + 'entryType' => $entryType->id, + ]; + + $this->saveFeed($feed); + } + } + } + } }