1 <?php
2
3 4 5 6 7 8 9 10
11 class SimpleOrderData extends DataObject {
12
13 static $db = array(
14 'Num' => 'Int',
15 'Client' => 'Varchar(255)',
16 'City' => 'Varchar',
17 'Address' => 'Text',
18 'Phone' => 'Varchar',
19 'Email' => 'Varchar',
20 'Comment' => 'Text',
21 'Status' => "Enum('new,processing,done','new')",
22 'TotalSum' => 'CatalogPrice'
23 );
24
25 static $defaults = array(
26 'Status' => 'new',
27 );
28
29 static $has_one = array(
30 'Parent' => 'SimpleOrderPage',
31 'Product' => 'Product',
32 'PaymentType' => 'PaymentType',
33 );
34
35 static $default_sort = "ID DESC";
36 static $searchable_fields = array('Product.Title', 'Client', 'Email', 'Phone', 'TotalSum', 'Status');
37 static $summary_fields = array('ID', 'Product.Title', 'Client', 'Email', 'Phone', 'TotalSum', 'PaymentType.Title', 'StatusText');
38
39 function fieldLabels($rel = true) {
40 $labels = parent::fieldLabels($rel);
41
42 $labels['Product.Title'] = _t('SimpleOrderData.has_one_Product', 'Product title');
43 $labels['PaymentType.Title'] = _t('SimpleOrderData.has_one_PaymentType', 'Payment type');
44 $labels['StatusText'] = _t('SimpleOrderData.db_Status', 'Order status');
45 return $labels;
46 }
47
48 49 50 51 52 53 54
55 static function text_status($status) {
56 return _t('SimpleOrderData.Status_' . $status, $status);
57 }
58
59 60 61
62 static $required_fields = array('ProductID', 'Num', 'Client', 'Email', 'Phone', 'PaymentTypeID');
63
64 65 66 67 68
69 static function set_required_fields($fields) {
70 self::$required_fields = $fields;
71 }
72
73 74 75
76 static $hidden_fields = array('Address');
77
78 79 80 81 82
83 static function hide_fields($fields) {
84 self::$hidden_fields = $fields;
85 }
86
87 88 89 90 91 92 93 94 95
96 function getOrderFields($payments, $item, $Num=1) {
97 if (count($payments) == 0) {
98 $paymentField = new HiddenField('PaymentTypeID', '', 'undefined');
99 } elseif (count($payments) == 1) {
100 $paymentField = new HiddenField('PaymentTypeID', '', array_shift(array_keys($payments)));
101 }
102 else {
103 $paymentField = new DropdownField('PaymentTypeID', $this->fieldLabel('PaymentType'), $payments, '', null, _t('SimpleOrderData.ChoosePaymentType', ' '));
104 }
105
106 $fields = new FieldSet(
107 new HiddenField('ProductID', '', $item->ID),
108 new HiddenField('itemId', '', $item->ID),
109 new HiddenField('Num', $this->fieldLabel('Num'), $Num),
110
111 $nameField = new TextField('Client', _t('SimpleOrderData.db_Client_Frontend', 'Your name')),
112 $cityField = new TextField('City', $this->fieldLabel('City')),
113 $addressField = new TextField('Address', $this->fieldLabel('Address')),
114 new EmailField('Email', $this->fieldLabel('Email')),
115 new PhoneField('Phone', $this->fieldLabel('Phone')),
116 $paymentField,
117 new TextareaField('Comment', $this->fieldLabel('Comment'))
118 );
119 $nameField->setAutocomplete('name');
120 $cityField->setAutocomplete('city');
121 $addressField->setAutocomplete('street-address');
122 foreach (self::$hidden_fields as $f) {
123 $fields->removeByName($f);
124 }
125 $siteConfig = SiteConfig::current_site_config();
126 if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
127 $fields->push($rulesField);
128 }
129 $this->extend('updateOrderFields', $fields);
130 return $fields;
131 }
132
133 134 135 136 137 138
139 function getOrderRequiredFields() {
140 $fields = self::$required_fields;
141 $siteConfig = SiteConfig::current_site_config();
142 if ($siteConfig->hasMethod('SiteAgreementField') && ($rulesField = $siteConfig->SiteAgreementField())) {
143 $fields[] = $rulesField->Name();
144 }
145 $this->extend('updateOrderRequiredFields', $fields);
146 return $fields;
147 }
148
149 function getCMSFields() {
150 $fields = parent::getCMSFields();
151 $statusValues = singleton('SimpleOrderData')->dbObject('Status')->enumValues();
152 foreach ($statusValues as $statusItem => $statusName) {
153 $statusValues[$statusItem] = SimpleOrderData::text_status($statusItem);
154 }
155 $fields->replaceField('Status', new DropdownField('Status', $this->fieldLabel('Status'), $statusValues));
156 $fields->replaceField('ParentID', new HiddenField('ParentID', $this->fieldLabel('ParentID')));
157 $fields->replaceField('ProductID', new HiddenField('ProductID', $this->fieldLabel('ProductID')));
158 return $fields;
159 }
160
161 function () {
162
163 $orderInfo = "<h3>" . _t('SimpleOrderData.OrderInfo', 'Order Info') . "</h3>
164 <p>
165 <a href=\"{$this->Product()->Link()}\" target=\"_blank\">{$this->Product()->Title}</a><br/>
166 " . _t('SimpleOrderData.ItemPrice', 'Price: ') . " {$this->Product()->Price} " . _t('OrderInfo.RUR', 'RUR') . " x {$this->Num} " . _t('OrderInfo.ED', 'ED') . "
167 </p>
168 <p>" . _t('SimpleOrderData.EndPrice', 'End Price') . " " . $this->EndPrice . "</p>
169 <h3>" . _t('SimpleOrderData.OrderInfoClient', 'Client') . "</h3>
170 <p><a href=\"mailto:{$this->Email}\">{$this->Name}</a> - {$this->Phone}</p>
171 <p>" . _t('SimpleOrderData.Adress', 'Adress') . ":<br/> {$this->Adress}</p>
172 <p>" . _t('SimpleOrderData.Comment', 'Comment') . ":<br/> {$this->Comment}</p>";
173
174 $status = array();
175 foreach ($this->dbObject('Status')->enumValues() as $val) {
176 $status[$val] = self::text_status($val);
177 }
178
179 $fields = new FieldSet(
180 new LiteralField('OrderInfo', $orderInfo),
181 new DropdownField('Status', _t('Catalog.StatusField', 'Status'), $status)
182 );
183
184 $this->extend('updateCMSField_popup', $fields);
185 return $fields;
186 }
187
188 189 190 191 192
193 function StatusText() {
194 return self::text_status($this->Status);
195 }
196 }
197
[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.
-