<?php

function plugin_tickettypes_variables()
{
    $variables = array(
        'Plugin Name'       => array(
            'type'        => 'hidden',
            'description' => '',
            'value'       => 'Support ticket type editor',
        ),
        '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_tickettypes_view()
{
    if (isset($_POST['tickettypes_update'])) {
        $myorder = $_POST['tickettypes_myorder'];
        $name = $_POST['tickettypes_name'];
        $query = "UPDATE troubleticket_type SET name = ? WHERE myorder = ?";
        $GLOBALS['db']->query($query, $name, $_POST['tickettypes_myorder']);
        $warning = 'Record has been updated ';
    }

    If (isset($_POST['tickettypes_delete'])) {
        $query = "DELETE FROM troubleticket_type WHERE myorder = ?";
        $GLOBALS['db']->query($query, $_POST['tickettypes_myorder']);
        $warning = 'Record has been deleted ';
    }
    
    If (isset($_POST['tickettypes_add'])) {
        $myorder = $_POST['tickettypes_myorder'];
        $name = $_POST['tickettypes_name'];
        If (!isset($myorder) || $myorder == '') {
            $warning = 'You must choose an id';
        }
        If (!isset($name) || $name == '') {
            $warning = 'You must choose a name';
        }
        If ((isset($myorder) && $myorder != '') && (isset($name) && $name != '')) {
            $checkquery = "SELECT COUNT(*) FROM troubleticket_type WHERE name = ?";
            $result = $GLOBALS['db']->query($checkquery, $name);
            list($repeated) = $result->fetch();
            if($repeated > 0) {
                $warning = 'Record already exists, choose another name';
            }
            $checkquery2 = "SELECT COUNT(*) FROM troubleticket_type WHERE myorder = ?";
            $result2 = $GLOBALS['db']->query($checkquery2, $myorder);
            list($repeated2) = $result2->fetch();
            if($repeated2 > 0) {
                $warning = 'Record already exists, choose another id';
            }
            if($repeated == 0 && $repeated2 == 0) {
                $query = "INSERT INTO troubleticket_type (myorder, name) VALUES(?, ?)";
                $GLOBALS['db']->query($query, $myorder, $name);
                $warning = 'Record has been added';
            }
        }
    }

    // Get list of troubleticket_type 's
    $sql = "SELECT name, myorder FROM troubleticket_type";
    $result = $GLOBALS['db']->query($sql);

    // Loop through results and add give update options
    //Setup the layout of the table
    $output = "<table border=1  width=90% bordercolor=dfdfdf>";
    if (isset($warning)) {
            $output .= "<tr><td colspan=\"3\"><span style=\"color: red; font-weight: bold;\">$warning</span></td></tr>";
    }
    $output .= "<tr>
            <td align=center><b>".lang("Ticket Type ID")."</b></td>
            <td align=center><b>".lang("Ticket Type Description")."</b></td>
            <td align=center><b>".lang("Actions")."</b></td></tr>";

    $fuse = cADMIN;
    $action = cADMIN_ADDONS;

    // Loop through results and generate the data
    while (list($name, $myorder) = $result->fetch())
    {
        $output .= "
        <tr>
            <td align=center><form action=\"index.php?fuse={$fuse}&action=$action&plugin=tickettypes\" method=\"POST\">
            <input type=\"hidden\" name=\"tickettypes_myorder\" value=\"$myorder\"><strong>" . $myorder . "</strong></td>
            <td align=center><input size=\"20\" maxlength=\"40\" type=\"text\" name=\"tickettypes_name\" value=\"$name\">
            </td>
            <td align=center><input type=\"submit\" name=\"tickettypes_update\" value=\"".lang("update")."\"  class=\"button_medium\">
            <input type=\"submit\" name=\"tickettypes_delete\" value=\"".lang("delete")."\"  class=\"button_medium\" onClick=\"return confirmSubmit()\"></form> </td></tr> ";
    }
    $output .= "<tr><form method=\"POST\" action=\"index.php?fuse={$fuse}&action=$action&plugin=tickettypes\">
            <td align=center> <input type=\"text\" name=\"tickettypes_myorder\" size=\"5\" maxlength=\"4\" value=\"\"></td>
             <td align=center> <input size=\"20\" maxlength=\"40\" type=\"text\" name=\"tickettypes_name\" value=\"$name\"></td>
              <td align=center><input  type=\"submit\" name=\"tickettypes_add\" value=\"".lang("add")."\" class=\"button_medium\" ></form></td>
              </tr></table> ";

    return $output;
}

?>
