Hesk Enhancements (v2.1)

This page outlines the enhancements I've made to the free Hesk Helpdesk software available from http://www.hesk.com.

Category Listing Modifications

Show a Single Problem Type

If you have Help Desk system serving a number of clients, or use it as a contact system, you may want to direct users to a specific problem type to eliminate the possibility of them entering the wrong data somewhere, or getting confused by the interface. Others have written similar code on the Hesk Forums, but this is what I wrote before I found those alternatives.

This is the code that normally loads the categories:

169 |h index.php
hesk_dbConnect();
$sql = 'SELECT * FROM `'.hesk_dbEscape($hesk_settings['db_pfix']).'categories` ORDER BY `cat_order` ASC';
$result = hesk_dbQuery($sql);

With a simple modification, we can add a category URL parameter, and limit what is returned:

169 |h index.php
hesk_dbConnect();
if(isset($_GET['c'])){
        $sql = 'SELECT * FROM `'.hesk_dbEscape($hesk_settings['db_pfix']).'categories` WHERE `id`=' . hesk_dbEscape($_GET['c']) . ' ORDER BY `cat_order` ASC';
}
else{
        $sql = 'SELECT * FROM `'.hesk_dbEscape($hesk_settings['db_pfix']).'categories` ORDER BY `cat_order` ASC';
}
$result = hesk_dbQuery($sql);
|h (Without Line Numbers) index.php |h
hesk_dbConnect();
if(isset($_GET['c'])){
                $sql = 'SELECT * FROM `'.hesk_dbEscape($hesk_settings['db_pfix']).'categories` WHERE `id`=' . hesk_dbEscape($_GET['c']) . ' ORDER BY `cat_order` ASC';
        }
        else{
                $sql = 'SELECT * FROM `'.hesk_dbEscape($hesk_settings['db_pfix']).'categories` ORDER BY `cat_order` ASC';
        }
$result = hesk_dbQuery($sql);

With this modification, you can send users directly to your Hesk Helpdesk and to a specific problem type through a URL like: http://www.yoursite.com/index.php?a=add&c=2 This URL would load category #2 from the DB only. You can get the category ID numbers through the Admin Panel when you create/view your categories.

Custom Fields

Display Custom Fields Dependant on Problem Types

Hesk allows you to insert custom fields into your ticket submissions, but if you are like me, and using this in a multi-project environment, not all fields will apply to all users. As there is no way natively to associate fields to categories, I've developed this little hack to do just that.

  • This modification depends on the modification above for a single problem type display. Please put this into place before you attempt to implement the solution below.
  • This solution also uses Javascript and currently provides no fallback if the user has it disabled. You may want to consider your audience before deploying this solution.
  • This tutorial walks you through the steps using line numbers based on the line numbers as modified by the tutorial. If you skip steps, the numbers may be off, or if you have your own customizations in Hesk, they may also throw off the line numbers. I'll include before and after for reference.
  • This solution currently only modifies the add ticket and view ticket screens, and not the admin views.

1: Modify The Initial Handler

The first step is to modify the code after the comment:

65|h index.php
/* What should we do? */

And add in a custom handler:

65|h index.php
/* What should we do? */
$action=hesk_input($_REQUEST['a']) or $action='start';
if ($action == 'start') {print_start();}
elseif(!isset($_GET['c'])) {print_add_ticket_start();}
elseif ($action == 'add') {print_add_ticket();}
elseif ($action == 'forgot_tid') {forgot_tid();}
else {hesk_error($hesklang['invalid_action']);}

Line 68 is the new line, and it directs the page to a new function if a category is not selected. As indicated above, this depends on my solution to a single category selection. If you are using another selection method, you will need to either switch to mine, or modify the variable within !isset() to check the variable you have set.

2: Add in Custom Function

Now, you will need to add in a new function. I based it on the print_add_ticket() function but stripped it down to the bare minimum. I've got the entire function below, please copy and paste it into your index.php file on line 79.

