Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/pinned-highlighted #14

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/GridFieldActions/ArticlesGridFieldAddExistingAutocompleter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace TheWebmen\Articles\GridFieldActions;

use SilverStripe\ORM\DB;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use TheWebmen\Articles\Pages\ArticlePage;

class ArticlesGridFieldAddExistingAutocompleter extends GridFieldAddExistingAutocompleter
{
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($fieldToUpdate = $this->getFieldToUpdate($gridField)) {
$IDToUpdate = $data['relationID'];

// Safety check for possible injection
if (!is_numeric($IDToUpdate)) {
return parent::handleAction($gridField, $actionName, $arguments, $data);
}

$item = ArticlePage::get_by_id($IDToUpdate);

if (!$item) {
return parent::handleAction($gridField, $actionName, $arguments, $data);
}

DB::query("UPDATE TheWebmen_ArticlePage SET $fieldToUpdate = 1 WHERE ID = $IDToUpdate");

if ($item->isPublished()) {
DB::query("UPDATE TheWebmen_ArticlePage_Live SET $fieldToUpdate = 1 WHERE ID = $IDToUpdate");
}
}

parent::handleAction($gridField, $actionName, $arguments, $data);
}

private function getFieldToUpdate(GridField $gridField): ?string
{
switch ($gridField->getList()->getJoinTable()) {
case 'TheWebmen_ArticlesPage_PinnedArticles':
return 'Pinned';

case 'TheWebmen_ArticlesPage_HighlightedArticles':
return 'Highlighted';

default:
return null;
}
}
}
52 changes: 52 additions & 0 deletions src/GridFieldActions/ArticlesGridFieldDeleteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace TheWebmen\Articles\GridFieldActions;

use SilverStripe\ORM\DB;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;

class ArticlesGridFieldDeleteAction extends GridFieldDeleteAction
{
public function __construct(bool $removeRelation = true)
{
parent::__construct($removeRelation);
}

public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($fieldToUpdate = $this->getFieldToUpdate($gridField)) {
$IDToUpdate = $arguments['RecordID'];

// Safety check for possible injection
if (!is_numeric($IDToUpdate)) {
return parent::handleAction($gridField, $actionName, $arguments, $data);
}

/** @var ArticlePage $item */
$item = $gridField->getList()->byID($IDToUpdate);

DB::query("UPDATE TheWebmen_ArticlePage SET $fieldToUpdate = 0 WHERE ID = $IDToUpdate");

if ($item->isPublished()) {
DB::query("UPDATE TheWebmen_ArticlePage_Live SET $fieldToUpdate = 0 WHERE ID = $IDToUpdate");
}
}

parent::handleAction($gridField, $actionName, $arguments, $data);
}

private function getFieldToUpdate(GridField $gridField): ?string
{
switch ($gridField->getList()->getJoinTable()) {
case 'TheWebmen_ArticlesPage_PinnedArticles':
return 'Pinned';

case 'TheWebmen_ArticlesPage_HighlightedArticles':
return 'Highlighted';

default:
return null;
}
}
}
103 changes: 68 additions & 35 deletions src/Pages/ArticlesPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\NumericField;
use SilverStripe\Lumberjack\Forms\GridFieldConfig_Lumberjack;
use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeAddNewButton;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Versioned\GridFieldArchiveAction;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use TheWebmen\Articles\Controllers\ArticlesPageController;
use TheWebmen\Articles\GridFieldActions\ArticlesGridFieldAddExistingAutocompleter;
use TheWebmen\Articles\GridFieldActions\ArticlesGridFieldDeleteAction;
use TheWebmen\Articles\Models\Author;
use TheWebmen\Articles\Models\Tag;

Expand Down Expand Up @@ -197,8 +201,16 @@ private function getGridConfig(string $sortColumn): GridFieldConfig_RelationEdit
$gridfieldConfig->removeComponentsByType(GridFieldArchiveAction::class);
$gridfieldConfig->removeComponentsByType(GridFieldEditButton::class);

