diff --git a/src/Plugin.php b/src/Plugin.php index 7925e88b..037e1018 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -4,6 +4,10 @@ use Craft; use craft\base\Model; +use craft\console\controllers\EntrifyController; +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; @@ -86,6 +90,7 @@ public function init(): void $this->_registerCpRoutes(); $this->_registerTwigExtensions(); $this->_registerVariables(); + $this->_listenToEvents(); } /** @@ -169,4 +174,36 @@ 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_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 b6e5dfef..d8c1dcba 100644 --- a/src/services/Feeds.php +++ b/src/services/Feeds.php @@ -7,11 +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; @@ -237,6 +246,39 @@ public function reorderFeeds(array $feedIds): bool return true; } + /** + * Adjust Category Feeds after entrification + * + * @param EntrifyCategoriesEvent $event + * @return void + */ + public function entrifyCategoryFeeds(EntrifyCategoriesEvent $event): void + { + $this->_entrifyFeeds(Category::class, $event->section, $event->entryType, $event->categoryGroup->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); + } + + /** + * 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]); + } + // Private Methods // ========================================================================= @@ -320,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); + } + } + } + } }