<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: CGClienttickets (c) 2014 by Robert Campbell
#         (calguy1000@cmsmadesimple.org)
#  An addon module for CMS Made Simple to allow interaction with customer
#  or admin created tickets about a specific data item.
#
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This projects homepage is: 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

namespace CGClientTickets;

final class utils
{
    private function __construct() {}

    public static function get_alert_groups()
    {
        $tmp = \cge_userops::get_grouplist(FALSE);
        if( is_array($tmp) && count($tmp) ) {
            $out = array();
            foreach( $tmp as $gid => $name ) {
                $out[$gid * -1] = $name;
            }
            return $out;
        }
    }

    public static function get_feu_list()
    {
        static $list;
        if( !is_array($list) ) {
            $list = array();

            // now add in feu users
            $feu = \cms_utils::get_module(MOD_FRONTENDUSERS);
            if( is_object($feu) ) {
                $query = new \feu_user_query;
                $query->add_and_opt(\feu_user_query_OPT::MATCH_NOTEXPIRED);
                $query->set_sortby(\feu_user_query::RESULT_SORTBY_USERNAME);
                $query->set_sortorder(\feu_user_query::RESULT_SORTORDER_ASC);
                $rs = $query->execute();
                while( !$rs->EOF() ) {
                    $list[$rs->fields['id']] = $rs->fields['username'];
                    $rs->MoveNext();
                }
            }
        }
        return $list;
    }

    public static function strip_html($input)
    {
        return strip_tags(cms_html_entity_decode($input));
    }

    public static function clean_html($input)
    {
        return \cge_utils::clean_input_html(cms_html_entity_decode($input));
    }

    public static function get_ticket_priority_list()
    {
        $mod = \cms_utils::get_module(MOD_CGCLIENTTICKETS);
        $out = array();
        $out[ticket::PRIORITY_HIGH] = $mod->Lang('priority_high');
        $out[ticket::PRIORITY_NORMAL] = $mod->Lang('priority_normal');
        $out[ticket::PRIORITY_LOW] = $mod->Lang('priority_low');
        return $out;
    }

    public static function get_ticket_status_list()
    {
        $mod = \cms_utils::get_module(MOD_CGCLIENTTICKETS);
        $out = array();
        $out[ticket::STATUS_OPEN] = $mod->Lang('status_open');
        $out[ticket::STATUS_CLOSED] = $mod->Lang('status_closed');
        $out[ticket::STATUS_EXPIRED] = $mod->Lang('status_expired');
        return $out;
    }


    /**
     * Get an array of email addresses given an a valid UID for this module.
     * If the input value is a positive integer, it is assumed that the user is an FEU user
     * and that users email address is returned.
     *
     * If the input value is a negative integer, it is assumed to be an admin group id
     * all email addresses for active members will be returned.
     *
     * @param int $in
     * @return string[]
     */
    public static function get_email_list($in)
    {
        if( $in > 0 ) {
            $feu = \cms_utils::get_module(MOD_FRONTENDUSERS);
            if( !is_object($feu) ) return;
            $tmp = $feu->GetEmail($in);
            if( $tmp ) {
                $rec = array('username'=>$feu->GetUserName($in),'email'=>$tmp);
                return array($rec);
            }
        }
        else {
            $gid = (int) $in * -1;
            $ops = cmsms()->GetUserOperations();
            $tmp = $ops->LoadUsersInGroup($groupid);
            if( is_array($tmp) && count($tmp) ) {
                // need username and email
                $out = array();
                foreach( $tmp as $one ) {
                    if( $one->active && $one->email != '' ) $out[] = array('username'=>$one->username,'email'=>$one->email);
                }
                return $out;
            }
        }
    }

    public static function set_error($str)
    {
        $mod = \cms_utils::get_module(MOD_CGCLIENTTICKETS);
        if( $str ) {
            $mod->session_put('_error',$str);
        }
        else {
            $mod->session_clear('_error');
        }
    }

    public static function get_error()
    {
        $mod = \cms_utils::get_module(MOD_CGCLIENTTICKETS);
        $str = $mod->session_get('_error');
        $mod->session_clear('_error');
        return $str;
    }

    public static function set_message($str)
    {
        $mod = \cms_utils::get_module(MOD_CGCLIENTTICKETS);
        if( $str ) {
            $mod->session_put('_message',$str);
        }
        else {
            $mod->session_clear('_message');
        }
    }

    public static function get_message()
    {
        $mod = \cms_utils::get_module(MOD_CGCLIENTTICKETS);
        $str = $mod->session_get('_message');
        $mod->session_clear('_message');
        return $str;
    }

} // end of class
