Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

src/Extension/OrderManipulationExtension.php (2 issues)

1
<?php
2
3
namespace SilverShop\Extension;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Forms\OrderActionsForm;
7
use SilverShop\Model\Order;
8
use SilverShop\ShopTools;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Core\Extension;
12
use SilverStripe\Forms\Form;
13
use SilverStripe\ORM\DataList;
14
use SilverStripe\ORM\PaginatedList;
15
use SilverStripe\Security\Security;
16
17
/**
18
 * Provides forms and processing to a controller for editing an
19
 * order that has been previously placed.
20
 *
21
 * @property Controller $owner
22
 */
23
class OrderManipulationExtension extends Extension
24
{
25
    private static $allowed_actions = array(
0 ignored issues
show
The private property $allowed_actions is not used, and could be removed.
Loading history...
26
        'ActionsForm',
27
        'order',
28
    );
29
30
    private static $sessname = 'OrderManipulation.historicalorders';
31
32
    /**
33
     * Add an order to the session-stored history of orders.
34
     */
35
    public static function add_session_order(Order $order)
36
    {
37
        $history = self::get_session_order_ids();
38
        if (!is_array($history)) {
39
            $history = array();
40
        }
41
        $history[$order->ID] = $order->ID;
42
        ShopTools::getSession()->set(self::$sessname, $history);
43
    }
44
45
    /**
46
     * Get historical orders for current session.
47
     */
48
    public static function get_session_order_ids()
49
    {
50
        $history = ShopTools::getSession()->get(self::$sessname);
51
        if (!is_array($history)) {
52
            $history = null;
53
        }
54
        return $history;
55
    }
56
57
    public static function clear_session_order_ids()
58
    {
59
        ShopTools::getSession()->set(self::$sessname, null)->clear(self::$sessname);
60
    }
61
62
    /**
63
     * Get the order via url 'ID' or form submission 'OrderID'.
64
     * It will check for permission based on session stored ids or member id.
65
     *
66
     * @return Order order
67
     */
68
    public function orderfromid()
69
    {
70
        $request = $this->owner->getRequest();
71
        $id = (int)$request->param('ID');
72
        if (!$id) {
73
            $id = (int)$request->postVar('OrderID');
74
        }
75
76
        return $this->allorders()->byID($id);
77
    }
78
79
    /**
80
     * Get all orders for current member / session.
81
     *
82
     * @return DataList of Orders
83
     */
84
    public function allorders()
85
    {
86
        $filters = array(
87
            'ID' => -1 //ensures no results are returned
88
        );
89
        if ($sessids = self::get_session_order_ids()) {
90
            $filters['ID'] = $sessids;
91
        }
92
        if ($member = Security::getCurrentUser()) {
93
            $filters['MemberID'] = $member->ID;
94
        }
95
96
        return Order::get()->filterAny($filters)
97
            ->filter('Status:not', Order::config()->hidden_status);
98
    }
99
100
    /**
101
     * Return all past orders for current member / session.
102
     */
103
    public function PastOrders($paginated = false)
104
    {
105
        $orders = $this->allorders()
106
            ->filter('Status', Order::config()->placed_status);
107
        if ($paginated) {
108
            $orders = PaginatedList::create($orders, $this->owner->getRequest());
109
        }
110
111
        return $orders;
112
    }
113
114
    /**
115
     * Return the {@link Order} details for the current
116
     * Order ID that we're viewing (ID parameter in URL).
117
     *
118
     * @param  HTTPRequest $request
119
     * @return array of template variables
120
     * @throws \SilverStripe\Control\HTTPResponse_Exception
121
     */
122
    public function order(HTTPRequest $request)
123
    {
124
        //move the shopping cart session id to past order ids, if it is now an order
125
        ShoppingCart::singleton()->archiveorderid($request->param('ID'));
126
127
        $order = $this->orderfromid();
128
        if (!$order) {
0 ignored issues
show
$order is of type SilverShop\Model\Order, thus it always evaluated to true.
Loading history...
129
            return $this->owner->httpError(404, 'Order could not be found');
130
        }
131
132
        return array(
133
            'Order' => $order,
134
            'Form' => $this->ActionsForm() //see OrderManipulation extension
135
        );
136
    }
137
138
    /**
139
     * Build a form for cancelling, or retrying payment for a placed order.
140
     *
141
     * @return Form
142
     */
143
    public function ActionsForm()
144
    {
145
        if ($order = $this->orderfromid()) {
146
            $form = OrderActionsForm::create($this->owner, 'ActionsForm', $order);
147
            $form->extend('updateActionsForm', $order);
148
            if (!$form->Actions()->exists()) {
149
                return null;
150
            }
151
152
            return $form;
153
        }
154
        return null;
155
    }
156
157
    protected $sessionmessage;
158
159
    protected $sessionmessagetype = null;
160
161
    public function setSessionMessage($message = 'success', $type = 'good')
162
    {
163
        $this->owner->getRequest()->getSession()
164
            ->set('OrderManipulation.Message', $message)
165
            ->set('OrderManipulation.MessageType', $type);
166
    }
167
168
    public function SessionMessage()
169
    {
170
        $session = $this->owner->getRequest()->getSession();
171
        if ($session && ($message = $session->get('OrderManipulation.Message'))) {
172
            $this->sessionmessage = $message;
173
            $session->clear('OrderManipulation.Message');
174
        }
175
176
        return $this->sessionmessage;
177
    }
178
179
    public function SessionMessageType()
180
    {
181
        $session = $this->owner->getRequest()->getSession();
182
        if ($session && ($type = $session->get('OrderManipulation.MessageType'))) {
183
            $this->sessionmessagetype = $type;
184
            $session->clear('OrderManipulation.MessageType');
185
        }
186
187
        return $this->sessionmessagetype;
188
    }
189
}
190