<?php

/**
 * Subclass for performing query and update operations on the 'katao_product_family' table.
 *
 * @package lib.model
 */
class KataoProductFamilyPeer extends BaseKataoProductFamilyPeer {
    /**
     * KataoProductFamilyPeer::retrieveByUrlIdentifier()
     *
     * @param string $url_identifier
     * @return KataoProductFamily
     */
    public static function retrieveByUrlIdentifier($url_identifier) {
        $criteria = new Criteria();
        $criteria->add(self::URL_IDENTIFIER, $url_identifier);
        return self::doSelectOne($criteria);
    }

    public static function getAllByProductCategoryUrlIdentifier($url_identifier) {
        $return = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addJoin(self::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
        $criteria->addJoin(self::ID, KataoProductPeer::KATAO_PRODUCT_FAMILY_ID);
        $criteria->add(KataoProductPeer::ID, sfContext::getInstance()->getUser()->getAvailableProducts(), Criteria::IN);
        $criterion = $criteria->getNewCriterion(KataoProductPeer::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(KataoProductPeer::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);
        $criteria->add(KataoProductCategoryPeer::URL_IDENTIFIER, $url_identifier);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $return[$rs->getInt(1)] = array('name' => $rs->getString(2), 'url' => $rs->getString(3));
        }

        return $return;
    }

    public static function getAllGroupedByCategory() {
        $return = array();

        $sf_user = sfContext::getInstance()->getUser();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(KataoProductCategoryPeer::NAME);
        $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
        $criteria->addAscendingOrderByColumn(KataoProductCategoryPeer::SORT_ORDER);
        $criteria->addAscendingOrderByColumn(KataoProductFamilyPeer::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $katao_product_family_id = $rs->getString(1);
            $katao_product_family_name = $rs->getString(2);
            $katao_product_category_name = $rs->getString(3);

            $return[$katao_product_category_name][$katao_product_family_id] = $katao_product_family_name;
        }

        return $return;
    }
}