<?php

class KataoPeriodPeer extends BaseKataoPeriodPeer {
    /**
     * KataoPeriodPeer::getActivePeriod()
     *
     * @return KataoPeriod
     */
    public static function getActivePeriod() {
        $criteria = new Criteria();
        $criteria->add(self::STATUS, KataoPeriod::STATUS_ACTIVE);
        return self::doSelectOne($criteria);
    }

    /**
     * KataoPeriodPeer::getAllByKataoNode()
     *
     * @param mixed $this
     * @return
     */
    public static function getAllByKataoNode(KataoNode $kataoNode) {
        $criteria = new Criteria();
        $criteria->addAnd(self::KATAO_NODE_ID, $kataoNode->getId());
        return self::doSelect($criteria);
    }

    public static function getAll($status = false) {
        $return = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        if ($status) {
            $criteria->add(self::STATUS, $status);
        }
        $criteria->addDescendingOrderByColumn(self::BEGIN_AT);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $return[$rs->getInt(1)] = $rs->getString(2);
        }

        return $return;
    }

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

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::STATUS);
        $criteria->addDescendingOrderByColumn(self::BEGIN_AT);

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

        return $return;
    }

    public static function getAllStatuses() {
        return array(KataoPeriod::STATUS_DRAFT => self::getStatusStr(KataoPeriod::STATUS_DRAFT), KataoPeriod::STATUS_ACTIVE => self::getStatusStr(KataoPeriod::STATUS_ACTIVE), KataoPeriod::STATUS_ARCHIVED => self::getStatusStr(KataoPeriod::STATUS_ARCHIVED));
    }

    public static function getStatusStr($status) {
        switch ($status) {
            case KataoPeriod::STATUS_DRAFT:
                return 'Provisoire';
            case KataoPeriod::STATUS_ACTIVE:
                return 'Active';
            case KataoPeriod::STATUS_ARCHIVED:
                return 'Terminée';
        }
        return '';
    }

    public static function getAllActiveWithoutCart($katao_node_id) {
        $return = self::getAll(KataoPeriod::STATUS_ACTIVE);

        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoCartPeer::KATAO_PERIOD_ID);
        $criteria->add(KataoCartPeer::KATAO_NODE_ID, $katao_node_id);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            unset($return[$rs->getInt(1)]);
        }

        return $return;
    }

    public static function hasConflictedPeriodDates($begin, $end, $exclude = false) {
        $criteria = new Criteria();
        $criteria->add(self::ID, $exclude, Criteria::NOT_EQUAL);

        $criterion1 = $criteria->getNewCriterion(self::BEGIN_AT, date('Y-m-d', $begin), Criteria::GREATER_EQUAL);
        $criterion1->addAnd($criteria->getNewCriterion(self::BEGIN_AT, date('Y-m-d', $end), Criteria::LESS_EQUAL));

        $criterion2 = $criteria->getNewCriterion(self::BEGIN_AT, date('Y-m-d', $begin), Criteria::LESS_EQUAL);
        $criterion2->addAnd($criteria->getNewCriterion(self::FINISH_AT, date('Y-m-d', $begin), Criteria::GREATER_EQUAL));

        $criterion1->addOr($criterion2);
        $criteria->addAnd($criterion1);

        return 0 < self::doCount($criteria);
    }

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

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::BEGIN_AT);
        $criteria->addSelectColumn(self::FINISH_AT);
        $criteria->addAscendingOrderByColumn(self::BEGIN_AT);
        $criteria->addAscendingOrderByColumn(self::FINISH_AT);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $return[$rs->getInt(1)] = array('begin' => date('Y-m-d', strtotime((string)$rs->getString(2))), 'end' => date('Y-m-d', strtotime((string)$rs->getString(3))));
        }

        return $return;
    }

    public static function getIdFromRangeDate($date, $katao_periods = array()) {
        $return = 0;
        foreach ($katao_periods as $katao_period_id => $dates) {
            if (!$return && $date >= $dates['begin'] && $date <= $dates['end']) {
                $return = $katao_period_id;
            }
        }
        return $return;
    }
}