/**
  * Update an existing db entry
  * 
  * @param array $entry
  */
 function editEntry($entry)
 {
     global $wpdb;
     $ad = new Advertisment($entry['advertisment_id']);
     $ad->setName($wpdb->escape($entry['advertisment_name']));
     $ad->setVendor($wpdb->escape($entry['advertisment_vendor']));
     $ad->setHtml($entry['advertisment_code']);
     $ad->setWeight($entry['advertisment_weight']);
     $ad->setSize($entry['advertisment_size']);
     $ad->setActive(isset($entry['advertisment_active']) ? true : false);
     $ad->updateDatabase();
 }
/**
 * Manage page
 * 
 * @since 0.1
 */
function was_manage()
{
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    $ads_class = new WAS_Class();
    if (isset($_GET['post-query-submit'])) {
        $sendback = remove_query_arg(array('post-query-submit'), wp_get_referer());
        $sendback = add_query_arg(array('page' => 'was-manage'), $sendback);
        if ($_GET['vendor'] != 'all') {
            $sendback = add_query_arg(array('vendor' => $_GET['vendor']), $sendback);
        }
        if ($_GET['size'] != 'all') {
            $sendback = add_query_arg(array('size' => $_GET['size']), $sendback);
        }
        ?>
		<script type="text/javascript"> window.location='<?php 
        echo $sendback;
        ?>
'; </script>
<?php 
        exit;
    }
    if (isset($_GET['doaction']) || isset($_GET['doaction2'])) {
        $sendback = remove_query_arg(array('activated', 'deactivated', 'deleted', 'ids'), wp_get_referer());
        $sendback = add_query_arg(array('page' => 'was-manage'), $sendback);
        $doaction = $_GET['action'];
        $was_ids = isset($_GET['was-ids']) ? array_map('intval', (array) $_GET['was-ids']) : explode(',', $_GET['ids']);
        switch ($doaction) {
            case 'activate':
                $activated = 0;
                foreach ((array) $was_ids as $was_id) {
                    $ad = new Advertisment($was_id);
                    if (!$ad->setActive(true, true)) {
                        wp_die(__('Error in activating...'));
                    }
                    $activated++;
                }
                $sendback = add_query_arg(array('activated' => $activated, 'ids' => join(',', $was_ids)), $sendback);
                break;
            case 'deactivate':
                $deactivated = 0;
                foreach ((array) $was_ids as $was_id) {
                    $ad = new Advertisment($was_id);
                    if (!$ad->setActive(false, true)) {
                        wp_die(__('Error in deactivating...'));
                    }
                    $deactivated++;
                }
                $sendback = add_query_arg(array('deactivated' => $deactivated, 'ids' => join(',', $was_ids)), $sendback);
                break;
            case 'delete':
                $deleted = 0;
                foreach ((array) $was_ids as $was_id) {
                    $ad = new Advertisment($was_id);
                    if (!$ad->delete()) {
                        wp_die(__('Error in deleting...'));
                    }
                    $deleted++;
                }
                $sendback = add_query_arg(array('deleted' => $deleted, 'ids' => join(',', $was_ids)), $sendback);
                break;
        }
        if (isset($_GET['action'])) {
            $sendback = remove_query_arg(array('action', 'action2'), $sendback);
        }
        ?>
		<script type="text/javascript"> window.location='<?php 
        echo $sendback;
        ?>
'; </script>
<?php 
        exit;
    }
    if (isset($_POST['advertisment_id'])) {
        $data = $_POST;
        $ads_class->editEntry($data);
    } elseif (isset($_POST['advertisment_name'])) {
        $data = $_POST;
        $ads_class->addEntry($data);
    }
    if (!empty($_GET['action']) && isset($_GET['id']) && (int) $_GET['id']) {
        if ($_GET['action'] == 'edit') {
            was_edit($_GET['id']);
        } elseif ($_GET['action'] == 'delete') {
            $sendback = remove_query_arg(array('delete', 'id'), wp_get_referer());
            $sendback = add_query_arg(array('page' => 'was-manage'), $sendback);
            if (!check_admin_referer('was-delete_' . $_GET['id'])) {
                wp_die(__('Cheating?'));
            }
            if (!$ads_class->deleteEntry($_GET['id'])) {
                wp_die(__('Error in deleting...'));
            }
            $sendback = add_query_arg(array('deleted' => 1, 'ids' => $_GET['id']), $sendback);
            ?>
			<script type="text/javascript"> window.location='<?php 
            echo $sendback;
            ?>
'; </script>
<?php 
            exit;
        } elseif ($_GET['action'] == 'activate') {
            $sendback = remove_query_arg(array('activate', 'id'), wp_get_referer());
            $sendback = add_query_arg(array('page' => 'was-manage'), $sendback);
            $ad = new Advertisment($_GET['id']);
            if (!$ad->setActive(true, true)) {
                wp_die(__('Error in activating...'));
            }
            $sendback = add_query_arg(array('activated' => 1, 'ids' => $_GET['id']), $sendback);
            ?>
			<script type="text/javascript"> window.location='<?php 
            echo $sendback;
            ?>
'; </script>
<?php 
            exit;
        } elseif ($_GET['action'] == 'deactivate') {
            $sendback = remove_query_arg(array('deactivate', 'id'), wp_get_referer());
            $sendback = add_query_arg(array('page' => 'was-manage'), $sendback);
            $ad = new Advertisment($_GET['id']);
            if (!$ad->setActive(false, true)) {
                wp_die(__('Error in deactivating...'));
            }
            $sendback = add_query_arg(array('deactivated' => 1, 'ids' => $_GET['id']), $sendback);
            ?>
			<script type="text/javascript"> window.location='<?php 
            echo $sendback;
            ?>
'; </script>
<?php 
            exit;
        }
    } else {
        was_list();
    }
}