1 <?php
2
3 class BookingPage extends Page {
4
5 static $db = array(
6 'DefaultInTime' => 'Time',
7 'DefaultOutTime' => 'Time',
8 'DefaultCitizenship' => 'Varchar(100)',
9 'BookingInfo' => 'HTMLText',
10 'OrderMessage' => 'HTMLText',
11 'AdminEmail' => 'Varchar(100)',
12 );
13
14 static $has_one = array(
15 'BookingRules' => 'Page',
16 'PrivacyPolicy' => 'Page',
17 );
18 19 20 21 22
23 static $defaults = array(
24 'DefaultInTime' => '12:00',
25 'DefaultOutTime' => '12:00',
26 );
27
28 static function find_links($action='') {
29 $page = DataObject::get_one('BookingPage');
30 if ($page) {
31 $link = $page->Link($action);
32 if (class_exists('Subsite') && Subsite::currentSubsiteID()) {
33
34 }
35 return $link;
36 }
37 return false;
38 }
39
40 41 42 43 44 45
46 static function getFilterDates() {
47 if (!Session::get('BookingFilter')) {
48 Session::set('BookingFilter', array(
49 'FilterStartDate' => date('d.m.Y', strtotime("+1 day")),
50 'FilterEndDate' => date('d.m.Y', strtotime("+2 day"))
51 ));
52 }
53 return Session::get('BookingFilter');
54 }
55
56 57 58 59 60
61 static function getFilterDatesPeriodLength() {
62 $dates = self::getFilterDates();
63 return RoomRate::period_length($dates['FilterStartDate'], $dates['FilterEndDate']);
64 }
65
66 function getAgreeWithRulesAndPolicyText() {
67 return sprintf(_t('BookingPage.AgreeWithRulesAndPolicy', 'I agree with <a target="_new" href="%s">Rules</a> and <a target="_new" href="%s">Policy</a>'), $this->BookingRules()->Link(), $this->PrivacyPolicy()->Link());
68
69 }
70
71 72 73 74 75 76
77 function getHoursList() {
78 $hours = array();
79 for($i = 0; $i < 24; $i++) {
80 $ind = ($i < 10 ? '0' . $i : $i) . ":00";
81 $hours["{$ind}:00"] = $ind;
82 }
83 return $hours;
84 }
85
86 function getCMSFields() {
87 $fields = parent::getCMSFields();
88
89 $fields->addFieldToTab('Root.Content', new Tab("FullContent", $this->fieldLabel('Content')), 'Metadata');
90 $fields->addFieldToTab('Root.Content.FullContent', $fields->dataFieldByName('Content'));
91
92 $hours = BookingPage::getHoursList();
93
94 $fields->addFieldToTab('Root.Content.Main', new HeaderField('', _t('BookingPage.tab_Params', 'Params')));
95 $fields->addFieldToTab('Root.Content.Main', new EmailField('AdminEmail', $this->fieldLabel('AdminEmail')));
96 $fields->addFieldToTab('Root.Content.Main', new TextField('DefaultCitizenship', $this->fieldLabel('DefaultCitizenship')));
97 $fields->addFieldToTab('Root.Content.Main', new DropdownField('DefaultOutTime', $this->fieldLabel('DefaultOutTime'), $hours, "12:00"));
98 $fields->addFieldToTab('Root.Content.Main', new DropdownField('DefaultInTime', $this->fieldLabel('DefaultInTime'), $hours, "12:00"));
99 $fields->addFieldToTab('Root.Content.Main', new TreeDropdownField('BookingRulesID', $this->fieldLabel('BookingRules'), 'Page'));
100 $fields->addFieldToTab('Root.Content.Main', new TreeDropdownField('PrivacyPolicyID', $this->fieldLabel('PrivacyPolicy'), 'Page'));
101
102 $tab = new Tab("Messages", _t('BookingPage.tab_Messages', 'Messages'));
103 $fields->addFieldToTab('Root.Content', $tab, 'Metadata');
104 $tab->push(new HTMLEditorField('BookingInfo', $this->fieldLabel('BookingInfo')));
105 $tab->push(new HTMLEditorField('OrderMessage', $this->fieldLabel('OrderMessage')));
106
107 return $fields;
108 }
109
110 111 112 113 114 115
116 function OrderServiceData() {
117 $fields = new FieldSet();
118 $services = OrderService::get_active();
119 if ($services) {
120 foreach($services as $service) {
121 $f = new TextField("OrderService[$service->ID]", '');
122 $service->FieldHTML = $f->FieldHolder();
123 }
124 }
125 return $services;
126 }
127
128 129 130 131 132
133 function FilterDatesPeriodLengthWord() {
134 return Convert::number2name(
135 self::getFilterDatesPeriodLength(),
136 _t('BookingPage.Night1'),
137 _t('BookingPage.Night4'),
138 _t('BookingPage.Night5')
139 );
140 }
141
142 143 144 145 146 147 148
149 function PersonWord($person) {
150 return Convert::number2name(
151 $person,
152 _t('BookingPage.Person1'),
153 _t('BookingPage.Person4'),
154 _t('BookingPage.Person5')
155 );
156 }
157
158 function getOrder() {
159 return BookingOrder::get_current_order();
160 }
161
162 function SelectRoomStepLink() {
163 return self::find_links();
164 }
165
166 function ServicesStepLink() {
167 return self::find_links('services');
168 }
169
170 function ContactsStepLink() {
171 return self::find_links('contacts');
172 }
173
174 function PaymentStepLink() {
175 return self::find_links('payments');
176 }
177
178 function ConfirmationStepLink() {
179 return self::find_links('confirm');
180 }
181
182 function SummaryStepLink() {
183 return self::find_links('summary');
184 }
185
186 function SelectRoomStepFormAction() {
187 return self::find_links('saveOrderRates');
188 }
189
190 function ServicesStepFormAction() {
191 return self::find_links('saveOrderServices');
192 }
193 }
194
195 class BookingPage_Controller extends Page_Controller {
196
197 function init() {
198 parent::init();
199
200 if (!$this->getOrder() && in_array($this->request->param('Action'), array('services', 'contacts', 'payments', 'confirm', 'summary'))) {
201 if (!$this->redirectedTo()) {
202 $this->redirect(BookingPage::SelectRoomStepLink());
203 }
204 }
205 }
206
207 208 209 210 211
212 function () {
213 $menu = new DataObjectSet();
214 $item = array();
215 $active = true;
216 $item['Title'] = _t('BookingPage.Step_Room');
217 $item['Link'] = BookingPage::SelectRoomStepLink();
218 $item['LinkOrCurrent'] = ($this->getAction() == 'index' ? 'Current' : 'Link');
219 $item['Active'] = $active;
220 $menu->push(new ArrayData($item));
221 if ($item['LinkOrCurrent'] == 'Current') {
222 $active = false;
223 }
224
225 $item['Title'] = _t('BookingPage.Step_Services');
226 $item['Link'] = BookingPage::ServicesStepLink();
227 $item['LinkOrCurrent'] = ($this->getAction() == 'services' ? 'Current' : 'Link');
228 $item['Active'] = $active;
229 $menu->push(new ArrayData($item));
230 if ($item['LinkOrCurrent'] == 'Current') {
231 $active = false;
232 }
233
234 $item['Title'] = _t('BookingPage.Step_Contacts');
235 $item['Link'] = BookingPage::ContactsStepLink();
236 $item['LinkOrCurrent'] = ($this->getAction() == 'contacts' ? 'Current' : 'Link');
237 $item['Active'] = $active;
238 $menu->push(new ArrayData($item));
239 if ($item['LinkOrCurrent'] == 'Current') {
240 $active = false;
241 }
242
243 $item['Title'] = _t('BookingPage.Step_Payment');
244 $item['Link'] = BookingPage::PaymentStepLink();
245 $item['LinkOrCurrent'] = ($this->getAction() == 'payments' ? 'Current' : 'Link');
246 $item['Active'] = $active;
247 $menu->push(new ArrayData($item));
248 if ($item['LinkOrCurrent'] == 'Current') {
249 $active = false;
250 }
251
252 $item['Title'] = _t('BookingPage.Step_Confirmation');
253 $item['Link'] = BookingPage::ConfirmationStepLink();
254 $item['LinkOrCurrent'] = ($this->getAction() == 'confirm' ? 'Current' : 'Link');
255 $item['Active'] = $active;
256 $menu->push(new ArrayData($item));
257 return $menu;
258 }
259
260 function () {
261 $menu = $this->BookingMenu();
262 $prev = false;
263 foreach($menu as $item) {
264 if ($item->LinkOrCurrent == 'Current') {
265 return $prev;
266 }
267 $prev = $item;
268 }
269 return false;
270 }
271
272 function index() {
273 Requirements::javascript(JQUERY_PATH);
274 Requirements::javascript('booking/javascript/script.js');
275
276 $rooms = Room::get_available_rooms();
277 $services = $this->OrderServiceData();
278
279 $this->Rooms = $rooms;
280 $this->OrderServices = $services;
281 $this->FormAction = $this->Link('saveOrderData');
282
283 $action = (Director::is_ajax()) ? 'ajax' : 'index';
284 return parent::defaultAction($action);
285 }
286
287 function DatesRoomsForm() {
288 $fields = new FieldSet();
289 $fields->push(new TextField('FilterStartDate', _t('BookingOrder.StartDate')));
290 $fields->push(new TextField('FilterEndDate', _t('BookingOrder.EndDate')));
291
292 $form = new Form(
293 $this,
294 'DatesRoomsForm',
295 $fields,
296 new FieldSet(new FormAction('doFilterDates', _t('BookingPage.DoSearch')))
297 );
298 $form->loadDataFrom(self::getFilterDates());
299
300 return $form;
301 }
302
303 function doFilterDates($data, $form) {
304 Session::set('BookingFilter', $data);
305 if (!$this->isAjax()) {
306 $this->redirectBack();
307 } else {
308 return $this->index();
309 }
310 }
311
312 function saveBookingOrder($request) {
313 $json = $request->getBody();
314 $data = json_decode($json, true);
315
316 $order = $this->getOrder();
317 if (!$order) {
318 $order = new BookingOrder();
319 $order->Status = 'Basket';
320 $order->write();
321 }
322 $order->saveOrderData($data);
323 $contactsLink = BookingPage::ServicesStepLink();
324 return json_encode(array('Link' => $contactsLink));
325 }
326
327
328 function saveOrderRates($request) {
329 $data = array();
330
331 $dates = BookingPage::getFilterDates();
332 $data['dateArrival'] = $dates['FilterStartDate'];
333 $data['dateDeparture'] = $dates['FilterEndDate'];
334
335 $data['roomsList'] = array();
336 $formData = $request->postVars();
337 $i = 1;
338 if (isset($formData['RoomCount']) && count($formData['RoomCount'])) {
339 foreach($formData['RoomCount'] as $roomID=>$rates) {
340 foreach($rates as $rateID=>$count) {
341 for($j = 1; $j <= $count; $j++) {
342 $data['roomsList'][$i]['room-id'] = $roomID;
343 $data['roomsList'][$i]['rate-id'] = $rateID;
344 $rate = DataObject::get_by_id('RoomRate', $rateID);
345 if ($rate && $rate->AdditionalPlaceCount) {
346 $data['roomsList'][$i]['additional-count'] = $rate->AdditionalPlaceCount;
347 }
348 $i++;
349 }
350 }
351 }
352 }
353
354 $order = $this->getOrder();
355 if (!$order) {
356 $order = new BookingOrder();
357 $order->Status = 'Basket';
358 $order->write();
359 }
360 $order->saveOrderData($data);
361 if ($bookingLink = BookingPage::find_links('services')) {
362 $this->redirect($bookingLink);
363 return;
364 }
365 $this->redirectBack();
366 return;
367 }
368
369 function getRoomServiceTemplate($request) {
370 $rateID = (int)$request->requestVar('RateID');
371
372 $rate = DataObject::get_by_id('RoomRate', $rateID);
373
374
375 $html = '';
376
377 foreach($rate->Room()->getRoomServices($rate) as $service) {
378 $html .= $service->HTML($rate);
379 }
380 return $this->customise(array(
381 'Rate' => $rate,
382 'HTML' => $html,
383 ))->renderWith('RoomServiceTemplate');
384 }
385
386 function getOrderServiceTemplate($request) {
387 $personCount = (int)$request->requestVar('PersonCount');
388 if (!$personCount) {
389 return false;
390 }
391
392 $subsiteID = 0;
393 if (class_exists('Subsite')) {
394 $subsiteID = Subsite::currentSubsiteID();
395 }
396 $services = OrderService::get_active($subsiteID);
397 $html = '';
398 foreach($services as $service) {
399 $html .= $service->HTML($personCount);
400 }
401 return $this->customise(array(
402 'HTML' => $html,
403 ))->renderWith('OrderServiceTemplate');
404 }
405
406 function getOrderServiceAdditionalFieldTemplate($request) {
407 $serviceID = (int)$request->requestVar('ServiceID');
408 $service = DataObject::get_by_id('OrderService', $serviceID);
409 if ($service) {
410 return $this->customise(array(
411 'AdditionalField' => $service->OrderServiceAdditionalField(),
412 ))->renderWith('OrderServiceAdditionalFieldTemplate');
413 }
414 return false;
415 }
416
417
418 function services() {
419 $order = $this->getOrder();
420 $roomServicesHTML = '';
421 $personCount = 0;
422 if ($order && $order->Rooms()) {
423 $pos = 1;
424 foreach($order->Rooms() as $roomOrder) {
425 $html = '';
426 427 428 429 430 431 432 433 434 435
436 foreach($roomOrder->Room()->getRoomServices($roomOrder->Rate()) as $service) {
437 $roomServiceValue = 0;
438 if ($roomService = $roomOrder->RoomServices()->find('RoomServiceID', $service->ID)) {
439 $roomServiceValue = $roomService->Count;
440 }
441 $html .= $service->HTML($roomOrder, $roomServiceValue);
442 }
443 $roomServicesHTML .= $this->customise(array(
444 'Rate' => $roomOrder->Rate(),
445 'RoomOrder' => $roomOrder,
446 'HTML' => $html,
447 'Position' => $pos,
448 ))->renderWith('RoomServiceTemplate');
449 $pos++;
450 $personCount += $roomOrder->Rate()->TotalPersonCount;
451 }
452 }
453
454 $orderServicesHTML = '';
455 if ($personCount) {
456 $subsiteID = 0;
457 if (class_exists('Subsite')) {
458 $subsiteID = Subsite::currentSubsiteID();
459 }
460 $services = OrderService::get_active($subsiteID);
461 if ($services) {
462 foreach($services as $service) {
463 $orderServiceValue = 0;
464 $orderServiceAdditionalData = false;
465 if ($service->HasAdditionalField) {
466 if ($orderServices = $order->OrderServices("OrderServiceID = {$service->ID}")) {
467 $orderServiceValue = $orderServices->Count();
468 $orderServiceAdditionalData = array_values($orderServices->map('ID', 'AdditionalFieldData'));
469 }
470 } else {
471 if ($orderService = $order->OrderServices()->find("OrderServiceID", $service->ID)) {
472 $orderServiceValue = $orderService->Count;
473 }
474 }
475 $orderServicesHTML .= $service->HTML($personCount, $orderServiceValue, $orderServiceAdditionalData);
476 }
477 }
478 $orderServicesHTML = $this->customise(array(
479 'HTML' => $orderServicesHTML
480 ))->renderWith('OrderServiceTemplate');
481 }
482
483 return $this->customise(array(
484 'RoomServicesHTML' => $roomServicesHTML,
485 'OrderServicesHTML' => $orderServicesHTML,
486 'PersonCount' => $personCount,
487 ));
488 }
489
490
491 function saveOrderServices($request) {
492 $order = $this->getOrder();
493 if ($order && ($formData = $request->postVars())) {
494 if (isset($formData['RoomService'])) {
495 foreach($formData['RoomService'] as $roomOrderID=>$serviceData) {
496 $roomOrder = $order->Rooms()->find('ID', $roomOrderID);
497 if ($roomOrder) {
498 foreach($roomOrder->RoomServices() as $roomService) {
499 $roomService->delete();
500 }
501 foreach($serviceData as $serviceID=>$serviceCount) {
502 if ($serviceCount > 0) {
503 $roomServiceOrder = new RoomServiceOrder();
504 $roomServiceOrder->RoomServiceID = $serviceID;
505 $roomServiceOrder->RoomOrderID = $roomOrder->ID;
506 $roomServiceOrder->Count = $serviceCount;
507 $roomServiceOrder->write();
508 }
509 }
510 }
511 }
512 }
513 if (isset($formData['OrderService'])) {
514 foreach($order->OrderServices() as $orderService) {
515 $orderService->delete();
516 }
517 foreach($formData['OrderService'] as $orderServiceID=>$count) {
518 if ($count > 0) {
519 $service = DataObject::get_by_id('OrderService', $orderServiceID);
520 if ($service->HasAdditionalField) {
521 for($i = 0; $i < $count; $i++) {
522 $orderServiceOrder = new OrderServiceOrder();
523 $orderServiceOrder->OrderServiceID = $service->ID;
524 $orderServiceOrder->OrderID = $order->ID;
525 $orderServiceOrder->Count = 1;
526 if (isset($formData['AdditionalFieldData'][$orderServiceID][$i+1]) && trim($formData['AdditionalFieldData'][$orderServiceID][$i+1])) {
527 $orderServiceOrder->AdditionalFieldData = trim($formData['AdditionalFieldData'][$orderServiceID][$i+1]);
528 }
529 $orderServiceOrder->write();
530 $order->OrderServices()->add($orderServiceOrder);
531 }
532 } else {
533 $orderServiceOrder = new OrderServiceOrder();
534 $orderServiceOrder->OrderServiceID = $service->ID;
535 $orderServiceOrder->OrderID = $order->ID;
536 $orderServiceOrder->Count = $count;
537 $orderServiceOrder->write();
538 $order->OrderServices()->add($orderServiceOrder);
539
540 }
541 }
542 }
543 }
544 }
545 $this->redirect(BookingPage::ContactsStepLink());
546 return;
547 }
548
549 function ContactDetailForm() {
550 $order = $this->getOrder();
551 if (!$order) {
552 return false;
553 }
554 $requiredFields = array('ContactPhone', 'Email', 'InTime', 'OutTime');
555
556 $fields = new FieldSet();
557 $fields->push(new HiddenField('BookingOrderID', 'BookingOrderID', $order->ID));
558 if ($order->OrderRooms()->Count()) {
559 $j_m = 0;
560 $j_o = 0;
561 $cachedPersons = array();
562 if (Session::get('Booking.CachedPersons')) {
563 $cachedPersons = @unserialize(Session::get('Booking.CachedPersons'));
564 }
565
566 foreach($order->OrderRooms() as $roomOrder) {
567 $fields->push(new LiteralField("Person_{$roomOrder->ID}", '<div class="room">'));
568 $fields->push(new LiteralField("RoomData", '<p class="room_data">'.$roomOrder->Title.'</p>'));
569
570 $additionalPersons = $roomOrder->AdditionalPersons();
571
572
573 $personsCount = $roomOrder->Rate()->TotalPersonCount;
574 for($i=0; $i < $personsCount; $i++) {
575 $person = new RoomOrderPerson();
576
577 if ($i == 0) {
578 if (isset($cachedPersons['main'][$j_m])) {
579 $person = new RoomOrderPerson($cachedPersons['main'][$j_m]);
580 $j_m++;
581 }
582
583
584 if (!$person->ID) {
585 $person->Citizenship = $this->DefaultCitizenship;
586 }
587 $class = 'Main';
588 $fField = "MainFirstName[{$roomOrder->ID}]";
589 $lField = "MainLastName[{$roomOrder->ID}]";
590 $sField = "MainSureName[{$roomOrder->ID}]";
591 $cField = "MainCitizenship[{$roomOrder->ID}]";
592 $requiredFields[] = $fField;
593 $requiredFields[] = $lField;
594
595 } else {
596 if (isset($cachedPersons['other'][$j_o])) {
597 $person = new RoomOrderPerson($cachedPersons['other'][$j_o]);
598 $j_o++;
599 }
600
601
602 $class = '';
603 $fField = "FirstName[{$roomOrder->ID}][$i]";
604 $lField = "LastName[{$roomOrder->ID}][$i]";
605 $sField = "SureName[{$roomOrder->ID}][$i]";
606 $cField = "Citizenship[{$roomOrder->ID}][$i]";
607
608 if (SiteConfig::current_site_config()->RequireAllPersonsFIO) {
609 $requiredFields[] = $fField;
610 $requiredFields[] = $lField;
611
612 $class = 'Main';
613 }
614 }
615 $fields->push(new LiteralField("OrderPerson", '<div class="order_person_fields '.$class.'">'));
616 $fields->push(new LiteralField("OrderPersonPerson", '<div class="order_person" data-num="'.($i+1).'"></div>'));
617
618 $f = new TextField($fField, $roomOrder->MainPerson()->fieldLabel('LastName'), $person->LastName);
619 $f->addExtraClass('LastName');
620 $fields->push($f);
621 $f = new TextField($lField, $roomOrder->MainPerson()->fieldLabel('FirstName'), $person->FirstName);
622 $f->addExtraClass('FirstName');
623 $fields->push($f);
624 $f = new TextField($sField, $roomOrder->MainPerson()->fieldLabel('SureName'), $person->SureName);
625 $f->addExtraClass('SureName');
626 $fields->push($f);
627 $f = new TextField($cField, $roomOrder->MainPerson()->fieldLabel('Citizenship'), $person->Citizenship);
628 $f->addExtraClass('Citizenship');
629 $fields->push($f);
630 $fields->push(new LiteralField("OrderPerson", '</div>'));
631 }
632 $fields->push(new LiteralField("PersonEnd_{$roomOrder->ID}", '</div>'));
633 }
634 }
635
636 $fields->push(new LiteralField('ContactInfo','<p><b>' . _t('BookingOrder.ContactInfo', 'ContactInfo').'</b></p>'));
637 $fields->push(new TextField('ContactPhone', $order->fieldLabel('ContactPhone'), $order->ContactPhone));
638 $fields->push(new EmailField('Email', $order->fieldLabel('Email'), $order->Email));
639
640 $defaultInTime = "12:00:00";
641 if ($this->DefaultInTime) {
642 $defaultInTime = $this->DefaultInTime;
643 }
644 $defaultOutTime = "12:00:00";
645 if ($this->DefaultOutTime) {
646 $defaultOutTime = $this->DefaultOutTime;
647 }
648 $hours = BookingPage::getHoursList();
649 $fields->push(new DropdownField('InTime', $order->fieldLabel('InTime'), $hours, ($order->InTime ? $order->InTime : $defaultInTime)));
650 $fields->push(new DropdownField('OutTime', $order->fieldLabel('OutTime'), $hours, ($order->OutTime ? $order->OutTime : $defaultOutTime)));
651 $fields->push(new TextAreaField('Comment', $order->fieldLabel('Comment'), $order->Comment));
652
653 $actions = new FieldSet();
654 $actions->push(new LiteralField('goBookingStep', '<a class="button_back button-link" href="'. BookingPage::find_links('services') .'">' . _t('BookingOrder.Back', 'Back') . '</a>'));
655 $actions->push(new FormAction('doSaveContactDetails', _t('BookingOrder.Next', 'Next')));
656 $form = new Form(
657 $this,
658 'ContactDetailForm',
659 $fields,
660 $actions,
661 new RequiredFields($requiredFields)
662 );
663
664 return $form;
665 }
666
667 function doSaveContactDetails($data, $form) {
668 $orderID = false;
669 if (isset($data['BookingOrderID'])) {
670 $orderID = (int)$data['BookingOrderID'];
671 }
672 if ($orderID && ($order = DataObject::get_by_id('BookingOrder', $orderID))) {
673 $fioList = '';
674 $cachedPersons = array();
675 $cachedPersons['main'] = array();
676 $cachedPersons['other'] = array();
677
678 $j = 0;
679 foreach($order->OrderRooms() as $roomOrder) {
680 $mainPerson = new RoomOrderPerson();
681 if (isset($data['MainFirstName'][$roomOrder->ID])) {
682 $mainPerson->FirstName = Convert::raw2sql($data['MainFirstName'][$roomOrder->ID]);
683 }
684 if (isset($data['MainLastName'][$roomOrder->ID])) {
685 $mainPerson->LastName = Convert::raw2sql($data['MainLastName'][$roomOrder->ID]);
686 }
687 if (isset($data['MainSureName'][$roomOrder->ID])) {
688 $mainPerson->SureName = Convert::raw2sql($data['MainSureName'][$roomOrder->ID]);
689 }
690 if (isset($data['MainCitizenship'][$roomOrder->ID])) {
691 $mainPerson->Citizenship = Convert::raw2sql($data['MainCitizenship'][$roomOrder->ID]);
692 }
693 $cachedPersons['main'][] = $mainPerson->toMap();
694 $roomOrder->MainPersonID = $mainPerson->write();
695 $fioList .= $mainPerson->Title . ' | ';
696 foreach($roomOrder->AdditionalPersons() as $addPerson) {
697 $addPerson->delete();
698 }
699 $roomOrder->AdditionalPersons()->removeAll();
700
701 for($i = 0; $i < 20; $i++) {
702 $otherPerson = new RoomOrderPerson();
703 $f = false;
704 if (isset($data['FirstName'][$roomOrder->ID][$i]) && trim($data['FirstName'][$roomOrder->ID][$i])) {
705 $otherPerson->FirstName = Convert::raw2sql($data['FirstName'][$roomOrder->ID][$i]);
706 $f = true;
707 }
708 if (isset($data['LastName'][$roomOrder->ID][$i]) && trim($data['LastName'][$roomOrder->ID][$i])) {
709 $otherPerson->LastName = Convert::raw2sql($data['LastName'][$roomOrder->ID][$i]);
710 $f = true;
711 }
712 if (isset($data['SureName'][$roomOrder->ID][$i]) && trim($data['SureName'][$roomOrder->ID][$i])) {
713 $otherPerson->SureName = Convert::raw2sql($data['SureName'][$roomOrder->ID][$i]);
714 $f = true;
715 }
716 if (isset($data['Citizenship'][$roomOrder->ID][$i]) && trim($data['Citizenship'][$roomOrder->ID][$i])) {
717 $otherPerson->Citizenship = Convert::raw2sql($data['Citizenship'][$roomOrder->ID][$i]);
718 $f = true;
719 }
720 if ($f) {
721 $cachedPersons['other'][] = $otherPerson->toMap();
722 $otherPerson->write();
723 $fioList .= $otherPerson->Title . ' | ';
724 $roomOrder->AdditionalPersons()->add($otherPerson);
725 }
726 }
727 $roomOrder->write();
728 }
729
730 if (isset($data['ContactPhone'])) {
731 $order->ContactPhone = Convert::raw2sql($data['ContactPhone']);
732 }
733 if (isset($data['Email'])) {
734 $order->Email = Convert::raw2sql($data['Email']);
735 }
736 if (isset($data['InTime'])) {
737 $order->InTime = Convert::raw2sql($data['InTime']);
738 }
739 if (isset($data['OutTime'])) {
740 $order->OutTime = Convert::raw2sql($data['OutTime']);
741 }
742 if (isset($data['Comment'])) {
743 $order->Comment = Convert::raw2sql($data['Comment']);
744 }
745 $order->FIOList = $fioList;
746 $order->write();
747
748 Session::set('Booking.CachedPersons', serialize($cachedPersons));
749 $this->redirect(BookingPage::PaymentStepLink());
750 }
751 }
752
753 function PaymentForm() {
754 $order = $this->getOrder();
755 if (!$order) {
756 return false;
757 }
758 $fields = new FieldSet();
759 $fields->push(new HiddenField('BookingOrderID', 'BookingOrderID', $order->ID));
760
761 $requiredFields = array();
762 $methods = PaymentMethod::getPaymentMethods();
763 if ($methods) {
764 $fields->push(new DropdownField('PaymentMethod', $order->fieldLabel('PaymentMethod'), $methods->map(), $order->PaymentMethodID));
765 $requiredFields[] = 'PaymentMethod';
766 }
767 $actions = new FieldSet();
768 $actions->push(new LiteralField('goContactsStep', '<a class="button_back button-link" href="'. BookingPage::ContactsStepLink() .'">' . _t('BookingOrder.Back', 'Back') . '</a>'));
769 $actions->push(new FormAction('doSaveOrder', _t('BookingOrder.Save', 'Save')));
770
771 $form = new Form(
772 $this,
773 'PaymentForm',
774 $fields,
775 $actions,
776 new RequiredFields($requiredFields)
777 );
778 return $form;
779 }
780
781 function doSaveOrder($data, $form) {
782 $orderID = false;
783 if (isset($data['BookingOrderID'])) {
784 $orderID = (int)$data['BookingOrderID'];
785 }
786 if ($orderID && ($order = DataObject::get_by_id('BookingOrder', $orderID))) {
787 if (isset($data['PaymentMethod'])) {
788 $order->PaymentMethodID = (int)$data['PaymentMethod'];
789 $order->write();
790 }
791 $this->redirect(BookingPage::ConfirmationStepLink());
792 }
793 }
794
795 function ConfirmationForm() {
796 $order = $this->getOrder();
797 if (!$order) {
798 return false;
799 }
800 $fields = new FieldSet();
801 $fields->push(new HiddenField('BookingOrderID', 'BookingOrderID', $order->ID));
802 $fields->push(new CheckboxField('AgreeWithRulesAndPolicy', $this->AgreeWithRulesAndPolicyText));
803
804 $actions = new FieldSet();
805 $actions->push(new LiteralField('goBookingStep', '<a class="button_back button-link" href="'. BookingPage::PaymentStepLink() .'">' . _t('BookingOrder.Back', 'Back') . '</a>'));
806 $actions->push(new FormAction('doConfirmOrder', _t('BookingOrder.ConfirmOrder', 'Confirm Order')));
807 $form = new Form(
808 $this,
809 'ConfirmationForm',
810 $fields,
811 $actions,
812 new RequiredFields(array('AgreeWithRulesAndPolicy'))
813 );
814 return $form;
815 }
816
817 function doConfirmOrder($data, $form) {
818 $orderID = false;
819 if (isset($data['BookingOrderID'])) {
820 $orderID = (int)$data['BookingOrderID'];
821 }
822 if ($orderID && ($order = DataObject::get_by_id('BookingOrder', $orderID))) {
823 if (isset($data['AgreeWithRulesAndPolicy']) && $data['AgreeWithRulesAndPolicy']) {
824 $order->Status = 'New';
825 $order->AgreeWithRulesAndPolicy = 1;
826 $order->write();
827 $order->sendReceipt();
828 }
829 $this->redirect(BookingPage::SummaryStepLink());
830 }
831 }
832
833 function summary() {
834 $order = $this->getOrder();
835 if ($order) {
836 Session::clear('CurrentBookingOrderID');
837 Session::clear('Booking.CachedPersons');
838 Session::save();
839 if (class_exists('PaymentMethod') && BookingOrder::$use_payments) {
840 $paymentResult = false;
841 $method = DataObject::get_by_id('PaymentMethod', $order->PaymentMethodID);
842
843 if ($method) {
844 $paymentResult = $method->makePayment($order);
845 } else {
846 user_error("Ошибочный тип платежа!", E_USER_ERROR);
847 }
848 }
849 return $this->customise(array(
850 'SavedOrder' => $order
851 ));
852 } else {
853 $this->redirect('index');
854 }
855 }
856
857 function payment($req) {
858 if ($orderHash = Convert::raw2sql($req->param('ID'))) {
859 $order = DataObject::get_one('BookingOrder', "HashLink = '$orderHash'");
860 if ($order) {
861 return $this->customise(array(
862 'Title' => _t('BookingPage.PayOrder'),
863 'Order' => $order,
864 ));
865 }
866 }
867 }
868
869 function cancel_order($req) {
870 if ($orderHash = Convert::raw2sql($req->param('ID'))) {
871 $order = DataObject::get_one('BookingOrder', "HashLink = '$orderHash'");
872 if ($order && $order->canCancel()) {
873 $order->Status = 'MemberCancelled';
874 $order->write();
875 $title = _t('BookingPage.OrderCanceled');
876 } else {
877 $title = _t('BookingPage.OrderCanceledFail');
878 }
879 return $this->customise(array(
880 'Title' => $title,
881 'Order' => $order
882 ));
883 }
884 }
885
886 function print_voucher($req) {
887 if ($orderHash = Convert::raw2sql($req->param('ID'))) {
888 $order = DataObject::get_one('BookingOrder', "HashLink = '$orderHash'");
889 if ($order) {
890 return $this->customise(array(
891 'Order' => $order,
892 ))->renderWith('PrintVoucher');
893 }
894 }
895 }
896
897 function download_voucher($req) {
898 if ($orderHash = Convert::raw2sql($req->param('ID'))) {
899 $order = DataObject::get_one('BookingOrder', "HashLink = '$orderHash'");
900 if ($order) {
901 $html = $this->customise(array(
902 'Order' => $order,
903 ))->renderWith('VoucherFile');
904
905 header('Content-Description: File Transfer');
906 header("Content-Disposition: attachment; filename=voucher.html");
907 header('Content-Transfer-Encoding: binary');
908 header('Expires: 0');
909 header('Cache-Control: must-revalidate');
910 header('Pragma: public');
911 Controller::curr()->getResponse()->addHeader("Content-Type", "application/html");
912 echo $html;
913
914
915
916 }
917 }
918 }
919 }
920
921
[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.
-