Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for rule criteria hooks #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use GlpiPlugin\Example\Dropdown;
use GlpiPlugin\Example\Example;
use Dropdown as GlpiDropdown;
use GlpiPlugin\Example\RuleCriteria\RandomCriteria;

// Hook called on profile change
// Good place to evaluate the user right on this plugin
Expand Down Expand Up @@ -711,3 +712,11 @@ function plugin_example_filter_actors(array $params = []): array {

return $params;
}

function plugin_example_getRuleCriteria() {
return RandomCriteria::getRuleCriteria();
}

function plugin_example_ruleCollectionPrepareInputDataForProcess(array $params) {
return RandomCriteria::ruleCollectionPrepareInputDataForProcess($params);
}
3 changes: 3 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ function plugin_init_example() {
$PLUGIN_HOOKS[Hooks::DASHBOARD_FILTERS]['example'] = [
ComputerModelFilter::class
];

// Enable rule hooks
$PLUGIN_HOOKS['use_rules']['example'] = [RuleTicket::class];
}


Expand Down
52 changes: 52 additions & 0 deletions src/RuleCriteria/RandomCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* -------------------------------------------------------------------------
* Example plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Example.
*
* Example is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Example is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Example. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2006-2022 by Example plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/example
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Example\RuleCriteria;

class RandomCriteria
{
public static function getRuleCriteria(): array
{
$criteria = [];

$criteria['_plugin_example_random']['name'] = __('Random (50% success)');
$criteria['_plugin_example_random']['type'] = 'yesno';

return $criteria;
}

public static function ruleCollectionPrepareInputDataForProcess($options)
{
$results = [];
$results['_plugin_example_random'] = mt_rand(0, 1);

return $results;
}
}