<?php

require_once 'classes/SnapinPlugin.php';

class PluginTicketsearch extends SnapinPlugin
{
    function getVariables()
    {
        $variables = array(
            /*T*/'Plugin Name'/*/T*/       => array(
                'type'        => 'hidden',
                'description' => /*T*/''/*/T*/,
                'value'       => 'Support tickets search',
            ),
        );
    
        return $variables;
    }
    
    function view()
    {
	if(!isset($this->configuration['modules']['support']['installedVersion'])){
                echo("Sorry!To see this page, you need to install support module");
                return "";
        }
        if (NE_Lib::isPublicDir()) {
            $this->tpl->define(array('ticketsearch' => '../../plugins/snapins/ticketsearch/PublicSection_ticketsearch.tpl'));
        } else {
            $this->tpl->define(array('ticketsearch' => '../../plugins/snapins/ticketsearch/ticketsearch.tpl'));
        }
        
        //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 = 10;
        }
        
        $keywords = str_replace(' ', '%', $keywords);
        $keywords = $this->db->escape_string($keywords);
        
        $this->tpl->setKey(array(
                'keywords'          =>      htmlspecialchars($keywords),
                'limit'             =>      $limit,
                ));
    
        $keywords = str_replace(' ', '%', $keywords);
    
        //Only execute the query if the Submit button is pressed
        if (isset($_POST['ticketsearch_submit']))
        {
            $constraint = "";
            $args = array();
            
            // 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 ? AND ttl.troubleticketid=tt.id AND ttl.id IS NOT NULL ";
            $args[] = '%'.$keywords.'%';
    
            if (!$this->user->isAdmin()) {
                $query .= " AND tt.userid=? ";
                $args[] = $this->user->getId();
            }
            
            $query .= " LIMIT 0,$limit";
            
            // Execute the query
            $result = $this->db->query($query, $args);
            
            // Loop through results and generate the data
            while (list($subject, $troubleticketid, $message, $userid, $mydatetime, $customerId) = $result->fetch())
            {
                if (strlen($subject) > 25) {
                    $subject = substr($subject, 0, 22)."...";
                }
                $user =& new User($userid);
                
                $this->tpl->setKey(array(
                        'customerid'        => $customerId,
                        'ticketid'          => $troubleticketid,
                        'ticketsubject'     => $subject,
                        'message'           => $message,
                        'username'          => $user->getFullName(),
                        'date'              => $mydatetime,
                        
                
                ));
                $this->tpl->parse('row_out', '.row');
            }
            
            $this->tpl->parse('results_out', 'results');
        }
    
        $this->tpl->parse('output', 'ticketsearch');
        return $this->tpl->fetch('output');
        
    }
    
    function getDashboardData($statsurl)
    {
    }
}
?>
