Skip to content

Commit

Permalink
Added richtext field
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeto-J committed Aug 9, 2023
1 parent 3172bb0 commit 1e6438e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 29 deletions.
104 changes: 75 additions & 29 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,15 +1141,28 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
*/
public function updateFieldsValues($data, $itemtype, $massiveaction = false)
{
global $DB;

if (self::validateValues($data, $itemtype, $massiveaction) === false) {
return false;
}

// Normalize values
foreach ($data as $key => $value) {
if (is_array($value)) {
// Convert "multiple" values into a JSON string
$data[$key] = json_encode($value);
// Convert "multiple" values into a JSON string
$multiple_fields_iterator = $DB->request([
'FROM' => PluginFieldsField::getTable(),
'WHERE' => [
'is_active' => 1,
'multiple' => 1,
'plugin_fields_containers_id' => $data['plugin_fields_containers_id'],
]
]);
foreach ($multiple_fields_iterator as $field_data) {
$field_name = $field_data['name'];
if ($field_data['type'] === 'dropdown') {
$field_name = 'plugin_fields_' . $field_data['name'] . 'dropdowns_id';
}
if (array_key_exists($field_name, $data)) {
$data[$field_name] = json_encode($data[$field_name]);
}
}

Expand All @@ -1159,39 +1172,62 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)
$items_id = $data['items_id'];
$classname = self::getClassname($itemtype, $container_obj->fields['name']);

//check if data already inserted
$obj = new $classname();
$found = $obj->find(['items_id' => $items_id]);
if (empty($found)) {
$obj = new $classname();
if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) {
// add fields data
$obj->add($data);

//construct history on itemtype object (Historical tab)
self::constructHistory(
$data['plugin_fields_containers_id'],
$items_id,
$itemtype,
$data,
$obj
);
} else {
$first_found = array_pop($found);
$data['id'] = $first_found['id'];
// update fields data
$data['id'] = $obj->fields['id'];
$obj->update($data);

//construct history on itemtype object (Historical tab)
self::constructHistory(
$data['plugin_fields_containers_id'],
$items_id,
$itemtype,
$data,
$obj
);
}

// Add files and images for richtext fields
$this->addRichTextFiles($obj);

//construct history on itemtype object (Historical tab)
self::constructHistory(
$obj->input['plugin_fields_containers_id'],
$items_id,
$itemtype,
$obj->input,
$obj
);

return true;
}

private function addRichTextFiles(CommonDBTM $object): void
{
global $DB;

$richtext_fields_iterator = $DB->request([
'FROM' => PluginFieldsField::getTable(),
'WHERE' => [
'is_active' => 1,
'type' => "richtext",
'plugin_fields_containers_id' => $object->input['plugin_fields_containers_id']
]
]);
foreach ($richtext_fields_iterator as $field_data) {
$field_name = $field_data['name'];

$object->input = $object->addFiles(
$object->input,
[
'force_update' => true,
'name' => $field_name,
'content_field' => $field_name
]
);

// remove uploaded file input to ensure they will not be added to history
unset($object->input[sprintf('_%s', $field_name)]);
unset($object->input[sprintf('_prefix_%s', $field_name)]);
unset($object->input[sprintf('_tag_%s', $field_name)]);
}
}

/**
* Add log in "itemtype" object on fields values update
* @param int $containers_id container id
Expand Down Expand Up @@ -1692,6 +1728,16 @@ private static function populateData($c_id, CommonDBTM $item)
$item->input[$input] = str_replace(",", ".", $item->input[$input]);
}
$data[$input] = $item->input[$input];

if ($field['type'] === 'richtext') {
$filename_input = sprintf('_%s', $input);
$prefix_input = sprintf('_prefix_%s', $input);
$tag_input = sprintf('_tag_%s', $input);

$data[$filename_input] = $item->input[$filename_input] ?? [];
$data[$prefix_input] = $item->input[$prefix_input] ?? [];
$data[$tag_input] = $item->input[$tag_input] ?? [];
}
}
}

Expand Down
1 change: 1 addition & 0 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ public static function getTypes(bool $flat_list = true)
'header' => __("Header", "fields"),
'text' => __("Text (single line)", "fields"),
'textarea' => __("Text (multiples lines)", "fields"),
'richtext' => __("Rich Text", "fields"),
'number' => __("Number", "fields"),
'url' => __("URL", "fields"),
'dropdown' => __("Dropdown", "fields"),
Expand Down
3 changes: 3 additions & 0 deletions inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static function getSQLFields(string $field_name, string $field_type, arra
case $field_type === 'url':
$fields[$field_name] = 'TEXT DEFAULT NULL';
break;
case $field_type === 'richtext':
$fields[$field_name] = 'LONGTEXT DEFAULT NULL';
break;
case $field_type === 'yesno':
$fields[$field_name] = 'INT NOT NULL DEFAULT 0';
break;
Expand Down
11 changes: 11 additions & 0 deletions templates/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,20 @@
'type': 'url',
'add_field_html': ext_link
})) }}

{% elseif type == 'textarea' %}
{{ macros.textareaField(name, value, label, field_options) }}

{% elseif type == 'richtext' %}
{{ macros.textareaField(name, value, label, field_options|merge({
'enable_richtext': true,
'field_class': 'col-12',
'label_class': '',
'input_class': '',
'align_label_right': false,
'mb': 'm-2'
})) }}

{% elseif type == 'yesno' %}
{{ macros.dropdownYesNo(name, value, label, field_options) }}

Expand Down

0 comments on commit 1e6438e

Please sign in to comment.