<?php

/**
 * Subclass for performing query and update operations on the 'katao_member' table.
 *
 * @package lib.model
 */
class KataoMemberPeer extends BaseKataoMemberPeer {
    /**
     * KataoMemberPeer::retrieveByKataoIndex()
     *
     * @param int $index
     * @return KataoMember
     */
    public static function retrieveByKataoIndex($index) {
        $criteria = new Criteria();
        $criteria->add(self::KATAO_INDEX, $index);
        return self::doSelectOne($criteria);
    }

    /**
     * KataoMemberPeer::retrieveByKataoUserId()
     *
     * @param int $id
     * @return KataoMember
     */
    public static function retrieveByKataoUserId($id) {
        $criteria = new Criteria();
        $criteria->addJoin(self::ID, KataoUserPeer::KATAO_MEMBER_ID);
        $criteria->add(KataoUserPeer::ID, $id);
        return self::doSelectOne($criteria);
    }

    /**
     * KataoMemberPeer::countByAccountingCode()
     *
     * @param string $accounting_code
     * @return boolean
     */
    public static function countByAccountingCode($accounting_code) {
        $criteria = new Criteria();
        $criteria->add(self::ACCOUNTING_CODE, $accounting_code);
        return self::doCount($criteria);
    }

    public static function getAll($is_referer = false, $is_delegate = false, $katao_node_id = false) {
        $return = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::LAST_NAME);
        $criteria->addSelectColumn(self::FIRST_NAME);
        if ($is_referer) {
            $criteria->add(self::IS_REFERER, true);
        }
        if ($is_delegate) {
            $criteria->add(self::IS_DELEGATE, true);
        }
        if ($katao_node_id) {
            $criteria->add(self::KATAO_NODE_ID, $katao_node_id);
        }
        $criteria->addAscendingOrderByColumn(self::LAST_NAME);
        $criteria->addAscendingOrderByColumn(self::FIRST_NAME);

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

        return $return;
    }

    public static function getAllDelegates() {
        return self::getAll(false, true);
    }

    public static function getAllReferers() {
        return self::getAll(true, false);
    }

    public static function getAllGroupedByNode($is_referer = false, $is_delegate = false, $only_with_email = false) {
        $return = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoNodePeer::CITY);
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::LAST_NAME);
        $criteria->addSelectColumn(self::FIRST_NAME);
        $criteria->addJoin(self::KATAO_NODE_ID, KataoNodePeer::ID);
        $criteria->addJoin(self::ID, KataoUserPeer::KATAO_MEMBER_ID);
        if ($is_referer) {
            $criteria->add(self::IS_REFERER, true);
        }
        if ($is_delegate) {
            $criteria->add(self::IS_DELEGATE, true);
        }
        if ($only_with_email) {
            $criteria->add(KataoUserPeer::EMAIL, null, Criteria::NOT_EQUAL);
            $criteria->add(KataoUserPeer::EMAIL, '', Criteria::NOT_EQUAL);
        }
        $criteria->addAscendingOrderByColumn(KataoNodePeer::CITY);
        $criteria->addAscendingOrderByColumn(self::LAST_NAME);
        $criteria->addAscendingOrderByColumn(self::FIRST_NAME);

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

        return $return;
    }

    public static function getAllDelegatesGroupedByNode() {
        return self::getAllGroupedByNode(false, true);
    }

    public static function getAllReferersGroupedByNode() {
        return self::getAllGroupedByNode(true, false);
    }

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

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

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

        return $return;
    }
}