Skip to content
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [UNRELEASED]

### Changed

- Make the plugin compatible with the Content Security Policy

### Fixed

- Remove redundant variable initialization in the question type AJAX controller
Expand Down
8 changes: 6 additions & 2 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,8 @@ public static function showFormItemtype($params = [])
*/
public static function showFormSubtype($params, $display = false)
{
$out = "<script type='text/javascript'>jQuery('#tab_tr').hide();</script>";
$out = '';
$tab_tr_display = 'hide';
if (isset($params['type']) && $params['type'] == 'domtab' && class_exists($params['itemtype'])) {
$dbu = new DbUtils();
$item = $dbu->getItemForItemtype($params['itemtype']);
Expand Down Expand Up @@ -1512,11 +1513,14 @@ public static function showFormSubtype($params, $display = false)
'display' => false,
],
);
$out .= "<script type='text/javascript'>jQuery('#tab_tr').show();</script>";
$tab_tr_display = 'show';
}
}
}

$out .= sprintf("<div hidden data-fields-tab-tr='%s'></div>", $tab_tr_display);
$out .= Html::script('/plugins/fields/js/container_subtype.js', [], false);

if ($display === false) {
return $out;
}
Expand Down
17 changes: 7 additions & 10 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,17 +690,14 @@ public function showSummary($container)
'plugin_fields_containers_id' => $cID,
'id' => -1,
];
echo Html::scriptBlock('
viewAddField' . $cID . $rand . ' = function() {
$("#viewField' . $cID . $rand . '").load(
"' . $CFG_GLPI['root_doc'] . '/ajax/viewsubitem.php",
' . json_encode($ajax_params) . '
);
};
');

echo "<div class='center'>"
. sprintf("<a href='javascript:viewAddField%s%d();'>", $cID, $rand);
. sprintf(
"<a href='#' data-fields-load-target='#viewField%s%d' data-fields-load-url='%s' data-fields-load-params='%s'>",
$cID,
$rand,
htmlescape($CFG_GLPI['root_doc'] . '/ajax/viewsubitem.php'),
htmlescape(json_encode($ajax_params)),
);
echo __('Add a new field', 'fields') . '</a></div><br>';

if (count($iterator) === 0) {
Expand Down
23 changes: 11 additions & 12 deletions inc/labeltranslation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,8 @@
'items_id' => $item->fields['id'],
'id' => -1,
];
echo Html::scriptBlock('
addTranslation' . $item->getID() . $rand . ' = function() {
$("#viewtranslation' . $item->getID() . $rand . '").load(
"' . $CFG_GLPI['root_doc'] . '/plugins/fields/ajax/viewtranslations.php",
' . json_encode($ajax_params) . '
);
};
');

echo "<div class='center'>"
. "<a class='vsubmit' href='javascript:addTranslation" . $item->getID() . ($rand . "();'>")
. "<a class='vsubmit' href='#' data-fields-load-target='#viewtranslation" . $item->getID() . $rand . "' data-fields-load-url='" . htmlescape($CFG_GLPI['root_doc'] . '/plugins/fields/ajax/viewtranslations.php') . "' data-fields-load-params='" . htmlescape(json_encode($ajax_params)) . "'>"
. __('Add a new translation') . '</a></div><br>';
}

Expand Down Expand Up @@ -238,25 +229,31 @@
echo '<th>' . __('Language', 'fields') . '</th>';
echo '<th>' . __('Label', 'fields') . '</th>';
foreach ($found as $data) {
echo "<tr class='tab_bg_1' " . ($canedit ? "style='cursor:pointer'
onClick=\"viewEditTranslation" . $data['id'] . ($rand . '();"') : '') . '>';
$row_params = htmlescape(json_encode([
'type' => self::class,
'itemtype' => $item::getType(),
'items_id' => $item->getID(),
'id' => $data['id'],
]));
echo "<tr class='tab_bg_1' " . ($canedit ? "style='cursor:pointer' data-fields-load-target='#viewtranslation" . $item->getID() . $rand . "' data-fields-load-url='" . htmlescape($CFG_GLPI['root_doc'] . '/plugins/fields/ajax/viewtranslations.php') . "' data-fields-load-params='" . $row_params . "'" : '') . '>';
if ($canedit) {
echo "<td class='center'>";
Html::showMassiveActionCheckBox(self::class, $data['id']);
echo '</td>';
}

echo '<td>';
<<<<<<< HEAD

Check failure on line 246 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_SL on line 246

Check failure on line 246 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_SL on line 246
if ($canedit) {
$ajax_params = [
'type' => self::class,
'itemtype' => $item::class,
'items_id' => $item->getID(),
'id' => $data['id'],

Check failure on line 252 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting '-' or T_STRING or T_VARIABLE or T_NUM_STRING on line 252

Check failure on line 252 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting '-' or T_STRING or T_VARIABLE or T_NUM_STRING on line 252
];
echo Html::scriptBlock('
viewEditTranslation' . $data['id'] . $rand . ' = function() {
$("#viewtranslation' . $item->getID() . $rand . '").load(

Check failure on line 256 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 256

Check failure on line 256 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 256
"' . $CFG_GLPI['root_doc'] . '/plugins/fields/ajax/viewtranslations.php",
' . json_encode($ajax_params) . '
);
Expand All @@ -264,6 +261,8 @@
');
}

=======
>>>>>>> 8a6ae60 (feat: make plugin CSP compliant (#1350))
echo Dropdown::getLanguageName($data['language']);
echo '</td><td>';
echo htmlspecialchars((string) $data['label'], ENT_QUOTES);
Expand Down Expand Up @@ -294,20 +293,20 @@
public function showFormForItem($itemtype, $items_id, $id = -1)
{
if ($id > 0) {
$this->check($id, READ);

Check failure on line 296 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 296

Check failure on line 296 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 296
} else {
// Create item
$this->check(-1, CREATE);

Check failure on line 299 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 299

Check failure on line 299 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 299
}

$this->showFormHeader();

Check failure on line 302 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 302

Check failure on line 302 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 302
echo "<tr class='tab_bg_1'>";
echo '<td>' . __('Language') . '&nbsp;:</td>';
echo '<td>';
echo sprintf("<input type='hidden' name='itemtype' value='%s'>", $itemtype);
echo sprintf("<input type='hidden' name='items_id' value='%d'>", $items_id);
if ($id > 0) {
echo Dropdown::getLanguageName($this->fields['language']);

Check failure on line 309 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 309

Check failure on line 309 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 309
} else {
Dropdown::showLanguages(
'language',
Expand All @@ -327,12 +326,12 @@
echo "<td><label for='label'>" . __('Label') . '</label></td>';
echo "<td colspan='3'>";
echo Html::input('label', [
'value' => $this->fields['label'],

Check failure on line 329 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 329

Check failure on line 329 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 329
'id' => 'label',
]);
echo '</td></tr>';

$this->showFormButtons();

Check failure on line 334 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 334

Check failure on line 334 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 334
}

/**
Expand All @@ -348,7 +347,7 @@
/** @var DBmysql $DB */
global $DB;

$iterator = $DB->request(

Check failure on line 350 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 350

Check failure on line 350 in inc/labeltranslation.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 350
[
'FROM' => self::getTable(),
'WHERE' => [
Expand Down
37 changes: 37 additions & 0 deletions public/js/container_subtype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields 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.
*
* Fields 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 Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2013-2023 by Fields plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

// Loaded right after its marker, not on DOM ready: keep the original timing.
$('[data-fields-tab-tr]:not([data-fields-ready])').each(function () {
const marker = $(this).attr('data-fields-ready', '');
if (marker.attr('data-fields-tab-tr') === 'show') {
$('#tab_tr').show();
} else {
$('#tab_tr').hide();
}
});
131 changes: 131 additions & 0 deletions public/js/fields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields 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.
*
* Fields 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 Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2013-2023 by Fields plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

// Load a subitem form into a target element. Used by links and table rows.
$(document).on('click', '[data-fields-load-target]', function (e) {
if ($(this).is('a')) {
e.preventDefault();
}
$($(this).attr('data-fields-load-target')).load(
$(this).attr('data-fields-load-url'),
JSON.parse($(this).attr('data-fields-load-params'))
);
});

// Container display conditions list
$(document).on('click', '[data-fields-conditions] button[name="edit"]', function (e) {
const row = $(e.currentTarget).closest('tr');
const id = row.find('input[name="id"]').val();
const container_id = row.find('input[name="plugin_fields_containers_id"]').val();

glpi_ajax_dialog(
{
title: __('Edit condition to hide block', 'fields'),
url: CFG_GLPI.root_doc + '/plugins/fields/ajax/container_display_condition.php',
method: 'get',
params: {
action: 'get_edit_form',
id: id,
plugin_fields_containers_id: container_id
},
dialogclass: 'modal-xl'
}
);
});

$(document).on('click', '[data-fields-conditions] button[name="switch_add"]', function (e) {
const container_id = $(e.currentTarget).attr('data-container-id');

glpi_ajax_dialog(
{
title: __('Add condition to hide block', 'fields'),
url: CFG_GLPI.root_doc + '/plugins/fields/ajax/container_display_condition.php',
method: 'get',
params: {
action: 'get_add_form',
plugin_fields_containers_id: container_id
},
dialogclass: 'modal-xl'
}
);
});

// Status overrides list
$(document).on('click', '[data-fields-status-overrides] button[name="edit"]', function (e) {
const row = $(e.currentTarget).closest('tr');
const id = row.find('input[name="id"]').val();
const container_id = row.find('input[name="container_id"]').val();

glpi_ajax_dialog(
{
title: __('Edit status override', 'fields'),
url: CFG_GLPI.root_doc + '/plugins/fields/ajax/status_override.php',
method: 'get',
params: {
action: 'get_edit_form',
id: id,
container_id: container_id
},
dialogclass: 'modal-xl'
}
);
});

$(document).on('click', '[data-fields-status-overrides] button[name="switch_add"]', function (e) {
const container_id = $(e.currentTarget).attr('data-container-id');

glpi_ajax_dialog(
{
title: __('Add new status override', 'fields'),
url: CFG_GLPI.root_doc + '/plugins/fields/ajax/status_override.php',
method: 'get',
params: {
action: 'get_add_form',
container_id: container_id
},
dialogclass: 'modal-xl'
}
);
});

// Status override form: refresh the status list when the item type changes
$(document).on('change', 'form[data-fields-status-override] select[name="itemtype"]', function () {
const form = $(this).closest('form');
const rand = form.attr('data-fields-status-override');

$.ajax({
url: CFG_GLPI.root_doc + '/plugins/fields/ajax/status_override.php',
method: 'GET',
data: {
action: 'get_status_dropdown',
itemtype: $(this).val()
}
}).then((result) => {
$('#status_inner_container' + rand).html(result);
});
});
51 changes: 51 additions & 0 deletions public/js/question_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields 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.
*
* Fields 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 Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2013-2023 by Fields plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

// Question type field: init (existing or new question) then lock the mandatory input.
// init is "none" on an AJAX reload, where only the lock applies.
$('[data-fields-question-type]:not([data-fields-ready])').each(function () {
const marker = $(this).attr('data-fields-ready', '');
const rand = marker.attr('data-fields-question-type');
const init = marker.attr('data-fields-question-type-init');
const mandatory = marker.attr('data-fields-mandatory') === '1' ? 1 : 0;

import(CFG_GLPI.root_doc + GLPI_PLUGINS_PATH.fields + '/js/modules/QuestionField.js').then((m) => {
const questionField = new m.GlpiPluginFieldsQuestionTypeField();

if (init === 'existing') {
questionField.initExistingQuestion(rand);
} else if (init === 'new') {
questionField.initEventHandlers();
}

questionField.lockMandatoryInput(
$('[data-glpi-form-editor-question-type-fields-field-id-selector="' + rand + '"]').closest('[data-glpi-form-editor-question-details]'),
mandatory
);
});
});
15 changes: 9 additions & 6 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ function plugin_init_fields()
// Add/delete profiles to automaticaly to container
$PLUGIN_HOOKS['item_add']['fields']['Profile'] = ['PluginFieldsProfile', 'addNewProfile'];
$PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] = ['PluginFieldsProfile', 'deleteProfile'];
// Event handlers for the subitem loaders and the display conditions and status overrides lists
$PLUGIN_HOOKS['add_javascript']['fields'][] = 'js/fields.js';

//load drag and drop javascript library on Package Interface

Expand Down Expand Up @@ -234,12 +236,13 @@ function plugin_fields_script_endswith($scriptname)
function plugin_version_fields()
{
return [
'name' => __('Additional fields', 'fields'),
'version' => PLUGIN_FIELDS_VERSION,
'author' => "Teclib', Olivier Moron",
'homepage' => 'https://github.com/pluginsGLPI/fields',
'license' => 'GPLv2+',
'requirements' => [
'name' => __('Additional fields', 'fields'),
'version' => PLUGIN_FIELDS_VERSION,
'author' => "Teclib', Olivier Moron",
'homepage' => 'https://github.com/pluginsGLPI/fields',
'license' => 'GPLv2+',
'csp_compliant' => true,
'requirements' => [
'glpi' => [
'min' => PLUGIN_FIELDS_MIN_GLPI,
'max' => PLUGIN_FIELDS_MAX_GLPI,
Expand Down
Loading
Loading