Пример #1
0
function process_request()
{
    $item_name = isset($_POST['item_name']) ? $_POST['item_name'] : null;
    $item_price = isset($_POST['item_price']) ? $_POST['item_price'] : null;
    $item_description = isset($_POST['item_description']) ? $_POST['item_description'] : null;
    $item_img = isset($_POST['item_img']) ? $_POST['item_img'] : null;
    if (is_null($item_name)) {
        die;
    } else {
        $item_name = htmlspecialchars(trim($item_name));
        if ($item_name === '') {
            die;
        }
    }
    if (is_null($item_price) || !preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) {
        die;
    }
    $item_price = str_replace(',', '.', $item_price);
    if (is_null($item_description)) {
        die;
    } else {
        $item_description = htmlspecialchars(trim($item_description));
    }
    if (is_null($item_img)) {
        $item_img = "Null";
    }
    $id = db_insert_item($item_name, $item_description, $item_price, $item_img);
    $mc_handler = memcache_connect('localhost');
    if (memcache_get($mc_handler, 'total_rows') !== false) {
        memcache_increment($mc_handler, 'total_rows');
        pagination_rebuild_ids($mc_handler, $id);
        pagination_rebuild_prices($mc_handler, $item_price);
    }
    header('Location: /view_item.php?id=' . $id);
}
Пример #2
0
function process_request()
{
    $item_id = isset($_POST['item_id']) ? intval($_POST['item_id']) : null;
    $item_name = isset($_POST['item_name']) ? $_POST['item_name'] : null;
    $item_price = isset($_POST['item_price']) ? $_POST['item_price'] : null;
    $item_description = isset($_POST['item_description']) ? $_POST['item_description'] : null;
    $item_img = isset($_POST['item_img']) ? $_POST['item_img'] : null;
    if (is_null($item_id) || $item_id <= 0) {
        die;
    }
    if (!is_null($item_name)) {
        $item_name = htmlspecialchars(trim($item_name));
        if ($item_name === '') {
            die;
        }
    }
    if (!is_null($item_price)) {
        if (!preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) {
            die;
        }
    }
    if (!is_null($item_description)) {
        $item_description = htmlspecialchars(trim($item_description));
    }
    $item = db_get_item($item_id);
    if (!$item) {
        die;
    }
    $values = [];
    if (!is_null($item_name)) {
        $values['name'] = $item_name;
    }
    if (!is_null($item_price)) {
        $item_price = str_replace(',', '.', $item_price);
        $values['price'] = $item_price;
    }
    if (!is_null($item_description)) {
        $values['description'] = $item_description;
    }
    if (!is_null($item_img)) {
        $values['imgurl'] = $item_img;
    }
    if (!empty($values)) {
        db_update_item($item_id, $values);
        $mc_handler = memcache_connect('localhost');
        memcache_delete($mc_handler, get_page_cache_key($item_id));
        $min_price = min($item_price, $item['price']);
        pagination_rebuild_ids($mc_handler, $item_id, 1);
        if ($item_price == $item['price']) {
            $edited_pages_amount = 1;
        } else {
            $edited_pages_amount = 0;
        }
        pagination_rebuild_prices($mc_handler, $min_price, $edited_pages_amount);
        pagination_rebuild_prices($mc_handler, $min_price);
    }
    header('Location: /view_item.php?id=' . $item_id);
}
Пример #3
0
function process_request()
{
    $item_id = isset($_GET['item_id']) ? intval($_GET['item_id']) : false;
    if ($item_id == false || $item_id <= 0) {
        api_wrong_args();
        return;
    }
    $item = db_get_item($item_id);
    if (!$item) {
        api_echo_as_json("Item not found", 'msg');
        return;
    }
    db_delete_item($item_id);
    $mc_handler = memcache_connect('localhost');
    pagination_rebuild_ids($mc_handler, $item['id']);
    pagination_rebuild_prices($mc_handler, $item['price']);
    if (memcache_get($mc_handler, 'total_rows') !== false) {
        memcache_decrement($mc_handler, 'total_rows');
    }
    api_echo_as_json('Item deleted', 'msg');
    memcache_delete($mc_handler, "item_" . $item_id);
}
Пример #4
0
function process_request()
{
    $item_id = isset($_POST['item_id']) ? intval($_POST['item_id']) : null;
    $item_name = isset($_POST['item_name']) ? $_POST['item_name'] : null;
    $item_price = isset($_POST['item_price']) ? $_POST['item_price'] : null;
    $item_description = isset($_POST['item_description']) ? $_POST['item_description'] : null;
    $item_img = isset($_POST['item_img']) ? $_POST['item_img'] : null;
    $errors = [];
    if (is_null($item_id) || $item_id <= 0) {
        $errors[] = 'Incorrect id';
    }
    if (!is_null($item_name)) {
        $item_name = htmlspecialchars(trim($item_name));
        if ($item_name === '') {
            $errors[] = 'Non-empty name required';
        }
    }
    if (!is_null($item_price)) {
        if (!preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) {
            $errors[] = 'Incorrect price number';
        }
    }
    if (!is_null($item_description)) {
        $item_description = htmlspecialchars(trim($item_description));
    }
    if (!empty($errors)) {
        api_echo_as_json($errors, 'errors', RESPONSE_STATUS_FAIL);
        return;
    }
    $item = db_get_item($item_id);
    if (!$item) {
        api_echo_as_json("Item not found", 'msg');
        return;
    }
    $values = [];
    if (!is_null($item_name)) {
        $values['name'] = $item_name;
    }
    if (!is_null($item_price)) {
        $item_price = str_replace(',', '.', $item_price);
        $values['price'] = $item_price;
    }
    if (!is_null($item_description)) {
        $values['description'] = $item_description;
    }
    if (!is_null($item_img)) {
        $values['imgurl'] = $item_img;
    }
    if (!empty($values)) {
        db_update_item($item_id, $values);
        $mc_handler = memcache_connect('localhost');
        memcache_delete($mc_handler, get_page_cache_key($item_id));
        pagination_rebuild_ids($mc_handler, $item_id, 1);
        $min_price = min($item_price, $item['price']);
        if ($item_price == $item['price']) {
            $edited_pages_amount = 1;
        } else {
            $edited_pages_amount = 0;
        }
        pagination_rebuild_prices($mc_handler, $min_price, $edited_pages_amount);
    }
    api_echo_as_json('Item successfully edited', 'msg');
}