Plugin:Header Filters

From Cerberus Helpdesk Wiki

Jump to: navigation, search

[edit] Header Filters

This was created out of a request to be able to copy the *contents* of an email header into a custom field (rather than just doing something static based on the contents of the header). To create this plugin, go to your cerb4/plugins directory, and make a couple directories and three files, like so:

plugins/wgm.header_filters
plugins/wgm.header_filters/plugin.xml
plugins/wgm.header_filters/plugin.php
plugins/wgm.header_filters/templates
plugins/wgm.header_filters/templates/header_filter_action.tpl

The contents of the files are:

plugin.xml

<!DOCTYPE plugin SYSTEM "../../libs/devblocks/plugin.dtd">
<plugin>
	<id>wgm.header_filters</id>
	<name>WGM Custom Dev: Header to Custom Field Filters</name>
	<description>Allows you to use mail filters to set Custom Fields based on the value of a specified header.</description>
	<author>WebGroup Media, LLC.</author>
	<revision>0</revision>
	<link>http://wiki.cerb4.com/wiki/Plugin:Header_Filters</link>	
	<extensions>
		<extension point="cerberusweb.mail_filter.action">
			<id>wgm.header_filters.action.copy</id>
			<name>Action: Copy Header Value to Custom Field</name>
			<class>
				<file>plugin.php</file>
				<name>HeaderFilterCopyAction</name>
			</class>
			<params/>
		</extension>
	</extensions>
</plugin>

plugin.php

<?php
class HeaderFilterCopyAction extends Extension_MailFilterAction {
	const EXTENSION_ID = 'wgm.header_filters.action.copy';
 
	function __construct($manifest) {
		$this->DevblocksExtension($manifest,1);
	}
 
	function run(Model_PreParseRule $filter, CerberusParserMessage $message) {
		$message_headers = $message->headers;
		$ticket_fields = DAO_CustomField::getAll();
		$params = $filter->actions[self::EXTENSION_ID];
		$headers = $params['headers'];
		$custom_fields = $params['custom_fields'];
 
		foreach($headers as $idx => $header) {
			if($message_headers[strtolower($header)] != null) {
				$header_value = $message_headers[strtolower($header)];
 
				// handle array headers
				if(is_array($header_value)) {
					$value = DevblocksPlatform::parseCrlfString(implode('\r\n',$header_value));
				} else {
					$value = DevblocksPlatform::parseCrlfString($header_value);
				}
 
				// collapse multi-line headers to single line for single-line text fields
				if($ticket_fields[$custom_fields[$idx]]->type == Model_CustomField::TYPE_SINGLE_LINE) {
					$message->custom_fields[$custom_fields[$idx]] 
						= trim(implode(' ',$value));
				} elseif($ticket_fields[$custom_fields[$idx]]->type == Model_CustomField::TYPE_MULTI_LINE) {
					$message->custom_fields[$custom_fields[$idx]] 
						= trim(implode('\r\n',$value));
				}
			}
		}
	}
 
	function renderConfig(Model_PreParseRule $filter=null) {
		$tpl = DevblocksPlatform::getTemplateService();
		$path = dirname(__FILE__) . '/templates/';
 
		$groups = DAO_Group::getAll();
		$tpl->assign('groups', $groups);
 
		$ticket_fields = DAO_CustomField::getBySource('cerberusweb.fields.source.ticket');
		$tpl->assign('ticket_fields', $ticket_fields);
 
		$params = $filter->actions[self::EXTENSION_ID];
		$tpl->assign('headers',$params['headers']);
		$tpl->assign('custom_fields',$params['custom_fields']);
 
		$tpl->display($path.'header_filter_action.tpl');
	}
 
	function saveConfig() {
		$headers = DevblocksPlatform::importGPC($_REQUEST['header'],'array',array());
		$custom_fields = DevblocksPlatform::importGPC($_REQUEST['custom_field'],'array',array());
 
		return array(
			'headers' => $headers,
			'custom_fields' => $custom_fields,
		);
	}
 
};

header_filter_action.tpl

Specify the header to copy to the selected Custom Field:
<br>
<table cellpadding="2" cellspacing="0" border="0" width="100%">
	{section name=header_section start=0 loop=5 max=5 step=1}
		<tr>
			<td width="50%" valign="top">
				<input type="text" name="header[]" size="45" value="{$headers[$smarty.section.header_section.index]}"></input>
			</td>
			<td width="50%" valign="top">
				<select name="custom_field[]">
					<option value=""></option>
					{foreach from=$ticket_fields item=f key=f_id}
						{if $f->type == "S" || $f->type == "T"}
							{assign var=field_group_id value=$f->group_id}
							<option value="{$f_id}" {if $f_id==$custom_fields[$smarty.section.header_section.index]}selected{/if}>
								{if isset($groups.$field_group_id)}{$groups.$field_group_id->name}: {/if}{$f->name|escape}
							</option>
						{/if}
					{/foreach}
				</select>
			</td>
		</tr>
	{/section}
</table>

After creating these files, go to your Helpdesk Setup -> Features & Plugins tab, enable the new plugin, and then you're all set to use it (Helpdesk Setup -> Mail Filters, create a filter to match the messages you want, and then add the custom field copying actions. For more than 5, create a second rule.)

Personal tools