<?php
// This file will be updated once we release the API for public use.

// - ipt_fsqm_form_elements_quick_preview_email_style -
add_filter( 'ipt_fsqm_form_elements_quick_preview_email_style', 'my_email_style' );
function my_email_style( $email_style ) {
	$email_style_new = array(
		'th' => 'border: 1px solid #888; vertical-align: top; padding: 5px; text-align: left;',
		'td' => 'border: 1px solid #888; vertical-align: middle; padding: 5px; text-align: left;',
		'icons' => 'border: 1px solid #888; vertical-align: middle; text-align: center; width: 20px; padding: 5px; line-height: 1;',
		'td_center' => 'border: 1px solid #888; vertical-align: middle; text-align: center; padding: 5px;',
		'description' => 'display: block; font-size: 90%; color: #888; text-transform: lowercase; font-style: italic;',
		'table' => 'margin-top: 10px; margin: bottom: 10px; width: 100%; border-collapse: collapse; border: 1px solid #888; background-color: #fff; color: #333; vertical-align: top; text-align: left;',
		'inner_table' => 'margin: 0px; width: 100%; border-collapse: collapse; border: 0 none; background-color: #fff; color: #333; vertical-align: top; text-align: left;',
		'tr' => 'border: 1px solid #888;',
		'thead' => 'border: 1px solid #888; background: #ddd;',
		'tfoot' => 'border: 1px solid #888; background: #ddd;',
		'tbody' => 'border: 1px solid #888;',
	);
	return wp_parse_args( $email_style_new, $email_style );
}

// - ipt_fsqm_hook_save_insert -
// - ipt_fsqm_hook_save_update -
add_action( 'ipt_fsqm_hook_save_insert', 'my_save_action', 10, 1 );
add_action( 'ipt_fsqm_hook_save_update', 'my_save_action', 10, 1 );
function my_save_action( $data ) {
	// Check for validity
	if ( ! ( $data instanceof IPT_FSQM_Form_Elements_Data ) ) {
		return;
	}

	// Check if it is an update or insert
	$doing_update = $data->doing_update;

	// Do something with the mcq submission data
	foreach ( $data->data->mcq as $m_key => $mcq ) {
		// Your code
	}

	// Do something with the freetype submission data
	foreach ( $data->data->freetype as $f_key => $freetype ) {
		// Your code
	}

	// Do something with the other elements submission data
	foreach ( $data->data->pinfo as $p_key => $pinfo ) {
		// Your code
	}

	// Other informative variables
	$user_meta = array(
		'f_name' => $data->data->f_name,
		'l_name' => $data->data->l_name,
		'email' => $data->data->email,
		'phone' => $data->data->phone,
		'score' => $data->data->score,
		'max_score' => $data->data->max_score,
		'user_id' => $data->data->user_id, // 0 when it is a gues (not logged in)
		'admin_remarks' => $data->data->comment,
		'date' => $data->data->date,
		'ip' => $data->data->ip,
	);

	// For accessing form related data
	// Please use the following member variables
	// $data->layout, $data->design, $data->mcq, $data->freetype, $data->pinfo
	// $data->settings, $data->name, $data->type
}

// jQuery related fixes for your theme
//
// Solution #1
// Remove code like this (just the deregister and register)
function modify_jquery() {
	if (!is_admin()) {
		wp_deregister_script('jquery');
		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js', false, '1.8.1');
		wp_enqueue_script('jquery');
	}
}
add_action('init', 'modify_jquery');

// Instead use this
function add_jquery() {
	if (!is_admin()) {
		wp_enqueue_script('jquery');
	}
}
add_action('init', 'add_jquery');

// Solution #2
// Remove code like this
function add_jquery_ui() {
	wp_enqueue_script('jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js', false, '1.8.8');
}
add_action('init', 'add_jquery_ui');
// Instead add this
function add_jquery_ui() {
	wp_enqueue_script( 'jquery-ui-core' );
	wp_enqueue_script( 'jquery-ui-widget' );
	wp_enqueue_script( 'jquery-ui-mouse' );
	wp_enqueue_script( 'jquery-ui-accordion' );
	wp_enqueue_script( 'jquery-ui-autocomplete' );
	wp_enqueue_script( 'jquery-ui-slider' );
	wp_enqueue_script( 'jquery-ui-tabs' );
	wp_enqueue_script( 'jquery-ui-sortable' );
	wp_enqueue_script( 'jquery-ui-draggable' );
	wp_enqueue_script( 'jquery-ui-droppable' );
	wp_enqueue_script( 'jquery-ui-datepicker' );
	wp_enqueue_script( 'jquery-ui-resize' );
	wp_enqueue_script( 'jquery-ui-dialog' );
	wp_enqueue_script( 'jquery-ui-button' );
}
add_action('init', 'add_jquery_ui');
