Skip to content

Commit

Permalink
Resolved linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DonutsNL committed Nov 16, 2023
1 parent c04d9e3 commit ab47fce
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 149 deletions.
4 changes: 2 additions & 2 deletions front/filterpattern.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
use GlpiPlugin\Ticketfilter\FilterPattern;
use Plugin;

include ("../../../inc/includes.php");
include_once '../../../inc/includes.php';

Plugin::load('ticketfilter', true);

$dropdown = new FilterPattern();
include (GLPI_ROOT . "/front/dropdown.common.form.php");
include_once GLPI_ROOT . "/front/dropdown.common.form.php";
4 changes: 2 additions & 2 deletions front/filterpattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
use GlpiPlugin\Ticketfilter\FilterPattern;
use Plugin;

include ("../../../inc/includes.php");
include_once "../../../inc/includes.php";

Plugin::load('ticketfilter', true);

$dropdown = new FilterPattern();
include(GLPI_ROOT . '/front/dropdown.common.php');
include_once GLPI_ROOT . '/front/dropdown.common.php';
9 changes: 4 additions & 5 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@
* test
*/
// phpcs:ignore PSR1.Function.CamelCapsMethodName
function plugin_ticketfilter_getDropdown() : array
function plugin_ticketfilter_getDropdown() : array
{
return [FilterPattern::class => __("Filterpatterns", 'ticketfilter')];
}


