<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: MAMS (c) 2020-2021 by CMS Made Simple Foundation
#  An add-on module for CMS Made Simple to provide useful functions
#  and commonly used gui capabilities to other modules.
#-------------------------------------------------------------------------
# A fork of:
#
# Module: FrontEndUsers (c) 2008-2014 by Robert Campbell
#         (calguy1000@cmsmadesimple.org)
#
#-------------------------------------------------------------------------
#
# CMSMS - CMS Made Simple is (c) 2006 - 2021 by CMS Made Simple Foundation
# CMSMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# Visit the CMSMS Homepage at: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE
if( !defined('CMS_VERSION') ) { exit; }
$consumer = mams_utils::get_auth_consumer();
if( $consumer->has_capability(mams_auth_consumer::CAPABILITY_LOGOUT) ) {
  // the consumer provides the login capabilities
  echo $consumer->get_logout_display($id,$returnid,$params);
  return;
}

try {
// do the default
    $uid = $this->LoggedInId();
    if( !$uid ) return; // nothing to do, user is not logged in.

    $returnto = \xt_param::get_string($params,'returnto'); // test me.
    $username = $this->LoggedInName();
    $thetemplate = \mams_utils::get_template($params,'logouttemplate','MAMS::logout form');
    $tpl = $this->CreateSmartyTemplate($thetemplate);
    // replace {$groupname} with the first groupname we can find that matches
    $groups = $this->GetMemberGroupsArray( $uid );
    $groupname = $this->GetGroupName( $groups[0]['groupid'] );
    $tpl->assign('prompt_loggedin', $this->Lang('msg_currentlyloggedinas')); // can get rid of this.
    $tpl->assign('userid', $uid);
    $tpl->assign('username', $username);
    $msg = \xt_param::get_string($params,'message');
    if( $msg ) $tpl->assign('message',$msg);

    // start ugly
    // todo: need some mechanism to tell 'which' authentication was used, if alt authentication is supported.
    $loggedin_using_std = FALSE;
    $manip = $this->GetManipulator();
    if( $manip->_std_LoggedInId() ) $loggedin_using_std = TRUE;
    // end ugly

    $parms = array();
    if( $returnto ) $parms['returnto'] = $returnto;
    $url = $this->create_url($id,'logout',$returnid,$parms);
    $tpl->assign('url_logout',$url);

    // replace {$groupname} with the first groupname we can find that matches
    $cspage = $returnid;
    if( ($tmp = $this->GetPreference('pageid_changesettings')) ) {  // test me
        $tpl2 = $smarty->CreateTemplate('string:'.$tmp);
        $tmp = $tpl2->fetch();
        if( $tmp ) $cspage = $this->resolve_alias_or_id($tmp,$returnid);
    }
    $parms = array('form'=>'changesettings');
    if( $returnto ) $parms['returnto'] = $returnto;
    $url = $this->create_url($id,'default',$cspage,$parms);
    $tpl->assign('url_changesettings',$url);
    $props = $this->GetUserProperties( $this->LoggedInId() );
    if( $props !== false ) {
        foreach( $props as $p ) {
            $tpl->assign($p['title'],$p['data']);
        }
    }

    if( $consumer->has_capability($consumer::CAPABILITY_ALTLOGIN) ) {
        // the consumer has alternate login capabilities....
        $res = $consumer->get_logout_display($id,$returnid,$params);
        $tpl->assign('alt_logoutform',$res);
    }

    $tpl->display();
}
catch( \Exception $e ) {
    // fatal error we cannot work around.
    echo $this->DisplayErrorMessage($e->GetMessage());
}

#
# EOF
#