/**
 * Main function for drawing property enquiry form.
 *
 * @param string $id
 * @return void
 */
function propertyhive_enquiry_form()
{
    $form_controls = ph_get_property_enquiry_form_fields();
    $form_controls = apply_filters('propertyhive_property_enquiry_form_fields', array('form_controls' => $form_controls));
    ph_get_template('global/make-enquiry-form.php', $form_controls);
}
 /**
  * Delete order note via ajax
  */
 public function make_property_enquiry()
 {
     global $post;
     $return = array();
     // Validate
     $errors = array();
     //if ( ! array_key_exists( 'property_id', $form_controls ) )
     //{
     //    $errors[] = __( 'Property ID is a required field and must be supplied when making an enquiry', 'propertyhive' );
     //}
     //else
     //{
     if (!isset($_POST['property_id']) || isset($_POST['property_id']) && empty($_POST['property_id'])) {
         $errors[] = __('Property ID is a required field and must be supplied when making an enquiry', 'propertyhive') . ': ' . $key;
     } else {
         $post = get_post($_POST['property_id']);
         $form_controls = ph_get_property_enquiry_form_fields();
         $form_controls = apply_filters('propertyhive_property_enquiry_form_fields', $form_controls);
     }
     //}
     foreach ($form_controls as $key => $control) {
         if (isset($control) && isset($control['required']) && $control['required'] === TRUE) {
             // This field is mandatory. Lets check we received it in the post
             if (!isset($_POST[$key]) || isset($_POST[$key]) && empty($_POST[$key])) {
                 $errors[] = __('Missing required field', 'propertyhive') . ': ' . $key;
             }
         }
         if (isset($control['type']) && $control['type'] == 'email' && isset($_POST[$key]) && !empty($_POST[$key]) && !is_email($_POST[$key])) {
             $errors[] = __('Invalid email address provided', 'propertyhive') . ': ' . $key;
         }
     }
     if (!empty($errors)) {
         // Failed validation
         $return['success'] = false;
         $return['reason'] = 'validation';
         $return['errors'] = $errors;
     } else {
         // Passed validation
         // Get recipient email address
         $to = '';
         // Try and get office's email address first, else fallback to admin email
         $office_id = get_post_meta($_POST['property_id'], '_office_id', TRUE);
         if ($office_id != '') {
             if (get_post_type($office_id) == 'office') {
                 $property_department = get_post_meta($_POST['property_id'], '_department', TRUE);
                 $fields_to_check = array();
                 switch ($property_department) {
                     case "residential-sales":
                         $fields_to_check[] = '_office_email_address_sales';
                         $fields_to_check[] = '_office_email_address_lettings';
                         $fields_to_check[] = '_office_email_address_commercial';
                         break;
                     case "residential-lettings":
                         $fields_to_check[] = '_office_email_address_lettings';
                         $fields_to_check[] = '_office_email_address_sales';
                         $fields_to_check[] = '_office_email_address_commercial';
                         break;
                     case "commercial":
                         $fields_to_check[] = '_office_email_address_commercial';
                         $fields_to_check[] = '_office_email_address_lettings';
                         $fields_to_check[] = '_office_email_address_sales';
                         break;
                 }
                 foreach ($fields_to_check as $field_to_check) {
                     $to = get_post_meta($office_id, $field_to_check, TRUE);
                     if ($to != '') {
                         break;
                     }
                 }
             }
         }
         if ($to == '') {
             $to = get_option('admin_email');
         }
         $subject = __('New Property Enquiry', 'propertyhive') . ': ' . get_the_title($_POST['property_id']);
         $message = __("You have received a property enquiry via your website. Please find details of the enquiry below", 'propertyhive') . "\n\n";
         $message .= __('Property', 'propertyhive') . ': ' . get_the_title($_POST['property_id']) . " (" . get_permalink($_POST['property_id']) . ")\n\n";
         unset($form_controls['action']);
         unset($_POST['action']);
         unset($form_controls['property_id']);
         // Unset so the fields dosn't get shown in the enquiry details
         foreach ($form_controls as $key => $control) {
             $label = isset($control['label']) ? $control['label'] : $key;
             $message .= $label . ": " . $_POST[$key] . "\n";
         }
         $headers = array();
         if (isset($_POST['name']) && isset($_POST['email_address']) && !empty($_POST['name']) && !empty($_POST['email_address'])) {
             $headers[] = 'From: ' . $_POST['name'] . ' <' . $_POST['email_address'] . '>';
         } elseif (isset($_POST['email_address']) && !empty($_POST['email_address'])) {
             $headers[] = 'From: <' . $_POST['email_address'] . '>';
         }
         $sent = wp_mail($to, $subject, $message, $headers);
         if (!$sent) {
             $return['success'] = false;
             $return['reason'] = 'nosend';
             $return['errors'] = $errors;
         } else {
             $return['success'] = true;
             // Now insert into enquiries section of WordPress
             $title = __('Property Enquiry', 'propertyhive') . ': ' . get_the_title($_POST['property_id']);
             if (isset($_POST['name']) && !empty($_POST['name'])) {
                 $title .= __(' from ', 'propertyhive') . sanitize_text_field($_POST['name']);
             }
             $enquiry_post = array('post_title' => $title, 'post_content' => '', 'post_type' => 'enquiry', 'post_status' => 'publish');
             // Insert the post into the database
             $enquiry_post_id = wp_insert_post($enquiry_post);
             add_post_meta($enquiry_post_id, '_status', 'open');
             add_post_meta($enquiry_post_id, '_source', 'website');
             add_post_meta($enquiry_post_id, '_negotiator_id', '');
             add_post_meta($enquiry_post_id, '_office_id', $office_id);
             foreach ($_POST as $key => $value) {
                 add_post_meta($enquiry_post_id, $key, $value);
             }
         }
     }
     $this->json_headers();
     echo json_encode($return);
     // Quit out
     die;
 }