/**
 * @method
 *
 * Returns a list or user.
 *
 * @name PMFGetUserEmailAddress
 * @label PMF Get User Email Address
 * @link http://wiki.processmaker.com/index.php/ProcessMaker_Functions#PMFGetUserEmailAddress.28.29
 *
 * @param string(32) or Array | $id | Case ID | Id of the case.
 * @param string(32) | $APP_UID = null | Application ID | Id of the Application.
 * @param string(32) | $prefix = "usr" | prefix | Id of the task.
 * @return array | $aRecipient | Array of the Recipient | Return an Array of the Recipient.
 *
 */
function PMFGetUserEmailAddress($id, $APP_UID = null, $prefix = 'usr')
{
    require_once 'classes/model/UsersPeer.php';
    require_once 'classes/model/AppDelegation.php';
    G::LoadClass('case');
    if (is_string($id) && trim($id) == "") {
        return false;
    }
    if (is_array($id) && count($id) == 0) {
        return false;
    }
    //recipient to store the email addresses
    $aRecipient = array();
    $aItems = array();
    /*
     * First at all the $id user input can be by example erik@colosa.com
     * 2.this $id param can be a array by example Array('000000000001','000000000002') in this case $prefix is necessary
     * 3.this same param can be a array by example Array('usr|000000000001', 'usr|-1', 'grp|2245141479413131441')
     */
    /*
     * The second thing is that the return type will be configurated depend of the input type (using $retType)
     */
    if (is_array($id)) {
        $aItems = $id;
        $retType = 'array';
    } else {
        $retType = 'string';
        if (strpos($id, ",") !== false) {
            $aItems = explode(',', $id);
        } else {
            array_push($aItems, $id);
        }
    }
    foreach ($aItems as $sItem) {
        //cleaning for blank spaces into each array item
        $sItem = trim($sItem);
        if (strpos($sItem, "|") !== false) {
            // explode the parameter because  always will be compose with pipe separator to indicate
            // the type (user or group) and the target mai
            list($sType, $sID) = explode('|', $sItem);
            $sType = trim($sType);
            $sID = trim($sID);
        } else {
            $sType = $prefix;
            $sID = $sItem;
        }
        switch ($sType) {
            case 'ext':
                if (G::emailAddress($sID)) {
                    array_push($aRecipient, $sID);
                }
                break;
            case 'usr':
                if ($sID == '-1') {
                    // -1: Curent user, load from user record
                    if (isset($APP_UID)) {
                        $oAppDelegation = new AppDelegation();
                        $aAppDel = $oAppDelegation->getLastDeleration($APP_UID);
                        if (isset($aAppDel)) {
                            $oUserRow = UsersPeer::retrieveByPK($aAppDel['USR_UID']);
                            if (isset($oUserRow)) {
                                $sID = $oUserRow->getUsrEmail();
                            } else {
                                throw new Exception('User with ID ' . $oAppDelegation->getUsrUid() . 'doesn\'t exist');
                            }
                            if (G::emailAddress($sID)) {
                                array_push($aRecipient, $sID);
                            }
                        }
                    }
                } else {
                    $oUserRow = UsersPeer::retrieveByPK($sID);
                    if ($oUserRow != null) {
                        $sID = $oUserRow->getUsrEmail();
                        if (G::emailAddress($sID)) {
                            array_push($aRecipient, $sID);
                        }
                    }
                }
                break;
            case 'grp':
                G::LoadClass('groups');
                $oGroups = new Groups();
                $oCriteria = $oGroups->getUsersGroupCriteria($sID);
                $oDataset = GroupwfPeer::doSelectRS($oCriteria);
                $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC);
                while ($oDataset->next()) {
                    $aGroup = $oDataset->getRow();
                    //to validate email address
                    if (G::emailAddress($aGroup['USR_EMAIL'])) {
                        array_push($aRecipient, $aGroup['USR_EMAIL']);
                    }
                }
                break;
            case 'dyn':
                $oCase = new Cases();
                $aFields = $oCase->loadCase($APP_UID);
                $aFields['APP_DATA'] = array_merge($aFields['APP_DATA'], G::getSystemConstants());
                //to validate email address
                if (isset($aFields['APP_DATA'][$sID]) && G::emailAddress($aFields['APP_DATA'][$sID])) {
                    array_push($aRecipient, $aFields['APP_DATA'][$sID]);
                }
                break;
        }
    }
    switch ($retType) {
        case 'array':
            return $aRecipient;
            break;
        case 'string':
            return implode(',', $aRecipient);
            break;
        default:
            return $aRecipient;
    }
}