/**
* Summary of plugin_ticketFilter install
* @return boolean
* @return booleansyste
* test
*/
// phpcs:ignore PSR1.Function.CamelCapsMethodName
//phpcs:ignore PSR1.Function.CamelCapsMethodName
function plugin_ticketfilter_install() : bool
{

Expand All @@ -70,11 +70,10 @@ function plugin_ticketfilter_install() : bool


/**
*
* Summary of plugin_ticketFilter uninstall
* @return boolean
*/
// phpcs:ignore PSR1.Function.CamelCapsMethodName
//phpcs:ignore PSR1.Function.CamelCapsMethodName
function plugin_ticketfilter_uninstall() : bool
{

Expand Down
11 changes: 2 additions & 9 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function plugin_init_ticketfilter() : void
// Add hook (callback) on the PRE_ITEM_ADD event.
// All new tickets are to be evaluated no matter the source.
$PLUGIN_HOOKS[HOOKS::PRE_ITEM_ADD]['ticketfilter'] = [
Ticket::class => [Filter::class, 'PreItemAdd']
Ticket::class => [Filter::class, 'preItemAdd']
];
}

Expand Down Expand Up @@ -102,9 +102,6 @@ function plugin_version_ticketfilter() : array
*/
function plugin_ticketfilter_check_prerequisites() : bool
{
if (false) {
return false;
}
return true;
}

Expand All @@ -116,12 +113,8 @@ function plugin_ticketfilter_check_prerequisites() : bool
*/
function plugin_ticketfilter_check_config($verbose = false) : bool
{
if (true) { // Your configuration check
return true;
}

if ($verbose) {
echo __('Installed / not configured', 'TICKETFILTER');
}
return false;
return (true) ? true : false;
}
58 changes: 25 additions & 33 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,19 @@ class Filter {
* @since 1.0.0
* @see setup.php hook
*/
public static function PreItemAdd(Ticket $item) : void
public static function preItemAdd(Ticket $item) : void
{
if(is_object($item)) {

if(is_array($item->input) // Fields should be an array with values.
&& key_exists('name', $item->input) // Name key (that is the Subject of the email) should exist.
&& !empty($item->input['name'])) { // Name should not be emtpy, could happen with recurring tickets.

// Search our pattern in the name field and find corresponding ticket(s) (if any)
self::searchForMatches($item);
} // Not an array with expected keys
} // Not a valid object.
if(is_object($item) &&
is_array($item->input) &&
key_exists('name', $item->input) && // Name key (that is the Subject of the email) should exist.
!empty($item->input['name'])) { // Name should not be emtpy, could happen with recurring tickets.
// Search our pattern in the name field and find corresponding ticket(s) (if any)
self::searchForMatches($item);
}
}

/**
* emptyReferencedObject(Ticket item) : void -
* emptyReferencedObject(Ticket item) : void -
* Clean the referenced item and delete any mailbox items remaining
*
* @param Ticket $item The original ticket passed by the pre_item_add hook.
Expand All @@ -86,15 +83,15 @@ private static function emptyReferencedObject(Ticket $item) : void
// We cancelled the ticket creation and by doing so the mailgate will not clean the
// email from the mailbox. We need to clean it manually.
// Send a meaningfull message if mailgate was triggered by user from UI.
if(self::deleteEmail($item) == true) {
if(self::deleteEmail($item) === true) {
Session::addMessageAfterRedirect(__("Ticket removed from mailbox, it is save to ignore related mailgate error"), true, WARNING);
}
$item->input = false;
$item->fields = false;
}

/**
* searchForMatches(Ticket item) : bool -
* searchForMatches(Ticket item) : bool -
* Perform a search in the glpi_tickets table using the searchString if one is found applying the
* provided ticket match patterns from dropdown on the ticket name (email subject).
*
Expand All @@ -111,7 +108,7 @@ private static function searchForMatches(Ticket $item) : bool

// Evaluate patterns
if(is_array($patterns)
&& count($patterns) >= 1
&& !empty($patterns)
&& array_key_exists(FilterPattern::TICKETMATCHSTR, $patterns['0']))
{
// Loop through the patterns
Expand All @@ -129,7 +126,6 @@ private static function searchForMatches(Ticket $item) : bool

// Protect against risky patterns or SQL injections by validating the length
// of the matchstring against what we expect it to be.
// todo: move to separate method for readability.
if(strlen($searchString) <= $Filterpattern[FilterPattern::TICKETMATCHSTRLEN]) {
$handler = new TicketHandler();
$r = $handler->searchTicketPool($searchString);
Expand All @@ -153,28 +149,28 @@ private static function searchForMatches(Ticket $item) : bool
trigger_error("TicketFilter: PregMatch failed! please review the pattern $p and correct it", E_USER_WARNING);
}
} // Pattern is configured inactive.
} // Loop.
} // Loop.
} else {
trigger_error("TicketFilter: No ticketfilter patterns found, please configure them or disable the plugin", E_USER_WARNING);
}

// Do we have at least 1 succesfull followup, prevent GLPI from creating a new ticket
if(is_array($itemIsMatched) &&
if(is_array($itemIsMatched) &&
in_array("matched", $itemIsMatched)) {
// Clear $item->input and fields to stop object from being created
// https://glpi-developer-documentation.readthedocs.io/en/master/plugins/hooks.html
self::emptyReferencedObject($item);
}
}
return true;
}

/**
* openMailGate(int Id) : MailCollector|null -
* Returns a connected mailCollector object or []
*
* @param int $Id Mail Collector ID
* @return MailCollector|null Union return types might be problematic with older php versions.
* @since 1.0.0
*
* @param int $Id Mail Collector ID
* @return MailCollector|null Union return types might be problematic with older php versions.
* @since 1.0.0
*/
private static function openMailGate(int $Id) : MailCollector|null
{
Expand All @@ -194,24 +190,20 @@ private static function openMailGate(int $Id) : MailCollector|null
/**
* deleteEmail(Ticket item) : bool -
* Delete original email from the mailbox to the accepted folder. This is not performed by the mailgate
* if the Ticket passed by reference is nullified as described in the hook documentation.
* if the Ticket passed by reference is nullified as described in the hook documentation.
* This actually causes an error where mailgate leaves the email untouched.
*
*
* @param Ticket $item Ticket object containing the mailgate uid
* @return bool Returns true on success and false on failure
* @since 1.0.0
* @return bool Returns true on success and false on failure
* @since 1.0.0
*/
private static function deleteEmail(Ticket $item) : bool
{
// Check if ticket is fetched from the mailCollector if so open it;
$mailCollector = (isset($item->input['_mailgate'])) ? self::openMailGate($item->input['_mailgate']) : false;
if(is_object($mailCollector)){
if($mailCollector->deleteMails($item->input['_uid'], MailCollector::ACCEPTED_FOLDER)) {
return true;
} else {
return false;
}
return ($mailCollector->deleteMails($item->input['_uid'], MailCollector::ACCEPTED_FOLDER)) ? true : false;
} // instantiation of mailCollector failed
return false;
}
}
}
59 changes: 21 additions & 38 deletions src/FilterPattern.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* ------------------------------------------------------------------------
* Chris Gralike Ticket Filter
Expand Down Expand Up @@ -67,14 +66,14 @@ class FilterPattern extends CommonDropdown
const SUPPRESNOTIF = 'SuppressNotification';

/**
* getTypeName(int nb) : string -
* getTypeName(int nb) : string -
* Method called by pre_item_add hook validates the object and passes
* it to the RegEx Matching then decides what to do.
*
*
* @param int $nb number of items.
* @return void
* @return void
*/
static function getTypeName($nb = 0) : string
public static function getTypeName($nb = 0) : string
{
if ($nb > 0) {
return __('Filterpatterns', 'ticketfilter');
Expand All @@ -87,8 +86,8 @@ static function getTypeName($nb = 0) : string
* getMenuContent() : array | bool -
* Method called by pre_item_add hook validates the object and passes
* it to the RegEx Matching then decides what to do.
*
* @return mixed boolean|array
*
* @return mixed boolean|array
*/
public static function getMenuContent()
{
Expand All @@ -107,20 +106,20 @@ public static function getMenuContent()
/**
* getIcon() : string -
* Sets icon for object.
*
* @return string $icon
*
* @return string $icon
*/
public static function getIcon() : string
{
return 'fas fa-filter';
{
return 'fas fa-filter';
}

/**
* getAdditionalFields() : array -
* Fetch fields for Dropdown 'add' form. Array order is equal with
* field order in the form
*
* @return string $icon
*
* @return string $icon
*/
public function getAdditionalFields()
{
Expand Down Expand Up @@ -211,8 +210,8 @@ public function getAdditionalFields()
/**
* rawSearchOptions() : array -
* Add fields to search and potential table columns
*
* @return array $rawSearchOptions
*
* @return array $rawSearchOptions
*/
public function rawSearchOptions() : array
{
Expand Down Expand Up @@ -242,23 +241,7 @@ public function rawSearchOptions() : array
'name' => __('Solved Match String', 'ticketfilter'),
'datatype' => 'text',
];
/*
$tab[] = [
'id' => '8',
'table' => $this->getTable(),
'field' => 'AutomaticallyMerge',
'name' => __('Automatically Merge', 'ticketfilter'),
'datatype' => 'text',
];
$tab[] = [
'id' => '9',
'table' => $this->getTable(),
'field' => 'LinkClosedTickets',
'name' => __('Link To Closed Tickets', 'ticketfilter'),
'datatype' => 'text',
];
*/

$tab[] = [
'id' => '9',
'table' => $this->getTable(),
Expand Down Expand Up @@ -306,9 +289,9 @@ public static function getFilterPatterns() : array
/**
* install(Migration migration) : void -
* Install table needed for Ticket Filter configuration dropdowns
*
*
* @return void
* @see hook.php:plugin_ticketfilter_install()
* @see hook.php:plugin_ticketfilter_install()
*/
public static function install(Migration $migration) : void
{
Expand Down Expand Up @@ -340,7 +323,7 @@ public static function install(Migration $migration) : void
`AutomaticallyMerge` tinyint NOT NULL DEFAULT '0',
`ReopenClosedTickets` tinyint NOT NULL DEFAULT '0',
`SearchTicketBody` tinyint NOT NULL DEFAULT '0',
`MatchSpecificSource` text NULL,
`MatchSpecificSource` text NULL,
`SuppressNotification` tinyint NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `name` (`name`),
Expand All @@ -353,7 +336,7 @@ public static function install(Migration $migration) : void

// insert example rule;
$query = <<<SQL
INSERT INTO `$table`(name, comment, is_active, TicketMatchString, TicketMatchStringLength, SolvedMatchString, SolvedMatchStringLength)
INSERT INTO `$table`(name, comment, is_active, TicketMatchString, TicketMatchStringLength, SolvedMatchString, SolvedMatchStringLength)
VALUES('example', 'this is an example expression', '1', '/.*?(?<match>\(JIRA-[0-9]{1,4}\)).*/', '11', '/.*?(?<solved>Closed).*/', '6');
SQL;
$DB->query($query) or die($DB->error());
Expand All @@ -369,7 +352,7 @@ public static function install(Migration $migration) : void
/**
* uninstall(Migration migration) : void -
* Uninstall tables uncomment the line to make plugin clean table.
*
*
* @return void
* @see hook.php:plugin_ticketfilter_uninstall()
*/
Expand All @@ -379,4 +362,4 @@ public static function uninstall(Migration $migration) : void
$migration->displayMessage("Uninstalling $table");
$migration->dropTable($table);
}
}
}
Loading

0 comments on commit ab47fce

Please sign in to comment.