<?php
session_start();
if (!isset($_SESSION['FirstName'])) {
	exit;
}

include ('includes/db1config.php');
include ('../includes/mysql_connect.php');

if (isset($_POST) && !empty($_POST)) {

	//tO update sequence of widgets
	if (isset($_POST['updateArray']) && !empty($_POST['updateArray'])) {
		
		$i = 1;
		foreach ($_POST['updateArray'] as $index => $id) {

			$sql = "UPDATE widget_fields
					SET sequence = '".$index."'
					WHERE recordkey = '".mysql_real_escape_string($id)."'";
			mysql_query($sql) or die(mysql_error());
			$i++;
		}
		die('1');
	}

	// to save newly added widgets fields
	if (isset($_POST['fieldType']) && !empty($_POST['fieldType'])) {

			$fieldType = $_POST['fieldType']; 
			$widgetId =  $_POST['widgetId'];
			$sql = 'INSERT into widget_fields
					(field_type,widget_id)
					values ("'.$fieldType.'","'.$widgetId.'")';
			mysql_query($sql) or die(mysql_error());
			echo mysql_insert_id();die;
			
	}

	// to delete  widgets field
	if (isset($_POST['deleteFieldKey']) && !empty($_POST['deleteFieldKey'])) {

			$key = $_POST['deleteFieldKey'];
			$sql = 'DELETE FROM widget_fields
					where recordkey = "'.$key.'"';
			mysql_query($sql) or die(mysql_error());
			die('1');
	}
} 

die('0');
?>