|h index.php
function print_add_ticket_start() {
global $hesk_settings, $hesklang;
?>
 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="3"><img src="img/headerleftsm.jpg" width="3" height="25" alt="" /></td>
<td class="headersm"><?php hesk_showTopBar($hesklang['submit_ticket']); ?></td>
<td width="3"><img src="img/headerrightsm.jpg" width="3" height="25" alt="" /></td>
</tr>
</table>
 
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><span class="smaller"><a href="<?php echo $hesk_settings['site_url']; ?>" class="smaller"><?php echo $hesk_settings['site_title']; ?></a> &gt;
<a href="<?php echo $hesk_settings['hesk_url']; ?>" class="smaller"><?php echo $hesk_settings['hesk_title']; ?></a>
&gt; <?php echo $hesklang['submit_ticket']; ?></span></td>
</tr>
</table>
 
</td>
</tr>
<tr>
<td>
 
	<?php
	    if(isset($_SESSION['HESK_ERROR']))
        {
	?>
	        <div align="center">
	        <table border="0" width="600" id="error" cellspacing="0" cellpadding="3">
		        <tr>
		        	<td align="left" class="error_header">&nbsp;<img src="img/error.gif" style="vertical-align:text-bottom" width="16" height="16" alt="" />&nbsp; <?php echo $hesklang['error']; ?></td>
		        </tr>
		        <tr>
		        	<td align="left" class="error_body"><ul><?php echo $_SESSION['HESK_MESSAGE']; ?></ul></td>
		        </tr>
	        </table>
	        </div>
            <br />
	<?php
	        unset($_SESSION['HESK_ERROR']);
	        unset($_SESSION['HESK_MESSAGE']);
	    }
	?>
 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
	<td width="7" height="7"><img src="img/roundcornerslt.jpg" width="7" height="7" alt="" /></td>
	<td class="roundcornerstop"></td>
	<td><img src="img/roundcornersrt.jpg" width="7" height="7" alt="" /></td>
</tr>
<tr>
	<td class="roundcornersleft">&nbsp;</td>
	<td>
    <!-- START FORM -->
 
	<p><?php echo $hesklang['use_form_below']; ?>
	<font class="important"> *</font></p>
 
	<form method="post" action="submit_ticket.php" name="form1" enctype="multipart/form-data"
	<?php
	if ($hesk_settings['kb_enable'] && $hesk_settings['kb_recommendanswers'] && $_SESSION['ARTICLES_SUGGESTED'] == false)
	{
		echo 'onsubmit="Javascript: return hesk_suggestKB();"';
	}
	?>
    >
 
	<!-- Contact info -->
 
	<!-- Department and priority -->
	<table border="0" width="100%">
	<tr>
	<td style="text-align:right" width="150"><?php echo $hesklang['category']; ?>: <font class="important">*</font></td>
	<td width="80%"><select name="category" onchange="window.location='index.php?a=add&c=' + this.value;">
	<?php
	require(HESK_PATH . 'inc/database.inc.php');
 
	hesk_dbConnect();
	if(isset($_GET['c'])){
		$sql = 'SELECT * FROM `'.hesk_dbEscape($hesk_settings['db_pfix']).'categories` WHERE `id`=' . hesk_dbEscape($_GET['c']) . ' ORDER BY `cat_order` ASC';
	}
	else{
 
		$sql = 'SELECT * FROM `'.hesk_dbEscape($hesk_settings['db_pfix']).'categories` ORDER BY `cat_order` ASC';
	}
	$result = hesk_dbQuery($sql);
	while ($row=hesk_dbFetchAssoc($result))
	{
	    if (isset($_SESSION['c_category']) && $_SESSION['c_category'] == $row['id']) {$selected = ' selected="selected"';}
	    else {$selected = '';}
	    echo '<option value="'.$row['id'].'"'.$selected.'>'.$row['name'].'</option>';
	}
 
	?>
	</select></td>
	</tr>
	</table>
 
	</form>
 
    <!-- END FORM -->
	</td>
	<td class="roundcornersright">&nbsp;</td>
</tr>
<tr>
	<td><img src="img/roundcornerslb.jpg" width="7" height="7" alt="" /></td>
	<td class="roundcornersbottom"></td>
	<td width="7" height="7"><img src="img/roundcornersrb.jpg" width="7" height="7" alt="" /></td>
</tr>
</table>
 
<?php
} // End print_add_ticket_start()

