<?php

namespace CGProductBuilder;

class ticket_disposition extends disposition
{
    public function get_friendlyname()
    {
        $mod = \cms_utils::get_module(MOD_CGPRODUCTBUILDER);
        return $mod->Lang('dispose_ticket_name');
    }

    public function get_buttontext()
    {
        $mod = \cms_utils::get_module(MOD_CGPRODUCTBUILDER);
        return $mod->Lang('dispose_ticket_button');
    }

    public function compatible()
    {
        $cgct = \cms_utils::get_module('CGClientTickets');
        if( !is_object($cgct) ) return FALSE;

        $feu = \cms_utils::get_module('FrontEndUsers');
        if( !is_object($feu) ) return FALSE;

        return TRUE;
    }

    public function ready()
    {
        if( !$this->compatible() ) return FALSE;

        $feu = \cms_utils::get_module('FrontEndUsers');
        if( !$feu->LoggedInId() ) return FALSE;
        return TRUE;
    }

    public function dispose(ProductSpec $productSpec)
    {
        $ticket = null;
        try {
            $mod = \cms_utils::get_module(MOD_CGPRODUCTBUILDER);
            $cgct = \cms_utils::get_module('CGClientTickets');
            if( !is_object($cgct) ) {
                audit('',$mod->GetName(),'tickets disposition enabled and selected, but the CGClientTickets module could not be found.');
                throw new \RuntimeException('Internal Error: The CGClientTickets module could not be found.');
            }
            $feu = \cms_utils::get_module('FrontEndUsers');
            if( !is_object($feu) ) {
                audit('',$mod->GetName(),'tickets disposition enabled and selected, but the FEU module could not be found');
                throw new \RuntimeException('Internal Error: The FrontEndUsers module could not be found.');
            }
            $feu_uid = $feu->LoggedInId();
            if( $feu_uid < 1 ) {
                audit('',$mod->GetName(),'tickets disposition enabled and selected, but user is not logged in to FEU');
                throw new \RuntimeException('Internal Error: The FrontEndUsers module could not be found.');
            }

            //
            // generate a PDF file in a temporary location
            //

            // give some stuff to smarty
            $smarty = cmsms()->GetSmarty();
            $smarty->assign('productSpec',$productSpec);
            $smarty->assign('content',$productSpec->content);
            $feu = \cms_utils::get_module('FrontEndUsers');
            $smarty->assign('feu_mod',$feu);
            $html = $mod->ProcessTemplateFromDatabase('pdf_template');
            $html2pdf = new \CGHTML2PDF\html2pdf(array());
            $html2pdf->writeHTML($html);
            $tmpfile = TMP_CACHE_LOCATION.'/'.md5(__FILE__.session_id().time()).'.pdf';
            $html2pdf->output($tmpfile,'F');
            $prefs = $mod->get_ticket_disposition_prefs();

            //
            // generate a new ticket
            //
            $ticket = new \CGClientTickets\ticket;
            $ticket->priority = $ticket::PRIORITY_NORMAL;
            $ticket->owner = $feu_uid;
            $ticket->alert_group = $prefs['notify_group'];
            $ticket->save();

            $message = new \CGClientTickets\message;
            $message->dest = $ticket->alert_group;
            $message->ticket_id = $ticket->id;
            $message->sender = $feu_uid;
            $message->subject = $mod->ProcessTemplateFromData($prefs['ticket_subject']);
            $message->body = $mod->ProcessTemplateFromData($prefs['ticket_template']);
            $message->queue_attach($tmpfile);
            $message->save();

            // now send the thing.
            $message->send();

            // now what do we do?
            switch( $prefs['after'] ) {
            case 'redirect':
                $page = $mod->resolve_alias_or_id($prefs['redirect_page'],\ContentOperations::get_instance()->GetDefaultContent());
                $mod->RedirectConent($page);
                break;

            case 'message':
            default:
                $msg = $smarty->fetch('string:'.$prefs['after_msg']);
                return $msg;
            }
        }
        catch( \Exception $e ) {
            if( is_object($ticket) ) $ticket->delete();
            throw $e;
        }
    }
} // end of class

?>