// Custom delete action that properly reflects the pinned/highlighted property to the removed relation
$gridfieldConfig->removeComponentsByType(GridFieldDeleteAction::class);
$gridfieldConfig->addComponent(new ArticlesGridFieldDeleteAction());

// Custom add action that properly reflects the pinned/highlighted property to the added relation
$gridfieldConfig->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
$gridfieldConfig->addComponent(new ArticlesGridFieldAddExistingAutocompleter());

/** @var GridFieldAddExistingAutocompleter $autocompleter */
$autocompleter = $gridfieldConfig->getComponentByType(GridFieldAddExistingAutocompleter::class);
$autocompleter = $gridfieldConfig->getComponentByType(ArticlesGridFieldAddExistingAutocompleter::class);
$autocompleter
->setSearchList(
ArticlePage::get()->filter(
Expand Down Expand Up @@ -263,42 +275,63 @@ public function getControllerName(): string

protected function onAfterWrite()
{
$articlePages = ArticlePage::get()->filter('ParentID', $this->ID);

$articlePages->each(
function (ArticlePage $articlePage) {
$pinned = $articlePage->Pinned;
$highlighted = $articlePage->Highlighted;

if (!$highlighted && in_array($articlePage->ID, $this->HighlightedArticles()->column('ID'))) {
$articlePage->Highlighted = true;
}

if ($highlighted && !in_array($articlePage->ID, $this->HighlightedArticles()->column('ID'))) {
$articlePage->Highlighted = false;
}

if (!$pinned && in_array($articlePage->ID, $this->PinnedArticles()->column('ID'))) {
$articlePage->Pinned = true;
}

if ($pinned && !in_array($articlePage->ID, $this->PinnedArticles()->column('ID'))) {
$articlePage->Pinned = false;
}

try {
$isModifiedOnDraft = $articlePage->isModifiedOnDraft();
$articlePage->write();

if (!$isModifiedOnDraft) {
$articlePage->publishRecursive();
}
} catch (\Exception $exception) {
Injector::inst()->get('LoggingService')->exception($exception);
}
}
$parentID = $this->ID;

DB::query("
UPDATE TheWebmen_ArticlePage SET Highlighted = 1 WHERE ID IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_HighlightedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree WHERE ParentID = $parentID)
");

DB::query("
UPDATE TheWebmen_ArticlePage SET Highlighted = 0 WHERE ID NOT IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_HighlightedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree WHERE ParentID = $parentID)"
);

DB::query("
UPDATE TheWebmen_ArticlePage SET Pinned = 1 WHERE ID IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_PinnedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree WHERE ParentID = $parentID)
");

DB::query("
UPDATE TheWebmen_ArticlePage SET Pinned = 0 WHERE ID NOT IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_PinnedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree WHERE ParentID = $parentID)"
);

parent::onAfterWrite();
}

public function onAfterPublish()
{
$parentID = $this->ID;

DB::query("
UPDATE TheWebmen_ArticlePage_Live SET Highlighted = 1 WHERE ID IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_HighlightedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree_Live WHERE ParentID = $parentID)
");

DB::query("
UPDATE TheWebmen_ArticlePage_Live SET Highlighted = 0 WHERE ID NOT IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_HighlightedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree_Live WHERE ParentID = $parentID)"
);

DB::query("
UPDATE TheWebmen_ArticlePage_Live SET Pinned = 1 WHERE ID IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_PinnedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree_Live WHERE ParentID = $parentID)
");

DB::query("
UPDATE TheWebmen_ArticlePage_Live SET Pinned = 0 WHERE ID NOT IN (
SELECT TheWebmen_ArticlePageID FROM TheWebmen_ArticlesPage_PinnedArticles WHERE TheWebmen_ArticlesPageID = $parentID
) AND ID IN (SELECT ID FROM SiteTree_Live WHERE ParentID = $parentID)"
);

parent::onAfterPublish();
}
}