<?php

/*
* Title                   : Pinpoint Booking System WordPress Plugin (PRO)
* Version                 : 2.2.3
* File                    : includes/reservations/class-backend-reservation-form.php
* File Version            : 1.1.0
* Created / Last Modified : 21 April 2016
* Author                  : Dot on Paper
* Copyright               : © 2012 Dot on Paper
* Website                 : http://www.dotonpaper.net
* Description             : Back end reservations PHP class.
*/

if (!class_exists('DOPBSPBackEndReservationForm')){
    class DOPBSPBackEndReservationForm{
        /*
         * Constructor
         */
        function __construct(){
        }

        /*
         * Edit reservation.
         *
         * @param reservation_id (integer): reservation ID
         * @post reservation_id (integer): reservation ID
         */
        function edit($reservation_id = 0){
            global $wpdb;
            global $DOPBSP;
            global $DOT;

            /*
             * Verify nonce.
             */
            $nonce = $DOT->post('nonce');

            if (!wp_verify_nonce($nonce,
                                 'dopbsp_user_nonce')){
                return false;
            }
            /*
             * End verify nonce.
             */

            $reservation_id = $reservation_id != 0
                    ? $reservation_id
                    : $DOT->post('id',
                                 'int');
            $form = $DOT->post('value',
                               false);
            $form = mb_convert_encoding($form,
                                        'UTF-8',
                                        'ISO-8859-1');
            $form = str_replace("'",
                                '&#39;',
                                $form);
            $edited_form = json_decode($form);
            foreach ($edited_form as $field){
                if ($field->is_email === 'true'){
                    $edited_email = $field->value;
                }
            }

            //phpcs:ignore WordPress.DB.DirectDatabaseQuery
            $wpdb->update($DOPBSP->tables->reservations,
                          array('form'  => $form,
                                'email' => $edited_email),
                          array('id' => $reservation_id));

            $DOT->echo('success');

            die();
        }
    }
}