function videogall_migrate()
{
    $current_videos = get_option('videogall_videos');
    $current_categories = get_option('videogall_categories');
    if (!empty($current_categories)) {
        foreach ($current_categories as $entry) {
            $category = array('name' => $entry);
            videogall_insert("videogall_categories", $category);
        }
        delete_option('videogall_categories');
    }
    if (!empty($current_videos)) {
        foreach ($current_videos as $entry) {
            $catid = videogall_get_category_id($entry['category']);
            if ($catid < 1) {
                $catid = 0;
            }
            $video = array('url' => $entry['url'], 'thumbnail' => $entry['thumbnail'], 'category' => $catid, 'caption' => $entry['caption'], 'description' => $entry['description']);
            videogall_insert("videogall_videos", $video);
        }
        delete_option('videogall_videos');
    }
}
/**
 * Add/Update video/categories
 */
function videogall_video_action()
{
    global $videogall_url;
    if (isset($_POST['delete-id']) and trim($_POST['delete-id']) != "") {
        $delete_id = videogall_sanitize($_POST['delete-id']);
        if ($delete_id != 0 and $delete_id != "") {
            $result = videogall_bulk_delete("videogall_videos", $delete_id);
            if (!$result) {
                add_settings_error('videogall_options', 'error', __('Videos cannot be deleted', 'videogall'), 'error');
            } else {
                add_settings_error('videogall_options', 'success', __('Videos deleted', 'videogall'), 'updated');
            }
        } else {
            add_settings_error('videogall_options', 'error', __('No videos selected. Select/Unselect videos to delete by clicking on them', 'videogall'), 'error');
        }
    }
    if (isset($_POST['edit-video-submit'])) {
        $id = videogall_sanitize($_POST['edit-id']);
        if ($id != 0 and $id != "") {
            $url = videogall_process_url(esc_attr(videogall_sanitize($_POST['edit-video-url-' . $id])));
            if ($url != "") {
                $thumbnail = esc_attr(videogall_sanitize($_POST['edit-video-thumb-' . $id]));
                if ($thumbnail == "") {
                    $thumbnail = videogall_get_thumbnail($url);
                }
                if ($thumbnail == "") {
                    $thumbnail = $videogall_url . 'images/default.png';
                }
                if (preg_match('/blip\\.tv/i', $url) > 0) {
                    $url = videogall_blip_url($url);
                }
                $category = esc_attr(videogall_sanitize($_POST['edit-video-category-' . $id]));
                $caption = esc_attr(videogall_sanitize($_POST['edit-video-caption-' . $id]));
                $description = videogall_sanitize($_POST['edit-video-description-' . $id]);
                $update_str = "url='" . $url . "', thumbnail='" . $thumbnail . "', category='" . $category . "', caption='" . $caption . "', description='" . $description . "'";
                $result = videogall_update("videogall_videos", $id, $update_str);
                if (!$result) {
                    add_settings_error('videogall_options', 'error', __('Video not updated', 'videogall'), 'error');
                } else {
                    add_settings_error('videogall_options', 'success', __('Video Updated', 'videogall'), 'updated');
                }
            } else {
                add_settings_error('videogall_options', 'error', __('Name & URL are required', 'videogall'), 'error');
            }
        }
    }
    if (isset($_POST['add-video-submit'])) {
        $url = videogall_process_url(esc_attr(videogall_sanitize($_POST['add-video-url'])));
        if ($url != "") {
            $thumbnail = esc_attr(videogall_sanitize($_POST['add-video-thumb']));
            if ($thumbnail == "") {
                $thumbnail = videogall_get_thumbnail($url);
            }
            if ($thumbnail == "") {
                $thumbnail = $videogall_url . 'images/default.png';
            }
            if (preg_match('/blip\\.tv/i', $url) > 0) {
                $url = videogall_blip_url($url);
            }
            $category = esc_attr(videogall_sanitize($_POST['add-video-category']));
            $caption = esc_attr(videogall_sanitize($_POST['add-video-caption']));
            $description = videogall_sanitize($_POST['add-video-description']);
            $video = array("url" => $url, "thumbnail" => $thumbnail, "category" => $category, "caption" => $caption, "description" => $description);
            $result = videogall_insert("videogall_videos", $video);
            if (!$result) {
                add_settings_error('videogall_options', 'error', __('Video cannot be added', 'videogall'), 'error');
            } else {
                add_settings_error('videogall_options', 'success', __('New Video Added', 'videogall'), 'updated');
            }
        } else {
            add_settings_error('videogall_options', 'error', __('URL is required', 'videogall'), 'error');
        }
    }
    if (isset($_POST['update-category-submit'])) {
        $existing_category = videogall_sanitize($_POST['update-video-category']);
        $new_category = videogall_sanitize($_POST['update-category-name']);
        if ($existing_category != "0") {
            if ($new_category != "") {
                $update_str = "name = '" . $new_category . "'";
                $result = videogall_update("videogall_categories", $existing_category, $update_str);
                if (!$result) {
                    add_settings_error('videogall_options', 'error', __('Category cannot be updated', 'videogall'), 'error');
                } else {
                    add_settings_error('videogall_options', 'success', __('Category Updated', 'videogall'), 'updated');
                }
            }
        } else {
            add_settings_error('videogall_options', 'error', __('Select category from dropdown to update', 'videogall'), 'error');
        }
    }
    if (isset($_POST['delete-category-submit'])) {
        $existing_category = videogall_sanitize($_POST['update-video-category']);
        if ($existing_category != "0") {
            $result = videogall_delete("videogall_categories", $existing_category);
            if (!$result) {
                add_settings_error('videogall_options', 'error', __('Category cannot be deleted', 'videogall'), 'error');
            } elseif ($result > 0) {
                $update_str = "category = 0";
                $result = videogall_update_video_by_category($existing_category, $update_str);
                add_settings_error('videogall_options', 'success', __('Category deleted', 'videogall'), 'updated');
            }
        }
    }
    if (isset($_POST['add-category-submit'])) {
        $name = videogall_sanitize($_POST['add-category-name']);
        if ($name != "") {
            $category = array("name" => $name);
            $result = videogall_insert("videogall_categories", $category);
            if (!$result) {
                add_settings_error('videogall_options', 'error', __('Category cannot be added', 'videogall'), 'error');
            } else {
                add_settings_error('videogall_options', 'success', __('New Category Added', 'videogall'), 'updated');
            }
        } else {
            add_settings_error('videogall_options', 'error', __('Category name is required', 'videogall'), 'error');
        }
    }
}