[
'form_id' => 'fasoon_form1',
'partner_class' => 'fasoon_form1_partners',
'entry_type_id' => 'entry_type',
'backing_type_id' => 'backing_type',
'summary_title_class' => 'fasoon_form1_summary_title',
'summary_price_class' => 'fasoon_form1_summary_price',
'summary_partners_class' => 'fasoon_form1_summary_partners',
'summary_partner_tpl_class' => 'fasoon_form1_summary_partner_tpl',
'summary_total_class' => 'fasoon_form1_summary_total',
],
'step2' => [
'form_id' => 'fasoon_form2',
'phone_id' => 'phone',
'plugins_url' => null,
],
'step3' => [
'form_id' => 'fasoon_form3',
'plugins_url' => null,
],
'step4' => [
'form_id' => 'fasoon_form4',
'plugins_url' => null,
'save_action' => 'fqp_step4_save_ajax',
'ajax_url' => null,
'persons_class' => 'indivuduals',
'add_person_class' => 'add_person',
'add_company_class' => 'add_company',
'new_person_form_class' => 'new_person_form',
'new_company_form_class' => 'new_company_form',
'load_contact_data_id' => 'load_contact_data',
],
'step5' => [
'form_id' => 'fasoon_form5',
],
'step6' => [
'form_id' => 'fasoon_form6',
],
];
public function __construct() {
add_shortcode( 'fasoon_quote_price', [ $this, 'exec' ] );
// init action
add_filter( 'admin_post_nopriv_fqp_init', [ $this, 'exec_init' ] );
add_filter( 'admin_post_fqp_init', [ $this, 'exec_init' ] );
add_filter( 'admin_post_nopriv_fqp_step1_save', [ $this, 'exec_step1_save' ] );
add_filter( 'admin_post_fqp_step1_save', [ $this, 'exec_step1_save' ] );
add_filter( 'admin_post_nopriv_fqp_step2_save', [ $this, 'exec_step2_save' ] );
add_filter( 'admin_post_fqp_step2_save', [ $this, 'exec_step2_save' ] );
add_filter( 'admin_post_nopriv_fqp_step3_save', [ $this, 'exec_step3_save' ] );
add_filter( 'admin_post_fqp_step3_save', [ $this, 'exec_step3_save' ] );
add_filter( 'admin_post_nopriv_fqp_step4_save', [ $this, 'exec_step4_save' ] );
add_filter( 'admin_post_fqp_step4_save', [ $this, 'exec_step4_save' ] );
if ( wp_doing_ajax() ) {
add_filter( "wp_ajax_nopriv_{$this->_template_vars['step4']['save_action']}", [ $this, 'exec_step4_save_ajax' ] );
add_filter( "wp_ajax_{$this->_template_vars['step4']['save_action']}", [ $this, 'exec_step4_save_ajax' ] );
}
add_filter( 'admin_post_nopriv_fqp_step5_save', [ $this, 'exec_step5_save' ] );
add_filter( 'admin_post_fqp_step5_save', [ $this, 'exec_step5_save' ] );
add_filter( 'admin_post_nopriv_fqp_step6_save', [ $this, 'exec_step6_save' ] );
add_filter( 'admin_post_fqp_step6_save', [ $this, 'exec_step6_save' ] );
add_filter( 'admin_post_nopriv_fqp_send', [ $this, 'exec_send' ] );
add_filter( 'admin_post_fqp_send', [ $this, 'exec_send' ] );
add_filter( 'admin_post_nopriv_fqp_cron_job', [ $this, 'exec_cron_job' ] );
add_filter( 'admin_post_fqp_cron_job', [ $this, 'exec_cron_job' ] );
}
protected function _get_method( $action ) {
return 'exec_' . strtolower($action);
}
protected function _get_template_vars( $action ) {
$vars = [];
if ( array_key_exists( $action, $this->_template_vars ) ) {
$vars = $this->_template_vars[$action];
$vars['plugins_url'] = plugins_url( '', FQP_PLUGIN_BASE_NAME );
$vars['currency'] = FQP\FQP::currency();
}
return $vars;
}
protected function _get_template_options() {
$options = parent::_get_template_options();
$options['page_url'] = self::get_page_url();
$options['step'] = $this->_action;
$options['data'] = $this->_session( $this->_action ) ?: [];
$options['all_data'] = $this->_get_all_data();
$options['tpl_vars'] = $this->_get_template_vars( $this->_action );
$options['form_errors'] = $this->_session( 'form_errors' );
$this->_session( 'form_errors', null );
return $options;
}
protected function _get_all_data() {
$data = [];
foreach ( array_keys( $this->_template_vars ) as $step ) {
$data[$step] = $this->_session( $step ) ?: [];
}
return $data;
}
protected function _get_post_data() {
$data = [];
$request_method = strtolower( $_SERVER['REQUEST_METHOD'] );
if ( 'post' == $request_method ) {
$data = $_POST;
} elseif ( 'get' == $request_method ) {
$data = $_GET;
} else {
wp_die( 'Wrong Request Method', __METHOD__ );
}
$data = wp_unslash( $data );
return $data;
}
public function exec() {
$action = @$_REQUEST['action'];
if ( $action ) {
if ( 0 === strpos( $action, 'fqp_' ) ) {
$this->_action = substr( $action, 4 );
} else {
$this->_action = null;
}
} else {
$this->_action = $this->_default_action;
}
if ( ! $this->_action ) {
return '';
}
$method = $this->_get_method( $this->_action );
if ( ! method_exists( __CLASS__, $method ) ) {
wp_die( __METHOD__ . ': Something went wrong!' );
}
if ( ! wp_doing_ajax() ) {
wp_enqueue_style( 'fasoon-jquery-ui', plugins_url( '/css/jquery-ui.css', FQP_PLUGIN_BASE_NAME ) );
wp_enqueue_style( 'fasoon-jquery-ui-structure', plugins_url( '/css/jquery-ui.structure.css', FQP_PLUGIN_BASE_NAME ) );
wp_enqueue_style( 'fasoon-jquery-ui-theme', plugins_url( '/css/jquery-ui.theme.css', FQP_PLUGIN_BASE_NAME ) );
//wp_enqueue_style( 'fasoon-jquery-ui-theme', '//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css' );
wp_enqueue_script( 'fasoon_quote_tooltip', plugins_url( '/js/fasoon-quote-tooltip.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery-ui-tooltip' ] );
wp_enqueue_script( 'fasoon_quote_form_background', plugins_url( '/js/fasoon-quote-form-background.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
//wp_enqueue_script( 'fasoon_quote_form_placeholder', plugins_url( '/js/fasoon-quote-form-placeholder.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
//add_action( 'wp_footer', function() { echo '
'; } );
}
return $this->{$method}();
}
public function exec_init() {
$data = $this->_get_post_data();
if ( !empty( $data ) ) {
FQP\Form\Step1::sanitize_data( $data );
$this->_session( 'step1', $data );
}
$this->_redirect_to( 'step1' );
}
/**
* @param string|FQP\FormAbstract $form
*/
protected function _set_form_entry_type( $form ) {
$step1 = $this->_session( 'step1' );
if ( empty( $step1 ) || empty( $step1['entry_type'] ) ) {
$this->_redirect_to( 'step1' );
}
$form::set_entry_type( $step1['entry_type'] );
if ( ! empty( $step1['backing_type'] ) ) {
$form::set_backing_type( $step1['backing_type'] );
}
}
public function exec_step1() {
wp_enqueue_style( 'swiper', plugins_url( '/css/swiper/swiper.min.css', FQP_PLUGIN_BASE_NAME ) );
wp_enqueue_script( 'swiper', plugins_url( '/js/swiper/swiper.min.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
wp_enqueue_script( 'fasoon_quote_step1', plugins_url( '/js/fasoon-quote-step1.js', FQP_PLUGIN_BASE_NAME ), [ 'underscore', 'swiper' ] );
$script_options = json_encode($this->_get_template_vars( $this->_action ));
wp_add_inline_script( 'fasoon_quote_step1', "jQuery(document).ready(function() { new fasoon.QuotePriceStep1({$script_options}); })" );
$this->_template = 'quote-step1.php';
return parent::exec();
}
public function exec_step1_save() {
$data = $this->_get_post_data();
if ( true === ( $errors = FQP\Form\Step1::sanitize_data( $data ) ) ) {
$this->_session( 'step1', $data );
$this->_session( 'form_errors', null );
$this->_redirect_to( 'step2' );
} else {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( 'step1' );
}
}
public function exec_step2() {
$this->_set_form_entry_type( 'FQP\Form\Step2' );
wp_enqueue_style('intl-tel-input', plugins_url('/css/intlTelInput.css', FQP_PLUGIN_BASE_NAME));
wp_enqueue_script('intl-tel-input', plugins_url('/js/intl-tel-input/intlTelInput.js', FQP_PLUGIN_BASE_NAME), ['jquery']);
wp_enqueue_script( 'fasoon_quote_step2', plugins_url( '/js/fasoon-quote-step2.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
$script_options = json_encode($this->_get_template_vars( $this->_action ));
wp_add_inline_script( 'fasoon_quote_step2', "jQuery(document).ready(function() { new fasoon.QuotePriceStep2({$script_options}); })" );
$this->_template = 'quote-step2.php';
return parent::exec();
}
public function exec_step2_save() {
$this->_set_form_entry_type( 'FQP\Form\Step2' );
$data = $this->_get_post_data();
$this->_session( 'step2', $data );
if ( true === ( $errors = FQP\Form\Step2::sanitize_data( $data ) ) ) {
$this->_session( 'form_errors', null );
if ( FQP\FQP::is_short_forms() ) {
$this->_redirect_to( 'send' );
} else {
$this->_redirect_to( 'step3' );
}
} else {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( 'step2' );
}
}
public function exec_step3() {
$this->_set_form_entry_type( 'FQP\Form\Step3' );
wp_enqueue_script( 'fasoon_quote_step3', plugins_url( '/js/fasoon-quote-step3.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
$script_options = json_encode($this->_get_template_vars( $this->_action ));
wp_add_inline_script( 'fasoon_quote_step3', "jQuery(document).ready(function() { new fasoon.QuotePriceStep3({$script_options}); })" );
$this->_template = 'quote-step3.php';
return parent::exec();
}
public function exec_step3_save() {
$this->_set_form_entry_type( 'FQP\Form\Step3' );
$data = $this->_get_post_data();
$this->_session( 'step3', $data );
if ( true === ( $errors = FQP\Form\Step3::sanitize_data( $data ) ) ) {
$this->_session( 'form_errors', null );
$this->_redirect_to( 'step4' );
} else {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( 'step3' );
}
}
public function exec_step4() {
$this->_set_form_entry_type( 'FQP\Form\Step4' );
wp_enqueue_script(
'fasoon_quote_step4',
plugins_url( '/js/fasoon-quote-step4.js', FQP_PLUGIN_BASE_NAME ),
[ 'jquery', 'jquery-serialize-object' ]
);
if ( 'de' == FQP\FQP::get_curr_lang() ) {
wp_enqueue_script(
'fasoon_quote_datepicker_de',
plugins_url( '/js/jquery-ui/datepicker-de.js', FQP_PLUGIN_BASE_NAME ),
[ 'jquery-ui-datepicker' ]
);
}
wp_enqueue_script(
'fasoon_quote_datepicker',
plugins_url( '/js/fasoon-quote-datepicker.js', FQP_PLUGIN_BASE_NAME ),
[ 'jquery-ui-datepicker' ]
);
$script_options = $this->_get_template_vars( $this->_action );
$script_options['ajax_url'] = self::get_form_ajax_action();
$script_options['persons_data'] = $this->_session( 'step4' );
$contact_data = (array) $this->_session( 'step2' );
$fields = array_keys( FQP\Form\Step4::get_fields( 'person' ) );
foreach ( array_keys( $contact_data ) as $key ) {
if ( ! in_array( $key, $fields ) ) {
unset( $contact_data[$key] );
}
}
$script_options['contact_data'] = $contact_data;
$script_options['contact_data_confirmation'] = fqp__( 'Do you really wont override entered data?' );
$script_options = json_encode( $script_options );
wp_add_inline_script(
'fasoon_quote_step4',
"jQuery(document).ready(function() { new fasoon.QuotePriceStep4({$script_options}); })"
);
$this->_template = 'quote-step4.php';
return parent::exec();
}
public function exec_step4_save() {
$this->_set_form_entry_type( 'FQP\Form\Step4' );
$data = $this->_session( 'step4' );
$data = array_filter($data);
if ( empty( $data ) ) {
/*$data = [
'_completed' => true, // треба для того щоб визначити чи юзер вже пройшов цей крок
];
$this->_session( 'step4', $data );
$this->_redirect_to( 'step5' );*/
$this->_session( 'form_errors', [ fqp__( 'Please add at least one person' ) ] );
$this->_redirect_to( 'step4' );
} else {
$this->_session( 'form_errors', null );
$this->_redirect_to( 'step5' );
}
}
public function exec_step4_save_ajax() {
$this->_set_form_entry_type( 'FQP\Form\Step4' );
$data = $this->_get_post_data();
$result = [];
if ( ! wp_verify_nonce( $data['_wpnonce'], "{$this->_template_vars['step4']['save_action']}_{$data['person_type']}" ) ) {
$result['is_success'] = false;
$result['wrong_nonce'] = true;
die( json_encode( $result ) );
}
$sub_action = @$data['sub_action'] ?: 'save';
switch ( $sub_action ) {
case 'save':
$result = $this->_step4_save_person( $data );
break;
case 'delete':
$result = $this->_step4_delete_person( $data );
break;
default:
$result['is_success'] = false;
$result['error'] = 'Unknown subaction!';
}
$this->_display_json( $result );
}
protected function _clear_person_data( &$data ) {
unset(
$data['_wpnonce'],
$data['_wp_http_referer'],
$data['id'],
$data['action'],
$data['sub_action']
);
}
protected function _step4_save_person( $data ) {
if ( true !== ( $errors = FQP\Form\Step4::sanitize_data( $data ) ) ) {
$result['is_success'] = false;
array_walk( $errors, function ( &$val ) {
$val = fqp_esc_html__( $val );
} );
$result['error'] = implode( '
', $errors );
return $result;
}
$session_data = $this->_session( 'step4' );
$type_data = ( ! empty( $session_data[$data['person_type']] ) ) ? $session_data[$data['person_type']] : [];
$id = @$data['id'];
$this->_clear_person_data( $data );
if ($id) {
if ( array_key_exists( $id, $type_data ) ) {
$type_data[$id] = $data;
}
} else {
$id = md5( json_encode( $data ) . microtime( true ) );
$type_data[$id] = $data;
}
$session_data[$data['person_type']] = $type_data;
$this->_session( 'step4', $session_data );
$entry_type = $this->_session( 'step1' )['entry_type'];
$result['is_success'] = true;
$result['persons_html'] = static::step4_render_list( $session_data, $entry_type );
$result['persons_data'] = $session_data;
return $result;
}
protected function _step4_delete_person( $data ) {
$id = $data['id'];
$type = $data['person_type'];
$session_data = $this->_session( 'step4' );
$type_data = ( ! empty( $session_data[$type] ) ) ? $session_data[$type] : [];
$this->_clear_person_data( $data );
if ( ! empty( $type_data[$id] ) && $type_data[$id] == $data ) {
unset( $type_data[$id] );
}
$session_data[$type] = $type_data;
$this->_session( 'step4', $session_data );
$entry_type = $this->_session( 'step1' )['entry_type'];
$result['is_success'] = true;
$result['persons_html'] = static::step4_render_list( $session_data, $entry_type );
$result['persons_data'] = $session_data;
return $result;
}
public static function step4_render_list( $data, $entry_type ) {
if ( empty( $data ) || ! $entry_type ) {
return '';
}
FQP\Form\Step4::filter_data( $data );
ob_start();
include FQP_PLUGIN_DIR_PATH . 'templates/quote-step4-persons-list.php';
$output = ob_get_contents();
ob_end_clean();
return $output;
}
public function exec_step5() {
$this->_set_form_entry_type( 'FQP\Form\Step5' );
if ( ! in_array( FQP\Form\Step5::get_entry_type(), [ FQP\FQP::ENTRY_TYPE_AG, FQP\FQP::ENTRY_TYPE_LTD, FQP\FQP::ENTRY_TYPE_COLLECTIVE_COMPANY ] ) ) {
$this->_redirect_to( 'step6' );
}
wp_enqueue_script( 'auto_numeric', plugins_url( '/js/autoNumeric/autoNumeric-min.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
wp_enqueue_script( 'fasoon_quote_step5', plugins_url( '/js/fasoon-quote-step5.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
$script_options = $this->_get_template_vars( $this->_action );
$script_options['shares_number_id'] = FQP\Form\Step5::get_html_id( '_shares_number' );
$script_options['equity_capital_id'] = FQP\Form\Step5::get_html_id( 'equity_capital' );
$script_options['share_price_id'] = FQP\Form\Step5::get_html_id( 'share_price' );
if ( FQP\FQP::ENTRY_TYPE_LTD == FQP\Form\Step5::get_entry_type() ) {
$script_options['share_text'] = [
'singular' => fqp__( 'EN Stammanteile' ),
'plural' => fqp__( 'EN Stammanteile' ),
];
} else {
$script_options['share_text'] = [
'singular' => fqp__( 'EN Aktie' ),
'plural' => fqp__( 'EN Aktien' ),
];
}
$script_options = json_encode( $script_options );
wp_add_inline_script( 'fasoon_quote_step5', "jQuery(document).ready(function() { new fasoon.QuotePriceStep5({$script_options}); })" );
$this->_template = 'quote-step5.php';
return parent::exec();
}
public function exec_step5_save() {
$this->_set_form_entry_type( 'FQP\Form\Step5' );
$data = $this->_get_post_data();
$this->_session( 'step5', $data );
$persons = $this->_session( 'step4' );
if ( true === ( $errors = FQP\Form\Step5::sanitize_data( $data, $persons ) ) ) {
$this->_session( 'step5', $data );
$this->_session( 'form_errors', null );
$this->_redirect_to( 'step6' );
} else {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( 'step5' );
}
}
public function exec_step6() {
$this->_set_form_entry_type( 'FQP\Form\Step2' );
$this->_set_form_entry_type( 'FQP\Form\Step3' );
$this->_set_form_entry_type( 'FQP\Form\Step4' );
$this->_set_form_entry_type( 'FQP\Form\Step5' );
$this->_set_form_entry_type( 'FQP\Form\Step6' );
/*wp_enqueue_script( 'fasoon_quote_step6', plugins_url( '/js/fasoon-quote-step6.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery' ] );
$script_options = $this->_get_template_vars( $this->_action );
$script_options = json_encode( $script_options );
wp_add_inline_script( 'fasoon_quote_step6', "jQuery(document).ready(function() { new fasoon.QuotePriceStep6({$script_options}); })" );*/
$this->_template = 'quote-step6.php';
return parent::exec();
}
public function exec_step6_save() {
$this->_set_form_entry_type( 'FQP\Form\Step6' );
$data = $this->_get_post_data();
$this->_session( 'step6', $data );
if ( true === ( $errors = FQP\Form\Step6::sanitize_data( $data ) ) ) {
$this->_session( 'step6', $data );
$this->_session( 'form_errors', null );
$this->_redirect_to( self::get_form_action() . '&action=fqp_send' );
} else {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( 'step6' );
}
}
public function exec_send() {
$this->_set_form_entry_type( 'FQP\Form\Step2' );
if ( FQP\FQP::is_short_forms() ) {
$this->_set_form_entry_type( 'FQP\Form\Step3' );
$this->_set_form_entry_type( 'FQP\Form\Step4' );
$this->_set_form_entry_type( 'FQP\Form\Step5' );
$this->_set_form_entry_type( 'FQP\Form\Step6' );
}
// validate & collect data
$all_data = [];
if ( FQP\FQP::is_short_forms() ) {
$steps = [ 'step1', 'step2' ];
} else {
$steps = array_keys( $this->_template_vars );
}
foreach ( $steps as $step ) {
$data = (array) $this->_session( $step );
/** @var FQP\FormAbstract $class */
$class = 'FQP\\Form\\' . ucfirst( $step );
if ( 'step4' == $step ) {
unset( $data['_completed'] );
foreach ( $data as $persons ) {
foreach ( $persons as $person ) {
/** @var FQP\Form\Step4 $class */
if ( true !== ( $errors = $class::sanitize_data( $person ) ) ) {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( $step );
}
}
}
$class::filter_data( $data );
} elseif ( 'step5' == $step ) {
/** @var FQP\Form\Step5 $class */
if ( true !== ( $errors = $class::sanitize_data( $data, $all_data['step4'] ) ) ) {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( $step );
}
$class::filter_data( $data, $all_data['step4'] );
} else {
if ( true !== ( $errors = $class::sanitize_data( $data ) ) ) {
$this->_session( 'form_errors', $errors );
$this->_redirect_to( $step );
}
$class::filter_data( $data );
}
$all_data[$step] = $data;
} // validate & collect data - end
if ( ! ( $submission_ID = $this->_save_submission( $all_data ) ) ) {
$errors = [ 'Can\'t save quote submission' ];
$this->_session( 'form_errors', $errors );
$this->_redirect_to( 'step1' );
}
$submission = DB::get_submission( $submission_ID );
if ( ! $this->_send_notifications( $submission ) ) {
$errors = [ 'Can\'t send admin notification' ];
$this->_session( 'form_errors', $errors );
$this->_redirect_to( 'step1' );
}
if ( FQP\FQP::is_zoho_export() ) {
$this->zoho_export( $submission );
}
$this->_clear_session();
$url = Settings::get_thanks_url();
if ( ! $url ) {
$url = get_site_url();
}
$this->_redirect_to( $url );
}
protected function _redirect_to( $step ) {
if ( false !== strpos( $step, 'http' ) ) {
$url = $step;
} else {
$url = self::get_page_url() . '?action=fqp_' . rawurlencode( $step );
}
if ( headers_sent() ) {
echo '';
} else {
wp_redirect( $url );
}
exit;
}
protected function _session() {
$args = func_get_args();
switch ( count( $args )) {
case 1:
return @$_SESSION[$this->_session_name][$args[0]];
break;
case 2:
$_SESSION[$this->_session_name][$args[0]] = $args[1];
return true;
break;
}
wp_die('Function params is wrong.', __METHOD__);
return false;
}
protected function _clear_session() {
$_SESSION[$this->_session_name] = [];
}
protected function _save_submission( array $data ) {
/** @var \wpdb $wpdb */
global $wpdb;
$entry_types = DB::get_entry_types();
$backing_types = DB::get_backing_types();
$prices = get_option( Settings::OPTION_PRICES );
$partners = [];
if ( ! empty( $data['step1']['partners'] ) && is_array( $data['step1']['partners'] ) ) {
$partners_list = DB::get_partners( true );
foreach ( $partners_list as $partner ) {
if ( in_array( $partner['ID'], $data['step1']['partners'] ) ) {
$partners[] = [
'submission_ID' => null,
'partner_ID' => $partner['ID'],
'title' => $partner['post_title'],
'discount' => 0,
];
}
}
}
$_data = $data['step1'] + $data['step2'];
unset( $_data['partners'] );
if ( ! empty( $data['step3'] ) ) {
$_data += $data['step3'];
}
$_data['entry_type_ID'] = $_data['entry_type'];
$_data['entry_type'] = $entry_types[$_data['entry_type_ID']]['title'];
$_data['backing_type_ID'] = $_data['backing_type'] ?: null;
if (null !== $_data['backing_type_ID']) {
$_data['backing_type'] = $backing_types[$_data['backing_type_ID']]['title'];
$_data['price'] = $prices['price'][$_data['entry_type_ID']][$_data['backing_type_ID']];
} else {
$_data['price'] = $prices['price'][$_data['entry_type_ID']];
}
if ( ! empty( $data['step5'] ) ) {
$_data += $data['step5'];
unset( $_data['person_shares'] );
}
if ( ! empty( $data['step6'] ) ) {
$_data += $data['step6'];
}
$_data['email_subscription'] = ( 'y' == @$_data['email_subscription'] ) ? 'y' : 'n';
$_data['agree'] = ( 'y' == @$_data['agree'] ) ? 'y' : 'n';
$this->clear_empty( $_data );
$result = $wpdb->insert( DB::get_table_name( 'submission' ), $_data );
if ( ! $result ) {
return $result;
}
$submission_ID = $wpdb->insert_id;
if ( $partners ) {
$partner_discount = $prices['per_partner_discount'];
$total = $price = $_data['price'];
foreach ( $partners as $partner ) {
$discount = bcdiv( bcmul( $price, $partner_discount ), 100, 2 );
if ( 1 == bccomp( $discount, $total, 2 ) ) {
$discount = $total;
}
$total = bcsub( $total, $discount, 2 );
if ( -1 == bccomp( $total, '0', 2 ) ) {
$total = '0';
}
$partner['submission_ID'] = $submission_ID;
$partner['discount'] = $discount;
$this->clear_empty( $partner );
$wpdb->insert( DB::get_table_name( 'submission_partner' ), $partner );
}
}
if ( ! empty( $data['step4'] ) ) {
$dictionary = [
'nationality' => $company_country = DB::get_nationality(),
'vr_function_in_company' => DB::get_vr_function_in_company(),
'function_in_company' => DB::get_function_in_company(),
'signature_authorization' => DB::get_signature_authorization(),
];
foreach ( $data['step4'] as $type => $persons ) {
foreach ( $persons as $person_id => $person ) {
$person['submission_ID'] = $submission_ID;
$person['type'] = $type;
$person['birth_date'] = $this->prepere_date($person['birth_date']);
if ( ! empty( $data['step5']['person_shares'] ) ) {
$person['shares_number'] = @$data['step5']['person_shares'][$person_id];
}
$dict_fields = [ 'nationality', 'vr_function_in_company', 'function_in_company', 'signature_authorization', 'company_country' ];
foreach ( $dict_fields as $field ) {
if ( array_key_exists( $field, $person ) ) {
$person["{$field}_ID"] = $person[$field];
$person[$field] = fqp_array_get(@$dictionary[$field], @$person["{$field}_ID"], '');
}
}
$this->clear_empty( $person );
$wpdb->insert( DB::get_table_name( 'submission_person' ), $person );
}
}
}
return $submission_ID;
}
protected function _send_notifications ( $submission ) {
if ( empty( $submission ) ) {
return false;
}
$options = get_option( Settings::OPTION_EMAILS );
$emails = $options['emails'];
$subject = $options['subject'];
$partners = $submission['partners'];
if ( $partners ) {
$submission['partners'] = '';
foreach ( $partners as $partner ) {
$title = fqp_esc_html__( $partner['title'] );
$discount = fqp_esc_html__( '-' . fqp_money_currency( $partner['discount'] ) );
$submission['partners'] .= "{$title} ({$discount}), ";
}
$submission['partners'] = trim( $submission['partners'], ', ' );
} else {
$submission['partners'] = '-';
}
$submission['email_subscription'] = ( 'y' == $submission['email_subscription'] ) ? 'Yes' : 'No';
$submission['terms_and_conditions_agree'] = ( 'y' == $submission['agree'] ) ? 'Yes' : 'No';
$body = nl2br( $options['body'] );
foreach ( $submission as $key => $val) {
if ( is_scalar( $val ) ) {
$val = esc_html( $val );
$body = str_replace( "{{$key}}", $val, $body );
}
}
add_filter( 'wp_mail_content_type', function () { return 'text/html'; } );
return wp_mail( $emails, $subject, $body );
}
protected function _display_json( $data ) {
die( json_encode( $data ) );
}
protected function prepere_date( $date ) {
/* dd.mm.yyyy */
$date = explode( '.', $date );
return sprintf( "%04d-%02d-%02d", $date[2], $date[1], $date[0] );
}
protected function clear_empty( array &$data ) {
foreach ( $data as $key => $val ) {
if ( null === $val || '' === $val ) {
unset( $data[$key] );
}
}
}
public function exec_cron_job() {
die( '' );
$data = FQP\DB::get_submission( 246 );
static::zoho_export( $data );
}
protected function zoho_log( $message, $is_error = false ) {
$file = WP_CONTENT_DIR . '/zoho.log';
$fh = fopen( $file, 'a' );
if ( $fh ) {
if ( ! is_array( $message ) ) {
$message = [ $message ];
}
foreach ( $message as $string ) {
if ( $string === PHP_EOL ) {
$string = '--------------------------------------------------------------------------------'
. PHP_EOL
. PHP_EOL;
} else {
$string = date('Y:m:d H:i:s - ') . ( ( $is_error ) ? 'ERROR - ' : '') . $string . PHP_EOL;
}
fwrite( $fh, $string );
}
}
fclose( $fh );
}
protected function zoho_export( $data ) {
if ( ! $data ) {
static::zoho_log( [ 'Data is empty!', PHP_EOL ], true );
return;
}
global $wpdb;
static::zoho_log( "Export Submission ID {$data['ID']}" );
try {
// перевіряємо чи є екаунт
$criteria = [ 'criteria' => "(Account Name:{$data['company_name']})" ];
$account = FQP\ZOHO::request( FQP\ZOHO::MODULE_ACCOUNTS, FQP\ZOHO::METHOD_SEARCH_RECORDS, $criteria );
if ( false !== ( $error = FQP\ZOHO::get_error_message( $account ) ) ) {
static::zoho_log( [ $error, PHP_EOL ], true );
return;
}
$account_id = null;
if ( ! empty( $account['response']['result']['Accounts']['row'] ) ) {
if ( array_key_exists( 'FL', $account['response']['result']['Accounts']['row'] ) ) {
if ( $account['response']['result']['Accounts']['row']['FL']['Account Name'] == $data['company_name'] ) {
$account_id = $account['response']['result']['Accounts']['row']['FL']['ACCOUNTID'];
}
} else {
foreach ( $account['response']['result']['Accounts']['row'] as $row ) {
if ( $row['FL']['Account Name'] == $data['company_name'] ) {
$account_id = $row['FL']['ACCOUNTID'];
break;
}
}
}
}
// якщо не знайшли екаунт, то треба створити його
if ( null === $account_id ) {
$account = FQP\ZOHO::request( FQP\ZOHO::MODULE_ACCOUNTS, FQP\ZOHO::METHOD_INSERT_RECORDS, $data );
if ( false !== ( $error = FQP\ZOHO::get_error_message( $account ) ) ) {
static::zoho_log( [ $error, PHP_EOL ], true );
return;
}
$account_id = @$account['response']['result']['recorddetail']['FL']['Id'];
if ( ! $account_id ) {
static::zoho_log( [ 'Can\'t Find Zoho Account ID: ' . print_r( $account, true ), PHP_EOL ], true );
return;
} else {
static::zoho_log( "Created Zoho Account ID: {$account_id}" );
}
} else {
static::zoho_log( "Use Zoho Account ID: {$account_id}" );
}
$wpdb->update(
DB::get_table_name('submission'),
[ 'zoho_account_id' => $account_id ],
[ 'ID' => $data['ID']]
);
// вибираємо екаунт по ID
$criteria = [ 'id' => $account_id ];
$account = FQP\ZOHO::request( FQP\ZOHO::MODULE_ACCOUNTS, FQP\ZOHO::METHOD_GET_RECORD_BY_ID, $criteria );
if ( false !== ( $error = FQP\ZOHO::get_error_message( $account ) ) ) {
static::zoho_log( [ $error, PHP_EOL ], true );
return;
}
$account_name = @$account['response']['result']['Accounts']['row']['FL']['Account Name'];
if ( ! $account_name ) {
static::zoho_log( [ 'Can\'t Find Zoho Account Name: ' . print_r( $account, true ), PHP_EOL ], true );
return;
}
$data['company_name'] = $account_name;
// перевіряємо чи є контакт
$criteria = [ 'criteria' => "(Email:{$data['email']})" ];
$contact = FQP\ZOHO::request( FQP\ZOHO::MODULE_CONTACTS, FQP\ZOHO::METHOD_SEARCH_RECORDS, $criteria );
if ( false !== ( $error = FQP\ZOHO::get_error_message( $contact ) ) ) {
static::zoho_log( [ $error, PHP_EOL ], true );
return;
}
$contact_id = null;
if ( ! empty( $contact['response']['result']['Contacts']['row'] ) ) {
if ( array_key_exists( 'FL', $contact['response']['result']['Contacts']['row'] ) ) {
if ( $contact['response']['result']['Contacts']['row']['FL']['Last Name'] == $data['last_name'] ) {
$contact_id = $contact['response']['result']['Contacts']['row']['FL']['CONTACTID'];
}
} else {
foreach ( $contact['response']['result']['Contacts']['row'] as $row ) {
if ( $row['FL']['Last Name'] == $data['last_name'] ) {
$contact_id = $row['FL']['CONTACTID'];
break;
}
}
}
}
// якщо не знайшли контакт, то треба створити його
if ( null === $contact_id ) {
$contact = FQP\ZOHO::request( FQP\ZOHO::MODULE_CONTACTS, FQP\ZOHO::METHOD_INSERT_RECORDS, $data );
if ( false !== ( $error = FQP\ZOHO::get_error_message( $contact ) ) ) {
static::zoho_log( [ $error, PHP_EOL ], true );
return;
}
$contact_id = @$contact['response']['result']['recorddetail']['FL']['Id'];
if ( ! $contact_id ) {
static::zoho_log( [ 'Can\'t Find Zoho Contact ID: ' . print_r( $contact, true ), PHP_EOL ], true );
return;
} else {
static::zoho_log( "Created Zoho Contact ID: {$contact_id}" );
}
} else {
static::zoho_log( "Use Zoho Contact ID: {$contact_id}" );
}
$wpdb->update(
DB::get_table_name('submission'),
[ 'zoho_contact_id' => $contact_id ],
[ 'ID' => $data['ID']]
);
$data['zoho_account_id'] = $account_id;
$data['zoho_contact_id'] = $contact_id;
$deal = FQP\ZOHO::request( FQP\ZOHO::MODULE_DEALS, FQP\ZOHO::METHOD_INSERT_RECORDS, $data );
if ( false !== ( $error = FQP\ZOHO::get_error_message( $deal ) ) ) {
static::zoho_log( [ $error, PHP_EOL ], true );
return;
}
$deal_id = $deal['response']['result']['recorddetail']['FL']['Id'];
if ( ! $deal_id ) {
static::zoho_log( [ 'Can\'t Find Zoho Deal ID: ' . print_r( $deal, true ), PHP_EOL ], true );
return;
} else {
static::zoho_log( "Created Zoho Deal ID: {$deal_id}" );
$wpdb->update(
DB::get_table_name('submission'),
[ 'zoho_deal_id' => $deal_id ],
[ 'ID' => $data['ID']]
);
}
static::zoho_log( PHP_EOL );
} catch (Exception $e) {
static::zoho_log( [ $e->getMessage(), PHP_EOL ], true );
}
}
}