|
| 1 | +<?php |
| 2 | +namespace Gerdemann\GoogleIndex\Controller\Module; |
| 3 | + |
| 4 | +use Gerdemann\GoogleIndex\Service\IndexingService; |
| 5 | +use Neos\ContentRepository\Domain\Model\NodeInterface; |
| 6 | +use Neos\Eel\FlowQuery\FlowQuery; |
| 7 | +use Neos\Error\Messages\Message; |
| 8 | +use Neos\Flow\Annotations as Flow; |
| 9 | +use Neos\Flow\I18n\Translator; |
| 10 | +use Neos\Flow\Mvc\View\ViewInterface; |
| 11 | +use Neos\Fusion\View\FusionView; |
| 12 | +use Neos\Neos\Controller\CreateContentContextTrait; |
| 13 | +use Neos\Neos\Controller\Module\AbstractModuleController; |
| 14 | + |
| 15 | +/** |
| 16 | + * @Flow\Scope("singleton") |
| 17 | + */ |
| 18 | +class IndexController extends AbstractModuleController |
| 19 | +{ |
| 20 | + use CreateContentContextTrait; |
| 21 | + |
| 22 | + const UPDATE = 'URL_UPDATED'; |
| 23 | + const DELETE = 'URL_UPDATED'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var FusionView |
| 27 | + */ |
| 28 | + protected $view; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $defaultViewObjectName = FusionView::class; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + protected $viewFormatToObjectNameMap = [ |
| 39 | + 'html' => FusionView::class, |
| 40 | + ]; |
| 41 | + |
| 42 | + /** |
| 43 | + * @Flow\Inject |
| 44 | + * @var IndexingService |
| 45 | + */ |
| 46 | + protected $indexingService; |
| 47 | + |
| 48 | + /** |
| 49 | + * @Flow\Inject |
| 50 | + * @var Translator |
| 51 | + */ |
| 52 | + protected $translator; |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the Fusion path pattern on the view to avoid conflicts with the frontend fusion |
| 56 | + * |
| 57 | + * This is not needed if your package does not register itself to `Neos.Neos.fusion.autoInclude.*` |
| 58 | + */ |
| 59 | + protected function initializeView(ViewInterface $view) |
| 60 | + { |
| 61 | + parent::initializeView($view); |
| 62 | + $view->setFusionPathPattern('resource://Gerdemann.GoogleIndex/Private/Fusion'); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Index action |
| 67 | + * |
| 68 | + * @param int $page |
| 69 | + * @param int $limit |
| 70 | + */ |
| 71 | + public function indexAction(int $page = 1, int $limit = 20) |
| 72 | + { |
| 73 | + $offset = ($page - 1) * $limit; |
| 74 | + $context = $this->createContentContext('live'); |
| 75 | + $pages = (new FlowQuery([$context->getCurrentSiteNode()]))->find('[instanceof Neos.Neos:Document]')->sort('_lastPublicationDateTime', 'DESC')->slice($offset, $offset + $limit)->get(); |
| 76 | + $pageCount = ceil((new FlowQuery([$context->getCurrentSiteNode()]))->find('[instanceof Neos.Neos:Document]')->count() / $limit); |
| 77 | + |
| 78 | + $paginationLinks = []; |
| 79 | + if ($pageCount > 1) { |
| 80 | + for ($i = 1; $i <= $pageCount; $i++) { |
| 81 | + if ($pageCount > 10) { |
| 82 | + if ($i > 2 && $i < ($pageCount - 1) && $i != $page && $i != ($page - 1) && $i != ($page + 1) && $i != ($page - 2) && $i != ($page + 2)) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + } |
| 86 | + $paginationLinks[] = [ |
| 87 | + 'uri' => $this->uriBuilder->reset()->uriFor('index', ['page' => $i]), |
| 88 | + 'page' => $i, |
| 89 | + 'current' => $page === $i, |
| 90 | + ]; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $this->view->assignMultiple([ |
| 95 | + 'documentNode' => $context->getCurrentSiteNode(), |
| 96 | + 'pages' => $pages, |
| 97 | + 'paginationLinks' => $paginationLinks, |
| 98 | + 'flashMessages' => $this->controllerContext->getFlashMessageContainer()->getMessagesAndFlush(), |
| 99 | + ]); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param string $url |
| 104 | + * @param string $title |
| 105 | + */ |
| 106 | + public function updateGoogleAction(string $uri, string $title) |
| 107 | + { |
| 108 | + try { |
| 109 | + $this->notifyGoogle($uri, self::UPDATE); |
| 110 | + $message = $this->translateById('successfully.updated', [$title]); |
| 111 | + $this->addFlashMessage('', $message, Message::SEVERITY_OK); |
| 112 | + } catch (\Google_Service_Exception $error) { |
| 113 | + $error = json_decode($error->getMessage(), true); |
| 114 | + $message = $this->translateById('failed.updated', [$title]); |
| 115 | + $this->addFlashMessage('', $message . '<br />(' . (!empty($error['error']['message']) ? $error['error']['message'] : '') . ')', Message::SEVERITY_ERROR); |
| 116 | + } |
| 117 | + |
| 118 | + $this->redirect('index'); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param string $url |
| 123 | + * @param string $title |
| 124 | + */ |
| 125 | + public function deleteGoogleAction(string $uri, string $title) |
| 126 | + { |
| 127 | + try { |
| 128 | + $this->notifyGoogle($uri, self::DELETE); |
| 129 | + $message = $this->translateById('successfully.deleted', [$title]); |
| 130 | + $this->addFlashMessage('', $message, Message::SEVERITY_OK); |
| 131 | + } catch (\Google_Service_Exception $error) { |
| 132 | + $error = json_decode($error->getMessage(), true); |
| 133 | + $message = $this->translateById('failed.deleted', [$title]); |
| 134 | + $this->addFlashMessage('', $message . '<br />(' . (!empty($error['error']['message']) ? $error['error']['message'] : '') . ')', Message::SEVERITY_ERROR); |
| 135 | + } |
| 136 | + |
| 137 | + $this->redirect('index'); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param string $uri |
| 142 | + * @param string $type |
| 143 | + */ |
| 144 | + protected function notifyGoogle(string $uri, string $type = self::UPDATE) |
| 145 | + { |
| 146 | + $urlNotification = new \Google_Service_Indexing_UrlNotification(); |
| 147 | + $today = new \DateTime(); |
| 148 | + $urlNotification->setNotifyTime($today->format(\DateTime::RFC3339)); |
| 149 | + $urlNotification->setUrl($uri); |
| 150 | + $urlNotification->setType($type); |
| 151 | + $this->indexingService->getUrlNotifications()->publish($urlNotification); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @param string|null $id |
| 156 | + * @param array $arguments |
| 157 | + * @return string |
| 158 | + */ |
| 159 | + protected function translateById(string $id, array $arguments = []): ?string |
| 160 | + { |
| 161 | + return $this->translator->translateById($id, $arguments, null, null, 'Main', 'Gerdemann.GoogleIndex'); |
| 162 | + } |
| 163 | +} |
0 commit comments