<?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

define('CGST_PERM','Use CGSimpleTickets');
define('CGST_PERM_DELETE','Delete Tickets from CGSimpleTickets');
define('CGST_TICKETS_TABLE',cms_db_prefix().'mod_cgct_tickets');
define('CGST_MSGS_TABLE',cms_db_prefix().'mod_cgct_msgs');
define('CGST_TPL_PROTOTYPE_MYTICKETS','prototype_mytickets');
define('CGST_PREF_DFLT_MYTICKETS','dflttemplate_mytickets');
define('CGST_TPL_PROTOTYPE_NEWTICKET','prototype_newticket');
define('CGST_PREF_DFLT_NEWTICKET','dflttemplate_newticket');
define('CGST_TPL_PROTOTYPE_VIEWTICKET','prototype_viewticket');
define('CGST_PREF_DFLT_VIEWTICKET','dflttemplate_viewticket');

class CGClientTickets extends CGExtensions
{
    const EVENT_NEWTICKET = 'TicketCreated';
    const EVENT_NEWTICKETMSG = 'TicketMessageCreated';
    const EVENT_DELETETICKETMSG = 'TicketMessageDeleted';
    const EVENT_DELETETICKET = 'TicketDeleted';

    public function __construct()
    {
        parent::__construct();
        $this->AddImageDir('images');

        $smarty = cmsms()->GetSmarty();
        if( !$smarty ) return;
        $smarty->registerClass('cgct_utils','\CGClientTickets\smarty_utils');

        \CGClientTickets\ticket::set_persist_actor(new \CGClientTickets\Actor\TicketPersistActor());
        \CGClientTickets\message::set_persist_actor(new \CGClientTickets\Actor\MessagePersistActor());
        \CGClientTickets\message::set_notify_actor(new \CGClientTickets\Actor\TicketEmailNotifyActor());
    }

    public function GetName() { return get_class($this); }
    public function GetDependencies() { return array('CGExtensions'=>'1.43.2','FrontEndUsers'=>'1.24.2','CGSimpleSmarty'=>'1.7.4'); }
    public function GetAdminSection() { return 'siteadmin'; }
    public function GetVersion() { return '1.0'; }
    public function MinimumCMSVersion() { return '1.11.10'; }
    public function GetHelp() { return @file_get_contents(__DIR__.'/doc/help.inc'); }
    public function GetAuthor() { return 'calguy1000'; }
    public function GetAuthorEmail() { return 'calguy1000@gmail.com'; }
    public function GetChangeLog() { return @file_get_contents(__DIR__.'/doc/changelog.html.inc'); }
    public function GetAdminDescription() { return $this->Lang('moddescription'); }
    public function AllowAutoInstall() { return FALSE; }
    public function AllowAutoUpgrade() { return FALSE; }
    public function InstallPostMessage() { return $this->Lang('postinstall'); }
    public function UninstallPostMessage() { return $this->Lang('postuninstall'); }
    public function UninstallPreMessage() { return $this->Lang('preuninstall'); }

    public function IsPluginModule() { return TRUE; }
    public function HasAdmin() { return TRUE; }
    public function LazyLoadAdmin() { return TRUE; }
    public function LazyLoadFrontend() { return TRUE; }

    public function GetFriendlyName() {
        return $this->CGGetPreference('friendlyname',$this->Lang('friendlyname'));
    }

    public function VisibleToAdminUser() {
        return $this->CheckPermission('Modify Site Preferences') ||
            $this->CheckPermission('Modify Templates') ||
            $this->CheckPermission(CGST_PERM);
    }

