1 <?php
2
3 4 5 6 7 8 9 10
11 class SimpleOrderPage extends Page {
12
13
14 static $can_be_root = true;
15 static $allowed_children = "none";
16
17 static $db = array(
18 'OrderEmail' => 'Varchar(255)',
19 'DeliveryPrice' => 'Text',
20 );
21
22 static $has_many = array(
23 'Orders' => 'SimpleOrderData',
24 'PaymentTypes' => 'PaymentType',
25 );
26
27 static $defaults = array(
28 'DevEditType' => 'Fixed',
29 );
30
31 32 33
34 static $use_spam_protection = true;
35
36 static function use_spam_protection($value = true) {
37 SimpleOrderPage::$use_spam_protection = $value;
38 }
39
40 41 42 43 44
45 function ActivePaymentTypes() {
46 return $this->PaymentTypes('Active=1');
47 }
48
49 function getCMSFields() {
50 SiteTree::disableCMSFieldsExtensions();
51 $fields = parent::getCMSFields();
52 SiteTree::enableCMSFieldsExtensions();
53
54
55 $fields->addFieldToTab('Root.Content.Main', new TextField('OrderEmail', $this->fieldLabel('OrderEmail')), 'Content');
56 $fields->addFieldToTab('Root.Content.Main', new TextField('DeliveryPrice', $this->fieldLabel('DeliveryPrice')), 'Content');
57
58 $fields->addFieldToTab('Root.Content', new Tab('Orders', _t('SimpleOrderPage.tab_Orders', 'Orders')), 'Metadata');
59 $fields->addFieldToTab(
60 'Root.Content.Orders',
61 $orders = new HasManyComplexTableField($this, 'Orders', 'SimpleOrderData')
62 );
63 $orders->setPermissions(array('delete', 'edit'));
64 $orders->setRelationAutoSetting(true);
65 $orders->Markable = false;
66
67 $fields->addFieldToTab('Root.Content', new Tab('Payments', _t('SimpleOrderPage.tab_PaymentTypes', 'Payment Types')), 'Metadata');
68 $fields->addFieldToTab(
69 'Root.Content.Payments',
70 $payTypes = new HasManyComplexTableField($this, 'PaymentTypes', 'PaymentType')
71 );
72 $payTypes->setPermissions(array('add', 'delete', 'edit'));
73 $payTypes->relationAutoSetting = true;
74 $payTypes->Markable = false;
75
76 $this->extend('updateCMSFields', $fields);
77
78 return $fields;
79 }
80
81 function canCreate() {
82 return (DataObject::get_one('SimpleOrderPage') || DataObject::get_one('SimpleOrderForm')) ? false : parent::canCreate();
83 }
84
85 function canDelete() {
86 return (Director::isDev()) ? parent::canDelete() : false;
87 }
88
89 function requireDefaultRecords() {
90 return;
91 }
92
93 function onBeforeDelete() {
94 parent::onBeforeDelete();
95 foreach ($this->Orders() as $order)
96 $order->delete();
97
98 foreach ($this->PaymentTypes() as $type)
99 $type->delete();
100 }
101 }
102
103 class SimpleOrderPage_Controller extends Page_Controller {
104
105 protected $item = false;
106 protected $itemCount = 1;
107
108 function init() {
109 parent::init();
110 $data = $this->getRequest();
111
112 if (isset($data['Num']) && is_numeric($data['Num']) && $data['Num'] > 0) {
113 $this->itemCount = (int) $data['Num'];
114 }
115
116 $id = 0;
117 if (isset($data['itemId']) && is_numeric($data['itemId'])) {
118 $id = $data['itemId'];
119 }
120 elseif (isset($data['ProductID']) && is_numeric($data['ProductID'])) {
121 $id = $data['ProductID'];
122 }
123 if ($id) {
124 $this->item = DataObject::get_by_id(singleton('Product')->ClassName, $id);
125 }
126 else {
127 Director::redirect('');
128 }
129 }
130
131 function index() {
132 return $this->checkout();
133 }
134
135 136 137 138 139
140 function checkout() {
141 if ($this->item) {
142 return $this->render(array(
143 'Product' => $this->item,
144 'ProductCount' => $this->itemCount,
145 'TotalSum' => $this->item->Price(1) * $this->itemCount,
146 'Form' => $this->SimpleOrderForm(),
147 ));
148 }
149 Director::redirect('');
150 }
151
152 153 154 155 156 157 158 159
160 public function complite($data, $form) {
161 if ($this->item) {
162 $order = new SimpleOrderData();
163
164 $form->saveInto($order);
165
166 $order->ProductID = $this->item->ID;
167 $order->ParentID = $this->ID;
168 $order->Num = $this->itemCount;
169 $order->TotalSum = $order->Product()->Price(1) * $order->Num;
170 $order->write();
171
172
173 $siteName = $this->SiteConfig()->Title;
174 if (!$siteName)
175 $siteName = Director::absoluteBaseURL();
176
177
178 $mail = new Email($this->SiteConfig()->SenderEmail(), $this->OrderEmail, sprintf(_t('SimpleOrderPage.SellerSubject', 'New order from %s'), $siteName));
179 $mail->setTemplate('SimpleOrderSeller');
180 $mail->populateTemplate($order);
181 $mail->send();
182 unset($mail);
183
184
185 $mail = new Email($this->SiteConfig()->SenderEmail(), $order->Email, sprintf(_t('SimpleOrderPage.CustomerSubject', 'Order from %s'), $siteName));
186 $mail->setTemplate('SimpleOrderCustomer');
187 $mail->populateTemplate($order);
188 $mail->send();
189 unset($mail);
190
191
192 return $this->renderWith(array('SimpleOrderComplite', 'Page'), array('Order' => $order));
193 }
194 }
195
196 197 198 199 200 201 202 203
204 public function SimpleOrderForm() {
205 if (!$this->item) return 'xxx';
206
207 $payments = $this->data()->ActivePaymentTypes();
208 if (!$payments) {
209 user_error(_t('SimpleOrderPage.PaymentTypeNeed', 'Create one payment type first'), E_USER_ERROR);
210 }
211 $order = singleton('SimpleOrderData');
212
213 $form = new Form(
214 $this,
215 'SimpleOrderForm',
216 $order->getOrderFields($payments->toDropDownMap(), $this->item, $this->itemCount),
217 new FieldSet(
218 new FormAction('complite', _t('SimpleOrderForm.OrderButton', 'Checkout'))
219 ),
220 new CustomRequiredFields($order->getOrderRequiredFields())
221 );
222 $form->setTemplate('SimpleOrderForm');
223
224 $this->extend('updateSimpleOrderForm', $form);
225
226
227 if (class_exists('SpamProtectorManager') && SimpleOrderPage::$use_spam_protection)
228 SpamProtectorManager::update_form($form);
229
230 return $form;
231 }
232 }
233
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-