Passed
Push — master ( 695cbc...df8580 )
by Thomas
02:38
created

HasPrevNextUtils   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 27
c 3
b 2
f 0
dl 0
loc 45
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B addPrevNextUtils() 0 39 6
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Forms\FieldList;
7
8
/**
9
 * Add prev next in utils
10
 * Simply call this method in your getCMSUtils
11
 * This is not so useful since silverstripe provides this by default now
12
 */
13
trait HasPrevNextUtils
14
{
15
    /**
16
     * @param FieldList $utils
17
     * @return FieldList
18
     */
19
    public function addPrevNextUtils(FieldList $utils)
20
    {
21
        $controller = Controller::curr();
22
        $request = $controller->getRequest();
23
        $url = rtrim($request->getURL(), '/') . '/';
24
25
        $query = $_GET;
26
        if (!empty($query)) {
27
            $url .= '?' . http_build_query($query);
28
        }
29
30
        $routeParams = $request->routeParams();
31
        $getPreviousRecordID = $routeParams['PreviousRecordID'] ?? $request->param('PreviousRecordID');
32
        $getNextRecordID = $routeParams['NextRecordID'] ?? $request->param('NextRecordID');
33
34
        $search = sprintf('/%d/', $this->ID);
35
        $replaceStr = '/%d/';
36
        if ($this->ID && $getNextRecordID) {
37
            $utils->unshift(
38
                $NextBtnLink = new CmsInlineFormAction(
39
                    'NextBtnLink',
0 ignored issues
show
Bug introduced by
'NextBtnLink' of type string is incompatible with the type LeKoala\CmsActions\action expected by parameter $action of LeKoala\CmsActions\CmsIn...rmAction::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                    /** @scrutinizer ignore-type */ 'NextBtnLink',
Loading history...
40
                    _t('HasPrevNextUtils.Next', 'Next') . ' >',
0 ignored issues
show
Bug introduced by
_t('HasPrevNextUtils.Next', 'Next') . ' >' of type string is incompatible with the type LeKoala\CmsActions\title expected by parameter $title of LeKoala\CmsActions\CmsIn...rmAction::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
                    /** @scrutinizer ignore-type */ _t('HasPrevNextUtils.Next', 'Next') . ' >',
Loading history...
41
                    'btn-secondary'
0 ignored issues
show
Bug introduced by
'btn-secondary' of type string is incompatible with the type LeKoala\CmsActions\extraClass expected by parameter $extraClass of LeKoala\CmsActions\CmsIn...rmAction::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
                    /** @scrutinizer ignore-type */ 'btn-secondary'
Loading history...
42
                )
43
            );
44
            $NextBtnLink->setLink(str_replace($search, sprintf($replaceStr, $getNextRecordID), $url));
45
        }
46
        if ($this->ID && $getPreviousRecordID) {
47
            $utils->unshift(
48
                $PrevBtnLink = new CmsInlineFormAction(
49
                    'PrevBtnLink',
50
                    '< ' . _t('HasPrevNextUtils.Previous', 'Previous'),
51
                    'btn-secondary'
52
                )
53
            );
54
            $PrevBtnLink->setLink(str_replace($search, sprintf($replaceStr, $getPreviousRecordID), $url));
55
        }
56
57
        return $utils;
58
    }
59
}
60