Passed
Push — master ( ea6437...0c63d3 )
by Sathish
03:02
created

UserReport::columns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
ccs 5
cts 8
cp 0.625
crap 2.2109
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace UserManagement\Reports;
3
4
use SilverStripe\Forms\DateField;
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Reports\Report;
7
use SilverStripe\Security\Member;
8
use SilverStripe\Security\Security;
9
use SilverStripe\Forms\TextField;
10
use SilverStripe\SiteConfig\SiteConfig;
11
12
/**
13
 *
14
 * Base class for creating reports that can be filtered to a specific range.
15
 * Record grouping is also supported.
16
 *
17
 * @package user-management
18
 *
19
 */
20
class UserReport extends Report
21
{
22
    protected $periodfield = '"Member"."Created"';
23
24
    /**
25
     * It returns Tile of the report
26
     * @return string
27
     */
28 1
    public function title()
29
    {
30 1
        return 'Customer List';
31
    }
32
33
    /**
34
     * @param array $params
35
     * @return ArrayList
0 ignored issues
show
Bug introduced by
The type UserManagement\Reports\ArrayList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     */
37 1
    public function sourceRecords($params)
38
    {
39
        
40 1
        $fields = $this->parameterFields();
41 1
        $fields->setValues($params);
42 1
        $start = $fields->fieldByName('StartPeriod')->dataValue();
43 1
        $end = $fields->fieldByName('EndPeriod')->dataValue();
44 1
        $firstName = $fields->fieldByName('FirstName')->dataValue();
45 1
        if ($end) {
46 1
            $end = date('Y-m-d', strtotime($end) + 86400);
47
        }
48 1
        $config = SiteConfig::current_site_config();
49 1
        $member = Member::get()->filter('Groups.Title', $config->CustomerGroup()->Title);
50 1
        $filter = $this->FilterByDate($start, $end);
51 1
        if ($this->FilterByName($filter, $firstName)) {
52 1
            return $member->where($filter);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $member->where($filter) returns the type SilverStripe\ORM\DataList which is incompatible with the documented return type UserManagement\Reports\ArrayList.
Loading history...
53
        }
54
        return $member;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $member returns the type SilverStripe\ORM\DataList which is incompatible with the documented return type UserManagement\Reports\ArrayList.
Loading history...
55
    }
56
57
    /**
58
    * Returns columns names of the reports
59
    * @return array
60
    */
61 1
    public function columns()
62
    {
63
        $fields = [
64 1
            'FirstName' => 'FirstName',
65
            'Surname' => 'Surname',
66
            'Email' => 'Email'
67
        ];
68 1
        $config = SiteConfig::current_site_config();
69 1
        if ($config->getField("ExportFields")) {
70
            $ExportFields = json_decode($config->getField("ExportFields"), true);
71
            $ExtraFields = array_combine($ExportFields, $ExportFields);
72
            $fields = array_merge($fields, $ExtraFields);
73
        }
74
75
76 1
        return $fields;
77
    }
78
    
79
    /**
80
    * Return a FieldList of the fields that can be used to filter
81
    * @return array
82
    */
83 2
    public function parameterFields()
84
    {
85 2
        $member = Security::getCurrentUser() ? Security::getCurrentUser() : Member::create();
86 2
        $dateformat = $member->getDateFormat();
0 ignored issues
show
Unused Code introduced by
The assignment to $dateformat is dead and can be removed.
Loading history...
87 2
        $fields = FieldList::create(
88 2
            $firstName = TextField::create('FirstName', 'FirstName'),
89 2
            $start = DateField::create('StartPeriod', 'Start Date'),
90 2
            $end = DateField::create('EndPeriod', 'End Date')
91
        );
92
        
93 2
        return $fields;
94
    }
95
    
96
    /**
97
    * @param string $start
98
    * @param string $end
99
    * @param string $firstName
100
    * @return string
101
    */
102 1
    public function FilterByDate($start, $end)
103
    {
104 1
        $filter = false;
105 1
        if ($start && $end) {
106 1
            $filter = "Member.Created BETWEEN '$start' AND '$end'";
107
        } elseif ($start) {
108
            $filter = "Member.Created > '$start'";
109
        } elseif ($end) {
110
            $filter = "Member.Created <= '$end'";
111
        }
112 1
        return $filter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $filter could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
113
    }
114
115
    /**
116
    * @param string $firstName
117
    * @param string $filter
118
    * @return string
119
    */
120 1
    public function FilterByName($filter, $firstName)
121
    {
122 1
        if ($firstName) {
123 1
            $filter = ($filter)? $filter . " AND FirstName Like '%$firstName%'" : "FirstName Like '%$firstName%'";
124
        }
125 1
        return $filter;
126
    }
127
}
128