<?php

class myUser extends sfBasicSecurityUser {
    /**
     * myUser::getInstance()
     *
     * @return KataoUser
     */
    public function getInstance() {
        return $this->getAttribute('katao_user_instance', null);
    }
    public function setInstance(KataoUser $instance) {
        $this->setAttribute('katao_user_instance', $instance);
    }

    /**
     * myUser::getActivePeriod()
     *
     * @return KataoPeriod
     */
    public function getActivePeriod() {
        return $this->getAttribute('katao_period_instance', KataoPeriodPeer::getActivePeriod());
    }
    public function setActivePeriod(KataoPeriod $instance = null) {
        $this->setAttribute('katao_period_instance', $instance);
    }

    /**
     * myUser::getActiveNode()
     *
     * @return KataoNode
     */
    public function getActiveNode() {
        return $this->getAttribute('katao_node_instance', null);
    }
    public function setActiveNode(KataoNode $instance = null) {
        $this->setAttribute('katao_node_instance', $instance);
        $this->initAvailableProducts();
    }

    /**
     * myUser::getDisplayOnlyActiveProducts()
     *
     * @return boolean
     */
    public function getDisplayOnlyActiveProducts() {
        return $this->getAttribute('katao_display_only_active_products', true);
    }
    public function setDisplayOnlyActiveProducts($value) {
        $this->setAttribute('katao_display_only_active_products', $value);
        $this->initAvailableProducts();
    }

    public function getAvailableProducts() {
    	if (-1 == $this->getAttribute('katao_available_products', -1)) {
    		$this->initAvailableProducts();
    	}
		return $this->getAttribute('katao_available_products', array());
    }
    public function getCartableProducts() {
        return $this->getAttribute('katao_cartable_products', array());
    }
    public function initAvailableProducts() {
        $active_products = array();
        $all_products = array();

        $katao_node_id = $this->getActiveNode()?$this->getActiveNode()->getId():false;
        $katao_period_id = $this->getActivePeriod()?$this->getActivePeriod()->getId():false;
        $display_only_active_products = $this->getDisplayOnlyActiveProducts();

        /* Retrieve all products from complete suppliers */
        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoProductPeer::ID);
        $criteria->addSelectColumn(KataoNodeSupplierPeer::KATAO_PERIOD_ID);
        $criteria->addJoin(KataoProductPeer::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
        $criteria->addJoin(KataoSupplierPeer::ID, KataoNodeSupplierPeer::KATAO_SUPPLIER_ID);
        if ($katao_node_id) {
            $criteria->add(KataoNodeSupplierPeer::KATAO_NODE_ID, $katao_node_id);
        }
        $criteria->add(KataoNodeSupplierPeer::INCLUDE_ALL_PRODUCTS, true);

        $rs = KataoProductPeer::doSelectRS($criteria);
        while ($rs->next()) {
            if (!$display_only_active_products) {
                $all_products[$rs->getInt(1)] = true;
                if ($rs->getInt(2) == $katao_period_id) {
                    $active_products[$rs->getInt(1)] = true;
                }
            } elseif ($rs->getInt(2) == $katao_period_id) {
                $all_products[$rs->getInt(1)] = true;
                $active_products[$rs->getInt(1)] = true;
            }
        }

        /* Retrieve selected products from incomplete suppliers */
        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoNodeProductPeer::KATAO_PRODUCT_ID);
        $criteria->addSelectColumn(KataoNodeProductPeer::KATAO_PERIOD_ID);
        if ($katao_node_id) {
            $criteria->add(KataoNodeProductPeer::KATAO_NODE_ID, $katao_node_id);
        }

        $rs = KataoNodeProductPeer::doSelectRS($criteria);
        while ($rs->next()) {
            if (!$display_only_active_products) {
                $all_products[$rs->getInt(1)] = true;
                if ($rs->getInt(2) == $katao_period_id) {
                    $active_products[$rs->getInt(1)] = true;
                }
            } elseif ($rs->getInt(2) == $katao_period_id) {
                $all_products[$rs->getInt(1)] = true;
                $active_products[$rs->getInt(1)] = true;
            }
        }

        $this->setAttribute('katao_available_products', array_keys($all_products));
        $this->setAttribute('katao_cartable_products', array_keys($active_products));
    }
}
