Completed
Push — master ( ad1b4b...e177f1 )
by Nicolaas
01:13
created

docontactusform()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 30
nc 5
nop 2
1
<?php
2
3
class ContactUsPageControllerExtension extends Extension
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 $add_placeholders = false;
0 ignored issues
show
Unused Code introduced by
The property $add_placeholders 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
8
    private static $allowed_actions = 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 $allowed_actions 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...
9
        'ContactUsForm' => true,
10
        'docontactusform' => true
11
    );
12
13
    protected $contactUsProcessingForm = false;
14
15
    function ContactUsProcessingForm() {
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...
16
        return $this->contactUsProcessingForm;
17
    }
18
19
    function ContactUsForm()
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...
20
    {
21
        $m = Member::currentUser();
22
        if(!$m) {
23
            $m = new Member();
24
        }
25
        $fields = new FieldList(
26
            TextField::create('FirstName',_t('ContactUsPageControllerExtension.FIRST_NAME', 'First Name'), $m->FirstName),
27
            TextField::create('Surname', _t('ContactUsPageControllerExtension.SURNAME', 'Surname'), $m->Surname),
28
            EmailField::create('Email',_t('ContactUsPageControllerExtension.EMAIL', 'Email'), $m->Email),
29
            TextField::create('Phone', _t('ContactUsPageControllerExtension.PHONE', 'Phone')),
30
            TextareaField::create('Enquiry', SiteConfig::current_site_config()->ContactUsFormEnquiryLabel)
31
        );
32
        if(Config::inst()->get('ContactUsPageControllerExtension', 'add_placeholders')) {
33
            foreach($fields as $field) {
34
                $field->setAttribute('placeholder', $field->Title());
35
            }
36
        }
37
        $actions = FieldList::create(
38
            FormAction::create('docontactusform', SiteConfig::current_site_config()->ContactUsFormSendLabel)
39
        );
40
        $form =  new Form(
41
            $this->owner,
42
            'ContactUsForm',
43
            $fields,
44
            $actions,
45
            RequiredFields::create(
46
                array("Email", "Enquiry")
47
            )
48
        );
49
        // Update the form to add the protecter field to it
50
51
        return $form;
52
53
    }
54
55
    function docontactusform ($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
        $data = Convert::raw2sql($data);
58
        $obj = ContactUsFormEntry::create_enquiry($data, $this->owner->dataRecord);
59
        $subject = _t('ContactUsPageControllerExtension.THANK_YOU_SUBJECT', 'Thank you for your enquiry').' - '.Director::absoluteBaseURL();
60
        $body = "<strong>$subject</strong><br /><br />";
61
        foreach($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
62
            if($key == "url") {
63
                $value = Director::absoluteURL($value);
64
                $value = trim($value, "ContactUsForm");
65
            }
66
            if($key == "SecurityID" || $key == "Send" || $key == "Captcha" || $key == 'action_docontactusform') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
67
                //do nothing
68
            }else {
69
                $body .=  "<br /><br />".$key.': '.strip_tags($value).'';
70
            }
71
        }
72
        $adminEmailAddress = SiteConfig::current_site_config()->ContactUsFormEmail;
73
        $customerEmailAddress = $data["Email"];
74
        $email = Email::create(
75
            $from = $customerEmailAddress,
76
            $to = $adminEmailAddress,
77
            $subject,
78
            $body
79
        );
80
        $obj->SentToAdmin = $email->send();
0 ignored issues
show
Documentation introduced by
The property SentToAdmin 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...
81
        $obj->AdminEmail = $adminEmailAddress;
0 ignored issues
show
Documentation introduced by
The property AdminEmail 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...
82
83
        $email = Email::create(
84
            $from = $adminEmailAddress,
85
            $to = $customerEmailAddress,
86
            $subject,
87
            $body
88
        );
89
        $obj->SentToCustomer = $email->send();
0 ignored issues
show
Documentation introduced by
The property SentToCustomer 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...
90
        $this->contactUsProcessingForm = true;
91
        $obj->write();
92
93
        return array();
94
    }
95
}
96