Completed
Push — master ( 3b65eb...dc665d )
by Nicolaas
11:00 queued 02:51
created

code/forms/OrderForm_Feedback.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
4
class OrderForm_Feedback extends Form
5
{
6
7
    protected $order = null;
8
9
    public function __construct(Controller $controller, $name, Order $order)
10
    {
11
        $this->order = $order;
12
        $values = $this->getValueFromOrderConfirmationPage('FeedbackValuesOptions');
13
        $values = explode(',', $values);
14
        $newValues = array();
15
        foreach($values as $value) {
16
            $value = trim($value);
17
            $newValues[Convert::raw2att($value)] = $value;
18
        }
19
        $fields = FieldList::create(
20
            array(
21
                OptionsetField::create(
22
                    'Rating',
23
                    $this->getValueFromOrderConfirmationPage('FeedbackValuesFieldLabel'),
24
                    $newValues
25
                ),
26
                TextareaField::create('Note', $this->getValueFromOrderConfirmationPage('FeedbackNotesFieldLabel'))
27
            )
28
        );
29
        $actions = FieldList::create(
30
            FormAction::create('dofeedback', $this->getValueFromOrderConfirmationPage('FeedbackFormSubmitLabel'))
31
        );
32
        $requiredFields = array(
33
            'FeedbackValue'
34
        );
35
        $validator = OrderForm_Feedback_Validator::create($requiredFields);
36
        parent::__construct($controller, $name, $fields, $actions, $validator);
37
38
        $oldData = Session::get("FormInfo.{$this->FormName()}.data");
39
        if ($oldData && (is_array($oldData) || is_object($oldData))) {
40
            $this->loadDataFrom($oldData);
41
        }
42
        $this->extend('updateOrderForm_Feedback', $this);
43
    }
44
45
    /**
46
     * @param array        $data The form request data submitted
47
     * @param Form         $form The {@link Form} this was submitted on
48
     * @param HTTPRequest  $request The {@link Form} this was submitted on
49
     */
50
    public function dofeedback(array $data, Form $form, SS_HTTPRequest $request)
51
    {
52
        $SQLData = Convert::raw2sql($data);
53
        if ($this->order) {
54
            $object = OrderFeedback::create($SQLData);
55
            $object->OrderID = $this->order->ID;
56
            $object->write();
57
            $form->sessionMessage(
58
                $this->getValueFromOrderConfirmationPage('FeedbackFormThankYou'),
59
                'good'
60
            );
61
62
            return $this->controller->redirect($this->order->FeedbackLink());
63
        }
64
        $form->sessionMessage(
65
            _t(
66
                'OrderForm_Feedback.COULD_NOT_RECORD_FEEDBACK',
67
                'Sorry, order feedback could not be recorded.'
68
            ),
69
            'bad'
70
        );
71
72
        return $this->controller->redirect($this->order->FeedbackLink());
73
74
    }
75
76
    /**
77
     * saves the form into session.
78
     *
79
     * @param array $data - data from form.
80
     */
81
    public function saveDataToSession()
82
    {
83
        $data = $this->getData();
84
        Session::set("FormInfo.{$this->FormName()}.data", $data);
85
    }
86
87
    protected function getValueFromOrderConfirmationPage($value)
88
    {
89
        if($page = $this->getOrderConfirmationPage()) {
0 ignored issues
show
Are you sure the assignment to $page is correct as $this->getOrderConfirmationPage() (which targets OrderForm_Feedback::getOrderConfirmationPage()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
90
            return $page->$value;
91
        } else {
92
            $defaults = Config::inst()->get('OrderConfirmationPage', 'defaults');
93
            if($defaults && is_array($defaults) && isset($defaults[$value])) {
94
                return $defaults[$value];
95
            }
96
            return _t('OrderForm_Feedback.'.$value, 'OrderForm_Feedback.'.$value.' value not set in translations');
97
        }
98
    }
99
100
    protected $_orderConfirmationPage = null;
101
102
    protected function getOrderConfirmationPage()
103
    {
104
        if(! $this->_orderConfirmationPage) {
105
            $this->_orderConfirmationPage = OrderConfirmationPage::get()->first();
106
        }
107
    }
108
109
}
110