GridFieldPrintReportButton   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 59
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B generatePrintData() 0 46 5
1
<?php
2
3
namespace SilverStripe\SecurityReport\Forms;
4
5
use SilverStripe\Forms\GridField\GridField;
6
use SilverStripe\Forms\GridField\GridFieldPrintButton;
7
use SilverStripe\ORM\ArrayList;
8
use SilverStripe\ORM\FieldType\DBDatetime;
9
use SilverStripe\Security\Security;
10
use SilverStripe\View\ArrayData;
11
12
/**
13
 * An extension to GridFieldPrintButton to support printing custom Reports
14
 *
15
 * This code was adapted from a solution posted in SilverStripe.org forums:
16
 * http://www.silverstripe.org/customising-the-cms/show/38202
17
 *
18
 * @package securityreport
19
 * @author Michael Armstrong <http://www.silverstripe.org/ForumMemberProfile/show/30887>
20
 * @author Michael Parkhill <[email protected]>
21
 */
22
class GridFieldPrintReportButton extends GridFieldPrintButton
23
{
24
25
    /**
26
     * Export core
27
     *
28
     * Replaces definition in GridFieldPrintButton
29
     * same as original except sources data from $gridField->getList() instead of $gridField->getManipulatedList()
30
     *
31
     * @param GridField
32
     * @return ArrayData
33
     */
34
    public function generatePrintData(GridField $gridField)
35
    {
36
        $printColumns = $this->getPrintColumnsForGridField($gridField);
37
        $header = null;
38
39
        if ($this->printHasHeader) {
40
            $header = new ArrayList();
41
            foreach ($printColumns as $field => $label) {
42
                $header->push(new ArrayData(array(
43
                    "CellString" => $label,
44
                )));
45
            }
46
        }
47
48
        // The is the only variation from the parent class, using getList() instead of getManipulatedList()
49
        $items = $gridField->getList();
50
51
        $itemRows = new ArrayList();
52
53
        foreach ($items as $item) {
54
            $itemRow = new ArrayList();
55
56
            foreach ($printColumns as $field => $label) {
57
                $value = $gridField->getDataFieldValue($item, $field);
58
                $itemRow->push(new ArrayData(array(
59
                    "CellString" => $value,
60
                )));
61
            }
62
63
            $itemRows->push(new ArrayData(array(
64
                "ItemRow" => $itemRow
65
            )));
66
67
            $item->destroy();
68
        }
69
70
        $ret = new ArrayData(array(
71
            "Title" => $this->getTitle($gridField),
72
            "Header" => $header,
73
            "ItemRows" => $itemRows,
74
            "Datetime" => DBDatetime::now(),
75
            "Member" => Security::getCurrentUser(),
76
        ));
77
78
        return $ret;
79
    }
80
}
81