Skip to content

Commit 21b94f5

Browse files
committed
fix(question): save images in description as inline base64
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 906ebeb commit 21b94f5

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

inc/abstractfield.class.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ public function show($domain, $canEdit = true) {
8888
$html .= '</label>';
8989
}
9090
if ($this->isEditableField() && !empty($this->question->fields['description'])) {
91-
$html .= '<div class="help-block">' . html_entity_decode(__($this->question->fields['description'], $domain)) . '</div>';
91+
$description = $this->question->fields['description'];
92+
foreach (PluginFormcreatorCommon::getDocumentsFromTag($description) as $document) {
93+
$prefix = uniqid('', true);
94+
$filename = $prefix . 'image_paste.' . pathinfo($document['filename'], PATHINFO_EXTENSION);
95+
if (!copy(GLPI_DOC_DIR . '/' . $document['filepath'], GLPI_TMP_DIR . '/' . $filename)) {
96+
continue;
97+
}
98+
}
99+
$html .= '<div class="help-block">' . html_entity_decode(__($description, $domain)) . '</div>';
92100
}
93101
$html .= '<div class="form_field">';
94102
$html .= $this->getRenderedHtml($domain, $canEdit);

inc/question.class.php

+57-1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,34 @@ public function prepareInputForAdd($input) {
440440
}
441441
}
442442

443+
// handle description field and its inline pictures
444+
if (isset($input['_description'])) {
445+
foreach ($input['_description'] as $id => $filename) {
446+
// TODO :replace PluginFormcreatorCommon::getDuplicateOf by Document::getDuplicateOf
447+
// when is merged https://github.com/glpi-project/glpi/pull/9335
448+
if ($document = PluginFormcreatorCommon::getDuplicateOf(Session::getActiveEntity(), GLPI_TMP_DIR . '/' . $filename)) {
449+
$this->value = str_replace('id="' . $input['_tag_description'] . '"', $document->fields['tag'], $this->value);
450+
$input['_tag_description'][$id] = $document->fields['tag'];
451+
}
452+
}
453+
454+
$input = $this->addFiles(
455+
$input,
456+
[
457+
'force_update' => true,
458+
'content_field' => null,
459+
'name' => 'description',
460+
]
461+
);
462+
463+
$input['description'] = Html::entity_decode_deep($input['description']);
464+
foreach ($input['_tag_description'] as $tag) {
465+
$regex = '/<img[^>]+' . preg_quote($tag, '/') . '[^<]+>/im';
466+
$input['description'] = preg_replace($regex, "#$tag#", $input['description']);
467+
}
468+
$input['description'] = Html::entities_deep($input['description']);
469+
}
470+
443471
// generate a unique id
444472
if (!isset($input['uuid'])
445473
|| empty($input['uuid'])) {
@@ -469,6 +497,34 @@ public function prepareInputForUpdate($input) {
469497
return false;
470498
}
471499

500+
// handle description field and its inline pictures
501+
if (isset($input['_description'])) {
502+
foreach ($input['_description'] as $id => $filename) {
503+
// TODO :replace PluginFormcreatorCommon::getDuplicateOf by Document::getDuplicateOf
504+
// when is merged https://github.com/glpi-project/glpi/pull/9335
505+
if ($document = PluginFormcreatorCommon::getDuplicateOf(Session::getActiveEntity(), GLPI_TMP_DIR . '/' . $filename)) {
506+
$this->value = str_replace('id="' . $input['_tag_description'] . '"', $document->fields['tag'], $this->value);
507+
$input['_tag_description'][$id] = $document->fields['tag'];
508+
}
509+
}
510+
511+
$input = $this->addFiles(
512+
$input,
513+
[
514+
'force_update' => true,
515+
'content_field' => null,
516+
'name' => 'description',
517+
]
518+
);
519+
520+
$input['description'] = Html::entity_decode_deep($input['description']);
521+
foreach ($input['_tag_description'] as $tag) {
522+
$regex = '/<img[^>]+' . preg_quote($tag, '/') . '[^<]+>/im';
523+
$input['description'] = preg_replace($regex, "#$tag#", $input['description']);
524+
}
525+
$input['description'] = Html::entities_deep($input['description']);
526+
}
527+
472528
// generate a unique id
473529
if (!isset($input['uuid'])
474530
|| empty($input['uuid'])) {
@@ -831,7 +887,7 @@ public function showForm($ID, $options = []) {
831887
echo Html::textarea([
832888
'name' => 'description',
833889
'id' => 'description',
834-
'value' => $this->fields['description'],
890+
'value' => Toolbox::convertTagToImage($this->fields['description'], $this),
835891
'enable_richtext' => true,
836892
'filecontainer' => 'description_info',
837893
'display' => false,

0 commit comments

Comments
 (0)