<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */


 namespace Apps\Booking\Helper;

use Apps\Booking\Widget\BookingReservationForm\BookingReservationForm;

use Apps\Booking\Entity\BookingReservation;


class ReservationHelper
{

    /***
     * Save a reservation
     */
    public static function SaveReservation($core, $data){

        $reservationForm = new BookingReservationForm($core, $data);

        if($reservationForm->Validate($data) ){

            $reservation = new BookingReservation($core);
            $reservationForm->Populate($reservation);

            $reservation->DateEnd->Value = $data["DateStart"];
            $reservation->Data->Value = json_encode($data);
            $reservation->Status->Value =1;

            $reservation->Save();

            EmailHelper::SendEmail($core, 
            "Booking-addReservation", 
            $data["Email"], 
            array("{{userName}}" => $data["FirstName"] . " " . $data["Name"]
            ),
            $core->GetCode("Booking.EmailSaveRegistrationTitle"),
            $core->GetCode("Booking.EmailSaveRegistrationMessage"),
        );

            return true;
        }

        return $reservationForm->Error();
    }
}