$transaction->commit();
             break;
         case 'remove':
             if (!array_key_exists('id', $_REQUEST) || !trim($_REQUEST['id'])) {
                 throw new Exception("Missing required parameter transaction id");
             }
             $transaction = new Transaction(new MySqlDB());
             $transaction->start();
             $activity = Factory::getView(new ActivityKey($_REQUEST['id']));
             $activity->requestDelete();
             $transaction->commit();
             break;
         case 'create':
             $transaction = new Transaction(new MySqlDB());
             $transaction->start();
             $activity = Factory::createView(new ActivityKey());
             $activity->setUser($user);
             $activity->setImportNumber(-1);
             $activity->setImportTime(new Date());
             $activity->setName($data['name']);
             $activity->setTransactionDate(new Date($_POST['data']['transdate']));
             $activity->setAmount($data['amount']);
             if ($data['category'] != null && trim($data['category']) && $data['category'] != 'null') {
                 $activity->setCategory(Factory::getView(new CategoryKey(trim($data['category']))));
             }
             $transaction->commit();
             break;
     }
     echo json_encode(array('success' => 'true'));
 } else {
     $smarty = new MySmarty($SMARTY_CONFIG);
            }
            $transactionDateCol = isset($_REQUEST['transactionDate']) ? htmlentities($_REQUEST['transactionDate']) : false;
            if ($transactionDateCol === false) {
                throw new Exception('Missing required parameter transaction date column');
            }
            $descriptionCol = isset($_REQUEST['description']) ? htmlentities($_REQUEST['description']) : false;
            if ($descriptionCol === false) {
                throw new Exception('Missing required parameter description column');
            }
            $amountCol = isset($_REQUEST['amount']) ? htmlentities($_REQUEST['amount']) : false;
            if ($amountCol === false) {
                throw new Exception('Missing required parameter amount column');
            }
            $transaction = new Transaction(new MySqlDB());
            $transaction->start();
            $mapping = Factory::createView(new MappingKey());
            $mapping->setUser($user);
            $mapping->setStartingRow($startingRow);
            $mapping->setName($name);
            $mapping->addMappingDetail(new MappingDetail($mapping, MappingDetail::$MAPPING_TRANSACTION_DATE, $transactionDateCol));
            $mapping->addMappingDetail(new MappingDetail($mapping, MappingDetail::$MAPPING_AMOUNT, $amountCol));
            $mapping->addMappingDetail(new MappingDetail($mapping, MappingDetail::$MAPPING_NAME, $descriptionCol));
            $transaction->commit();
        } else {
            $smarty = new MySmarty($SMARTY_CONFIG);
            $smarty->assign('user', $user);
            $smarty->assign('left_menu', true);
            $smarty->display('custommapping.tpl');
        }
    }
} catch (AccessDeniedException $e) {
try {
    $fullPage = true;
    $access = new Access();
    $access->authenticate();
    $user = $access->getUser();
    $smarty = new MySmarty($SMARTY_CONFIG);
    if (!empty($_POST)) {
        $fullPage = false;
        $action = htmlentities($_POST['action']);
        if ($action === "add") {
            $categoryId = htmlentities($_POST['categoryId']);
            //TODO: check amount is valid
            $budgetedAmount = htmlentities($_POST['budgetedAmount']);
            $transaction = new Transaction(new MySqlDB());
            $transaction->start();
            $budget = Factory::createView(new BudgetKey());
            $category = Factory::getView(new CategoryKey($categoryId));
            $budget->setUser($user);
            $budget->setActive(TRUE);
            $budget->setAmount($budgetedAmount);
            $budget->setCategory($category);
            $transaction->commit();
        } elseif ($action === "remove") {
            $key = htmlentities($_POST['key']);
            $transaction = new Transaction(new MySqlDB());
            $transaction->start();
            $budgetItem = Factory::getView(new BudgetKey($key));
            $budgetItem->setActive(0);
            $transaction->commit();
        }
    }
     $smarty->display('error.tpl');
     return;
 }
 //check username is email
 if (!preg_match("/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\$/i", $username)) {
     $smarty->display('error.tpl');
     return;
 }
 //check password meets standards
 if (!preg_match("/((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9\\s]).{8,})/", $password)) {
     $smarty->display('error.tpl');
     return;
 }
 $transaction = new Transaction(new MySqlDB());
 $transaction->start();
 $user = Factory::createView(new UserKey());
 $date = new Date();
 $user->setUsername($username);
 $user->setPassword($password);
 $user->setActive(true);
 $user->setCreatedDate($date->getTimestamp());
 $user->setLastLoginDate($date->getTimestamp());
 $user->setNumberOfAttempts(1);
 $user->setFirstName($firstname);
 $user->setLastName($lastname);
 $transaction->commit();
 $access = new Access();
 if ($access->authenticate($username, $password)) {
     header("Location: dashboard.php");
     exit;
 } else {
if (isset($_POST['submit'])) {
    try {
        $name = isset($_REQUEST['name']) ? htmlentities($_REQUEST['name']) : false;
        $email = isset($_REQUEST['email']) && preg_match("/^[_a-z0-9]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})\$/i", $_REQUEST['email']) ? htmlentities($_REQUEST['email']) : false;
        $message = isset($_REQUEST['message']) ? htmlentities($_REQUEST['message']) : false;
        // inserting into table contact
        $transaction = new Transaction(new MySqlDB());
        $transaction->start();
        if (!$user) {
            // User is not logged in, so trying to find user by his provided email
            $temp = User::getUserByUserName($email);
            if ($temp !== null) {
                $user = $temp;
            }
        }
        $contact = Factory::createView(new ContactKey());
        if ($user) {
            $contact->setUser($user);
        }
        $contact->setName($name);
        $contact->setEmail($email);
        $contact->setContent($message);
        $contact->setDateSubmitted(new Date());
        $transaction->commit();
        $smarty->assign('message', 'Message sent to iBudget successfully.');
    } catch (Exception $e) {
        if ($transaction && !$transaction->isComplete()) {
            $transaction->rollBack();
        }
        $smarty->assign('message', 'An error had occurred, please try again in a few minutes.');
        // echo "<PRE>A error had occured: " . $e->getMessage() . "\n" . $e->getTraceAsString() . "\n</PRE>";
require_once 'activity.inc';
require_once 'date.inc';
try {
    $fullPage = true;
    $access = new Access();
    $access->authenticate();
    $user = $access->getUser();
    $smarty = new MySmarty($SMARTY_CONFIG);
    if (!empty($_POST)) {
        $fullPage = false;
        $action = htmlentities($_POST['action']);
        if ($action === "add") {
            $categoryName = htmlentities($_POST['categoryName']);
            $transaction = new Transaction(new MySqlDB());
            $transaction->start();
            $category = Factory::createView(new CategoryKey());
            $category->setUser($user);
            $category->setActive(TRUE);
            $category->setName($categoryName);
            $transaction->commit();
        } elseif ($action === "remove") {
            try {
                $key = htmlentities($_POST['key']);
                $transaction = new Transaction(new MySqlDB());
                $transaction->start();
                $categoryItem = Factory::getView(new CategoryKey($key));
                $categoryItem->setActive(0);
                $transaction->commit();
            } catch (Exception $e) {
                print_r($e);
            }