<?php

class wpPersistenceManager {
    public static function addItemToSelection($type, $id) {
        $items = self::getSelectedItems($type);
        $items[$id] = true;
        self::setSelectedItems($type, $items);
    }

    public static function removeItemFromSelection($type, $id) {
        $items = self::getSelectedItems($type);
        unset($items[$id]);
        self::setSelectedItems($type, $items);
    }

    public static function isItemSelected($type, $id) {
        $items = self::getSelectedItems($type);
        return !empty($items[$id]);
    }

    public static function getSelectedItemCount($type) {
        return count(self::getSelectedItems($type));
    }

    public static function cleanSelection($type) {
        self::setSelectedItems($type);
    }

    public static function setSelectedItems($type = 'default:selected_items', $items = array()) {
        sfContext::getInstance()->getUser()->setAttribute(__CLASS__ . ':' . $type, $items);
    }

    public static function getSelectedItems($type = 'default:selected_items') {
        return sfContext::getInstance()->getUser()->getAttribute(__CLASS__ . ':' . $type, array());
    }

    /**
     * wpPersistenceManager::isEmpty()
     *
     * @param mixed $type
     * @return true if the type is empty, false otherwise
     */
    public static function isEmpty($type) {
        return ! self::getSelectedItemCount($type);
    }
}