<?php

//        # Copyright (C) 2012-2013 GNU remotecontrol authors.
//        #
//        # This program is free software: you can redistribute it and/or modify
//        # it under the terms of the GNU Affero General Public License as
//        # published by the Free Software Foundation, either version 3 of the
//        # License, or (at your option) any later version.
//        #
//        # This program is distributed in the hope that it will be useful,
//        # but WITHOUT ANY WARRANTY; without even the implied warranty of
//        # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        # GNU Affero General Public License for more details.
//        #
//        # You should have received a copy of the GNU Affero General Public License
//        # along with this program.  If not, see <http://www.gnu.org/licenses/>.

	require_once('../lib/Global.php');

	$action = intval($_POST['Action']);

	@ $userid = $_POST['UserId'];
	@ $username = $_POST['UserName'];
	@ $maxnumtherms = $_POST['MaxNumTherms'];

	$dataAccess = new MySqlDataAccess(Common::ConnectionString);
	$dataAccess->Command->CommandType = CommandType::StoredProcedure;

	switch($action)
	{
		case 1:

			$dataAccess->Command->CommandText = "CALL sp_User_Insert_Update(null, '$username', $maxnumtherms)";
			
			$userid = intval($dataAccess->Command->ExecuteScalar());

			if($userid)
			{
				printf('%s;%s;%s', $userid, $username, $maxnumtherms);
			}
			else
			{
				$error = $dataAccess->Command->Connection->error;

				printf('ERR:The following error occurred while accessing the database: %s', $error);
			}

			break;

		case 2:

			$dataAccess->Command->CommandText = "CALL sp_User_Insert_Update($userid, '$username', $maxnumtherms)";

			if($dataAccess->Command->ExecuteNonQuery())
			{
				printf('%s;%s;%s', $userid, $username, $maxnumtherms);
			}
			else
			{
				$error = $dataAccess->Command->Connection->error;

				printf('ERR:The following error occurred while accessing the database: %s', $error);
			}

			break;

		case 3:

			$dataAccess->Command->CommandText = "CALL sp_User_Delete($userid)";

			if($dataAccess->Command->ExecuteNonQuery())
			{
				print $userid;
			}
			else
			{
				$error = $dataAccess->Command->Connection->error;

				printf('ERR:The following error occurred while accessing the database: %s', $error);
			}

			break;
	}

	$dataAccess->Cleanup();

?>
