ContactUsFormEntry::getNiceData()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 2
nop 0
1
<?php
2
3
class ContactUsFormEntry extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
        'AdminEmail' => 'Varchar(255)',
8
        'Email' => 'Varchar(255)',
9
        'FirstName' => 'Varchar(255)',
10
        'Surname' => 'Varchar(255)',
11
        'Phone' => 'Varchar(255)',
12
        'Enquiry' => 'Text',
13
        'Data' => 'Text',
14
        'Responded' => 'Boolean',
15
        'SentToAdmin' => 'Boolean',
16
        'SentToCustomer' => 'Boolean'
17
    );
18
19
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
        'Page' => 'SiteTree'
21
    );
22
23
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $casting is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
        'NiceData' => 'HTMLText'
25
    );
26
27
    public static function create_enquiry($sqlSafeData, $page)
28
    {
29
        $obj = ContactUsFormEntry::create(
30
            array(
31
                'Email'=> ($sqlSafeData['Email']),
32
                'FirstName' => ($sqlSafeData['FirstName']),
33
                'Surname' => ($sqlSafeData['Surname']),
34
                'Phone' => ($sqlSafeData['Phone']),
35
                'Enquiry' => ($sqlSafeData['Enquiry']),
36
                //data is a backup for any additional fields in the form...
37
                "PageID" => $page->ID
38
            )
39
        );
40
        unset($sqlSafeData['url']);
41
        unset($sqlSafeData['action_docontactusform']);
42
        unset($sqlSafeData['SecurityID']);
43
        unset($sqlSafeData['Enquiry']);
44
        unset($sqlSafeData['Phone']);
45
        unset($sqlSafeData['FirstName']);
46
        unset($sqlSafeData['Surname']);
47
        unset($sqlSafeData['Email']);
48
        $obj->Data = serialize($sqlSafeData);
0 ignored issues
show
Documentation introduced by
The property Data does not exist on object<ContactUsFormEntry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
        $obj->write();
50
51
        return $obj;
52
    }
53
54
    private static $singular_name = "Customer Enquiry";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
55
        function i18n_singular_name() { return self::$singular_name;}
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
57
    private static $plural_name = "Customer Enquiries";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
58
        function i18n_plural_name() { return self::$plural_name;}
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
60
    private static $indexes = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $indexes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
61
        "Email" => true
62
    );
63
64
    private static $default_sort = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $default_sort is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
65
        'Created' => 'Desc'
66
    );
67
68
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
69
        'Email' => 'From',
70
        'AdminEmail' => 'To',
71
        'Created' => 'Created',
72
        'SentToAdmin.Nice' => 'Sent to Admin',
73
        'SentToCustomer.Nice' => 'Sent to Customer',
74
        'Enquiry' => 'Enquiry',
75
        'Responded.Nice' => 'Replied',
76
        'Page.Title' => 'Page'
77
    );
78
79
    private static $field_labes = array(
0 ignored issues
show
Unused Code introduced by
The property $field_labes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
80
        'Email' => 'From',
81
        'AdminEmail' => 'To'
82
    );
83
84
    public function canCreate($member = null) {
85
        return false;
86
    }
87
88
    public function canDelete($member = null) {
89
        return false;
90
    }
91
92
    public function getCMSFields()
93
    {
94
        $fields = parent::getCMSFields();
95
        $fields->replaceField(
96
                'Data',
97
                LiteralField::create('NiceData', $this->getNiceData())
98
         );
99
        $fixedFields = array(
100
            'AdminEmail',
101
            'SentToCustomer',
102
            'SentToAdmin',
103
            'FirstName',
104
            'Email',
105
            'Surname',
106
            'Phone',
107
            'Enquiry'
108
        );
109
110
        $labels = $this->FieldLabels();
111
        foreach($fixedFields as $fixedField) {
112
            $fields->replaceField(
113
                $fixedField,
114
                ReadonlyField::create($fixedField, $labels[$fixedField])
115
            );
116
        }
117
        $fields->removeByName('PageID');
118
119
        return $fields;
120
   }
121
122
    /**
123
     * returns list of fields as they are exported
124
     * @return array
125
     * Field => Label
126
     */
127
  //  public function getExportFields();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
129
    function getNiceData()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
130
    {
131
        $array = unserialize($this->Data);
0 ignored issues
show
Documentation introduced by
The property Data does not exist on object<ContactUsFormEntry>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
        $html = '<ul>';
133
        if(is_array($array)){
134
            foreach($array as $field => $value){
135
                $html .= '<li><strong>'.$field.':</strong> '.$value;
136
            }
137
        }
138
        $html .= '</ul>';
139
140
        return $html;
141
    }
142
143
}
144