<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: CMSMSExt (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: CGExtensions (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

/**
 * Utility methods to deal with admin users.
 *
 * @package CMSMSExt
 * @category Utilities
 * @author  calguy1000 <calguy1000@cmsmadesimple.org>
 * @copyright Copyright 2010 by Robert Campbell
 */

/**
 * Utility methods to deal with admin users.
 *
 * @package CMSMSExt
 */
final class xt_userops
{
    /**
     * @ignore
     */
    private function __construct() {}

    /**
     * A function to return an expanded list of user id's given an input list
     * if one of the id's specified is negative, it is assumed to be a group id
     * and is expanded to its members.
     *
     * @param mixed $useridlist A comma separated string, or an array of userid's or negative group id's.
     * @return array
     */
    static public function expand_userlist($useridlist)
    {
        $users = array();

        if( !is_array($useridlist) ) $useridlist = explode(',',$useridlist);
        if( !count($useridlist) ) return $users;

        $userops = UserOperations::get_instance();
        foreach( $useridlist as $oneuid ) {
            if( $oneuid < 0 ) {
                // assume its a group id
                // and get all the uids for that group
                $groupusers = $userops->LoadUsersInGroup($oneuid * -1);
                foreach( $groupusers as $oneuser ) {
                    $users[] = $oneuser->id;
                }
            }
            else {
                $users[] = $oneuid;
            }
        }

        $users = array_unique($users);
        return $users;
    }


    /**
     * Retrieve an associative array containing a list of CMSMS admin groups that
     * is suitable for formatting in a dropdown.
     *
     * @param boolean $inclnone Flag indicating whether "none" should be the first item.
     * @return array
     */
    public static function get_grouplist(bool $inclnone = TRUE)
    {
        static $list;
        if( !is_array($list) ) {
            $ops = CmsApp::get_instance()->GetGroupOperations();
            $groups = $ops->LoadGroups();
            $list = array();
            foreach( $groups as $onegroup ) {
                if( !$onegroup->active ) continue;
                $list[$onegroup->id] = $onegroup->name;
            }
        }

        $out = $list;
        if( $inclnone ) {
            $mod = cms_utils::get_module(MOD_XTMODULE);
            $tmp = [ -1 => $mod->Lang('none') ];
            $out = array_merge( $tmp, $list );
        }
        return $out;
    }


    /**
     * Given a group name, return a group id.
     *
     * @param string $groupname
     * @return int|null
     */
    public static function get_groupid(string $groupname)
    {
        $groupname = trim($groupname);
        if( !$groupname ) return;

        $list = self::get_grouplist(FALSE);
        $list = array_flip( $list );
        if( isset( $list[$groupname] ) ) return $list[$groupname];
    }

    /**
     * Get all of the GID members for all users in the specified group
     *
     * @param int $groupid The CMSMS admin group id
     * @param bool $all Optionally include inactive users
     * @return int[]
     */
    public static function get_group_members(int $groupid, bool $all = false)
    {
        if( $groupid < 1 ) return;

        $out = [];
        $ops = UserOperations::get_instance();
        $list = $ops->LoadUsersInGroup($groupid);
        if( is_array($list) && count($list) ) {
            foreach( $list as $oneuser ) {
                if( ($oneuser->active || $all) && !in_array($oneuser->id,$out) ) $out[] = $oneuser->id;
            }
        }

        if( count($out) == 0 ) return;
        return $out;
    }

    /**
     * Get all of the known email addresses for an admin group id
     *
     * @param int $groupid The CMSMS admin group id
     * @return string[]
     */
    public static function expand_group_emails(int $groupid)
    {
        if( $groupid <= 0 ) return;

        $emails = array();
        $ops = UserOperations::get_instance();
        $list = $ops->LoadUsersInGroup($groupid);
        if( is_array($list) && count($list) ) {
            foreach( $list as $oneuser ) {
                if( $oneuser->active && $oneuser->email != '' && !in_array($oneuser->email,$emails) ) $emails[] = $oneuser->email;
            }
        }

        if( count($emails) == 0 ) return;
        return $emails;
    }


    /**
     * Get a list of email addresses matching a uid list
     *
     * @param int|int[] $list The list of CMSMS admin UIDs
     * @return string[]
     */
    static public function get_uid_emails($list)
    {
        if( !is_array($list) ) $list = array($list);

        $userops = UserOperations::get_instance();
        $allusers = $userops->LoadUsers();
        $emails = array();
        foreach( $list as $uid ) {
            $uid = (int)$uid;
            if( $uid < 1 ) continue;

            // find it.
            foreach( $allusers as $rec ) {
                if( $rec->id != $uid ) continue;
                if( !$rec->active ) continue;
                if( !$rec->email ) continue;
                $emails[] = $rec->email;
                break;
            }
        }

        if( count($emails) ) return $emails;
    }


    /**
     * Get the email address (if any) for the specified uid
     * If no uid is specified, use the currently logged in admin uid
     *
     * @param int $uid The desired CMSMS admin user id.  If no userid is provided, the currently logged in admin user account (if any) is used.
     * @return string
     */
    static public function get_uid_email(int $uid = null)
    {
        $uid = (int)$uid;
        if( $uid < 1 ) $uid = get_userid(false);
        if( $uid < 1 ) return;

        $out = self::get_uid_emails(array($uid));
        if( count($out) == 1 ) return $out[0];
    }

    /**
     * Given an admin Userid get his member groups (if any)
     *
     * @param int $uid The admin userid
     * @return int[] Array of integer groups, or null.
     */
    static public function get_admin_membergroups(int $uid)
    {
        $uid = (int) $uid;
        if( $uid < 1 ) return;

        $db = cmsms()->GetDb();
        $query = 'SELECT DISTINCT group_id FROM '.cms_db_prefix().'user_groups WHERE user_id = ?';
        $tmp = $db->GetCol($query,array($uid));
        return $tmp;
    }

    /**
     * Given an array of userids,  get a hash of uids and usernames
     * suitable for use in a dropdown.
     *
     * @param int[] $uid_list
     * @return array
     */
    static public function get_userlist(array $uid_list = null)
    {
        $ops = UserOperations::get_instance();
        $allusers = $ops->LoadUsers();

        if( !count($allusers) ) return;
        $out = [];
        foreach( $allusers as $user ) {
            if( is_null($uid_list) || in_array($user->id,$uid_list) ) $out[$user->id] = $user->username;
        }
        return $out;
    }
} // end of class

#
# EOF
#
