Beispiel #1
0
function mf_display_form($dbh, $form_id, $form_params = array())
{
    global $mf_lang;
    //parameters mapping
    if (isset($form_params['page_number'])) {
        $page_number = $form_params['page_number'];
    } else {
        $page_number = 1;
    }
    if (isset($form_params['populated_values'])) {
        $populated_values = $form_params['populated_values'];
    } else {
        $populated_values = array();
    }
    if (isset($form_params['error_elements'])) {
        $error_elements = $form_params['error_elements'];
    } else {
        $error_elements = array();
    }
    if (isset($form_params['custom_error'])) {
        $custom_error = $form_params['custom_error'];
    } else {
        $custom_error = '';
    }
    if (isset($form_params['edit_id'])) {
        $edit_id = (int) $form_params['edit_id'];
    } else {
        $edit_id = 0;
    }
    if (isset($form_params['integration_method'])) {
        //valid values are empty string, 'iframe' or 'php'
        $integration_method = $form_params['integration_method'];
    } else {
        $integration_method = '';
    }
    if (!empty($form_params['machform_path'])) {
        $machform_path = $form_params['machform_path'];
    } else {
        $machform_path = '';
    }
    if (!empty($form_params['machform_data_path'])) {
        $machform_data_path = $form_params['machform_data_path'];
    } else {
        $machform_data_path = '';
    }
    $mf_settings = mf_get_settings($dbh);
    //if there is custom error, don't show other errors
    if (!empty($custom_error)) {
        $error_elements = array();
    }
    //get form properties data
    $query = "SELECT \r\n\t\t\t\t\t\t form_name,\r\n\t\t\t\t\t\t form_description,\r\n\t\t\t\t\t\t form_redirect,\r\n\t\t\t\t\t\t form_success_message,\r\n\t\t\t\t\t\t form_password,\r\n\t\t\t\t\t\t form_unique_ip,\r\n\t\t\t\t\t\t form_frame_height,\r\n\t\t\t\t\t\t form_has_css,\r\n\t\t\t\t\t\t form_active,\r\n\t\t\t\t\t\t form_captcha,\r\n\t\t\t\t\t\t form_captcha_type,\r\n\t\t\t\t\t\t form_review,\r\n\t\t\t\t\t\t form_label_alignment,\r\n\t\t\t\t\t\t form_language,\r\n\t\t\t\t\t\t form_page_total,\r\n\t\t\t\t\t\t form_lastpage_title,\r\n\t\t\t\t\t\t form_submit_primary_text,\r\n\t\t\t\t\t\t form_submit_secondary_text,\r\n\t\t\t\t\t\t form_submit_primary_img,\r\n\t\t\t\t\t\t form_submit_secondary_img,\r\n\t\t\t\t\t\t form_submit_use_image,\r\n\t\t\t\t\t\t form_pagination_type,\r\n\t\t\t\t\t\t form_review_primary_text,\r\n\t\t\t\t\t\t form_review_secondary_text,\r\n\t\t\t\t\t\t form_review_primary_img,\r\n\t\t\t\t\t\t form_review_secondary_img,\r\n\t\t\t\t\t\t form_review_use_image,\r\n\t\t\t\t\t\t form_review_title,\r\n\t\t\t\t\t\t form_review_description,\r\n\t\t\t\t\t\t form_resume_enable,\r\n\t\t\t\t\t\t form_theme_id,\r\n\t\t\t\t\t\t payment_show_total,\r\n\t\t\t\t\t\t payment_total_location,\r\n\t\t\t\t\t\t payment_enable_merchant,\r\n\t\t\t\t\t\t payment_currency,\r\n\t\t\t\t\t\t payment_price_type,\r\n\t\t\t\t\t\t payment_price_amount,\r\n\t\t\t\t\t\t form_limit_enable,\r\n\t\t\t\t\t\t form_limit,\r\n\t\t\t\t\t\t form_schedule_enable,\r\n\t\t\t\t\t\t form_schedule_start_date,\r\n\t\t\t\t\t\t form_schedule_end_date,\r\n\t\t\t\t\t\t form_schedule_start_hour,\r\n\t\t\t\t\t\t form_schedule_end_hour\r\n\t\t\t\t     FROM \r\n\t\t\t\t     \t " . MF_TABLE_PREFIX . "forms \r\n\t\t\t\t    WHERE \r\n\t\t\t\t    \t form_id = ?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    //check for non-existent or currently drafted forms
    if (empty($row) || $row['form_active'] != 1) {
        die('This is not valid form URL.');
    }
    $form = new stdClass();
    $form->id = $form_id;
    $form->name = $row['form_name'];
    $form->description = $row['form_description'];
    $form->redirect = $row['form_redirect'];
    $form->success_message = $row['form_success_message'];
    $form->password = $row['form_password'];
    $form->frame_height = $row['form_frame_height'];
    $form->unique_ip = $row['form_unique_ip'];
    $form->has_css = $row['form_has_css'];
    $form->active = $row['form_active'];
    $form->captcha = $row['form_captcha'];
    $form->captcha_type = $row['form_captcha_type'];
    $form->review = $row['form_review'];
    $form->label_alignment = $row['form_label_alignment'];
    $form->page_total = $row['form_page_total'];
    $form->lastpage_title = $row['form_lastpage_title'];
    $form->submit_primary_text = $row['form_submit_primary_text'];
    $form->submit_secondary_text = $row['form_submit_secondary_text'];
    $form->submit_primary_img = $row['form_submit_primary_img'];
    $form->submit_secondary_img = $row['form_submit_secondary_img'];
    $form->submit_use_image = (int) $row['form_submit_use_image'];
    $form->pagination_type = $row['form_pagination_type'];
    $form->review_primary_text = $row['form_review_primary_text'];
    $form->review_secondary_text = $row['form_review_secondary_text'];
    $form->review_primary_img = $row['form_review_primary_img'];
    $form->review_secondary_img = $row['form_review_secondary_img'];
    $form->review_use_image = (int) $row['form_review_use_image'];
    $form->review_title = $row['form_review_title'];
    $form->review_description = $row['form_review_description'];
    $form->resume_enable = $row['form_resume_enable'];
    $form->theme_id = (int) $row['form_theme_id'];
    $form->payment_show_total = (int) $row['payment_show_total'];
    $form->payment_total_location = $row['payment_total_location'];
    $form->payment_enable_merchant = (int) $row['payment_enable_merchant'];
    if ($form->payment_enable_merchant < 1) {
        $form->payment_enable_merchant = 0;
    }
    $form->payment_currency = $row['payment_currency'];
    $form->payment_price_type = $row['payment_price_type'];
    $form->payment_price_amount = $row['payment_price_amount'];
    $form->limit_enable = (int) $row['form_limit_enable'];
    $form->limit = (int) $row['form_limit'];
    $form->schedule_enable = (int) $row['form_schedule_enable'];
    $form->schedule_start_date = $row['form_schedule_start_date'];
    $form->schedule_end_date = $row['form_schedule_end_date'];
    $form->schedule_start_hour = $row['form_schedule_start_hour'];
    $form->schedule_end_hour = $row['form_schedule_end_hour'];
    $form->language = trim($row['form_language']);
    if (!empty($form->language)) {
        mf_set_language($form->language);
    }
    if (empty($error_elements)) {
        $form->is_error = 0;
    } else {
        $form->is_error = 1;
    }
    if (!empty($edit_id)) {
        $form->active = 1;
    }
    if ($form->page_total == 1) {
        //if this form has review enabled and user are having $_SESSION['review_id'], then populate the form with that values
        if (!empty($form->review) && !empty($_SESSION['review_id']) && empty($populated_values)) {
            $entry_params = array();
            $entry_params['machform_data_path'] = $machform_data_path;
            $populated_values = mf_get_entry_values($dbh, $form_id, $_SESSION['review_id'], true, $entry_params);
        } elseif (!empty($form->review) && !empty($_SESSION['review_id']) && !empty($populated_values)) {
            //if form review enabled and there is some validation error, the uploaded files needs to be displayed
            $entry_params = array();
            $entry_params['machform_data_path'] = $machform_data_path;
            $populated_file_values = mf_get_entry_values($dbh, $form_id, $_SESSION['review_id'], true, $entry_params);
        }
    } else {
        //if this is multipage form, always populate the fields
        $session_id = session_id();
        //if there is form resume key, load the record from ap_form_x table to ap_form_x_review table
        if (!empty($_SESSION['mf_form_resume_key'][$form_id])) {
            $resume_key = $_SESSION['mf_form_resume_key'][$form_id];
            //first delete existing record within review table
            $query = "DELETE from `" . MF_TABLE_PREFIX . "form_{$form_id}_review` where session_id=? or resume_key=?";
            $params = array($session_id, $resume_key);
            mf_do_query($query, $params, $dbh);
            //copy data from ap_form_x table to ap_form_x_review table
            $query = "SELECT * FROM `" . MF_TABLE_PREFIX . "form_{$form_id}` WHERE resume_key=?";
            $params = array($resume_key);
            $sth = mf_do_query($query, $params, $dbh);
            $row = mf_do_fetch_result($sth);
            $columns = array();
            foreach ($row as $column_name => $column_data) {
                if ($column_name != 'id') {
                    $columns[] = $column_name;
                }
            }
            if (empty($columns)) {
                //invalid resume_key given, display error message
                $custom_error = 'Invalid Link! <br/>Please open the complete URL to resume your saved progress.';
            } else {
                $columns_joined = implode("`,`", $columns);
                $columns_joined = '`' . $columns_joined . '`';
                //copy data from main table
                $query = "INSERT INTO `" . MF_TABLE_PREFIX . "form_{$form_id}_review`({$columns_joined}) SELECT {$columns_joined} from `" . MF_TABLE_PREFIX . "form_{$form_id}` WHERE resume_key=?";
                $params = array($resume_key);
                mf_do_query($query, $params, $dbh);
                $query = "UPDATE `" . MF_TABLE_PREFIX . "form_{$form_id}_review` set session_id=? WHERE resume_key=?";
                $params = array($session_id, $resume_key);
                mf_do_query($query, $params, $dbh);
                for ($i = 1; $i <= $form->page_total; $i++) {
                    $_SESSION['mf_form_loaded'][$form_id][$i] = true;
                }
                unset($_SESSION['mf_form_resume_key'][$form_id]);
            }
        }
        $query = "SELECT `id` from `" . MF_TABLE_PREFIX . "form_{$form_id}_review` where session_id=?";
        $params = array($session_id);
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        //we need to check mf_form_loaded to make sure default values of fields are being loaded on the first view of the form
        if (empty($populated_values) && !empty($_SESSION['mf_form_loaded'][$form_id][$page_number])) {
            $entry_params = array();
            $entry_params['machform_data_path'] = $machform_data_path;
            $populated_values = mf_get_entry_values($dbh, $form_id, $row['id'], true, $entry_params);
        } else {
            //if there is some validation error, the uploaded files needs to be displayed
            $entry_params = array();
            $entry_params['machform_data_path'] = $machform_data_path;
            $populated_file_values = mf_get_entry_values($dbh, $form_id, $row['id'], true, $entry_params);
        }
    }
    //get price definitions for fields, if the merchant feature is enabled
    if (!empty($form->payment_enable_merchant)) {
        $query = "select \r\n\t\t\t\t\t\t\telement_id,\r\n\t\t\t\t\t\t\toption_id,\r\n\t\t\t\t\t\t\t`price` \r\n\t\t\t\t\t   from \r\n\t\t\t\t\t   \t\t`" . MF_TABLE_PREFIX . "element_prices` \r\n\t\t\t\t\t   where \r\n\t\t\t\t\t   \t\tform_id=? \r\n\t\t\t\t   order by \r\n\t\t\t\t   \t\t\telement_id,option_id asc";
        $params = array($form_id);
        $sth = mf_do_query($query, $params, $dbh);
        while ($row = mf_do_fetch_result($sth)) {
            $element_prices_array[$row['element_id']][$row['option_id']] = $row['price'];
        }
    }
    //get elements data
    //get element options first and store it into array
    $query = "SELECT \r\n\t\t\t\t\t\telement_id,\r\n\t\t\t\t\t\toption_id,\r\n\t\t\t\t\t\t`position`,\r\n\t\t\t\t\t\t`option`,\r\n\t\t\t\t\t\toption_is_default \r\n\t\t\t\t    FROM \r\n\t\t\t\t    \t" . MF_TABLE_PREFIX . "element_options \r\n\t\t\t\t   where \r\n\t\t\t\t   \t\tform_id = ? and live=1 \r\n\t\t\t\torder by \r\n\t\t\t\t\t\telement_id asc,`position` asc";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    while ($row = mf_do_fetch_result($sth)) {
        $element_id = $row['element_id'];
        $option_id = $row['option_id'];
        $options_lookup[$element_id][$option_id]['position'] = $row['position'];
        $options_lookup[$element_id][$option_id]['option'] = $row['option'];
        $options_lookup[$element_id][$option_id]['option_is_default'] = $row['option_is_default'];
        if (isset($element_prices_array[$element_id][$option_id])) {
            $options_lookup[$element_id][$option_id]['price_definition'] = $element_prices_array[$element_id][$option_id];
        }
    }
    $matrix_elements = array();
    //get elements data
    $element = array();
    if ($page_number === 0) {
        //if page_number is 0, display all pages (this is being used on edit_entry page)
        $page_number_clause = '';
        $params = array($form_id);
    } else {
        $page_number_clause = 'and element_page_number = ?';
        $params = array($form_id, $page_number);
    }
    $query = "SELECT \r\n\t\t\t\t\t\telement_id,\r\n\t\t\t\t\t\telement_title,\r\n\t\t\t\t\t\telement_guidelines,\r\n\t\t\t\t\t\telement_size,\r\n\t\t\t\t\t\telement_is_required,\r\n\t\t\t\t\t\telement_is_unique,\r\n\t\t\t\t\t\telement_is_private,\r\n\t\t\t\t\t\telement_type,\r\n\t\t\t\t\t\telement_position,\r\n\t\t\t\t\t\telement_default_value,\r\n\t\t\t\t\t\telement_constraint,\r\n\t\t\t\t\t\telement_choice_has_other,\r\n\t\t\t\t\t\telement_choice_other_label,\r\n\t\t\t\t\t\telement_choice_columns,\r\n\t\t\t\t\t\telement_time_showsecond, \r\n\t\t\t\t\t\telement_time_24hour,\r\n\t\t\t\t\t\telement_address_hideline2,\r\n\t\t\t\t\t\telement_address_us_only,\r\n\t\t\t\t\t\telement_date_enable_range,\r\n\t\t\t\t\t\telement_date_range_min,\r\n\t\t\t\t\t\telement_date_range_max,\r\n\t\t\t\t\t\telement_date_enable_selection_limit,\r\n\t\t\t\t\t\telement_date_selection_max,\r\n\t\t\t\t\t\telement_date_disable_past_future,\r\n\t\t\t\t\t\telement_date_past_future,\r\n\t\t\t\t\t\telement_date_disable_weekend,\r\n\t\t\t\t\t\telement_date_disable_specific,\r\n\t\t\t\t\t\telement_date_disabled_list,\r\n\t\t\t\t\t\telement_file_enable_type_limit,\r\n\t\t\t\t\t\telement_file_block_or_allow,\r\n\t\t\t\t\t\telement_file_type_list,\r\n\t\t\t\t\t\telement_file_as_attachment,\r\n\t\t\t\t\t\telement_file_enable_advance,\r\n\t\t\t\t\t\telement_file_auto_upload,\r\n\t\t\t\t\t\telement_file_enable_multi_upload,\r\n\t\t\t\t\t\telement_file_max_selection,\r\n\t\t\t\t\t\telement_file_enable_size_limit,\r\n\t\t\t\t\t\telement_file_size_max,\r\n\t\t\t\t\t\telement_matrix_allow_multiselect,\r\n\t\t\t\t\t\telement_matrix_parent_id,\r\n\t\t\t\t\t\telement_range_min,\r\n\t\t\t\t\t\telement_range_max,\r\n\t\t\t\t\t\telement_range_limit_by,\r\n\t\t\t\t\t\telement_css_class\r\n\t\t\t\t\tFROM \r\n\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_elements \r\n\t\t\t\t   WHERE \r\n\t\t\t\t   \t\tform_id = ? and element_status='1' {$page_number_clause} and element_type <> 'page_break'\r\n\t\t\t\tORDER BY \r\n\t\t\t\t\t\telement_position asc";
    $sth = mf_do_query($query, $params, $dbh);
    $j = 0;
    $has_calendar = false;
    //assume the form doesn't have calendar, so it won't load calendar.js
    $has_advance_uploader = false;
    $has_guidelines = false;
    while ($row = mf_do_fetch_result($sth)) {
        $element[$j] = new stdClass();
        $element_id = $row['element_id'];
        //lookup element options first
        if (!empty($options_lookup[$element_id])) {
            $element_options = array();
            $i = 0;
            foreach ($options_lookup[$element_id] as $option_id => $data) {
                $element_options[$i] = new stdClass();
                $element_options[$i]->id = $option_id;
                $element_options[$i]->option = $data['option'];
                $element_options[$i]->is_default = $data['option_is_default'];
                $element_options[$i]->is_db_live = 1;
                if (isset($data['price_definition'])) {
                    $element_options[$i]->price_definition = $data['price_definition'];
                }
                $i++;
            }
        }
        //populate elements
        $element[$j]->title = nl2br($row['element_title']);
        $element[$j]->guidelines = $row['element_guidelines'];
        if (!empty($row['element_guidelines']) && $row['element_type'] != 'section' && $row['element_type'] != 'matrix') {
            $has_guidelines = true;
        }
        $element[$j]->size = $row['element_size'];
        $element[$j]->is_required = $row['element_is_required'];
        $element[$j]->is_unique = $row['element_is_unique'];
        $element[$j]->is_private = $row['element_is_private'];
        $element[$j]->type = $row['element_type'];
        $element[$j]->position = $row['element_position'];
        $element[$j]->id = $row['element_id'];
        $element[$j]->is_db_live = 1;
        $element[$j]->form_id = $form_id;
        $element[$j]->choice_has_other = (int) $row['element_choice_has_other'];
        $element[$j]->choice_other_label = $row['element_choice_other_label'];
        $element[$j]->choice_columns = (int) $row['element_choice_columns'];
        $element[$j]->time_showsecond = (int) $row['element_time_showsecond'];
        $element[$j]->time_24hour = (int) $row['element_time_24hour'];
        $element[$j]->address_hideline2 = (int) $row['element_address_hideline2'];
        $element[$j]->address_us_only = (int) $row['element_address_us_only'];
        $element[$j]->date_enable_range = (int) $row['element_date_enable_range'];
        $element[$j]->date_range_min = $row['element_date_range_min'];
        $element[$j]->date_range_max = $row['element_date_range_max'];
        $element[$j]->date_enable_selection_limit = (int) $row['element_date_enable_selection_limit'];
        $element[$j]->date_selection_max = (int) $row['element_date_selection_max'];
        $element[$j]->date_disable_past_future = (int) $row['element_date_disable_past_future'];
        $element[$j]->date_past_future = $row['element_date_past_future'];
        $element[$j]->date_disable_weekend = (int) $row['element_date_disable_weekend'];
        $element[$j]->date_disable_specific = (int) $row['element_date_disable_specific'];
        $element[$j]->date_disabled_list = $row['element_date_disabled_list'];
        $element[$j]->file_enable_type_limit = (int) $row['element_file_enable_type_limit'];
        $element[$j]->file_block_or_allow = $row['element_file_block_or_allow'];
        $element[$j]->file_type_list = $row['element_file_type_list'];
        $element[$j]->file_as_attachment = (int) $row['element_file_as_attachment'];
        $element[$j]->file_enable_advance = (int) $row['element_file_enable_advance'];
        if (!empty($element[$j]->file_enable_advance)) {
            $has_advance_uploader = true;
        }
        $element[$j]->file_auto_upload = (int) $row['element_file_auto_upload'];
        $element[$j]->file_enable_multi_upload = (int) $row['element_file_enable_multi_upload'];
        $element[$j]->file_max_selection = (int) $row['element_file_max_selection'];
        $element[$j]->file_enable_size_limit = (int) $row['element_file_enable_size_limit'];
        $element[$j]->file_size_max = (int) $row['element_file_size_max'];
        $element[$j]->matrix_allow_multiselect = (int) $row['element_matrix_allow_multiselect'];
        $element[$j]->matrix_parent_id = (int) $row['element_matrix_parent_id'];
        $element[$j]->upload_dir = $mf_settings['upload_dir'];
        $element[$j]->range_min = $row['element_range_min'];
        $element[$j]->range_max = $row['element_range_max'];
        $element[$j]->range_limit_by = $row['element_range_limit_by'];
        $element[$j]->css_class = $row['element_css_class'];
        $element[$j]->machform_path = $machform_path;
        $element[$j]->machform_data_path = $machform_data_path;
        //this data came from db or form submit
        //being used to display edit form or redisplay form with errors and previous inputs
        //this should be optimized in the future, only pass necessary data, not the whole array
        $element[$j]->populated_value = $populated_values;
        //set prices for price-enabled field
        if ($row['element_type'] == 'money' && isset($element_prices_array[$row['element_id']][0])) {
            $element[$j]->price_definition = 0;
        }
        //if there is file upload type, set form enctype to multipart
        if ($row['element_type'] == 'file') {
            $form_enc_type = 'enctype="multipart/form-data"';
            //if this is single page form with review enabled or multipage form
            if (!empty($form->review) && !empty($_SESSION['review_id']) && !empty($populated_file_values) || $form->page_total > 1 && !empty($populated_file_values)) {
                //populate the default value for uploaded files, when validation error occured
                //make sure to keep the file token if exist
                if (!empty($populated_values['element_' . $row['element_id']]['file_token'])) {
                    $populated_file_values['element_' . $row['element_id']]['file_token'] = $populated_values['element_' . $row['element_id']]['file_token'];
                }
                $element[$j]->populated_value = $populated_file_values;
            }
            if (!empty($edit_id) && $_SESSION['mf_logged_in'] === true) {
                //if this is edit_entry page
                $element[$j]->is_edit_entry = true;
            }
        }
        if (!empty($error_elements[$element[$j]->id])) {
            $element[$j]->is_error = 1;
            $element[$j]->error_message = $error_elements[$element[$j]->id];
        }
        $element[$j]->default_value = htmlspecialchars($row['element_default_value']);
        $element[$j]->constraint = $row['element_constraint'];
        if (!empty($element_options)) {
            $element[$j]->options = $element_options;
        } else {
            $element[$j]->options = '';
        }
        //check for calendar type
        if ($row['element_type'] == 'date' || $row['element_type'] == 'europe_date') {
            $has_calendar = true;
            //if the field has date selection limit, we need to do query to existing entries and disable all date which reached the limit
            if (!empty($row['element_date_enable_selection_limit']) && !empty($row['element_date_selection_max'])) {
                $sub_query = "select \r\n\t\t\t\t\t\t\t\t\t\tselected_date \r\n\t\t\t\t\t\t\t\t\tfrom (\r\n\t\t\t\t\t\t\t\t\t\t\tselect \r\n\t\t\t\t\t\t\t\t\t\t\t\t  date_format(element_{$row['element_id']},'%m/%d/%Y') as selected_date,\r\n\t\t\t\t\t\t\t\t\t\t\t\t  count(element_{$row['element_id']}) as total_selection \r\n\t\t\t\t\t\t\t\t\t\t      from \r\n\t\t\t\t\t\t\t\t\t\t      \t  " . MF_TABLE_PREFIX . "form_{$form_id} \r\n\t\t\t\t\t\t\t\t\t\t     where \r\n\t\t\t\t\t\t\t\t\t\t     \t  status=1 and element_{$row['element_id']} is not null \r\n\t\t\t\t\t\t\t\t\t\t  group by \r\n\t\t\t\t\t\t\t\t\t\t  \t\t  element_{$row['element_id']}\r\n\t\t\t\t\t\t\t\t\t\t ) as A\r\n\t\t\t\t\t\t\t\t   where \r\n\t\t\t\t\t\t\t\t\t\t A.total_selection >= ?";
                $params = array($row['element_date_selection_max']);
                $sub_sth = mf_do_query($sub_query, $params, $dbh);
                $current_date_disabled_list = array();
                $current_date_disabled_list_joined = '';
                while ($sub_row = mf_do_fetch_result($sub_sth)) {
                    $current_date_disabled_list[] = $sub_row['selected_date'];
                }
                $current_date_disabled_list_joined = implode(',', $current_date_disabled_list);
                if (!empty($element[$j]->date_disable_specific)) {
                    //add to existing disable date list
                    if (empty($element[$j]->date_disabled_list)) {
                        $element[$j]->date_disabled_list = $current_date_disabled_list_joined;
                    } else {
                        $element[$j]->date_disabled_list .= ',' . $current_date_disabled_list_joined;
                    }
                } else {
                    //'disable specific date' is not enabled, we need to override and enable it from here
                    $element[$j]->date_disable_specific = 1;
                    $element[$j]->date_disabled_list = $current_date_disabled_list_joined;
                }
            }
        }
        //if the element is a matrix field and not the parent, store the data into a lookup array for later use when rendering the markup
        if ($row['element_type'] == 'matrix' && !empty($row['element_matrix_parent_id'])) {
            $parent_id = $row['element_matrix_parent_id'];
            $el_position = $row['element_position'];
            $matrix_elements[$parent_id][$el_position]['title'] = $element[$j]->title;
            $matrix_elements[$parent_id][$el_position]['id'] = $element[$j]->id;
            $matrix_child_option_id = '';
            foreach ($element_options as $value) {
                $matrix_child_option_id .= $value->id . ',';
            }
            $matrix_child_option_id = rtrim($matrix_child_option_id, ',');
            $matrix_elements[$parent_id][$el_position]['children_option_id'] = $matrix_child_option_id;
            //remove it from the main element array
            $element[$j] = array();
            unset($element[$j]);
            $j--;
        }
        $j++;
    }
    //add captcha if enabled
    //on multipage form, captcha should be displayed on the last page only
    if (!empty($form->captcha) && empty($edit_id)) {
        if ($form->page_total == 1 || $form->page_total == $page_number) {
            $element[$j] = new stdClass();
            $element[$j]->type = 'captcha';
            $element[$j]->captcha_type = $form->captcha_type;
            $element[$j]->form_id = $form_id;
            $element[$j]->is_private = 0;
            if (!empty($error_elements['element_captcha'])) {
                $element[$j]->is_error = 1;
                $element[$j]->error_message = $error_elements['element_captcha'];
            }
        }
    }
    //generate html markup for each element
    $container_class = '';
    $all_element_markup = '';
    foreach ($element as $element_data) {
        if ($element_data->is_private && empty($edit_id)) {
            //don't show private element on live forms
            continue;
        }
        //if this is matrix field, build the children data from $matrix_elements array
        if ($element_data->type == 'matrix') {
            $element_data->matrix_children = $matrix_elements[$element_data->id];
        }
        $all_element_markup .= call_user_func('mf_display_' . $element_data->type, $element_data);
    }
    if (!empty($custom_error)) {
        $form->error_message = <<<EOT
\t\t\t<li id="error_message">
\t\t\t\t\t<h3 id="error_message_title">{$custom_error}</h3>
\t\t\t</li>\t
EOT;
    } elseif (!empty($error_elements)) {
        $form->error_message = <<<EOT
\t\t\t<li id="error_message">
\t\t\t\t\t<h3 id="error_message_title">{$mf_lang['error_title']}</h3>
\t\t\t\t\t<p id="error_message_desc">{$mf_lang['error_desc']}</p>
\t\t\t</li>\t
EOT;
    }
    //if this form is using custom theme and not on edit entry page
    if (!empty($form->theme_id) && empty($edit_id)) {
        //get the field highlight color for the particular theme
        $query = "SELECT \r\n\t\t\t\t\t\t\thighlight_bg_type,\r\n\t\t\t\t\t\t\thighlight_bg_color,\r\n\t\t\t\t\t\t\tform_shadow_style,\r\n\t\t\t\t\t\t\tform_shadow_size,\r\n\t\t\t\t\t\t\tform_shadow_brightness,\r\n\t\t\t\t\t\t\tform_button_type,\r\n\t\t\t\t\t\t\tform_button_text,\r\n\t\t\t\t\t\t\tform_button_image,\r\n\t\t\t\t\t\t\ttheme_has_css  \r\n\t\t\t\t\t\tFROM \r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_themes \r\n\t\t\t\t\t   WHERE \r\n\t\t\t\t\t   \t\ttheme_id = ?";
        $params = array($form->theme_id);
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        $form_shadow_style = $row['form_shadow_style'];
        $form_shadow_size = $row['form_shadow_size'];
        $form_shadow_brightness = $row['form_shadow_brightness'];
        $theme_has_css = (int) $row['theme_has_css'];
        //if the theme has css file, make sure to refer to that file
        //otherwise, generate the css dynamically
        if (!empty($theme_has_css)) {
            $theme_css_link = '<link rel="stylesheet" type="text/css" href="' . $machform_path . $mf_settings['data_dir'] . '/themes/theme_' . $form->theme_id . '.css" media="all" />';
        } else {
            $theme_css_link = '<link rel="stylesheet" type="text/css" href="' . $machform_path . 'css_theme.php?theme_id=' . $form->theme_id . '" media="all" />';
        }
        if ($row['highlight_bg_type'] == 'color') {
            $field_highlight_color = $row['highlight_bg_color'];
        } else {
            //if the field highlight is using pattern instead of color, set the color to empty string
            $field_highlight_color = '';
        }
        //get the css link for the fonts
        $font_css_markup = mf_theme_get_fonts_link($dbh, $form->theme_id);
        //get the form shadow classes
        if (!empty($form_shadow_style) && $form_shadow_style != 'disabled') {
            preg_match_all("/[A-Z]/", $form_shadow_style, $prefix_matches);
            //this regex simply get the capital characters of the shadow style name
            //example: RightPerspectiveShadow result to RPS and then being sliced to RP
            $form_shadow_prefix_code = substr(implode("", $prefix_matches[0]), 0, -1);
            $form_shadow_size_class = $form_shadow_prefix_code . ucfirst($form_shadow_size);
            $form_shadow_brightness_class = $form_shadow_prefix_code . ucfirst($form_shadow_brightness);
            if (empty($integration_method)) {
                //only display shadow if the form is not being embedded using any method
                $form_container_class = $form_shadow_style . ' ' . $form_shadow_size_class . ' ' . $form_shadow_brightness_class;
            }
        }
        //get the button text/image setting
        if (empty($form->review)) {
            if ($row['form_button_type'] == 'text') {
                $submit_button_markup = '<input id="submit_form" class="button_text" type="submit" name="submit_form" value="' . $row['form_button_text'] . '" />';
            } else {
                $submit_button_markup = '<input class="submit_img_primary" type="image" alt="Submit" id="submit_form" name="submit_form" src="' . $row['form_button_image'] . '" />';
            }
        } else {
            $submit_button_markup = '<input id="submit_form" class="button_text" type="submit" name="submit_form" value="' . $mf_lang['continue_button'] . '" />';
        }
    } else {
        //if the form doesn't have any theme being applied
        $field_highlight_color = '#FFF7C0';
        if (empty($integration_method)) {
            $form_container_class = 'WarpShadow WLarge WNormal';
            //default shadow
        } else {
            $form_container_class = '';
            //dont show any shadow when the form being embedded
        }
        if (empty($form->review)) {
            $submit_button_markup = '<input id="submit_form" class="button_text" type="submit" name="submit_form" value="' . $mf_lang['submit_button'] . '" />';
        } else {
            $submit_button_markup = '<input id="submit_form" class="button_text" type="submit" name="submit_form" value="' . $mf_lang['continue_button'] . '" />';
        }
    }
    //display edit_id if there is any, this is being called on edit_entry.php page
    if (!empty($edit_id)) {
        $edit_markup = "<input type=\"hidden\" name=\"edit_id\" value=\"{$edit_id}\" />\n";
        $submit_button_markup = '<input id="submit_form" class="button_text" type="submit" name="submit_form" value="Save Changes" />';
    } else {
        $edit_markup = '';
    }
    //check for specific form css, if any, use it instead
    if ($form->has_css) {
        $css_dir = $mf_settings['data_dir'] . "/form_{$form_id}/css/";
    }
    if (!empty($form->password) && empty($_SESSION['user_authenticated'])) {
        //if form require password and password hasn't set yet
        $show_password_form = true;
    } elseif (!empty($form->password) && !empty($_SESSION['user_authenticated']) && $_SESSION['user_authenticated'] != $form_id) {
        //if user authenticated but not for this form
        $show_password_form = true;
    } else {
        //user authenticated for this form, or no password required
        $show_password_form = false;
    }
    if ($show_password_form) {
        $submit_button_markup = '<input id="submit_form" class="button_text" type="submit" name="submit_form" value="' . $mf_lang['submit_button'] . '" />';
    }
    //default markup for single page form submit button
    $button_markup = <<<EOT
\t\t<li id="li_buttons" class="buttons">
\t\t\t    <input type="hidden" name="form_id" value="{$form->id}" />
\t\t\t    {$edit_markup}
\t\t\t    <input type="hidden" name="submit_form" value="1" />
\t\t\t    <input type="hidden" name="page_number" value="{$page_number}" />
\t\t\t\t{$submit_button_markup}
\t\t</li>
EOT;
    //check for form limit rule
    $form_has_maximum_entries = false;
    if (!empty($form->limit_enable)) {
        $query = "select count(*) total_row from " . MF_TABLE_PREFIX . "form_{$form_id} where `status`=1";
        $params = array();
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        $total_entries = $row['total_row'];
        if ($total_entries >= $form->limit) {
            $form_has_maximum_entries = true;
        }
    }
    //check for automatic scheduling limit, if enabled
    if (!empty($form->schedule_enable)) {
        $schedule_start_time = strtotime($form->schedule_start_date . ' ' . $form->schedule_start_hour);
        $schedule_end_time = strtotime($form->schedule_end_date . ' ' . $form->schedule_end_hour);
        $current_time = strtotime(date("Y-m-d H:i:s"));
        if (!empty($schedule_start_time)) {
            if ($current_time < $schedule_start_time) {
                $form->active = 0;
            }
        }
        if (!empty($schedule_end_time)) {
            if ($current_time > $schedule_end_time) {
                $form->active = 0;
            }
        }
    }
    if (empty($form->active) || $form_has_maximum_entries) {
        //if form is not active, don't show the fields
        $form_desc_div = '';
        $all_element_markup = '';
        $button_markup = '';
        $ul_class = 'class="password"';
        if ($form_has_maximum_entries) {
            $inactive_message = $mf_lang['form_limited'];
        } else {
            $inactive_message = $mf_lang['form_inactive'];
        }
        $custom_element = <<<EOT
\t\t\t<li>
\t\t\t\t<h2>{$inactive_message}</h2>
\t\t\t</li>
EOT;
    } elseif ($show_password_form) {
        //don't show form description if this page is password protected and user not authenticated
        $form_desc_div = '';
        $all_element_markup = '';
        $custom_element = <<<EOT
\t\t\t<li>
\t\t\t\t<h2>{$mf_lang['form_pass_title']}</h2>
\t\t\t\t<div>
\t\t\t\t<input type="password" value="" class="text" name="password" id="password" />
\t\t\t\t<label for="password" class="desc">{$mf_lang['form_pass_desc']}</label>
\t\t\t\t</div>
\t\t\t</li>
EOT;
        $ul_class = 'class="password"';
    } else {
        if (!empty($form->name) || !empty($form->description)) {
            $form->description = nl2br($form->description);
            $form_desc_div = <<<EOT
\t\t<div class="form_description">
\t\t\t<h2>{$form->name}</h2>
\t\t\t<p>{$form->description}</p>
\t\t</div>
EOT;
        }
    }
    if (!$has_guidelines) {
        $container_class .= " no_guidelines";
    }
    if ($integration_method == 'iframe') {
        $html_class_tag = 'class="embed"';
    }
    if ($has_calendar) {
        $calendar_init = '<script type="text/javascript" src="' . $machform_path . 'js/datepick/jquery.datepick.js"></script>' . "\n" . '<script type="text/javascript" src="' . $machform_path . 'js/datepick/jquery.datepick.ext.js"></script>' . "\n" . '<link type="text/css" href="' . $machform_path . 'js/datepick/smoothness.datepick.css" rel="stylesheet" />';
    } else {
        $calendar_init = '';
    }
    //if the form has multiple pages
    //display the pagination header
    if ($form->page_total > 1 && $show_password_form === false) {
        //build pagination header based on the selected type. possible values:
        //steps - display multi steps progress
        //percentage - display progress bar with percentage
        //disabled - disabled
        $page_breaks_data = array();
        $page_title_array = array();
        //get page titles
        $query = "SELECT \r\n\t\t\t\t\t\t\telement_page_title,\r\n\t\t\t\t\t\t\telement_page_number,\r\n\t\t\t\t\t\t\telement_submit_use_image,\r\n\t\t\t\t\t\t    element_submit_primary_text,\r\n\t\t\t\t\t\t\telement_submit_secondary_text,\r\n\t\t\t\t\t\t\telement_submit_primary_img,\r\n\t\t\t\t\t\t\telement_submit_secondary_img \r\n\t\t\t\t\t\tFROM \r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_elements\r\n\t\t\t\t\t   WHERE\r\n\t\t\t\t\t\t\tform_id = ? and element_status = 1 and element_type = 'page_break'\r\n\t\t\t\t\tORDER BY \r\n\t\t\t\t\t   \t\telement_page_number asc";
        $params = array($form_id);
        $sth = mf_do_query($query, $params, $dbh);
        while ($row = mf_do_fetch_result($sth)) {
            $temp_page_number = $row['element_page_number'];
            $page_breaks_data[$temp_page_number]['use_image'] = $row['element_submit_use_image'];
            $page_breaks_data[$temp_page_number]['primary_text'] = $row['element_submit_primary_text'];
            $page_breaks_data[$temp_page_number]['secondary_text'] = $row['element_submit_secondary_text'];
            $page_breaks_data[$temp_page_number]['primary_img'] = $row['element_submit_primary_img'];
            $page_breaks_data[$temp_page_number]['secondary_img'] = $row['element_submit_secondary_img'];
            $page_title_array[] = $row['element_page_title'];
        }
        //add the last page buttons info into the array for easy lookup
        $page_breaks_data[$form->page_total]['use_image'] = $form->submit_use_image;
        $page_breaks_data[$form->page_total]['primary_text'] = $form->submit_primary_text;
        $page_breaks_data[$form->page_total]['secondary_text'] = $form->submit_secondary_text;
        $page_breaks_data[$form->page_total]['primary_img'] = $form->submit_primary_img;
        $page_breaks_data[$form->page_total]['secondary_img'] = $form->submit_secondary_img;
        if ($form->pagination_type == 'steps') {
            $page_titles_markup = '';
            $i = 1;
            foreach ($page_title_array as $page_title) {
                if ($i == $page_number) {
                    $ap_tp_num_active = ' ap_tp_num_active';
                    $ap_tp_text_active = ' ap_tp_text_active';
                } else {
                    $ap_tp_num_active = '';
                    $ap_tp_text_active = '';
                }
                $page_titles_markup .= '<td align="center"><span id="page_num_' . $i . '" class="ap_tp_num' . $ap_tp_num_active . '">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text' . $ap_tp_text_active . '">' . $page_title . '</span></td><td align="center" class="ap_tp_arrow">&gt;</td>' . "\n";
                $i++;
            }
            //add the last page title into the pagination header markup
            if ($i == $page_number) {
                $ap_tp_num_active = ' ap_tp_num_active';
                $ap_tp_text_active = ' ap_tp_text_active';
            } else {
                $ap_tp_num_active = '';
                $ap_tp_text_active = '';
            }
            $page_titles_markup .= '<td align="center"><span id="page_num_' . $i . '" class="ap_tp_num' . $ap_tp_num_active . '">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text' . $ap_tp_text_active . '">' . $form->lastpage_title . '</span></td>';
            //if form review enabled, we need to add the pagination header
            if (!empty($form->review)) {
                $i++;
                $page_titles_markup .= '<td align="center" class="ap_tp_arrow">&gt;</td><td align="center"><span id="page_num_' . $i . '" class="ap_tp_num">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text">' . $form->review_title . '</span></td>';
            }
            $pagination_header = <<<EOT
\t\t\t<li id="pagination_header" class="li_pagination">
\t\t\t <table class="ap_table_pagination" width="100%" border="0" cellspacing="0" cellpadding="0">
\t\t\t  <tr> 
\t\t\t  \t{$page_titles_markup}
\t\t\t  </tr>
\t\t\t</table>
\t\t\t</li>
EOT;
        } else {
            if ($form->pagination_type == 'percentage') {
                $page_total = count($page_title_array) + 1;
                if (!empty($form->review)) {
                    $page_total++;
                }
                $percent_value = round($page_number / $page_total * 100);
                if ($percent_value == 100) {
                    //it's not make sense to display 100% when the form is not really submitted yet
                    $percent_value = 99;
                }
                if (!empty($form->review)) {
                    if ($page_total - 1 == $page_number) {
                        //if this is last page of the form
                        $current_page_title = $form->lastpage_title;
                    } else {
                        $current_page_title = $page_title_array[$page_number - 1];
                    }
                } else {
                    if ($page_total == $page_number) {
                        //if this is last page of the form
                        $current_page_title = $form->lastpage_title;
                    } else {
                        $current_page_title = $page_title_array[$page_number - 1];
                    }
                }
                $page_number_title = sprintf($mf_lang['page_title'], $page_number, $page_total);
                $pagination_header = <<<EOT
\t\t\t<li id="pagination_header" class="li_pagination" title="Click to edit">
\t\t\t    <h3 id="page_title_{$page_number}">{$page_number_title} - {$current_page_title}</h3>
\t\t\t\t<div class="mf_progress_container">          
\t\t\t    \t<div id="mf_progress_percentage" class="mf_progress_value" style="width: {$percent_value}%"><span>{$percent_value}%</span></div>
\t\t\t\t</div>
\t\t\t</li>
EOT;
            } else {
                $pagination_header = '';
            }
        }
        //build the submit buttons markup
        if (empty($edit_id)) {
            if (empty($page_breaks_data[$page_number]['use_image'])) {
                //if using text buttons as submit
                if ($page_number > 1) {
                    $button_secondary_markup = '<input class="button_text btn_secondary" type="submit" id="submit_secondary" name="submit_secondary" value="' . $page_breaks_data[$page_number]['secondary_text'] . '" />';
                }
                $button_markup = <<<EOT
\t\t\t<li id="li_buttons" class="buttons">
\t\t\t\t    <input type="hidden" name="form_id" value="{$form->id}" />
\t\t\t\t    {$edit_markup}
\t\t\t\t    <input type="hidden" name="submit_form" value="1" />
\t\t\t\t    <input type="hidden" name="page_number" value="{$page_number}" />
\t\t\t\t\t<input class="button_text btn_primary" type="submit" id="submit_primary" name="submit_primary" value="{$page_breaks_data[$page_number]['primary_text']}" />
\t\t\t\t\t{$button_secondary_markup}
\t\t\t</li>
EOT;
            } else {
                //if using images as submit
                if ($page_number > 1) {
                    $button_secondary_markup = '<input class="submit_img_secondary" type="image" alt="Previous" id="submit_secondary" name="submit_secondary" src="' . $page_breaks_data[$page_number]['secondary_img'] . '" />';
                }
                $button_markup = <<<EOT
\t\t\t<li id="li_buttons" class="buttons">
\t\t\t\t    <input type="hidden" name="form_id" value="{$form->id}" />
\t\t\t\t    {$edit_markup}
\t\t\t\t    <input type="hidden" name="submit_form" value="1" />
\t\t\t\t    <input type="hidden" name="page_number" value="{$page_number}" />
\t\t\t\t \t<input class="submit_img_primary" type="image" alt="Continue" id="submit_primary" name="submit_primary" src="{$page_breaks_data[$page_number]['primary_img']}" />
\t\t\t\t\t{$button_secondary_markup}
\t\t\t</li>
EOT;
            }
        } else {
            //if there is edit_id, then this is edit_entry page, display a standard button
            $button_markup = <<<EOT
\t\t\t<li id="li_buttons" class="buttons">
\t\t\t\t    <input type="hidden" name="form_id" value="{$form->id}" />
\t\t\t\t    {$edit_markup}
\t\t\t\t    <input type="hidden" name="submit_form" value="1" />
\t\t\t\t    <input type="hidden" name="page_number" value="{$page_number}" />
\t\t\t\t\t<input class="button_text btn_primary" type="submit" id="submit_primary" name="submit_primary" value="Save Changes" />
\t\t\t</li>
EOT;
        }
    }
    if ($has_advance_uploader) {
        if (!empty($machform_path)) {
            $mf_path_script = <<<EOT
<script type="text/javascript">
var __machform_path = '{$machform_path}';
</script>
EOT;
        }
        $advance_uploader_js = <<<EOT
<script type="text/javascript" src="{$machform_path}js/uploadify/swfobject.js"></script>
<script type="text/javascript" src="{$machform_path}js/uploadify/jquery.uploadify.js"></script>
<script type="text/javascript" src="{$machform_path}js/jquery.jqplugin.min.js"></script>
{$mf_path_script}
EOT;
    }
    if ($integration_method == 'iframe') {
        $auto_height_js = <<<EOT
<script type="text/javascript" src="{$machform_path}js/jquery.ba-postmessage.min.js"></script>
<script type="text/javascript">
    \$(function(){
    \t\$.postMessage({mf_iframe_height: \$('body').outerHeight(true)}, '*', parent );
    });
</script>
EOT;
    }
    //if the form has resume enabled and this is multi page form (single page form doesn't have resume option)
    if (!empty($form->resume_enable) && $form->page_total > 1) {
        if (!empty($error_elements['element_resume_email'])) {
            $li_resume_email_style = '';
            $li_resume_error_message = "<p class=\"error\">{$error_elements['element_resume_email']}</p>";
            $li_resume_class = 'class="error"';
            $li_resume_checked = 'checked="checked"';
            $li_resume_button_status = 1;
        } else {
            $li_resume_email_style = 'style="display: none"';
            $li_resume_error_message = '';
            $li_resume_class = '';
            $li_resume_checked = '';
            $li_resume_button_status = 0;
        }
        $form_resume_markup = <<<EOT
\t\t\t<li id="li_resume_checkbox">
\t\t\t<div>
\t\t\t\t<span><input type="checkbox" value="1" class="element checkbox" name="element_resume_checkbox" id="element_resume_checkbox" {$li_resume_checked}>
\t\t\t\t\t<label for="element_resume_checkbox" class="choice">{$mf_lang['resume_checkbox_title']}</label>
\t\t\t\t</span>
\t\t\t</div> 
\t\t\t</li>
\t\t\t<li id="li_resume_email" {$li_resume_class} {$li_resume_email_style} data-resumebutton="{$li_resume_button_status}" data-resumelabel="{$mf_lang['resume_submit_button_text']}">
\t\t\t\t<label for="element_resume_email" class="description">{$mf_lang['resume_email_input_label']}</label>
\t\t\t\t<div>
\t\t\t\t\t<input type="text" value="{$populated_values['element_resume_email']}" class="element text medium" name="element_resume_email" id="element_resume_email"> 
\t\t\t\t</div><p id="guide_resume_email" class="guidelines"><small>{$mf_lang['resume_guideline']}</small></p> {$li_resume_error_message}
\t\t\t</li>
EOT;
    }
    //if the form has enabled merchant support and set the total payment to be displayed
    if (!empty($form->payment_enable_merchant) && !empty($form->payment_show_total)) {
        $currency_symbol = '&#36;';
        switch ($form->payment_currency) {
            case 'USD':
                $currency_symbol = '&#36;';
                break;
            case 'EUR':
                $currency_symbol = '&#8364;';
                break;
            case 'GBP':
                $currency_symbol = '&#163;';
                break;
            case 'AUD':
                $currency_symbol = 'A&#36;';
                break;
            case 'CAD':
                $currency_symbol = 'C&#36;';
                break;
            case 'JPY':
                $currency_symbol = '&#165;';
                break;
            case 'THB':
                $currency_symbol = '&#3647;';
                break;
            case 'HUF':
                $currency_symbol = '&#70;&#116;';
                break;
            case 'CHF':
                $currency_symbol = 'CHF';
                break;
            case 'CZK':
                $currency_symbol = '&#75;&#269;';
                break;
            case 'SEK':
                $currency_symbol = 'kr';
                break;
            case 'DKK':
                $currency_symbol = 'kr';
                break;
            case 'PHP':
                $currency_symbol = '&#36;';
                break;
            case 'MYR':
                $currency_symbol = 'RM';
                break;
            case 'PLN':
                $currency_symbol = '&#122;&#322;';
                break;
            case 'BRL':
                $currency_symbol = 'R&#36;';
                break;
            case 'HKD':
                $currency_symbol = 'HK&#36;';
                break;
            case 'MXN':
                $currency_symbol = 'Mex&#36;';
                break;
            case 'TWD':
                $currency_symbol = 'NT&#36;';
                break;
            case 'TRY':
                $currency_symbol = 'TL';
                break;
        }
        if ($form->payment_price_type == 'variable') {
            //if this is multipage form, we need to get the total selected price from other pages
            if ($form->page_total > 1) {
                $other_page_total_payment = (double) mf_get_payment_total($dbh, $form_id, $session_id, $page_number);
                $other_page_total_data_tag = 'data-basetotal="' . $other_page_total_payment . '"';
            } else {
                $other_page_total_data_tag = 'data-basetotal="0"';
            }
        } elseif ($form->payment_price_type == 'fixed') {
            $other_page_total_data_tag = 'data-basetotal="' . $form->payment_price_amount . '"';
        }
        $payment_total_markup = <<<EOT
\t\t\t<li class="total_payment" {$other_page_total_data_tag}>
\t\t\t\t<span>
\t\t\t\t\t<h3>{$currency_symbol}<var>0</var></h3>
\t\t\t\t\t<h5>{$mf_lang['payment_total']}</h5>
\t\t\t\t</span>
\t\t\t</li>
EOT;
        if ($form->payment_total_location == 'top') {
            $payment_total_markup_top = $payment_total_markup;
        } else {
            if ($form->payment_total_location == 'bottom') {
                $payment_total_markup_bottom = $payment_total_markup;
            } else {
                if ($form->payment_total_location == 'top-bottom' || $form->payment_total_location == 'all') {
                    $payment_total_markup_top = $payment_total_markup;
                    $payment_total_markup_bottom = $payment_total_markup;
                }
            }
        }
    }
    if (empty($mf_settings['disable_machform_link'])) {
        $powered_by_markup = 'Powered by MachForm';
    } else {
        $powered_by_markup = '';
    }
    //if advanced form code being used, display the form without body container
    if ($integration_method == 'php') {
        $container_class .= " integrated";
        if (!empty($edit_id)) {
            $view_css_markup = '<link rel="stylesheet" type="text/css" href="css/edit_entry.css" media="all" />';
        } else {
            $view_css_markup = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$machform_path}{$css_dir}view.css\" media=\"all\" />";
        }
        $form_markup = <<<EOT
{$view_css_markup}
{$theme_css_link}
{$font_css_markup}
<style>
html{
\tbackground: none repeat scroll 0 0 transparent;
\tbackground-color: transparent;
}
</style>
<script type="text/javascript" src="{$machform_path}js/jquery.min.js"></script>
<script type="text/javascript" src="{$machform_path}js/jquery-ui/ui/jquery.effects.core.js"></script>
<script type="text/javascript" src="{$machform_path}view.js"></script>
{$advance_uploader_js}
{$calendar_init}

<div id="main_body" class="{$container_class}">

\t<div id="form_container">
\t
\t\t<h1><a>{$form->name}</a></h1>
\t\t<form id="form_{$form->id}" class="appnitro {$form->label_alignment}" {$form_enc_type} method="post" data-highlightcolor="{$field_highlight_color}" action="#main_body">
\t\t\t{$form_desc_div}\t\t\t\t\t\t
\t\t\t<ul {$ul_class}>
\t\t\t{$pagination_header}
\t\t\t{$payment_total_markup_top}
\t\t\t{$form->error_message}
\t\t\t{$all_element_markup}
\t\t\t{$custom_element}
\t\t\t{$payment_total_markup_bottom}
\t\t\t{$form_resume_markup}
\t\t\t{$button_markup}
\t\t\t</ul>
\t\t</form>\t
\t\t<div id="footer">
\t\t\t{$powered_by_markup}
\t\t</div>
\t</div>\t
</div>

EOT;
    } else {
        $form_markup = <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html {$html_class_tag} xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{$form->name}</title>
<link rel="stylesheet" type="text/css" href="data/form_default/css/view.css" media="all" />
{$theme_css_link}
{$font_css_markup}
<script type="text/javascript" src="{$machform_path}js/jquery.min.js"></script>
<script type="text/javascript" src="{$machform_path}js/jquery-ui/ui/jquery.effects.core.js"></script>
<script type="text/javascript" src="{$machform_path}view.js"></script>
{$advance_uploader_js}
{$calendar_init}
{$auto_height_js}
</head>
<body id="main_body" class="{$container_class}">
\t
\t<div id="form_container" class="{$form_container_class}">
\t
\t\t<h1><a>{$form->name}</a></h1>
\t\t<form id="form_{$form->id}" class="appnitro {$form->label_alignment}" {$form_enc_type} method="post" data-highlightcolor="{$field_highlight_color}" action="#main_body">
\t\t\t{$form_desc_div}\t\t\t\t\t\t
\t\t\t<ul {$ul_class}>
\t\t\t{$pagination_header}
\t\t\t{$payment_total_markup_top}
\t\t\t{$form->error_message}
\t\t\t{$all_element_markup}
\t\t\t{$custom_element}
\t\t\t{$payment_total_markup_bottom}
\t\t\t{$form_resume_markup}
\t\t\t{$button_markup}
\t\t\t</ul>
\t\t</form>\t
\t\t<div id="footer">
\t\t\t{$powered_by_markup}
\t\t</div>
\t</div>
\t
\t</body>
</html>
EOT;
    }
    return $form_markup;
}
            $form_params['custom_error'] = $custom_error;
            $form_params['edit_id'] = $input_array['edit_id'];
            $form_params['integration_method'] = 'php';
            $form_params['page_number'] = 0;
            //display all pages (if any) as a single page
            $form_markup = mf_display_form($dbh, $input_array['form_id'], $form_params);
        }
    }
} else {
    //otherwise, display the form with the values
    //set session value to override password protected form
    $_SESSION['user_authenticated'] = $form_id;
    //set session value to bypass unique checking
    $_SESSION['edit_entry']['form_id'] = $form_id;
    $_SESSION['edit_entry']['entry_id'] = $entry_id;
    $form_values = mf_get_entry_values($dbh, $form_id, $entry_id);
    $form_params = array();
    $form_params['populated_values'] = $form_values;
    $form_params['edit_id'] = $entry_id;
    $form_params['integration_method'] = 'php';
    $form_params['page_number'] = 0;
    //display all pages (if any) as a single page
    $form_markup = mf_display_form($dbh, $form_id, $form_params);
}
$header_data = <<<EOT
<link type="text/css" href="js/jquery-ui/themes/base/jquery.ui.all.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="css/entry_print.css" media="print">
EOT;
$current_nav_tab = 'manage_forms';
require 'includes/header.php';
?>
Beispiel #3
0
function mf_send_notification($dbh, $form_id, $entry_id, $to_emails, $email_param)
{
    $from_name = $email_param['from_name'];
    $from_email = $email_param['from_email'];
    $subject = $email_param['subject'];
    $content = $email_param['content'];
    $as_plain_text = $email_param['as_plain_text'];
    //if set to 'true' the email content will be a simple plain text
    $target_is_admin = $email_param['target_is_admin'];
    //if set to 'false', the download link for uploaded file will be removed
    //get settings first
    $mf_settings = mf_get_settings($dbh);
    //get data for the particular entry id
    if ($target_is_admin === false) {
        $options['strip_download_link'] = true;
    }
    $options['strip_checkbox_image'] = true;
    $options['machform_base_path'] = $email_param['machform_base_path'];
    //the path to machform
    $entry_details = mf_get_entry_details($dbh, $form_id, $entry_id, $options);
    //populate field values to template variables
    $i = 0;
    foreach ($entry_details as $data) {
        $template_variables[$i] = '{element_' . $data['element_id'] . '}';
        $template_values[$i] = $data['value'];
        if ($data['element_type'] == 'textarea') {
            $template_values[$i] = nl2br($data['value']);
        } elseif ($data['element_type'] == 'file') {
            if ($target_is_admin === false) {
                $template_values[$i] = strip_tags($data['value']);
            } else {
                $template_values[$i] = strip_tags($data['value'], '<a><br/>');
            }
        } else {
            $template_values[$i] = $data['value'];
        }
        $i++;
    }
    $entry_values = mf_get_entry_values($dbh, $form_id, $entry_id);
    //get template variables for some complex fields (name and address)
    $query = "select \r\n\t\t\t\t\t\t element_id,\r\n\t\t\t\t\t\t element_type \r\n\t\t\t\t     from\r\n\t\t\t\t     \t `" . MF_TABLE_PREFIX . "form_elements` \r\n\t\t\t\t    where \r\n\t\t\t\t    \t form_id=? and \r\n\t\t\t\t    \t element_type != 'section' and \r\n\t\t\t\t    \t element_status=1 and\r\n\t\t\t\t    \t element_type in('simple_name','simple_name_wmiddle','name','name_wmiddle','address')\r\n\t\t\t\t order by \r\n\t\t\t\t \t\t element_position asc";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    while ($row = mf_do_fetch_result($sth)) {
        $element_id = $row['element_id'];
        $element_type = $row['element_type'];
        if ('simple_name' == $element_type) {
            $total_sub_field = 2;
        } else {
            if ('simple_name_wmiddle' == $element_type) {
                $total_sub_field = 3;
            } else {
                if ('name' == $element_type) {
                    $total_sub_field = 4;
                } else {
                    if ('name_wmiddle' == $element_type) {
                        $total_sub_field = 5;
                    } else {
                        if ('address' == $element_type) {
                            $total_sub_field = 6;
                        }
                    }
                }
            }
        }
        for ($j = 1; $j <= $total_sub_field; $j++) {
            $template_variables[$i] = '{element_' . $element_id . '_' . $j . '}';
            $template_values[$i] = $entry_values['element_' . $element_id . '_' . $j]['default_value'];
            $i++;
        }
    }
    //get entry timestamp
    $query = "select date_created,ip_address from `" . MF_TABLE_PREFIX . "form_{$form_id}` where id=?";
    $params = array($entry_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $date_created = $row['date_created'];
    $ip_address = $row['ip_address'];
    //get form name
    $query = "select form_name\tfrom `" . MF_TABLE_PREFIX . "forms` where form_id=?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $form_name = $row['form_name'];
    $template_variables[$i] = '{date_created}';
    $template_values[$i] = $date_created;
    $i++;
    $template_variables[$i] = '{ip_address}';
    $template_values[$i] = $ip_address;
    $i++;
    $template_variables[$i] = '{form_name}';
    $template_values[$i] = $form_name;
    $i++;
    $template_variables[$i] = '{entry_no}';
    $template_values[$i] = $entry_id;
    $i++;
    $template_variables[$i] = '{form_id}';
    $template_values[$i] = $form_id;
    //compose {entry_data} based on 'as_plain_text' preferences
    $email_body = '';
    $files_to_attach = array();
    if (!$as_plain_text) {
        //compose html format
        $email_body = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px;text-align:left">' . "\n";
        $toggle = false;
        $j = 0;
        foreach ($entry_details as $data) {
            //0 should be displayed, empty string don't
            if ((empty($data['value']) || $data['value'] == '&nbsp;') && $data['value'] !== 0 && $data['value'] !== '0') {
                continue;
            }
            //skip pagebreak
            if ($data['label'] == 'mf_page_break' && $data['value'] == 'mf_page_break') {
                continue;
            }
            if ($toggle) {
                $toggle = false;
                $row_style = 'style="background-color:#F3F7FB"';
            } else {
                $toggle = true;
                $row_style = '';
            }
            if ($data['element_type'] == 'textarea') {
                $data['value'] = nl2br($data['value']);
            } elseif ($data['element_type'] == 'file') {
                if ($target_is_admin === false) {
                    $data['value'] = strip_tags($data['value']);
                } else {
                    $data['value'] = strip_tags($data['value'], '<a><br/>');
                    $data['value'] = str_replace('&nbsp;', '', $data['value']);
                    //if there is file to be attached
                    if (!empty($data['filedata'])) {
                        foreach ($data['filedata'] as $file_info) {
                            $files_to_attach[$j]['filename_path'] = $file_info['filename_path'];
                            $files_to_attach[$j]['filename_value'] = $file_info['filename_value'];
                            $j++;
                        }
                    }
                }
            }
            $email_body .= "<tr {$row_style}>\n";
            $email_body .= '<td width="40%" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;"><strong>' . $data['label'] . '</strong> </td>' . "\n";
            $email_body .= '<td width="60%" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;">' . $data['value'] . '</td>' . "\n";
            $email_body .= '</tr>' . "\n";
            $i++;
        }
        $email_body .= "</table>\n";
    } else {
        $money_symbols = array('&#165;', '&#163;', '&#8364;', '&#3647;', '&#75;&#269;', '&#122;&#322;', '&#65020;');
        $money_plain = array('¥', '£', '€', '฿', 'Kč', 'zł', '﷼');
        //compose text format
        foreach ($entry_details as $data) {
            //0 should be displayed, empty string don't
            if ((empty($data['value']) || $data['value'] == '&nbsp;') && $data['value'] !== 0 && $data['value'] !== '0') {
                continue;
            }
            $data['value'] = str_replace('<br />', "\n", $data['value']);
            if ($data['element_type'] == 'textarea' || $data['element_type'] == 'matrix') {
                $data['value'] = trim($data['value'], "\n");
                $email_body .= "{$data['label']}: \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'checkbox' || $data['element_type'] == 'address') {
                $email_body .= "{$data['label']}: \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'file') {
                $data['value'] = strip_tags($data['value']);
                $data['value'] = str_replace('&nbsp;', "\n- ", $data['value']);
                $email_body .= "{$data['label']}: {$data['value']}\n";
            } elseif ($data['element_type'] == 'money') {
                $data['value'] = str_replace($money_symbols, $money_plain, $data['value']);
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            } elseif ($data['element_type'] == 'url') {
                $data['value'] = strip_tags($data['value']);
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            } else {
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            }
        }
    }
    $i = count($template_variables);
    $template_variables[$i] = '{entry_data}';
    $template_values[$i] = $email_body;
    //create the mail transport
    if (!empty($mf_settings['smtp_enable'])) {
        $s_transport = Swift_SmtpTransport::newInstance($mf_settings['smtp_host'], $mf_settings['smtp_port']);
        if (!empty($mf_settings['smtp_secure'])) {
            $s_transport->setEncryption('tls');
        }
        if (!empty($mf_settings['smtp_auth'])) {
            $s_transport->setUsername($mf_settings['smtp_username']);
            $s_transport->setPassword($mf_settings['smtp_password']);
        }
    } else {
        $s_transport = Swift_MailTransport::newInstance();
        //use PHP mail() transport
    }
    //create mailer instance
    $s_mailer = Swift_Mailer::newInstance($s_transport);
    if (file_exists($mf_settings['upload_dir'] . "/form_{$form_id}/files")) {
        Swift_Preferences::getInstance()->setCacheType('disk')->setTempDir($mf_settings['upload_dir'] . "/form_{$form_id}/files");
    }
    //create the message
    //parse from_name template
    if (!empty($from_name)) {
        $from_name = str_replace($template_variables, $template_values, $from_name);
        $from_name = str_replace('&nbsp;', '', $from_name);
        //decode any html entity
        $from_name = html_entity_decode($from_name, ENT_QUOTES);
    } else {
        $from_name = 'MachForm';
    }
    //parse from_email_address template
    if (!empty($from_email)) {
        $from_email = str_replace($template_variables, $template_values, $from_email);
    } else {
        $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
        $from_email = "no-reply@{$domain}";
    }
    //parse subject template
    if (!empty($subject)) {
        $subject = str_replace($template_variables, $template_values, $subject);
        $subject = str_replace('&nbsp;', '', $subject);
    } else {
        if ($target_is_admin) {
            $subject = utf8_encode("{$form_name} [#{$entry_id}]");
        } else {
            $subject = utf8_encode("{$form_name} - Receipt");
        }
    }
    //decode any html entity
    $subject = html_entity_decode($subject, ENT_QUOTES);
    //parse content template
    $email_content = str_replace($template_variables, $template_values, $content);
    if (!$as_plain_text) {
        //html type
        //add footer
        if (empty($mf_settings['disable_machform_link'])) {
            $email_content .= "<br /><br /><br /><br /><br /><b style=\"font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px\">Powered by MachForm</b>";
        }
        //enclose with container div
        $email_content = '<div style="font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px">' . $email_content . '</div>';
    }
    $to_emails = str_replace('&nbsp;', '', str_replace($template_variables, $template_values, $to_emails));
    if (!empty($to_emails)) {
        $email_address = explode(',', $to_emails);
    }
    if (!empty($email_address)) {
        if (!$as_plain_text) {
            $email_content_type = 'text/html';
        } else {
            $email_content_type = 'text/plain';
        }
        $s_message = Swift_Message::newInstance()->setCharset('utf-8')->setMaxLineLength(1000)->setSubject($subject)->setFrom(array($from_email => $from_name))->setSender($from_email)->setReturnPath($from_email)->setTo($email_address)->setBody($email_content, $email_content_type);
        //attach files, if any
        if (!empty($files_to_attach)) {
            foreach ($files_to_attach as $file_data) {
                $s_message->attach(Swift_Attachment::fromPath($file_data['filename_path'])->setFilename($file_data['filename_value']));
            }
        }
        //send the message
        $send_result = $s_mailer->send($s_message);
        if (empty($send_result)) {
            echo "Error sending email!";
        }
    }
}
function mf_send_notification($dbh, $form_id, $entry_id, $to_emails, $email_param)
{
    global $mf_hook_emails;
    global $mf_lang;
    $from_name = $email_param['from_name'];
    $from_email = $email_param['from_email'];
    $subject = $email_param['subject'];
    $content = $email_param['content'];
    $as_plain_text = $email_param['as_plain_text'];
    //if set to 'true' the email content will be a simple plain text
    $target_is_admin = $email_param['target_is_admin'];
    //if set to 'false', the download link for uploaded file will be removed
    $check_hook_file = $email_param['check_hook_file'];
    //get settings first
    $mf_settings = mf_get_settings($dbh);
    //get data for the particular entry id
    if ($target_is_admin === false) {
        $options['strip_download_link'] = false;
        //as of v3, receipt email should display download link
    }
    $options['strip_checkbox_image'] = true;
    $options['machform_path'] = $email_param['machform_base_path'];
    //the path to machform
    $entry_details = mf_get_entry_details($dbh, $form_id, $entry_id, $options);
    //if the form has payment enabled, get the payment details
    //start getting payment details -----------------------
    $query = "select \r\n\t\t\t\t\t payment_enable_merchant,\r\n\t\t\t\t\t payment_merchant_type,\r\n\t\t\t\t\t payment_price_type,\r\n\t\t\t\t\t payment_price_amount,\r\n\t\t\t\t\t payment_currency,\r\n\t\t\t\t\t payment_ask_billing,\r\n\t\t\t\t\t payment_ask_shipping\r\n\t\t\t     from \r\n\t\t\t     \t " . MF_TABLE_PREFIX . "forms \r\n\t\t\t    where \r\n\t\t\t    \t form_id = ?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    if (!empty($row)) {
        $payment_enable_merchant = (int) $row['payment_enable_merchant'];
        if ($payment_enable_merchant < 1) {
            $payment_enable_merchant = 0;
        }
        $payment_price_amount = (double) $row['payment_price_amount'];
        $payment_merchant_type = $row['payment_merchant_type'];
        $payment_price_type = $row['payment_price_type'];
        $form_payment_currency = strtoupper($row['payment_currency']);
        $payment_ask_billing = (int) $row['payment_ask_billing'];
        $payment_ask_shipping = (int) $row['payment_ask_shipping'];
    }
    if (!empty($payment_enable_merchant)) {
        $query = "SELECT \r\n\t\t\t\t\t\t\t`payment_id`,\r\n\t\t\t\t\t\t\t date_format(payment_date,'%e %b %Y - %r') payment_date, \r\n\t\t\t\t\t\t\t`payment_status`, \r\n\t\t\t\t\t\t\t`payment_fullname`, \r\n\t\t\t\t\t\t\t`payment_amount`, \r\n\t\t\t\t\t\t\t`payment_currency`, \r\n\t\t\t\t\t\t\t`payment_test_mode`,\r\n\t\t\t\t\t\t\t`payment_merchant_type`, \r\n\t\t\t\t\t\t\t`status`, \r\n\t\t\t\t\t\t\t`billing_street`, \r\n\t\t\t\t\t\t\t`billing_city`, \r\n\t\t\t\t\t\t\t`billing_state`, \r\n\t\t\t\t\t\t\t`billing_zipcode`, \r\n\t\t\t\t\t\t\t`billing_country`, \r\n\t\t\t\t\t\t\t`same_shipping_address`, \r\n\t\t\t\t\t\t\t`shipping_street`, \r\n\t\t\t\t\t\t\t`shipping_city`, \r\n\t\t\t\t\t\t\t`shipping_state`, \r\n\t\t\t\t\t\t\t`shipping_zipcode`, \r\n\t\t\t\t\t\t\t`shipping_country`\r\n\t\t\t\t\t\tFROM\r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_payments\r\n\t\t\t\t\t   WHERE\r\n\t\t\t\t\t   \t\tform_id = ? and record_id = ? and `status` = 1\r\n\t\t\t\t\tORDER BY\r\n\t\t\t\t\t\t\tpayment_date DESC\r\n\t\t\t\t\t   LIMIT 1";
        $params = array($form_id, $entry_id);
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        $payment_id = $row['payment_id'];
        $payment_date = $row['payment_date'];
        $payment_status = $row['payment_status'];
        $payment_fullname = $row['payment_fullname'];
        $payment_amount = (double) $row['payment_amount'];
        $payment_currency = strtoupper($row['payment_currency']);
        $payment_test_mode = (int) $row['payment_test_mode'];
        $payment_merchant_type = $row['payment_merchant_type'];
        $billing_street = htmlspecialchars(trim($row['billing_street']));
        $billing_city = htmlspecialchars(trim($row['billing_city']));
        $billing_state = htmlspecialchars(trim($row['billing_state']));
        $billing_zipcode = htmlspecialchars(trim($row['billing_zipcode']));
        $billing_country = htmlspecialchars(trim($row['billing_country']));
        $same_shipping_address = (int) $row['same_shipping_address'];
        if (!empty($same_shipping_address)) {
            $shipping_street = $billing_street;
            $shipping_city = $billing_city;
            $shipping_state = $billing_state;
            $shipping_zipcode = $billing_zipcode;
            $shipping_country = $billing_country;
        } else {
            $shipping_street = htmlspecialchars(trim($row['shipping_street']));
            $shipping_city = htmlspecialchars(trim($row['shipping_city']));
            $shipping_state = htmlspecialchars(trim($row['shipping_state']));
            $shipping_zipcode = htmlspecialchars(trim($row['shipping_zipcode']));
            $shipping_country = htmlspecialchars(trim($row['shipping_country']));
        }
        if (!empty($billing_street) || !empty($billing_city) || !empty($billing_state) || !empty($billing_zipcode) || !empty($billing_country)) {
            $billing_address = "{$billing_street}<br />{$billing_city}, {$billing_state} {$billing_zipcode}<br />{$billing_country}";
        }
        if (!empty($shipping_street) || !empty($shipping_city) || !empty($shipping_state) || !empty($shipping_zipcode) || !empty($shipping_country)) {
            $shipping_address = "{$shipping_street}<br />{$shipping_city}, {$shipping_state} {$shipping_zipcode}<br />{$shipping_country}";
        }
        if (!empty($row)) {
            $payment_has_record = true;
        } else {
            //if the entry doesn't have any record within ap_form_payments table
            //we need to calculate the total amount
            $payment_has_record = false;
            $payment_status = "unpaid";
            if ($payment_price_type == 'variable') {
                $payment_amount = (double) mf_get_payment_total($dbh, $form_id, $entry_id, 0, 'live');
            } else {
                if ($payment_price_type == 'fixed') {
                    $payment_amount = $payment_price_amount;
                }
            }
            $payment_currency = $form_payment_currency;
        }
        if (!$as_plain_text) {
            switch ($payment_currency) {
                case 'USD':
                    $currency_symbol = '&#36;';
                    break;
                case 'EUR':
                    $currency_symbol = '&#8364;';
                    break;
                case 'GBP':
                    $currency_symbol = '&#163;';
                    break;
                case 'AUD':
                    $currency_symbol = '&#36;';
                    break;
                case 'CAD':
                    $currency_symbol = '&#36;';
                    break;
                case 'JPY':
                    $currency_symbol = '&#165;';
                    break;
                case 'THB':
                    $currency_symbol = '&#3647;';
                    break;
                case 'HUF':
                    $currency_symbol = '&#70;&#116;';
                    break;
                case 'CHF':
                    $currency_symbol = 'CHF';
                    break;
                case 'CZK':
                    $currency_symbol = '&#75;&#269;';
                    break;
                case 'SEK':
                    $currency_symbol = 'kr';
                    break;
                case 'DKK':
                    $currency_symbol = 'kr';
                    break;
                case 'NOK':
                    $currency_symbol = 'kr';
                    break;
                case 'PHP':
                    $currency_symbol = '&#36;';
                    break;
                case 'MYR':
                    $currency_symbol = 'RM';
                    break;
                case 'PLN':
                    $currency_symbol = '&#122;&#322;';
                    break;
                case 'BRL':
                    $currency_symbol = 'R&#36;';
                    break;
                case 'HKD':
                    $currency_symbol = '&#36;';
                    break;
                case 'MXN':
                    $currency_symbol = 'Mex&#36;';
                    break;
                case 'TWD':
                    $currency_symbol = 'NT&#36;';
                    break;
                case 'TRY':
                    $currency_symbol = 'TL';
                    break;
                case 'NZD':
                    $currency_symbol = '&#36;';
                    break;
                case 'SGD':
                    $currency_symbol = '&#36;';
                    break;
                default:
                    $currency_symbol = '';
                    break;
            }
        } else {
            switch ($payment_currency) {
                case 'USD':
                    $currency_symbol = '$';
                    break;
                case 'EUR':
                    $currency_symbol = '€';
                    break;
                case 'GBP':
                    $currency_symbol = '£';
                    break;
                case 'AUD':
                    $currency_symbol = '$';
                    break;
                case 'CAD':
                    $currency_symbol = '$';
                    break;
                case 'JPY':
                    $currency_symbol = '¥';
                    break;
                case 'THB':
                    $currency_symbol = '฿';
                    break;
                case 'HUF':
                    $currency_symbol = 'Ft';
                    break;
                case 'CHF':
                    $currency_symbol = 'CHF';
                    break;
                case 'CZK':
                    $currency_symbol = 'Kč';
                    break;
                case 'SEK':
                    $currency_symbol = 'kr';
                    break;
                case 'DKK':
                    $currency_symbol = 'kr';
                    break;
                case 'NOK':
                    $currency_symbol = 'kr';
                    break;
                case 'PHP':
                    $currency_symbol = '$';
                    break;
                case 'MYR':
                    $currency_symbol = 'RM';
                    break;
                case 'PLN':
                    $currency_symbol = 'zł';
                    break;
                case 'BRL':
                    $currency_symbol = 'R$';
                    break;
                case 'HKD':
                    $currency_symbol = '$';
                    break;
                case 'MXN':
                    $currency_symbol = '$';
                    break;
                case 'TWD':
                    $currency_symbol = '$';
                    break;
                case 'TRY':
                    $currency_symbol = 'TL';
                    break;
                case 'NZD':
                    $currency_symbol = '$';
                    break;
                case 'SGD':
                    $currency_symbol = '$';
                    break;
                default:
                    $currency_symbol = '';
                    break;
            }
        }
        $total_payment_amount = $currency_symbol . $payment_amount . ' ' . $payment_currency;
        $total_entry_details = count($entry_details);
        //blank row for separator
        if (!$as_plain_text) {
            $entry_details[$total_entry_details]['value'] = '&nbsp;&nbsp;';
            $entry_details[$total_entry_details]['label'] = '&nbsp;&nbsp;';
        } else {
            $entry_details[$total_entry_details]['value'] = '';
            $entry_details[$total_entry_details]['label'] = '';
        }
        //get total amount
        $total_entry_details++;
        $entry_details[$total_entry_details]['value'] = $total_payment_amount;
        $entry_details[$total_entry_details]['label'] = $mf_lang['payment_total'];
        //get payment status
        $total_entry_details++;
        if (!empty($payment_test_mode)) {
            $entry_details[$total_entry_details]['value'] = strtoupper($payment_status) . ' (TEST mode)';
        } else {
            $entry_details[$total_entry_details]['value'] = strtoupper($payment_status);
        }
        $entry_details[$total_entry_details]['label'] = $mf_lang['payment_status'];
        if ($payment_has_record) {
            //get payment id
            $total_entry_details++;
            $entry_details[$total_entry_details]['value'] = $payment_id;
            $entry_details[$total_entry_details]['label'] = $mf_lang['payment_id'];
            //get payment date
            $total_entry_details++;
            $entry_details[$total_entry_details]['value'] = $payment_date;
            $entry_details[$total_entry_details]['label'] = $mf_lang['payment_date'];
            //blank row for separator
            $total_entry_details++;
            if (!$as_plain_text) {
                $entry_details[$total_entry_details]['value'] = '&nbsp;&nbsp;';
                $entry_details[$total_entry_details]['label'] = '&nbsp;&nbsp;';
            } else {
                $entry_details[$total_entry_details]['value'] = '';
                $entry_details[$total_entry_details]['label'] = '';
            }
            //get full name
            $total_entry_details++;
            $entry_details[$total_entry_details]['value'] = htmlspecialchars($payment_fullname, ENT_QUOTES);
            $entry_details[$total_entry_details]['label'] = $mf_lang['payment_fullname'];
            //get billing address
            if (!empty($payment_ask_billing) && !empty($billing_address)) {
                $total_entry_details++;
                $entry_details[$total_entry_details]['value'] = $billing_address;
                $entry_details[$total_entry_details]['label'] = $mf_lang['payment_billing'];
            }
            //get shipping address
            if (!empty($payment_ask_shipping) && !empty($shipping_address)) {
                $total_entry_details++;
                $entry_details[$total_entry_details]['value'] = $shipping_address;
                $entry_details[$total_entry_details]['label'] = $mf_lang['payment_shipping'];
            }
        }
    }
    //end payment enable merchant
    //end getting payment details -----------------------
    //populate field values to template variables
    $i = 0;
    foreach ($entry_details as $data) {
        $template_variables[$i] = '{element_' . $data['element_id'] . '}';
        $template_values[$i] = $data['value'];
        if ($data['element_type'] == 'textarea' && !$as_plain_text) {
            $template_values[$i] = nl2br($data['value']);
        } elseif ($data['element_type'] == 'file') {
            if (!$as_plain_text) {
                $template_values[$i] = strip_tags($data['value'], '<a><br/>');
            } else {
                $template_values[$i] = strip_tags($data['value']);
                $template_values[$i] = str_replace('&nbsp;', "\n- ", $template_values[$i]);
            }
        } elseif ($data['element_type'] == 'signature') {
            //skip the signature, we will construct the value later below
            continue;
        } else {
            $template_values[$i] = $data['value'];
        }
        $i++;
    }
    $entry_values = mf_get_entry_values($dbh, $form_id, $entry_id);
    //get template variables for some complex fields (name and address)
    $query = "select \r\n\t\t\t\t\t\t element_id,\r\n\t\t\t\t\t\t element_type \r\n\t\t\t\t     from\r\n\t\t\t\t     \t `" . MF_TABLE_PREFIX . "form_elements` \r\n\t\t\t\t    where \r\n\t\t\t\t    \t form_id=? and \r\n\t\t\t\t    \t element_type != 'section' and \r\n\t\t\t\t    \t element_status=1 and\r\n\t\t\t\t    \t element_type in('simple_name','simple_name_wmiddle','name','name_wmiddle','address')\r\n\t\t\t\t order by \r\n\t\t\t\t \t\t element_position asc";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    while ($row = mf_do_fetch_result($sth)) {
        $element_id = $row['element_id'];
        $element_type = $row['element_type'];
        if ('simple_name' == $element_type) {
            $total_sub_field = 2;
        } else {
            if ('simple_name_wmiddle' == $element_type) {
                $total_sub_field = 3;
            } else {
                if ('name' == $element_type) {
                    $total_sub_field = 4;
                } else {
                    if ('name_wmiddle' == $element_type) {
                        $total_sub_field = 5;
                    } else {
                        if ('address' == $element_type) {
                            $total_sub_field = 6;
                        }
                    }
                }
            }
        }
        for ($j = 1; $j <= $total_sub_field; $j++) {
            $template_variables[$i] = '{element_' . $element_id . '_' . $j . '}';
            $template_values[$i] = $entry_values['element_' . $element_id . '_' . $j]['default_value'];
            $i++;
        }
    }
    //get entry timestamp
    $query = "select date_created,ip_address from `" . MF_TABLE_PREFIX . "form_{$form_id}` where id=?";
    $params = array($entry_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $date_created = $row['date_created'];
    $ip_address = $row['ip_address'];
    //get form name
    $query = "select form_name\tfrom `" . MF_TABLE_PREFIX . "forms` where form_id=?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $form_name = $row['form_name'];
    $template_variables[$i] = '{date_created}';
    $template_values[$i] = $date_created;
    $i++;
    $template_variables[$i] = '{ip_address}';
    $template_values[$i] = $ip_address;
    $i++;
    $template_variables[$i] = '{form_name}';
    $template_values[$i] = $form_name;
    $i++;
    $template_variables[$i] = '{entry_no}';
    $template_values[$i] = $entry_id;
    $i++;
    $template_variables[$i] = '{form_id}';
    $template_values[$i] = $form_id;
    //populate template variables for payment details
    if (!empty($total_payment_amount)) {
        $i++;
        $template_variables[$i] = '{total_amount}';
        $template_values[$i] = $total_payment_amount;
    }
    if (!empty($payment_status)) {
        $i++;
        $template_variables[$i] = '{payment_status}';
        if (!empty($payment_test_mode)) {
            $template_values[$i] = strtoupper($payment_status) . ' (TEST mode)';
        } else {
            $template_values[$i] = strtoupper($payment_status);
        }
    }
    if (!empty($payment_id)) {
        $i++;
        $template_variables[$i] = '{payment_id}';
        $template_values[$i] = $payment_id;
    }
    if (!empty($payment_date)) {
        $i++;
        $template_variables[$i] = '{payment_date}';
        $template_values[$i] = $payment_date;
    }
    if (!empty($payment_fullname)) {
        $i++;
        $template_variables[$i] = '{payment_fullname}';
        $template_values[$i] = $payment_fullname;
    }
    if (!empty($billing_address)) {
        if ($as_plain_text) {
            $billing_address = str_replace('<br />', "\n", $billing_address);
        }
        $i++;
        $template_variables[$i] = '{billing_address}';
        $template_values[$i] = $billing_address;
    }
    if (!empty($shipping_address)) {
        if ($as_plain_text) {
            $shipping_address = str_replace('<br />', "\n", $shipping_address);
        }
        $i++;
        $template_variables[$i] = '{shipping_address}';
        $template_values[$i] = $shipping_address;
    }
    //compose {entry_data} based on 'as_plain_text' preferences
    $email_body = '';
    $files_to_attach = array();
    if (!$as_plain_text) {
        //compose html format
        $email_body = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px;text-align:left">' . "\n";
        $toggle = false;
        $j = 0;
        foreach ($entry_details as $data) {
            //0 should be displayed, empty string don't
            if ((empty($data['value']) || $data['value'] == '&nbsp;') && $data['value'] !== 0 && $data['value'] !== '0' && $data['element_type'] !== 'section') {
                continue;
            }
            //skip pagebreak
            if ($data['label'] == 'mf_page_break' && $data['value'] == 'mf_page_break') {
                continue;
            }
            if ($toggle) {
                $toggle = false;
                $row_style = 'style="background-color:#F3F7FB"';
            } else {
                $toggle = true;
                $row_style = '';
            }
            if ($data['element_type'] == 'textarea') {
                $data['value'] = nl2br($data['value']);
            } elseif ($data['element_type'] == 'file') {
                if ($target_is_admin === false) {
                    $data['value'] = strip_tags($data['value'], '<a><br/>');
                    $data['value'] = str_replace('&nbsp;', '', $data['value']);
                } else {
                    $data['value'] = strip_tags($data['value'], '<a><br/>');
                    $data['value'] = str_replace('&nbsp;', '', $data['value']);
                    //if there is file to be attached
                    if (!empty($data['filedata'])) {
                        foreach ($data['filedata'] as $file_info) {
                            $files_to_attach[$j]['filename_path'] = $file_info['filename_path'];
                            $files_to_attach[$j]['filename_value'] = $file_info['filename_value'];
                            $j++;
                        }
                    }
                }
            } elseif ($data['element_type'] == 'signature') {
                $element_id = $data['element_id'];
                $signature_hash = md5($data['value']);
                //encode the long query string for more readibility
                $q_string = base64_encode("form_id={$form_id}&id={$entry_id}&el=element_{$element_id}&hash={$signature_hash}");
                if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
                    $ssl_suffix = 's';
                } else {
                    $ssl_suffix = '';
                }
                if (!empty($email_param['machform_base_path'])) {
                    //if the form is called from advanced form code
                    $data['value'] = '<a href="' . $email_param['machform_base_path'] . 'signature.php?q=' . $q_string . '">View Signature</a>';
                } else {
                    $data['value'] = '<a href="http' . $ssl_suffix . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/signature.php?q=' . $q_string . '">View Signature</a>';
                }
                //construct template variables
                $template_variables[$i] = '{element_' . $data['element_id'] . '}';
                $template_values[$i] = $data['value'];
                $i++;
            }
            if ($data['element_type'] == 'section') {
                if (!empty($data['label']) && !empty($data['value']) && $data['value'] != '&nbsp;') {
                    $section_separator = '<br/>';
                } else {
                    $section_separator = '';
                }
                $section_break_content = '<span><strong>' . nl2br($data['label']) . '</strong></span>' . $section_separator . '<span>' . nl2br($data['value']) . '</span>';
                $email_body .= "<tr {$row_style}>\n";
                $email_body .= '<td width="100%" colspan="2" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;">' . $section_break_content . '</td>' . "\n";
                $email_body .= '</tr>' . "\n";
            } else {
                $email_body .= "<tr {$row_style}>\n";
                $email_body .= '<td width="40%" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;"><strong>' . $data['label'] . '</strong></td>' . "\n";
                $email_body .= '<td width="60%" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;">' . $data['value'] . '</td>' . "\n";
                $email_body .= '</tr>' . "\n";
            }
            $i++;
        }
        $email_body .= "</table>\n";
    } else {
        $money_symbols = array('&#165;', '&#163;', '&#8364;', '&#3647;', '&#75;&#269;', '&#122;&#322;', '&#65020;');
        $money_plain = array('¥', '£', '€', '฿', 'Kč', 'zł', '﷼');
        //compose text format
        foreach ($entry_details as $data) {
            $data['value'] = htmlspecialchars_decode($data['value'], ENT_QUOTES);
            //0 should be displayed, empty string don't
            if ((empty($data['value']) || $data['value'] == '&nbsp;') && $data['value'] !== 0 && $data['value'] !== '0') {
                continue;
            }
            //skip pagebreak
            if ($data['label'] == 'mf_page_break' && $data['value'] == 'mf_page_break') {
                continue;
            }
            $data['value'] = str_replace('<br />', "\n", $data['value']);
            if ($data['element_type'] == 'textarea' || $data['element_type'] == 'matrix') {
                $data['value'] = trim($data['value'], "\n");
                $email_body .= "{$data['label']}: \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'section') {
                $data['value'] = trim($data['value'], "\n");
                $email_body .= "{$data['label']} \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'checkbox' || $data['element_type'] == 'address') {
                $email_body .= "{$data['label']}: \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'file') {
                $data['value'] = strip_tags($data['value']);
                $data['value'] = str_replace('&nbsp;', "\n- ", $data['value']);
                $email_body .= "{$data['label']}: {$data['value']}\n";
                //if there is file to be attached
                if (!empty($data['filedata'])) {
                    foreach ($data['filedata'] as $file_info) {
                        $files_to_attach[$j]['filename_path'] = $file_info['filename_path'];
                        $files_to_attach[$j]['filename_value'] = $file_info['filename_value'];
                        $j++;
                    }
                }
            } elseif ($data['element_type'] == 'money') {
                $data['value'] = str_replace($money_symbols, $money_plain, $data['value']);
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            } elseif ($data['element_type'] == 'url') {
                $data['value'] = strip_tags($data['value']);
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            } elseif ($data['element_type'] == 'signature') {
                $element_id = $data['element_id'];
                $signature_hash = md5($data['value']);
                //encode the long query string for more readibility
                $q_string = base64_encode("form_id={$form_id}&id={$entry_id}&el=element_{$element_id}&hash={$signature_hash}");
                if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
                    $ssl_suffix = 's';
                } else {
                    $ssl_suffix = '';
                }
                if (!empty($email_param['machform_base_path'])) {
                    //if the form is called from advanced form code
                    $data['value'] = $email_param['machform_base_path'] . 'signature.php?q=' . $q_string;
                } else {
                    $data['value'] = 'http' . $ssl_suffix . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/signature.php?q=' . $q_string;
                }
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
                //construct template variables
                $template_variables[$i] = '{element_' . $data['element_id'] . '}';
                $template_values[$i] = $data['value'];
                $i++;
            } else {
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            }
        }
    }
    $i = count($template_variables);
    $template_variables[$i] = '{entry_data}';
    $template_values[$i] = $email_body;
    //create the mail transport
    if (!empty($mf_settings['smtp_enable'])) {
        $s_transport = Swift_SmtpTransport::newInstance($mf_settings['smtp_host'], $mf_settings['smtp_port']);
        if (!empty($mf_settings['smtp_secure'])) {
            $s_transport->setEncryption('tls');
        }
        if (!empty($mf_settings['smtp_auth'])) {
            $s_transport->setUsername($mf_settings['smtp_username']);
            $s_transport->setPassword($mf_settings['smtp_password']);
        }
    } else {
        $s_transport = Swift_MailTransport::newInstance();
        //use PHP mail() transport
    }
    //create mailer instance
    $s_mailer = Swift_Mailer::newInstance($s_transport);
    if (file_exists($mf_settings['upload_dir'] . "/form_{$form_id}/files") && is_writable($mf_settings['upload_dir'] . "/form_{$form_id}/files")) {
        Swift_Preferences::getInstance()->setCacheType('disk')->setTempDir($mf_settings['upload_dir'] . "/form_{$form_id}/files");
    } else {
        Swift_Preferences::getInstance()->setCacheType('array');
    }
    //create the message
    //parse from_name template
    if (!empty($from_name)) {
        $from_name = str_replace($template_variables, $template_values, $from_name);
        $from_name = str_replace('&nbsp;', '', $from_name);
        //decode any html entity
        $from_name = html_entity_decode($from_name, ENT_QUOTES);
        if (empty($from_name)) {
            if (!empty($mf_settings['default_from_name'])) {
                $from_name = $mf_settings['default_from_name'];
            } else {
                $from_name = 'MachForm';
            }
        }
    } else {
        if (!empty($mf_settings['default_from_name'])) {
            $from_name = $mf_settings['default_from_name'];
        } else {
            $from_name = 'MachForm';
        }
    }
    //parse from_email_address template
    if (!empty($from_email)) {
        $from_email = str_replace($template_variables, $template_values, $from_email);
        if (empty($from_email)) {
            if (!empty($mf_settings['default_from_email'])) {
                $from_email = $mf_settings['default_from_email'];
            } else {
                $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
                $from_email = "no-reply@{$domain}";
            }
        }
    } else {
        if (!empty($mf_settings['default_from_email'])) {
            $from_email = $mf_settings['default_from_email'];
        } else {
            $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
            $from_email = "no-reply@{$domain}";
        }
    }
    //parse subject template
    if (!empty($subject)) {
        $subject = str_replace($template_variables, $template_values, $subject);
        $subject = str_replace('&nbsp;', '', $subject);
    } else {
        if ($target_is_admin) {
            $subject = utf8_encode("{$form_name} [#{$entry_id}]");
        } else {
            $subject = utf8_encode("{$form_name} - Receipt");
        }
    }
    //decode any html entity
    $subject = html_entity_decode($subject, ENT_QUOTES);
    //parse content template
    $email_content = str_replace($template_variables, $template_values, $content);
    if (!$as_plain_text) {
        //html type
        //add footer
        if (empty($mf_settings['disable_machform_link'])) {
            $email_content .= "<br /><br /><br /><br /><br /><b style=\"font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px\">Powered by <a href=\"http://www.appnitro.com\">MachForm</a></b>";
        }
        //enclose with container div
        $email_content = '<div style="font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px">' . $email_content . '</div>';
    }
    $to_emails = str_replace('&nbsp;', '', str_replace($template_variables, $template_values, $to_emails));
    if (!empty($to_emails)) {
        $email_address = explode(',', $to_emails);
    }
    if (!empty($email_address)) {
        if (!$as_plain_text) {
            $email_content_type = 'text/html';
        } else {
            $email_content_type = 'text/plain';
        }
        //check for hook file (currently being used to set the destination email based on dropdown/radio button/checkboxes selection)
        if ($check_hook_file === true) {
            $hook_emails = $mf_hook_emails[$form_id];
            if (!empty($hook_emails)) {
                $hook_element_id = $hook_emails['element_id'];
                //get the field type of this element_id
                $query = "select element_type from " . MF_TABLE_PREFIX . "form_elements where form_id=? and element_id=? and element_status=1";
                $params = array($form_id, $hook_element_id);
                $sth = mf_do_query($query, $params, $dbh);
                $row = mf_do_fetch_result($sth);
                if ($row['element_type'] == 'checkbox') {
                    //get all selected checkboxes
                    $query = "select \r\n\t\t\t\t\t\t\t\t\t\toption_id,\r\n\t\t\t\t\t\t\t\t\t\t`option` option_title \r\n\t\t\t\t\t\t\t\t\tfrom \r\n\t\t\t\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "element_options \r\n\t\t\t\t\t\t\t\t   where \r\n\t\t\t\t\t\t\t\t   \t\tform_id=? and element_id=? and live=1 \r\n\t\t\t\t\t\t\t\torder by \r\n\t\t\t\t\t\t\t\t\t\toption_id asc";
                    $params = array($form_id, $hook_element_id);
                    $sth = mf_do_query($query, $params, $dbh);
                    $checkbox_element_names_array = array();
                    while ($row = mf_do_fetch_result($sth)) {
                        $checkbox_hook_lookup[$row['option_id']] = $row['option_title'];
                        $checkbox_element_names_array[] = 'element_' . $hook_element_id . '_' . $row['option_id'];
                    }
                    $checkbox_element_names_joined = implode(',', $checkbox_element_names_array);
                    $query = "select {$checkbox_element_names_joined} from " . MF_TABLE_PREFIX . "form_{$form_id} where `id`=?";
                    $params = array($entry_id);
                    $sth = mf_do_query($query, $params, $dbh);
                    $row = mf_do_fetch_result($sth);
                    $selected_checkbox_array = array();
                    foreach ($checkbox_hook_lookup as $option_id => $option_title) {
                        if (!empty($row['element_' . $hook_element_id . '_' . $option_id])) {
                            $selected_checkbox_array[] = $option_title;
                        }
                    }
                    if (!empty($selected_checkbox_array)) {
                        $email_address = array();
                        foreach ($selected_checkbox_array as $selected_option_title) {
                            $selected_hook_email = $mf_hook_emails[$form_id][$selected_option_title];
                            if (!empty($selected_hook_email)) {
                                $temp_email_address = explode(",", $selected_hook_email);
                            }
                            $email_address = array_merge($email_address, (array) $temp_email_address);
                        }
                    }
                } else {
                    $query = "select \r\n\t\t\t\t\t\t\t\t\t\tB.`option` selected_value \r\n\t\t\t\t\t\t\t\t\tfrom \r\n\t\t\t\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_{$form_id} A left join " . MF_TABLE_PREFIX . "element_options B \r\n\t\t\t\t\t\t\t\t\t  on \r\n\t\t\t\t\t\t\t\t\t    B.form_id=? and A.element_{$hook_element_id}=B.option_id and B.live=1 and B.element_id=?\r\n\t\t\t\t\t\t\t\t\twhere \r\n\t\t\t\t\t\t\t\t\t\tA.`id`=?";
                    $params = array($form_id, $hook_element_id, $entry_id);
                    $sth = mf_do_query($query, $params, $dbh);
                    $row = mf_do_fetch_result($sth);
                    $selected_value = $row['selected_value'];
                    $selected_hook_email = $mf_hook_emails[$form_id][$selected_value];
                    if (!empty($selected_hook_email)) {
                        $email_address = explode(",", $selected_hook_email);
                    }
                }
            }
        }
        array_walk($email_address, 'mf_trim_value');
        $s_message = Swift_Message::newInstance()->setCharset('utf-8')->setMaxLineLength(1000)->setSubject($subject)->setFrom(array($from_email => $from_name))->setSender($from_email)->setReturnPath($from_email)->setTo($email_address)->setBody($email_content, $email_content_type);
        //attach files, if any
        if (!empty($files_to_attach)) {
            foreach ($files_to_attach as $file_data) {
                $s_message->attach(Swift_Attachment::fromPath($file_data['filename_path'])->setFilename($file_data['filename_value']));
            }
        }
        //send the message
        $send_result = $s_mailer->send($s_message);
        if (empty($send_result)) {
            echo "Error sending email!";
        }
    }
}