<?php

function plugin_ticketsearch_variables()
{
    $variables = array(
        'Plugin Name'       => array(
            'type'        => 'hidden',
            'description' => '',
            'value'       => 'Support tickets search',
        ),
        'Viewable by Support Managers'   => array(
            'type'        => 'yesno',
            'description' => '',
            'value'       => false,
        ),
        'Viewable by Support Staff members'   => array(
            'type'        => 'yesno',
            'description' => '',
            'value'       => false,
        ),
        'Viewable by Billing Staff members'   => array(
            'type'        => 'yesno',
            'description' => '',
            'value'       => false,
        ),
        'Viewable by Customers'   => array(
            'type'        => 'yesno',
            'description' => '',
            'value'       => false,
        ),
    );

    return $variables;
}

function plugin_ticketsearch_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 ($GLOBALS['user']->isAdmin()) {
        $fuse = cADMIN;
        $action = cADMIN_ADDONS;
    } else {
        $fuse = cCLIENTS;
        $action = cCLIENTS_SHOWADDONS_CUSTOMER;
    }
    $output = <<<EOD
        <form action="{$_SERVER['PHP_SELF']}?fuse={$fuse}&action={$action}&plugin=ticketsearch" method="POST">
            Search Words: <input type="text" name="ticketsearch_keywords" value="$keywords"><BR>
            Limit to # results: <input type="text" name="ticketsearch_limit" size="3" value="$limit">
            <input type="submit" name="ticketsearch_submit" class=button_medium value="submit">
            <input type="reset"class=button_medium>
        </form>
EOD;

    $keywords = str_replace(' ', '%', $keywords);
    $keywords = $GLOBALS['db']->escape_string($keywords);

    //Only execute the query if the Submit button is pressed
    if (isset($_POST['ticketsearch_submit']))
    {
        $constraint = "";
        if (!$GLOBALS['user']->isAdmin()) {
            $constraint = " AND tt.userid=".$GLOBALS['user']->getId();
        }
        // Get list of support tickets according to search criteria
        $query = "SELECT tt.subject, ttl.troubleticketid, ttl.message, ttl.userid, ttl.mydatetime 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 = $GLOBALS['db']->query($query);

        //Setup the layout of the table
        $output .= <<<EOD
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>keyword searched for</strong>: $keywords
        <br><br><b><center>Result of Support Ticket Search</center></b><br><br>
        <table border=1  width=100% bordercolor=dfdfdf><tr><td align=center><b>Ticket Number</b></td>
        <td align=center><b>Ticket Subject</b></td><td align=center><b>Message</b></td>
        <td align=center><b>User</b></td><td align=center><b>Date</b></td></tr>
EOD;

        // Loop through results and generate the data
        while (list($subject, $troubleticketid, $message, $userid, $mydatetime) = $result->fetch())
        {
            $user = new User($userid);
            $user->populateCustomFields();
            if (strlen($subject) > 25) {
                $subject = substr($subject, 0, 22)."...";
            }
            $output .= " <tr><td align=center><a href=index.php?frmClientID=$userid&fuse=5&action=5005&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;
}

?>
