<?php

require_once 'classes/SnapinPlugin.php';

class PluginTicketsearch extends SnapinPlugin
{
    var $menuLocation = 'support';

    function getVariables()
    {
        $variables = array(
            /*T*/'Plugin Name'/*/T*/       => array(
                'type'        => 'hidden',
                'description' => /*T*/''/*/T*/,
                'value'       => 'Support tickets search',
            ),
            /*T*/'Viewable by Support Managers'/*/T*/   => array(
                'type'        => 'yesno',
                'description' => /*T*/''/*/T*/,
                'value'       => false,
            ),
            /*T*/'Viewable by Support Staff members'/*/T*/   => array(
                'type'        => 'yesno',
                'description' => /*T*/''/*/T*/,
                'value'       => false,
            ),
            /*T*/'Viewable by Billing Staff members'/*/T*/   => array(
                'type'        => 'yesno',
                'description' => /*T*/''/*/T*/,
                'value'       => false,
            ),
            /*T*/'Viewable by Customers'/*/T*/   => array(
                'type'        => 'yesno',
                'description' => /*T*/''/*/T*/,
                'value'       => false,
            ),
        );
    
        return $variables;
    }
    
    function view()
    {
        //Create the form to allow input of keywords
        $keywords   = isset($_POST['ticketsearch_keywords']) ? $_POST['ticketsearch_keywords'] : '';
        $limit      = isset($_POST['ticketsearch_limit'])? $_POST['ticketsearch_limit'] : '';;
        if ($limit <1 || !is_numeric($limit)) { $limit = 1; }
    
        if ($this->user->isAdmin()) {
            $fuse = 'admin';
            $action = 'Snapins';
        } else {
            $fuse = 'clients';
            $action = 'ShowSnapinsCustomer';
        }
        $output = '            <form action="'.$_SERVER['PHP_SELF'].'?fuse='.$fuse.'&view='.$action.'&plugin=ticketsearch" method="POST">'
                 ."\n".'                '.$this->user->lang("Search Words").': <input type="text" name="ticketsearch_keywords" value="'.htmlspecialchars($keywords).'"><BR>'
                 ."\n".'                '.$this->user->lang("Limit to # results").': <input type="text" name="ticketsearch_limit" size="3" value="'.$limit.'">'
                 ."\n".'                <input type="submit" name="ticketsearch_submit" class=button_medium value='.$this->user->lang("Submit").'>'
                 ."\n".'                <input type="reset"class=button_medium value='.$this->user->lang("Reset").'>'
                 ."\n".'            </form>';
    
        $keywords = str_replace(' ', '%', $keywords);
        $keywords = $this->db->escape_string($keywords);
    
        //Only execute the query if the Submit button is pressed
        if (isset($_POST['ticketsearch_submit']))
        {
            $constraint = "";
            if (!$this->user->isAdmin()) {
                $constraint = " AND tt.userid=".$this->user->getId();
            }
            // Get list of support tickets according to search criteria
            $query = "SELECT tt.subject, ttl.troubleticketid, ttl.message, ttl.userid, ttl.mydatetime, tt.userid FROM troubleticket_log ttl, troubleticket tt  WHERE ttl.message LIKE '%$keywords%' AND ttl.troubleticketid=tt.id $constraint AND ttl.id IS NOT NULL LIMIT 0,$limit";
    
            // Execute the query
            $result = $this->db->query($query);
    
            //Setup the layout of the table
            $output .= '            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>'.$this->user->lang("keyword searched for").'</strong>: '.htmlspecialchars($keywords)
                      ."\n".'            <br><br><b><center>'.$this->user->lang("Result of Support Ticket Search").'</center></b><br><br>'
                      ."\n".'            <table border=1  width=100% bordercolor=dfdfdf><tr><td align=center><b>'.$this->user->lang("Ticket Number").'</b></td>'
                      ."\n".'            <td align=center><b>'.$this->user->lang("Ticket Subject").'</b></td><td align=center><b>'.$this->user->lang("Message").'</b></td>'
                      ."\n".'            <td align=center><b>'.$this->user->lang("User").'</b></td><td align=center><b>'.$this->user->lang("Date").'</b></td></tr>';
    
            // Loop through results and generate the data
            while (list($subject, $troubleticketid, $message, $userid, $mydatetime, $customerId) = $result->fetch())
            {
                $user = new User($userid);
                if (strlen($subject) > 25) {
                    $subject = substr($subject, 0, 22)."...";
                }
                $output .= " <tr><td align=center><a href=index.php?frmClientID=$customerId&fuse=support&view=ViewTicketDetails&ticketID=$troubleticketid>$troubleticketid</a></td><td align=left>$subject</td><td align=left style=\"padding-left: 2px;\">$message</a></td><td align=center>".$user->getFullName()."</td><td align=center>$mydatetime</td></tr> ";
            }
            $output .=" </table> ";
        }
    
        return $output;
    }
    
    function getDashboardData($statsurl)
    {
    }
}
?>