3: Modify print_add_ticket()

Now comes the hard part. Modifying the print_add_ticket function to handle our custom fields. Make sure you don't skip/leave out any part of this, as it may cause the whole page to fail to load/function correctly.

On line 303, add a link that can take users back to another Category if needed:

295|h index.php
	while ($row=hesk_dbFetchAssoc($result))
	{
	    if (isset($_SESSION['c_category']) && $_SESSION['c_category'] == $row['id']) {$selected = ' selected="selected"';}
	    else {$selected = '';}
	    echo '<option value="'.$row['id'].'"'.$selected.'>'.$row['name'].'</option>';
	}
 
	?>
	</select> <a href="index.php?a=add">Select Another Category</a></td>
	</tr>

Now, we will need to go into the custom fields loop and do a check for the fields we want to show. This bit of code looks at the name of each field, splits on the _ and checks to see if the category ID matches the one specified in the URL. For this reason, you cannot use _ in the name of a field any more. You can specify multiple category ids be separating them with commas.

So, on line 323, the first loop starts. Add the code in line 325 to 327.

317|h index.php
	<!-- START CUSTOM BEFORE -->
	<?php
	/* custom fields BEFORE comments */
 
	$print_table = 0;
 
	foreach ($hesk_settings['custom_fields'] as $k=>$v)
	{
		$catsforcustom = explode('_',$v['name']);
		$catsforcustom = explode(',',$catsforcustom[0]);
		if(!in_array($_GET['c'],$catsforcustom)){continue;}
		if ($v['use'] && $v['place']==0)

The code basically tells the script to not print custom fields if the category ID isn't specified before the name.

Deeper in the function we need to modify the printed name of the custom field, otherwise it will print the numbers before the name. Add lines 358 and 359 below.

353|h index.php
            else
            {
            	$k_value  = '';
            }
 
		$temp = explode('_',$v['name']);
		$v['name'] = $temp[1];
	        switch ($v['type'])
	        {
	        	/* Radio box */

Now we need to make the same two changes to the custom fields that are set to display after the Message box. Add lines 509 - 511.

502|h index.php
	<!-- START CUSTOM AFTER -->
	<?php
	/* custom fields AFTER comments */
	$print_table = 0;
 
	foreach ($hesk_settings['custom_fields'] as $k=>$v)
	{
		$catsforcustom = explode('_',$v['name']);
		$catsforcustom = explode(',',$catsforcustom[0]);
		if(!in_array($_GET['c'],$catsforcustom)){continue;}
		if ($v['use'] && $v['place'])
		{

And lines 542 - 543.

537|h index.php
            else
            {
            	$k_value  = '';
            }
 
		$temp = explode('_',$v['name']);
		$v['name'] = $temp[1];
	        switch ($v['type'])
	        {
	        	/* Radio box */

4: Modify ticket.php

You will now need to modify ticket.php to filter out unneeded custom fields, and to lop off the catids at the start. This will affect the clients view of the ticket to not include unnecessary fields. Only a few changes needed here, same as the custom field loops in the index.php file. Add lines 244 to 246, 254 and 255, 280 to 282, 290 and 291.

238|h ticket.php
		/* custom fields before message */
		$print_table = 0;
		$myclass = ' class="tickettd"';
 
		foreach ($hesk_settings['custom_fields'] as $k=>$v)
		{
			$catsforcustom = explode('_',$v['name']);
			$catsforcustom = explode(',',$catsforcustom[0]);
			if(!in_array($ticket['category'],$catsforcustom)){continue;}
			if ($v['use'] && $v['place']==0)
		    {
		    	if ($print_table == 0)
		        {
		        	echo '<table border="0" cellspacing="1" cellpadding="2">';
		        	$print_table = 1;
		        }
				$temp = explode('_',$v['name']);
				$v['name'] = $temp[1];
		        echo '
				<tr>
				<td valign="top" '.$myclass.'>'.$v['name'].':</td>
				<td valign="top" '.$myclass.'>'.$ticket[$k].'</td>
				</tr>
		        ';
		    }
		}
		if ($print_table)
		{
			echo '</table>';
		}
		?>
 
		<p><b><?php echo $hesklang['message']; ?>:</b></p>
		<p><?php echo $ticket['message']; ?><br />&nbsp;</p>
 
		<?php
		/* custom fields after message */
		$print_table = 0;
		$myclass = 'class="tickettd"';
 
		foreach ($hesk_settings['custom_fields'] as $k=>$v)
		{
			$catsforcustom = explode('_',$v['name']);
			$catsforcustom = explode(',',$catsforcustom[0]);
			if(!in_array($ticket['category'],$catsforcustom)){continue;}
			if ($v['use'] && $v['place'])
		    {
		    	if ($print_table == 0)
		        {
		        	echo '<table border="0" cellspacing="1" cellpadding="2">';
		        	$print_table = 1;
		        }
				$temp = explode('_',$v['name']);
				$v['name'] = $temp[1];
		        echo '
				<tr>
				<td valign="top" '.$myclass.'>'.$v['name'].':</td>
				<td valign="top" '.$myclass.'>'.$ticket[$k].'</td>
				</tr>
		        ';
		    }
		}
		if ($print_table)
		{
			echo '</table>';
		}

(Optional) 5. Modify admin_ticket.php

Optionally you can choose to customize the admin_ticket.php file. This will cause the custom fields to appear to administrators and helpdesk staff as they appear to end users.

You will need to make the following changes to the admin_ticket.php file. Line numbers are displayed assuming the additions are done in this order.

Add line 484-486:

478|h admin_ticket.php
                        /* custom fields before message */
                        $print_table = 0;
                        $myclass = ' class="tickettd"';
 
                        foreach ($hesk_settings['custom_fields'] as $k=>$v)
                        {
                                $catsforcustom = explode('_',$v['name']);
                                $catsforcustom = explode(',',$catsforcustom[0]);
                                if(!in_array($ticket['category'],$catsforcustom)){continue;}
                                if ($v['use'] && $v['place']==0)
                            {

Add line 493-495:

488|h admin_ticket.php
                        /* custom fields after message */
                        $print_table = 0;
 
                        foreach ($hesk_settings['custom_fields'] as $k=>$v)
                        {
                                $catsforcustom = explode('_',$v['name']);
                                $catsforcustom = explode(',',$catsforcustom[0]);
                                if(!in_array($ticket['category'],$catsforcustom)){continue;}
                                if ($v['use'] && $v['place']==0)
                            {

Add line 494-495:

489|h admin_ticket.php
                                if ($print_table == 0)
                                {
                                        echo '<table border="0" cellspacing="1" cellpadding="2">';
                                        $print_table = 1;
                                }
                                $temp = explode('_',$v['name']);
                                $v['name'] = $temp[1];

Add line 529-530:

524|h admin_ticket.php
                                if ($print_table == 0)
                                {
                                        echo '<table border="0" cellspacing="1" cellpadding="2">';
                                        $print_table = 1;
                                }
                                $temp = explode('_',$v['name']);
                                $v['name'] = $temp[1];
                                echo '

Setting Up Your Fields

When you enter your custom fields, you will now need to prefix each field with the id number of the category it should appear for. So, if you want some fields to appear for Category 5, and one to appear for both 3 and 5, you will enter them like this:

You can get the category ids by visiting the Categories menu in the Admin screen. Hover your mouse over the move/delete buttons, and the URL that appears will contain the catid= of the category.

Concluding Thoughts

Now, that is all you will need to do. Save your file and upload it to your site, and you now have custom fields for problem types. Make sure you test this before you deploy it, as it may negate some other functionality you were using.

Also, you may notice that I didn't modify the admin view of tickets. This means that as an admin all custom fields will appear, with the category numbers in front of them. This is currently by design, so you can see where everything is/came from. I will consider changing the admin view too depending on my usage and comments I receive.

hesk/hesk21.txt · Last modified: 2010/10/01 19:59 (external edit)
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0