Skip to content

Commit

Permalink
fix(question): save images in description as inline base64
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Aug 9, 2021
1 parent 906ebeb commit 21b94f5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
10 changes: 9 additions & 1 deletion inc/abstractfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ public function show($domain, $canEdit = true) {
$html .= '</label>';
}
if ($this->isEditableField() && !empty($this->question->fields['description'])) {
$html .= '<div class="help-block">' . html_entity_decode(__($this->question->fields['description'], $domain)) . '</div>';
$description = $this->question->fields['description'];
foreach (PluginFormcreatorCommon::getDocumentsFromTag($description) as $document) {
$prefix = uniqid('', true);
$filename = $prefix . 'image_paste.' . pathinfo($document['filename'], PATHINFO_EXTENSION);
if (!copy(GLPI_DOC_DIR . '/' . $document['filepath'], GLPI_TMP_DIR . '/' . $filename)) {
continue;
}
}
$html .= '<div class="help-block">' . html_entity_decode(__($description, $domain)) . '</div>';
}
$html .= '<div class="form_field">';
$html .= $this->getRenderedHtml($domain, $canEdit);
Expand Down
58 changes: 57 additions & 1 deletion inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,34 @@ public function prepareInputForAdd($input) {
}
}

// handle description field and its inline pictures
if (isset($input['_description'])) {
foreach ($input['_description'] as $id => $filename) {
// TODO :replace PluginFormcreatorCommon::getDuplicateOf by Document::getDuplicateOf
// when is merged https://github.com/glpi-project/glpi/pull/9335
if ($document = PluginFormcreatorCommon::getDuplicateOf(Session::getActiveEntity(), GLPI_TMP_DIR . '/' . $filename)) {
$this->value = str_replace('id="' . $input['_tag_description'] . '"', $document->fields['tag'], $this->value);
$input['_tag_description'][$id] = $document->fields['tag'];
}
}

$input = $this->addFiles(
$input,
[
'force_update' => true,
'content_field' => null,
'name' => 'description',
]
);

$input['description'] = Html::entity_decode_deep($input['description']);
foreach ($input['_tag_description'] as $tag) {
$regex = '/<img[^>]+' . preg_quote($tag, '/') . '[^<]+>/im';
$input['description'] = preg_replace($regex, "#$tag#", $input['description']);
}
$input['description'] = Html::entities_deep($input['description']);
}

// generate a unique id
if (!isset($input['uuid'])
|| empty($input['uuid'])) {
Expand Down Expand Up @@ -469,6 +497,34 @@ public function prepareInputForUpdate($input) {
return false;
}

// handle description field and its inline pictures
if (isset($input['_description'])) {
foreach ($input['_description'] as $id => $filename) {
// TODO :replace PluginFormcreatorCommon::getDuplicateOf by Document::getDuplicateOf
// when is merged https://github.com/glpi-project/glpi/pull/9335
if ($document = PluginFormcreatorCommon::getDuplicateOf(Session::getActiveEntity(), GLPI_TMP_DIR . '/' . $filename)) {
$this->value = str_replace('id="' . $input['_tag_description'] . '"', $document->fields['tag'], $this->value);
$input['_tag_description'][$id] = $document->fields['tag'];
}
}

$input = $this->addFiles(
$input,
[
'force_update' => true,
'content_field' => null,
'name' => 'description',
]
);

$input['description'] = Html::entity_decode_deep($input['description']);
foreach ($input['_tag_description'] as $tag) {
$regex = '/<img[^>]+' . preg_quote($tag, '/') . '[^<]+>/im';
$input['description'] = preg_replace($regex, "#$tag#", $input['description']);
}
$input['description'] = Html::entities_deep($input['description']);
}

// generate a unique id
if (!isset($input['uuid'])
|| empty($input['uuid'])) {
Expand Down Expand Up @@ -831,7 +887,7 @@ public function showForm($ID, $options = []) {
echo Html::textarea([
'name' => 'description',
'id' => 'description',
'value' => $this->fields['description'],
'value' => Toolbox::convertTagToImage($this->fields['description'], $this),
'enable_richtext' => true,
'filecontainer' => 'description_info',
'display' => false,
Expand Down

0 comments on commit 21b94f5

Please sign in to comment.