Skip to content
Merged
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

- Fix form submission error when a multiple dropdown field question is left empty
Expand Down
8 changes: 6 additions & 2 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,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 @@ -1509,11 +1510,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;
} else {
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
37 changes: 8 additions & 29 deletions inc/labeltranslation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,8 @@ public static function showTranslations(CommonDBTM $item)
'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,32 +229,20 @@ public static function showTranslations(CommonDBTM $item)
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>';
if ($canedit) {
$ajax_params = [
'type' => self::class,
'itemtype' => $item::getType(),
'items_id' => $item->getID(),
'id' => $data['id'],
];
echo Html::scriptBlock('
viewEditTranslation' . $data['id'] . $rand . ' = function() {
$("#viewtranslation' . $item->getID() . $rand . '").load(
"' . $CFG_GLPI['root_doc'] . '/plugins/fields/ajax/viewtranslations.php",
' . json_encode($ajax_params) . '
);
};
');
}

echo Dropdown::getLanguageName($data['language']);
echo '</td><td>';
echo htmlspecialchars((string) $data['label'], ENT_QUOTES);
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 @@ -186,6 +186,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 @@ -238,12 +240,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