<?php

class KataoUserPeer extends BaseKataoUserPeer {
    public static function getAllStatuses($is_supplier = false) {
        $return = array();
        if (!$is_supplier) {
            $return[KataoUser::STATUS_PENDING] = self::getStatusStr(KataoUser::STATUS_PENDING);
        }
        $return[KataoUser::STATUS_ACTIVE] = self::getStatusStr(KataoUser::STATUS_ACTIVE);
        $return[KataoUser::STATUS_DISABLED] = self::getStatusStr(KataoUser::STATUS_DISABLED);
        return $return;
    }

    public static function getStatusStr($status) {
        switch ($status) {
            case KataoUser::STATUS_PENDING:
                return 'Contact';
            case KataoUser::STATUS_ACTIVE:
                return 'Actif';
            case KataoUser::STATUS_DISABLED:
                return 'Inactif';
        }
        return '';
    }

    /**
     * KataoUserPeer::isValidUser()
     *
     * @param string $login
     * @param string $password
     * @return boolean
     */
    public static function isValidUser($login, $password) {
        $criteria = new Criteria();
        $criteria->add(self::LOGIN, $login);
        $criteria->add(self::PASSWORD, $password);
        $criteria->add(self::STATUS, KataoUser::STATUS_ACTIVE);
        return self::doCount($criteria);
    }

    /**
     * KataoUserPeer::getUserByLoginAndPassword()
     *
     * @param string $form_values
     * @param string $form_values2
     * @return KataoUser
     */
    public static function getUserByLoginAndPassword($login, $password) {
        $criteria = new Criteria();
        $criteria->add(self::LOGIN, $login);
        $criteria->add(self::PASSWORD, $password);
        $criteria->add(self::STATUS, KataoUser::STATUS_ACTIVE);
        return self::doSelectOne($criteria);
    }

    /**
     * KataoUserPeer::getMembersByNodeToArray()
     *
     * @param int $kataoNodeId
     * @return array
     */
    public static function getMembersByNodeToArray($kataoNodeId = false) {
        $return = array();
        // member
        $criteria = new Criteria();
        $criteria->addJoin(self::KATAO_MEMBER_ID, KataoMemberPeer::ID);
        $criteria->add(KataoMemberPeer::KATAO_NODE_ID, $kataoNodeId);
        foreach (self::doSelect($criteria) as/*(KataoUser)*/ $kataoUser) {
            $return[$kataoUser->getId()] = $kataoUser->getFullName();
        }
        // admin
        $criteria = new Criteria();
        $criterion = $criteria->getNewCriterion(self::IS_SEO, true);
        $criterion->addOr($criteria->getNewCriterion(self::IS_ADMIN, true));
        $criteria->addAnd($criterion);
        foreach (self::doSelect($criteria) as/*(KataoUser)*/ $kataoUser) {
            $return[$kataoUser->getId()] = $kataoUser->getFullName();
        }
        return $return;
    }

    /**
     * KataoUserPeer::retrieveByKataoSupplierId()
     *
     * @param int $katao_supplier_id
     * @return KataoUser
     */
    public static function retrieveByKataoSupplierId($katao_supplier_id) {
        $criteria = new Criteria();
        $criteria->add(self::KATAO_SUPPLIER_ID, $katao_supplier_id);
        return self::doSelectOne($criteria);
    }

    /**
     * KataoUserPeer::retrieveByKataoMemberId()
     *
     * @param int $katao_member_id
     * @return KataoUser
     */
    public static function retrieveByKataoMemberId($katao_member_id) {
        $criteria = new Criteria();
        $criteria->add(self::KATAO_MEMBER_ID, $katao_member_id);
        return self::doSelectOne($criteria);
    }

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

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(KataoMemberPeer::LAST_NAME);
        $criteria->addSelectColumn(KataoMemberPeer::FIRST_NAME);
        $criteria->addJoin(self::KATAO_MEMBER_ID, KataoMemberPeer::ID);
        $criteria->addAscendingOrderByColumn(KataoMemberPeer::LAST_NAME);
        $criteria->addAscendingOrderByColumn(KataoMemberPeer::FIRST_NAME);

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

        return $return;
    }

    /**
     * KataoUserPeer::retrieveByEmail()
     *
     * @param string $email
     * @return KataoUser
     */
    public static function retrieveByEmail($email) {
        $criteria = new Criteria();
        $criteria->add(self::EMAIL, $email);
        return self::doSelectOne($criteria);
    }

	public static function retrieveMemberById($id){
		$c = new Criteria();
		$c->add(self::ID, $id);
		return self::doSelectOne($c);
	}
}