From a29dbde9d03eabf4488f98de0f9a9717dd5317d2 Mon Sep 17 00:00:00 2001 From: Tony Air Date: Fri, 19 Jul 2024 14:42:35 +0200 Subject: [PATCH] IMPR: next/prev buttons for elements --- composer.json | 3 +- src/Extensions/BaseElementEx.php | 96 ++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 81a59f3..d5d07c5 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "gorriecoe/silverstripe-linkfield": "*", "lekoala/silverstripe-filepond": "*", "symbiote/silverstripe-addressable": "*", - "a2nt/silverstripe-mapboxfield": "*" + "a2nt/silverstripe-mapboxfield": "*", + "lekoala/silverstripe-cms-actions": "*" }, "autoload": { "psr-4": { diff --git a/src/Extensions/BaseElementEx.php b/src/Extensions/BaseElementEx.php index ca5e80e..e58e8d8 100755 --- a/src/Extensions/BaseElementEx.php +++ b/src/Extensions/BaseElementEx.php @@ -15,6 +15,9 @@ use SilverStripe\ORM\DataExtension; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\LiteralField; use DNADesign\Elemental\Models\ElementalArea; +use LeKoala\CmsActions\CmsInlineFormAction; +use LeKoala\CmsActions\HasPrevNextUtils; +use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\ORM\DataObject; @@ -125,4 +128,97 @@ class BaseElementEx extends DataExtension self::MoveElementToPage($el->ID, $moveID); } } + + public function onAfterUpdateCMSActions(FieldList $fields) + { + self::addPrevNextUtils($this->owner, $fields); + } + + public function PrevRecord() + { + $obj = $this->owner; + $parent = $obj->Parent(); + if (!$parent) { + return; + } + + $els = $parent->Elements(); + if (!$els) { + return; + } + + return $els->filter('Sort:LessThan', $obj->Sort)->first(); + } + + public function NextRecord() + { + $obj = $this->owner; + $parent = $obj->Parent(); + if (!$parent) { + return; + } + + $els = $parent->Elements(); + if (!$els) { + return; + } + + return $els->filter('Sort:GreaterThan', $obj->Sort)->first(); + } + + /** + * @param FieldList $utils + * @return FieldList + */ + public static function addPrevNextUtils($obj, FieldList $utils) + { + $controller = Controller::curr(); + $request = $controller->getRequest(); + $url = rtrim($request->getURL(), '/') . '/'; + + $query = $_GET; + if (!empty($query)) { + $url .= '?' . http_build_query($query); + } + + $routeParams = $request->routeParams(); + $recordClass = get_class($obj); + + $getPreviousRecordID = $routeParams['cmsactions'][$recordClass]['PreviousRecordID'] ?? $request->param('PreviousRecordID'); + $getNextRecordID = $routeParams['cmsactions'][$recordClass]['NextRecordID'] ?? $request->param('NextRecordID'); + + $search = sprintf('/%d/', $obj->ID); + $replaceStr = '/%d/'; + $PrevRecordLink = $routeParams['cmsactions'][$recordClass]['PrevRecordLink'] ?? null; + $NextRecordLink = $routeParams['cmsactions'][$recordClass]['NextRecordLink'] ?? null; + if (!$PrevRecordLink) { + $PrevRecordLink = str_replace($search, sprintf($replaceStr, $getPreviousRecordID), $url); + } + if (!$NextRecordLink) { + $NextRecordLink = str_replace($search, sprintf($replaceStr, $getNextRecordID), $url); + } + + if ($obj->ID && $getNextRecordID) { + $utils->unshift( + $NextBtnLink = new CmsInlineFormAction( + 'NextBtnLink', + _t('HasPrevNextUtils.Next', 'Next') . ' >', + 'btn-secondary' + ) + ); + $NextBtnLink->setLink($NextRecordLink); + } + if ($obj->ID && $getPreviousRecordID) { + $utils->unshift( + $PrevBtnLink = new CmsInlineFormAction( + 'PrevBtnLink', + '< ' . _t('HasPrevNextUtils.Previous', 'Previous'), + 'btn-secondary' + ) + ); + $PrevBtnLink->setLink($PrevRecordLink); + } + + return $utils; + } }