    public function InitializeFrontend()
    {
        $this->RestrictUnknownParams();
        $this->RegisterModulePlugin();

        $this->SetParameterType('ticket',CLEAN_INT);
        $this->SetParameterType('msgid',CLEAN_INT);
        $this->SetParameterType('file',CLEAN_STRING);
        $this->SetParameterType('view',CLEAN_INT);
        $this->SetParameterType('key1',CLEAN_STRING);
        $this->SetParameterType('key2',CLEAN_STRING);
        $this->SetParameterType('key3',CLEAN_STRING);
        $this->SetParameterType('page',CLEAN_STRING);
        $this->SetParameterType('return',CLEAN_STRING);
        $this->SetParameterType('formtemplate',CLEAN_STRING);
        $this->SetParameterType('listtemplate',CLEAN_STRING);
        $this->SetParameterType('viewtemplate',CLEAN_STRING);
        $this->SetParameterType(CLEAN_REGEXP.'/cgct_.*/',CLEAN_STRING);
    }

    public function InitializeAdmin()
    {
        $this->CreateParameter('action','default',$this->Lang('help_param_action'));
        $this->CreateParameter('ticket','',$this->Lang('help_param_ticket'));
        $this->CreateParameter('msgid','',$this->Lang('help_param_msgid'));
        $this->CreateParameter('file','',$this->Lang('help_param_file'));
        $this->CreateParameter('view','',$this->Lang('help_param_view'));
        $this->CreateParameter('key1','',$this->Lang('help_param_key1'));
        $this->CreateParameter('key2','',$this->Lang('help_param_key2'));
        $this->CreateParameter('key3','',$this->Lang('help_param_key3'));
        $this->CreateParameter('listtemplate','default.tpl',$this->Lang('help_param_listtemplate'));
        $this->CreateParameter('formtemplate','newticket.tpl',$this->Lang('help_param_formtemplate'));
        $this->CreateParameter('viewtemplate','view.tpl',$this->Lang('help_param_viewtemplate'));
    }

    public function GetEventHelp( $eventname ) {
        return $this->Lang('eventhelp_'.$eventname);
    }

    public function GetEventDescription( $eventname ) {
        return $this->Lang('eventdesc_'.$eventname);
    }

    public function GetNotificationOutput($priority=2)
    {
        $priority = (int) $priority;
        if( !$this->CheckPermission(CGST_PERM) ) return;

        if( $priority > 2) {
            // get the number of tickets that have unread replies
            $filter = new \CGClientTickets\Repository\UnreadTicketResultset();
            $priority_list = \CGClientTickets\utils::get_ticket_priority_list();
            if( $filter->TotalMatches() > 0 ) {
                $all = $filter->FetchAll();
                $out = array();
                foreach( $all as $one ) {
                    $obj = new StdClass;
                    switch( $one['priority'] ) {
                    case \CGClientTickets\ticket::PRIORITY_HIGH:
                        $obj->priority = 1;
                        $obj->name = $this->GetFriendlyName();
                        $obj->link = $this->create_url('m1_','defaultadmin');
                        $obj->html = $this->CreateLink('m1_','defaultadmin','',$this->Lang('notify_n_unread_high',$one['cnt']));
                        break;
                    case \CGClientTickets\ticket::PRIORITY_NORMAL:
                        $obj->priority = 2;
                        $obj->name = $this->GetFriendlyName();
                        $obj->link = $this->create_url('m1_','defaultadmin');
                        $obj->html = $this->CreateLink('m1_','defaultadmin','',$this->Lang('notify_n_unread_normal',$one['cnt']));
                        break;
                    case \CGClientTickets\ticket::PRIORITY_LOW:
                        $obj->priority = 3;
                        $obj->name = $this->GetFriendlyName();
                        $obj->link = $this->create_url('m1_','defaultadmin');
                        $obj->html = $this->CreateLink('m1_','defaultadmin','',$this->Lang('notify_n_unread_low',$one['cnt']));
                        break;
                    }
                    $out[] = $obj;
                }
                return $out;
            }
        }
    }

    public function get_tasks()
    {
        $obj = new AutoExpireTickets;
        return $obj;
    }
} // end of class

#
# EOF
#
?>

