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

class AutoExpireTickets implements CmsRegularTask
{
    public function get_name()
    {
        return 'Auto Expire Tickets';
    }

    public function get_description()
    {
        $mod = cms_utils::get_module(MOD_CGCLIENTTASKS);
        return $mod->Lang('autoexpire_task_description');
    }

    public function test($time = '')
    {
        $mod = cms_utils::get_module(MOD_CGCLIENTTASKS);
        $ndays = (int) $mod->GetPreference('autoexpire_days',0);
        if( $ndays < 1 ) return FALSE;

        $lastrun = (int) $mod->GetPreference(__CLASS__.'/lastrun',0);
        $oneday = 24 * 3600;
        if( $time >= $lastrun + $oneday ) return TRUE;
        return FALSE;
    }

    public function execute($time = '')
    {
        // find all open tickets that have not had a response within N days
        $mod = cms_utils::get_module(MOD_CGCLIENTTASKS);
        $ndays = (int) $mod->GetPreference('autoexpire_days',0);
        if( $ndays < 1 ) return FALSE;
        $timelimit = $ndays * 24 * 3600;

        $query = 'SELECT T.id,MAX(M.sent) AS sent FROM '.CGST_TICKETS_TABLE.' T
                  LEFT JOIN '.CGST_MSGS_TABLE.' M ON T.id = M.ticket_id
                  WHERE T.status = ? GROUP BY T.id HAVING sent <= ?';
        $list = $db->GetArray($query,array(\CGClientTickets\ticket::STATUS_OPEN,$timelimit));

        // load all the tickets
        // change the status
        $cnt = 0;
        foreach( $list as $rec ) {
            try {
                $ticket = ticket::load($rec['id']);
                $ticket->status = $ticket::STATUS_EXPIRED;
                $ticket->save();
                $cnt++;
            }
            catch( \Exception $e ) {
                // do nothing (yet)
            }
        }
        audit('',$mod->GetName(),sprintf('Auto expired %d tickets',$cnt));

        return TRUE;
    }

    public function on_success($time = '')
    {
        $mod = cms_utils::get_module(MOD_CGCLIENTTASKS);
        $mod->SetPreference(__CLASS__.'/lastrun',$time);
    }

    public function on_failure($time = '')
    {
        // do nothing
    }
} // end of class

#
# EOF
#
?>
