-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from DRuiter/fix/pinned-highlighted
fix/pinned-highlighted
- Loading branch information
Showing
3 changed files
with
171 additions
and
35 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/GridFieldActions/ArticlesGridFieldAddExistingAutocompleter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters