Add php-cs-fixer

This commit is contained in:
Rom1-B
2025-06-27 08:22:27 +02:00
parent e283c254cd
commit c5e110fc73
32 changed files with 3774 additions and 1864 deletions

View File

@@ -34,6 +34,7 @@
// ----------------------------------------------------------------------
namespace GlpiPlugin\Example;
use CommonDBChild;
use Session;
@@ -43,64 +44,58 @@ use Session;
// enhancements.
// For CommonDBRelation, the variable are quiet equivalent, but they use _1 and _2 for each side
// parent
class Child extends CommonDBChild {
class Child extends CommonDBChild
{
// A child rely on an item. If $itemtype=='itemtype', then that is a variable item.
public static $itemtype = 'itemtype';
public static $items_id = 'items_id';
// A child rely on an item. If $itemtype=='itemtype', then that is a variable item.
static public $itemtype = 'itemtype';
static public $items_id = 'items_id';
// With 0.84, you have to specify each right (create, view, update and delete), because
// CommonDBChild(s) and CommonDBRelation(s) mainly depend on the rights on the parent item
// All these methods rely on parent:can*. Two attributs are usefull :
// * $checkParentRights: define what to check regarding the parent :
// - CommonDBConnexity::DONT_CHECK_ITEM_RIGHTS don't eaven relly on parents rights
// - CommonDBConnexity::HAVE_VIEW_RIGHT_ON_ITEM view right on the item is enough
// - CommonDBConnexity::HAVE_SAME_RIGHT_ON_ITEM we must have at least update right
// on the item
// * $mustBeAttached: some CommonDBChild can be free, without any parent.
public static function canCreate()
{
return (Session::haveRight('internet', UPDATE)
&& parent::canCreate());
}
public static function canView()
{
return (Session::haveRight('internet', READ)
&& parent::canView());
}
// With 0.84, you have to specify each right (create, view, update and delete), because
// CommonDBChild(s) and CommonDBRelation(s) mainly depend on the rights on the parent item
// All these methods rely on parent:can*. Two attributs are usefull :
// * $checkParentRights: define what to check regarding the parent :
// - CommonDBConnexity::DONT_CHECK_ITEM_RIGHTS don't eaven relly on parents rights
// - CommonDBConnexity::HAVE_VIEW_RIGHT_ON_ITEM view right on the item is enough
// - CommonDBConnexity::HAVE_SAME_RIGHT_ON_ITEM we must have at least update right
// on the item
// * $mustBeAttached: some CommonDBChild can be free, without any parent.
static function canCreate() {
public static function canUpdate()
{
return (Session::haveRight('internet', UPDATE)
&& parent::canUpdate());
}
return (Session::haveRight('internet', UPDATE)
&& parent::canCreate());
}
public static function canDelete()
{
return (Session::haveRight('internet', DELETE)
&& parent::canDelete());
}
// By default, post_addItem, post_updateItem and post_deleteFromDB are defined.
// They define the history to add to the parents
// This method define the name to set inside the history of the parent.
// All these methods use $log_history_add, $log_history_update and $log_history_delete to
// define the level of log (Log::HISTORY_ADD_DEVICE, Log::HISTORY_UPDATE_DEVICE ...)
public function getHistoryName_for_item($case) {}
static function canView() {
return (Session::haveRight('internet', READ)
&& parent::canView());
}
static function canUpdate() {
return (Session::haveRight('internet', UPDATE)
&& parent::canUpdate());
}
static function canDelete() {
return (Session::haveRight('internet', DELETE)
&& parent::canDelete());
}
// By default, post_addItem, post_updateItem and post_deleteFromDB are defined.
// They define the history to add to the parents
// This method define the name to set inside the history of the parent.
// All these methods use $log_history_add, $log_history_update and $log_history_delete to
// define the level of log (Log::HISTORY_ADD_DEVICE, Log::HISTORY_UPDATE_DEVICE ...)
function getHistoryName_for_item($case) {
}
// CommonDBChild also check if we can add or updatethe item regarding the new item
// ($input[static::$itemtype] and $input[static::$items_id]).
// But don't forget to call parent::prepareInputForAdd()
function prepareInputForAdd($input) {
// My preparation on $input
return parent::prepareInputForAdd($input);
}
// CommonDBChild also check if we can add or updatethe item regarding the new item
// ($input[static::$itemtype] and $input[static::$items_id]).
// But don't forget to call parent::prepareInputForAdd()
public function prepareInputForAdd($input)
{
// My preparation on $input
return parent::prepareInputForAdd($input);
}
}

View File

@@ -34,50 +34,49 @@
// ----------------------------------------------------------------------
namespace GlpiPlugin\Example;
use CommonDBTM;
// Class of the defined type
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
die("Sorry. You can't access directly to this file");
}
class Computer extends CommonDBTM {
class Computer extends CommonDBTM
{
public static function showInfo()
{
echo '<table class="tab_glpi" width="100%">';
echo '<tr>';
echo '<th>' . __('More information') . '</th>';
echo '</tr>';
echo '<tr class="tab_bg_1">';
echo '<td>';
echo __('Test successful');
echo '</td>';
echo '</tr>';
echo '</table>';
}
static function showInfo() {
public static function item_can($item)
{
if (($item->getType() == 'Computer')
&& ($item->right == READ)
&& ($item->fields['groups_id'] > 0)
&& !in_array($item->fields['groups_id'], $_SESSION['glpigroups'])) {
$item->right = 0; // unknown, so denied.
}
}
echo '<table class="tab_glpi" width="100%">';
echo '<tr>';
echo '<th>'.__('More information').'</th>';
echo '</tr>';
echo '<tr class="tab_bg_1">';
echo '<td>';
echo __('Test successful');
echo '</td>';
echo '</tr>';
echo '</table>';
}
static function item_can($item) {
if (($item->getType() == 'Computer')
&& ($item->right == READ)
&& ($item->fields['groups_id'] > 0)
&& !in_array($item->fields['groups_id'], $_SESSION["glpigroups"])) {
$item->right = 0; // unknown, so denied.
}
}
static function add_default_where($in) {
list($itemtype, $condition) = $in;
if ($itemtype == 'Computer') {
$table = getTableForItemType($itemtype);
$condition .= " (".$table.".groups_id NOT IN (".implode(',', $_SESSION["glpigroups"])."))";
}
return [$itemtype, $condition];
}
public static function add_default_where($in)
{
list($itemtype, $condition) = $in;
if ($itemtype == 'Computer') {
$table = getTableForItemType($itemtype);
$condition .= ' (' . $table . '.groups_id NOT IN (' . implode(',', $_SESSION['glpigroups']) . '))';
}
return [$itemtype, $condition];
}
}

View File

@@ -29,6 +29,7 @@
*/
namespace GlpiPlugin\Example;
use CommonDBTM;
use CommonGLPI;
use Config as GlpiConfig;
@@ -37,60 +38,63 @@ use Html;
use Session;
use Toolbox;
class Config extends CommonDBTM {
class Config extends CommonDBTM
{
protected static $notable = true;
static protected $notable = true;
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
if (!$withtemplate) {
if ($item->getType() == 'Config') {
return __('Example plugin');
}
}
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
return '';
}
if (!$withtemplate) {
if ($item->getType() == 'Config') {
return __('Example plugin');
}
}
return '';
}
public static function configUpdate($input)
{
$input['configuration'] = 1 - $input['configuration'];
static function configUpdate($input) {
$input['configuration'] = 1 - $input['configuration'];
return $input;
}
return $input;
}
function showFormExample() {
global $CFG_GLPI;
public function showFormExample()
{
global $CFG_GLPI;
if (!Session::haveRight("config", UPDATE)) {
return false;
}
if (!Session::haveRight('config', UPDATE)) {
return false;
}
$my_config = GlpiConfig::getConfigurationValues('plugin:Example');
$my_config = GlpiConfig::getConfigurationValues('plugin:Example');
echo "<form name='form' action=\"".Toolbox::getItemTypeFormURL('Config')."\" method='post'>";
echo "<div class='center' id='tabsbody'>";
echo "<table class='tab_cadre_fixe'>";
echo "<tr><th colspan='4'>" . __('Example setup') . "</th></tr>";
echo "<td >" . __('My boolean choice :') . "</td>";
echo "<td colspan='3'>";
echo "<input type='hidden' name='config_class' value='".__CLASS__."'>";
echo "<input type='hidden' name='config_context' value='plugin:Example'>";
Dropdown::showYesNo("configuration", $my_config['configuration']);
echo "</td></tr>";
echo "<form name='form' action=\"" . Toolbox::getItemTypeFormURL('Config') . "\" method='post'>";
echo "<div class='center' id='tabsbody'>";
echo "<table class='tab_cadre_fixe'>";
echo "<tr><th colspan='4'>" . __('Example setup') . '</th></tr>';
echo '<td >' . __('My boolean choice :') . '</td>';
echo "<td colspan='3'>";
echo "<input type='hidden' name='config_class' value='" . __CLASS__ . "'>";
echo "<input type='hidden' name='config_context' value='plugin:Example'>";
Dropdown::showYesNo('configuration', $my_config['configuration']);
echo '</td></tr>';
echo "<tr class='tab_bg_2'>";
echo "<td colspan='4' class='center'>";
echo "<input type='submit' name='update' class='submit' value=\""._sx('button', 'Save')."\">";
echo "</td></tr>";
echo "<tr class='tab_bg_2'>";
echo "<td colspan='4' class='center'>";
echo "<input type='submit' name='update' class='submit' value=\"" . _sx('button', 'Save') . '">';
echo '</td></tr>';
echo "</table></div>";
Html::closeForm();
}
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
if ($item->getType() == 'Config') {
$config = new self();
$config->showFormExample();
}
}
echo '</table></div>';
Html::closeForm();
}
public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
{
if ($item->getType() == 'Config') {
$config = new self();
$config->showFormExample();
}
}
}

View File

@@ -34,19 +34,20 @@
// ----------------------------------------------------------------------
namespace GlpiPlugin\Example;
use CommonDevice;
// Class of the defined type
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
die("Sorry. You can't access directly to this file");
}
/// Class DeviceCamera
class DeviceCamera extends CommonDevice {
static function getTypeName($nb = 0) {
return _n('Camera', 'Cameras', $nb);
}
class DeviceCamera extends CommonDevice
{
public static function getTypeName($nb = 0)
{
return _n('Camera', 'Cameras', $nb);
}
}

View File

@@ -55,14 +55,15 @@ along with GLPI. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GlpiPlugin\Example;
use Document as GlpiDocument;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
die("Sorry. You can't access this file directly");
}
class Document extends GlpiDocument {
class Document extends GlpiDocument
{
/**
* Return the table used to store this object. Overloads the implementation in CommonDBTM
*
@@ -70,155 +71,159 @@ class Document extends GlpiDocument {
*
* @return string
**/
public static function getTable($classname = null) {
if ($classname === null) {
$classname = get_called_class();
}
if ($classname == get_called_class()) {
return parent::getTable(Document::class);
}
public static function getTable($classname = null)
{
if ($classname === null) {
$classname = get_called_class();
}
if ($classname == get_called_class()) {
return parent::getTable(Document::class);
}
return parent::getTable($classname);
}
return parent::getTable($classname);
}
/**
* Prepare creation of an item
*
* @param array $input
* @return array|false
*/
public function prepareInputForAdd($input) {
$input['_only_if_upload_succeed'] = true;
if (!isset($_FILES['file'])) {
return false;
}
/**
* Prepare creation of an item
*
* @param array $input
* @return array|false
*/
public function prepareInputForAdd($input)
{
$input['_only_if_upload_succeed'] = true;
if (!isset($_FILES['file'])) {
return false;
}
// Move the uploaded file to GLPi's tmp dir
while (count($_FILES['file']['name']) > 0) {
$source = array_pop($_FILES['file']['name']);
$destination = GLPI_TMP_DIR . '/' . $source;
move_uploaded_file($source, $destination);
$input['_filename'][] = $source;
}
// Move the uploaded file to GLPi's tmp dir
while (count($_FILES['file']['name']) > 0) {
$source = array_pop($_FILES['file']['name']);
$destination = GLPI_TMP_DIR . '/' . $source;
move_uploaded_file($source, $destination);
$input['_filename'][] = $source;
}
return parent::prepareInputForAdd($input);
}
return parent::prepareInputForAdd($input);
}
/**
* Prepare update of an item
*
* @param array $input
* @return array|false
*/
public function prepareInputForUpdate($input) {
// Do not allow update of document
return false;
}
/**
* Prepare update of an item
*
* @param array $input
* @return array|false
*/
public function prepareInputForUpdate($input)
{
// Do not allow update of document
return false;
}
/**
* Process required after loading an object from DB
* In this example, a file is sent as a byte strem then stops execution.
*
* @return void
*/
public function post_getFromDB() {
// Check the user can view this itemtype and can view this item
if ($this->canView() && $this->canViewItem()) {
if (isset($_SERVER['HTTP_ACCEPT']) && $_SERVER['HTTP_ACCEPT'] == 'application/octet-stream'
|| isset($_GET['alt']) && $_GET['alt'] == 'media') {
$this->sendFile(); // and terminate script
}
}
}
/**
* Send a byte stream to the HTTP client and stops execution
*
* @return void
*/
protected function sendFile() {
$streamSource = GLPI_DOC_DIR . '/' . $this->fields['filepath'];
// Ensure the file exists
if (!file_exists($streamSource) || !is_file($streamSource)) {
header("HTTP/1.0 404 Not Found");
exit(0);
}
// Download range defaults to the full file
// get file metadata
$size = filesize($streamSource);
$begin = 0;
$end = $size - 1;
$mimeType = 'application/octet-stream';
$time = date('r', filemtime($streamSource));
// Open the file
$fileHandle = @fopen($streamSource, 'rb');
if (!$fileHandle) {
header ("HTTP/1.0 500 Internal Server Error");
exit(0);
}
// set range if specified by the client
if (isset($_SERVER['HTTP_RANGE'])) {
if (preg_match('/bytes=\h*(\d+)?-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
if (!empty($matches[1])) {
$begin = intval($matches[1]);
/**
* Process required after loading an object from DB
* In this example, a file is sent as a byte strem then stops execution.
*
* @return void
*/
public function post_getFromDB()
{
// Check the user can view this itemtype and can view this item
if ($this->canView() && $this->canViewItem()) {
if (isset($_SERVER['HTTP_ACCEPT']) && $_SERVER['HTTP_ACCEPT'] == 'application/octet-stream'
|| isset($_GET['alt']) && $_GET['alt'] == 'media') {
$this->sendFile(); // and terminate script
}
if (!empty($matches[2])) {
$end = min(intval($matches[2]), $end);
}
}
}
}
}
// seek to the begining of the range
$currentPosition = $begin;
if (fseek($fileHandle, $begin, SEEK_SET) < 0) {
header("HTTP/1.0 500 Internal Server Error");
exit(0);
}
/**
* Send a byte stream to the HTTP client and stops execution
*
* @return void
*/
protected function sendFile()
{
$streamSource = GLPI_DOC_DIR . '/' . $this->fields['filepath'];
// send headers to ensure the client is able to detect a corrupted download
// example : less bytes than the expected range
// send meta data
// setup client's cache behavior
header("Expires: Mon, 26 Nov 1962 00:00:00 GMT");
header('Pragma: private'); /// IE BUG + SSL
header('Cache-control: private, must-revalidate'); /// IE BUG + SSL
header("Content-disposition: attachment; filename=\"" . $this->fields['filename'] . "\"");
header("Content-type: $mimeType");
header("Last-Modified: $time");
header('Accept-Ranges: bytes');
header('Content-Length:' . ($end - $begin + 1));
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
// Prepare HTTP response
if ($begin > 0 || $end < $size - 1) {
header('HTTP/1.0 206 Partial Content');
} else {
header('HTTP/1.0 200 OK');
}
// Sends bytes until the end of the range or connection closed
while (!feof($fileHandle) && $currentPosition < $end && (connection_status() == 0)) {
// allow a few seconds to send a few KB.
set_time_limit(10);
$content = fread($fileHandle, min(1024 * 16, $end - $currentPosition + 1));
if ($content === false) {
header("HTTP/1.0 500 Internal Server Error", true); // Replace previously sent headers
// Ensure the file exists
if (!file_exists($streamSource) || !is_file($streamSource)) {
header('HTTP/1.0 404 Not Found');
exit(0);
} else {
print $content;
}
flush();
$currentPosition += 1024 * 16;
}
}
// End now to prevent any unwanted bytes
exit(0);
}
// Download range defaults to the full file
// get file metadata
$size = filesize($streamSource);
$begin = 0;
$end = $size - 1;
$mimeType = 'application/octet-stream';
$time = date('r', filemtime($streamSource));
// Open the file
$fileHandle = @fopen($streamSource, 'rb');
if (!$fileHandle) {
header('HTTP/1.0 500 Internal Server Error');
exit(0);
}
// set range if specified by the client
if (isset($_SERVER['HTTP_RANGE'])) {
if (preg_match('/bytes=\h*(\d+)?-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
if (!empty($matches[1])) {
$begin = intval($matches[1]);
}
if (!empty($matches[2])) {
$end = min(intval($matches[2]), $end);
}
}
}
// seek to the begining of the range
$currentPosition = $begin;
if (fseek($fileHandle, $begin, SEEK_SET) < 0) {
header('HTTP/1.0 500 Internal Server Error');
exit(0);
}
// send headers to ensure the client is able to detect a corrupted download
// example : less bytes than the expected range
// send meta data
// setup client's cache behavior
header('Expires: Mon, 26 Nov 1962 00:00:00 GMT');
header('Pragma: private'); /// IE BUG + SSL
header('Cache-control: private, must-revalidate'); /// IE BUG + SSL
header('Content-disposition: attachment; filename="' . $this->fields['filename'] . '"');
header("Content-type: $mimeType");
header("Last-Modified: $time");
header('Accept-Ranges: bytes');
header('Content-Length:' . ($end - $begin + 1));
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');
// Prepare HTTP response
if ($begin > 0 || $end < $size - 1) {
header('HTTP/1.0 206 Partial Content');
} else {
header('HTTP/1.0 200 OK');
}
// Sends bytes until the end of the range or connection closed
while (!feof($fileHandle) && $currentPosition < $end && (connection_status() == 0)) {
// allow a few seconds to send a few KB.
set_time_limit(10);
$content = fread($fileHandle, min(1024 * 16, $end - $currentPosition + 1));
if ($content === false) {
header('HTTP/1.0 500 Internal Server Error', true); // Replace previously sent headers
exit(0);
} else {
print $content;
}
flush();
$currentPosition += 1024 * 16;
}
// End now to prevent any unwanted bytes
exit(0);
}
}

View File

@@ -32,18 +32,20 @@
// Original Author of file:
// Purpose of file:
// ----------------------------------------------------------------------
namespace GlpiPlugin\Example;
use CommonDropdown;
// Class for a Dropdown
class Dropdown extends CommonDropdown {
class Dropdown extends CommonDropdown
{
public static function getTypeName($nb = 0)
{
if ($nb > 0) {
return __('Plugin Example Dropdowns', 'example');
}
static function getTypeName($nb = 0) {
if ($nb > 0) {
return __('Plugin Example Dropdowns', 'example');
}
return __('Plugin Example Dropdowns', 'example');
}
return __('Plugin Example Dropdowns', 'example');
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -38,12 +38,12 @@ class ComputerModelFilter extends AbstractFilter
{
public static function getName(): string
{
return __("Computer model");
return __('Computer model');
}
public static function getId(): string
{
return "plugin_example_computer_model";
return 'plugin_example_computer_model';
}
public static function canBeApplied(string $table): bool
@@ -57,9 +57,9 @@ class ComputerModelFilter extends AbstractFilter
{
return self::displayList(
self::getName(),
is_string($value) ? $value : "",
is_string($value) ? $value : '',
self::getId(),
ComputerModel::class
ComputerModel::class,
);
}
@@ -67,10 +67,11 @@ class ComputerModelFilter extends AbstractFilter
{
if ((int) $value > 0) {
$field = ComputerModel::getForeignKeyField();
return [
"WHERE" => [
"$table.$field" => (int) $value
]
'WHERE' => [
"$table.$field" => (int) $value,
],
];
}
@@ -88,9 +89,9 @@ class ComputerModelFilter extends AbstractFilter
'field' => self::getSearchOptionID(
$table,
ComputerModel::getForeignKeyField(),
ComputerModel::getTable()
ComputerModel::getTable(),
),
]
],
];
}

View File

@@ -39,21 +39,21 @@ use Ticket;
* Example of *_item_form implementation
* @see http://glpi-developer-documentation.rtfd.io/en/master/plugins/hooks.html#items-display-related
* */
class ItemForm {
class ItemForm
{
/**
* Display contents at the begining of ITILObject section (right panel).
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
public static function preSection($params)
{
$item = $params['item'];
$options = $params['options'];
/**
* Display contents at the begining of ITILObject section (right panel).
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
static public function preSection($params) {
$item = $params['item'];
$options = $params['options'];
echo TemplateRenderer::getInstance()->renderFromStringTemplate(<<<TWIG
echo TemplateRenderer::getInstance()->renderFromStringTemplate(<<<TWIG
<section class="accordion-item" aria-label="a label">
<h2 class="accordion-header" id="example-heading" title="example-heading-id" data-bs-toggle="tooltip">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#example-pre-content" aria-expanded="true" aria-controls="example-pre-content">
@@ -70,21 +70,21 @@ class ItemForm {
</div>
</section>
TWIG, []);
}
}
/**
* Display contents at the end of ITILObject section (right panel).
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
public static function postSection($params)
{
$item = $params['item'];
$options = $params['options'];
/**
* Display contents at the end of ITILObject section (right panel).
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
static public function postSection($params) {
$item = $params['item'];
$options = $params['options'];
echo TemplateRenderer::getInstance()->renderFromStringTemplate(<<<TWIG
echo TemplateRenderer::getInstance()->renderFromStringTemplate(<<<TWIG
<section class="accordion-item" aria-label="a label">
<h2 class="accordion-header" id="example-heading" title="example-heading-id" data-bs-toggle="tooltip">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#example-post-content" aria-expanded="true" aria-controls="example-post-content">
@@ -101,106 +101,105 @@ TWIG, []);
</div>
</section>
TWIG, []);
}
}
/**
* Display contents at the begining of item forms.
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
public static function preItemForm($params)
{
$item = $params['item'];
$options = $params['options'];
$firstelt = ($item::getType() == Ticket::class ? 'th' : 'td');
$out = '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('Start %1$s hook call for %2$s type'),
'pre_item_form',
$item::getType(),
);
$out .= '</th></tr>';
$out .= "<tr><$firstelt>";
$out .= '<label for="example_pre_form_hook">' . __('First pre form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_pre_form_hook" id="example_pre_form_hook"/>';
$out .= "</td><$firstelt>";
$out .= '<label for="example_pre_form_hook2">' . __('Second pre form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_pre_form_hook2" id="example_pre_form_hook2"/>';
$out .= '</td></tr>';
/**
* Display contents at the begining of item forms.
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
static public function preItemForm($params) {
$item = $params['item'];
$options = $params['options'];
$out .= '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('End %1$s hook call for %2$s type'),
'pre_item_form',
$item::getType(),
);
$out .= '</th></tr>';
$firstelt = ($item::getType() == Ticket::class ? 'th' : 'td');
echo $out;
}
$out = '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('Start %1$s hook call for %2$s type'),
'pre_item_form',
$item::getType()
);
$out .= '</th></tr>';
/**
* Display contents at the begining of item forms.
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
public static function postItemForm($params)
{
$item = $params['item'];
$options = $params['options'];
$out .= "<tr><$firstelt>";
$out .= '<label for="example_pre_form_hook">' . __('First pre form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_pre_form_hook" id="example_pre_form_hook"/>';
$out .= "</td><$firstelt>";
$out .= '<label for="example_pre_form_hook2">' . __('Second pre form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_pre_form_hook2" id="example_pre_form_hook2"/>';
$out .= '</td></tr>';
$firstelt = ($item::getType() == Ticket::class ? 'th' : 'td');
$out .= '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('End %1$s hook call for %2$s type'),
'pre_item_form',
$item::getType()
);
$out .= '</th></tr>';
$out = '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('Start %1$s hook call for %2$s type'),
'post_item_form',
$item::getType(),
);
$out .= '</th></tr>';
echo $out;
}
$out .= "<tr><$firstelt>";
$out .= '<label for="example_post_form_hook">' . __('First post form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_post_form_hook" id="example_post_form_hook"/>';
$out .= "</td><$firstelt>";
$out .= '<label for="example_post_form_hook2">' . __('Second post form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_post_form_hook2" id="example_post_form_hook2"/>';
$out .= '</td></tr>';
/**
* Display contents at the begining of item forms.
*
* @param array $params Array with "item" and "options" keys
*
* @return void
*/
static public function postItemForm($params) {
$item = $params['item'];
$options = $params['options'];
$out .= '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('End %1$s hook call for %2$s type'),
'post_item_form',
$item::getType(),
);
$out .= '</th></tr>';
$firstelt = ($item::getType() == Ticket::class ? 'th' : 'td');
echo $out;
}
$out = '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('Start %1$s hook call for %2$s type'),
'post_item_form',
$item::getType()
);
$out .= '</th></tr>';
public static function timelineActions($params = [])
{
$rand = $params['rand'];
$ticket = $params['item'];
$out .= "<tr><$firstelt>";
$out .= '<label for="example_post_form_hook">' . __('First post form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_post_form_hook" id="example_post_form_hook"/>';
$out .= "</td><$firstelt>";
$out .= '<label for="example_post_form_hook2">' . __('Second post form hook') . '</label>';
$out .= "</$firstelt><td>";
$out .= '<input type="text" name="example_post_form_hook2" id="example_post_form_hook2"/>';
$out .= '</td></tr>';
if (get_class($ticket) !== 'Ticket') {
return false;
}
$out .= '<tr><th colspan="' . (isset($options['colspan']) ? $options['colspan'] * 2 : '4') . '">';
$out .= sprintf(
__('End %1$s hook call for %2$s type'),
'post_item_form',
$item::getType()
);
$out .= '</th></tr>';
echo $out;
}
static public function timelineActions($params = []) {
$rand = $params['rand'];
$ticket = $params['item'];
if (get_class($ticket) !== "Ticket") {
return false;
}
$edit_panel = "viewitem".$ticket->fields['id'].$rand;
$JS = <<<JAVASCRIPT
$edit_panel = 'viewitem' . $ticket->fields['id'] . $rand;
$JS = <<<JAVASCRIPT
$(function() {
$(document).on('click', '#email_transfer_{$rand}', function(event) {
$('#{$edit_panel}').html('email send');
@@ -208,10 +207,10 @@ TWIG, []);
});
JAVASCRIPT;
echo "<li class='followup' id='email_transfer_$rand'>
<i class='far fa-envelope'></i>".
__("Send a notification").
Html::scriptBlock($JS)."
</li>";
}
echo "<li class='followup' id='email_transfer_$rand'>
<i class='far fa-envelope'></i>" .
__('Send a notification') .
Html::scriptBlock($JS) . '
</li>';
}
}

View File

@@ -36,21 +36,21 @@
// Class of the defined type
namespace GlpiPlugin\Example;
use GlpiPlugin\Example\DeviceCamera;
use Item_Devices;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
die("Sorry. You can't access directly to this file");
}
/**
* Relation between item and devices
**/
class Item_DeviceCamera extends Item_Devices {
static public $itemtype_2 = DeviceCamera::class;
static public $items_id_2 = 'plugin_example_devicecameras_id';
static protected $notable = false;
class Item_DeviceCamera extends Item_Devices
{
public static $itemtype_2 = DeviceCamera::class;
public static $items_id_2 = 'plugin_example_devicecameras_id';
protected static $notable = false;
}

View File

@@ -29,22 +29,25 @@
*/
namespace GlpiPlugin\Example;
use NotificationTarget;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
die("Sorry. You can't access directly to this file");
}
// Class NotificationTarget
class NotificationTargetExample extends NotificationTarget {
class NotificationTargetExample extends NotificationTarget
{
public function getEvents()
{
return ['alert' => 'alert example'];
}
function getEvents() {
return ['alert' => 'alert example'];
}
public function addDataForTemplate($event, $options = [])
{
global $DB, $CFG_GLPI;
function addDataForTemplate($event, $options = []) {
global $DB, $CFG_GLPI;
$this->data['##example.name##'] = __('Example', 'example');
}
$this->data['##example.name##'] = __('Example', 'example');
}
}

View File

@@ -66,9 +66,9 @@ final class Profile extends \Profile
$rights = [
[
'itemtype' => Example::class,
'label' => Example::getTypeName(Session::getPluralNumber()),
'field' => Example::$rightname
]
'label' => Example::getTypeName(Session::getPluralNumber()),
'field' => Example::$rightname,
],
];
$matrix_options['title'] = self::getTypeName(1);
$this->displayRightsChoiceMatrix($rights, $matrix_options);

View File

@@ -34,10 +34,11 @@
// ----------------------------------------------------------------------
namespace GlpiPlugin\Example;
use Rule;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
die("Sorry. You can't access directly to this file");
}
@@ -48,40 +49,39 @@ if (!defined('GLPI_ROOT')) {
* - actions
*
**/
class RuleTest extends Rule {
class RuleTest extends Rule
{
// From Rule
public static $rightname = 'rule_import';
public $can_sort = true;
// From Rule
public static $rightname = 'rule_import';
public $can_sort = true;
public function getTitle()
{
return 'test';
}
public function maxActionsCount()
{
return 1;
}
function getTitle() {
return 'test';
}
public function getCriterias()
{
$criterias = [];
$criterias['name']['field'] = 'name';
$criterias['name']['name'] = __('Software');
$criterias['name']['table'] = 'glpi_softwares';
return $criterias;
}
function maxActionsCount() {
return 1;
}
public function getActions()
{
$actions = [];
$actions['softwarecategories_id']['name'] = __('Category (class)', 'example');
$actions['softwarecategories_id']['type'] = 'dropdown';
$actions['softwarecategories_id']['table'] = 'glpi_softwarecategories';
function getCriterias() {
$criterias = [];
$criterias['name']['field'] = 'name';
$criterias['name']['name'] = __('Software');
$criterias['name']['table'] = 'glpi_softwares';
return $criterias;
}
function getActions() {
$actions = [];
$actions['softwarecategories_id']['name'] = __('Category (class)', 'example');
$actions['softwarecategories_id']['type'] = 'dropdown';
$actions['softwarecategories_id']['table'] = 'glpi_softwarecategories';
return $actions;
}
return $actions;
}
}

View File

@@ -34,21 +34,23 @@
// ----------------------------------------------------------------------
namespace GlpiPlugin\Example;
use RuleCollection;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
die("Sorry. You can't access directly to this file");
}
class RuleTestCollection extends RuleCollection {
class RuleTestCollection extends RuleCollection
{
// From RuleCollection
public $stop_on_first_match = true;
public static $rightname = 'rule_import';
public $menu_option = 'test';
// From RuleCollection
public $stop_on_first_match = true;
public static $rightname = 'rule_import';
public $menu_option = 'test';
function getTitle() {
return 'Rulesengine test';
}
public function getTitle()
{
return 'Rulesengine test';
}
}

View File

@@ -51,45 +51,47 @@ namespace GlpiPlugin\Example;
* post_show_tab will be fired after the tab show
*
* */
class Showtabitem {
class Showtabitem
{
/**
* Summary of pre_show_tab
* @param array $params is an array like following
* array( 'item', 'options')
* where 'item' is the parent object (like 'Ticket'),
* and 'options' are options like following
* array( 'tabnum', 'itemtype')
* where 'tabnum' is the internal name of the tab that will be shown
* and 'itemtype' is the type of the tab (ex: 'ITILFollowup' when showing followup tab in a ticket)
* Note: you may pass datas to post_show_tab using the $param['options'] array (see example below)
*/
public static function pre_show_tab($params)
{
switch ($params['item']->getType()) {
case 'Ticket':
if ($params['options']['itemtype'] == 'TicketValidation' && $params['options']['tabnum'] == 2) {
// if tasks are not all done
// then prevent solution div to show
// this is an example to prevent solving of ticket
if (true) { // here you should test if some tasks are in todo status.
$params['options']['prevent_solution'] = true; // this will be passed to the post_show hook
echo "<div id='toHideSolution' style='display: none;'>"; // in order to hide the default solution div
}
}
}
}
/**
* Summary of pre_show_tab
* @param array $params is an array like following
* array( 'item', 'options')
* where 'item' is the parent object (like 'Ticket'),
* and 'options' are options like following
* array( 'tabnum', 'itemtype')
* where 'tabnum' is the internal name of the tab that will be shown
* and 'itemtype' is the type of the tab (ex: 'ITILFollowup' when showing followup tab in a ticket)
* Note: you may pass datas to post_show_tab using the $param['options'] array (see example below)
*/
static function pre_show_tab($params) {
switch ($params['item']->getType()) {
case 'Ticket':
if ($params['options']['itemtype']=='TicketValidation' && $params['options']['tabnum']==2) {
// if tasks are not all done
// then prevent solution div to show
// this is an example to prevent solving of ticket
if (true) { // here you should test if some tasks are in todo status.
$params['options']['prevent_solution'] = true; // this will be passed to the post_show hook
echo "<div id='toHideSolution' style='display: none;'>"; // in order to hide the default solution div
}
}
}
}
/**
* Summary of post_show_tab
* @param array $params is identical to pre_show_tab parameter
* Note: you may get datas from pre_show_tab in $param['options'] array (see example below)
*/
static function post_show_tab($params) {
switch ($params['item']->getType()) {
case 'Ticket':
if (isset($params['options']['prevent_solution'])) {
echo "</div>";
echo "<div style='margin-bottom: 20px;' class='box'>
/**
* Summary of post_show_tab
* @param array $params is identical to pre_show_tab parameter
* Note: you may get datas from pre_show_tab in $param['options'] array (see example below)
*/
public static function post_show_tab($params)
{
switch ($params['item']->getType()) {
case 'Ticket':
if (isset($params['options']['prevent_solution'])) {
echo '</div>';
echo "<div style='margin-bottom: 20px;' class='box'>
<div class='box-tleft'>
<div class='box-tright'>
<div class='box-tcenter'>
@@ -100,12 +102,12 @@ class Showtabitem {
<div class='box-mright'>
<div class='box-mcenter'>
<h3>
<span class='red'>"."Can't solve ticket"."
<span class='red'>" . "Can't solve ticket" . '
<br>
</span>
</h3>
<h3>
<span >"."Tasks are waiting to be done"."
<span >' . 'Tasks are waiting to be done' . "
</span>
</h3>
</div>
@@ -118,83 +120,84 @@ class Showtabitem {
</div>
</div>
</div> ";
}
break;
case 'Computer':
break;
}
}
/**
* Summary of pre_show_item
* @param array $params is an array like following
* array( 'item', 'options')
* where 'item' is the object to show (like 'Ticket', 'TicketTask', ...),
* BEWARE that sometimes it can be an array of data and not an object (ex: for solution item)
* and 'options' are options like following
* if item is a main object like a ticket, change, problem, ... then it contains
* array( 'id' )
* where 'id' is the id of object that will be shown (same than $param['item']->fields['id'])
* or if item contains a sub-object like followup, task, ... then it contains
* array( 'parent', 'rand', 'showprivate')
* where 'parent' is the main object related to the current item (ex: if 'item' is ITILFollowup then it will be the related Ticket)
* and 'rand' contains the random number that will be used to render the item
* and 'showprivate' is the right to show private items
* Note: you may pass datas to post_show_item using the $param['options'] array
*/
public static function pre_show_item($params)
{
if (!is_array($params['item'])) {
switch ($params['item']->getType()) {
case 'Ticket':
//echo 'test' ;
break;
case 'TicketTask':
//echo 'test' ;
break;
case 'ITILFollowup':
//echo 'test' ;
break;
}
break;
case 'Computer':
break;
}
}
/**
* Summary of pre_show_item
* @param array $params is an array like following
* array( 'item', 'options')
* where 'item' is the object to show (like 'Ticket', 'TicketTask', ...),
* BEWARE that sometimes it can be an array of data and not an object (ex: for solution item)
* and 'options' are options like following
* if item is a main object like a ticket, change, problem, ... then it contains
* array( 'id' )
* where 'id' is the id of object that will be shown (same than $param['item']->fields['id'])
* or if item contains a sub-object like followup, task, ... then it contains
* array( 'parent', 'rand', 'showprivate')
* where 'parent' is the main object related to the current item (ex: if 'item' is ITILFollowup then it will be the related Ticket)
* and 'rand' contains the random number that will be used to render the item
* and 'showprivate' is the right to show private items
* Note: you may pass datas to post_show_item using the $param['options'] array
*/
static function pre_show_item($params) {
if (!is_array($params['item'])) {
switch ($params['item']->getType()) {
case 'Ticket':
//echo 'test' ;
break;
case 'TicketTask' :
//echo 'test' ;
break;
case 'ITILFollowup' :
//echo 'test' ;
break;
}
} else {
// here we are going to view a Solution
return;
}
}
/**
* Summary of post_show_item
* @param array $params is an array like following
* array( 'item', 'options')
* where 'item' is the object to show (like 'Ticket', 'TicketTask', ...),
* and 'options' are options like following
* if item is a main object like a ticket, change, problem, ... then it contains
* array( 'id' )
* where 'id' is the id of object that will be shown (same than $param['item']->fields['id'])
* or if item contains a sub-object like followup, task, ... then it contains
* array( 'parent', 'rand', 'showprivate')
* where 'parent' is the main object related to the current item (ex: if 'item' is ITILFollowup then it will be the related Ticket)
* and 'rand' contains the random number that will be used to render the item
* and 'showprivate' is the right to show private items
* Note: you may get datas from pre_show_item using the $param['options'] array
*/
static function post_show_item($params) {
if (!is_array($params['item'])) {
switch ($params['item']->getType()) {
case 'Ticket':
//echo 'test' ;
break;
case 'TicketTask' :
//echo 'test' ;
break;
case 'ITILFollowup' :
//echo 'test' ;
break;
}
} else {
// here we are going to view a Solution
return;
}
}
} else {
// here we are going to view a Solution
return;
}
}
/**
* Summary of post_show_item
* @param array $params is an array like following
* array( 'item', 'options')
* where 'item' is the object to show (like 'Ticket', 'TicketTask', ...),
* and 'options' are options like following
* if item is a main object like a ticket, change, problem, ... then it contains
* array( 'id' )
* where 'id' is the id of object that will be shown (same than $param['item']->fields['id'])
* or if item contains a sub-object like followup, task, ... then it contains
* array( 'parent', 'rand', 'showprivate')
* where 'parent' is the main object related to the current item (ex: if 'item' is ITILFollowup then it will be the related Ticket)
* and 'rand' contains the random number that will be used to render the item
* and 'showprivate' is the right to show private items
* Note: you may get datas from pre_show_item using the $param['options'] array
*/
public static function post_show_item($params)
{
if (!is_array($params['item'])) {
switch ($params['item']->getType()) {
case 'Ticket':
//echo 'test' ;
break;
case 'TicketTask':
//echo 'test' ;
break;
case 'ITILFollowup':
//echo 'test' ;
break;
}
} else {
// here we are going to view a Solution
return;
}
}
}