/**
* Convert tag to image
*
* @since version 0.85
*
* @param $content_text text content of input
* @param $force_update force update of content in item (false by default
* @param $doc_data array of filenames and tags
*
* @return nothing
**/
function convertTagToImage($content_text, $force_update = false, $doc_data = array())
{
global $CFG_GLPI;
$matches = array();
// If no doc data available we match all tags in content
if (!count($doc_data)) {
$doc = new Document();
preg_match_all('/' . Document::getImageTag('(([a-z0-9]+|[\\.\\-]?)+)') . '/', $content_text, $matches, PREG_PATTERN_ORDER);
if (isset($matches[1]) && count($matches[1])) {
$doc_data = $doc->find("`tag` IN('" . implode("','", array_unique($matches[1])) . "')");
}
}
if (count($doc_data)) {
foreach ($doc_data as $id => $image) {
// Add only image files : try to detect mime type
$ok = false;
$mime = '';
if (isset($image['filepath'])) {
$fullpath = GLPI_DOC_DIR . "/" . $image['filepath'];
$mime = Toolbox::getMime($fullpath);
$ok = Toolbox::getMime($fullpath, 'image');
}
if (isset($image['tag'])) {
if ($ok || empty($mime)) {
// Replace tags by image in textarea
$img = "<img alt='" . $image['tag'] . "' src='" . $CFG_GLPI['root_doc'] . "/front/document.send.php?docid=" . $id . "&tickets_id=" . $this->fields['id'] . "'/>";
// Replace tag by the image
$content_text = preg_replace('/' . Document::getImageTag($image['tag']) . '/', Html::entities_deep($img), $content_text);
// Replace <br> TinyMce bug
$content_text = str_replace(array('>rn<', '>\\r\\n<', '>\\r<', '>\\n<'), '><', $content_text);
// If the tag is from another ticket : link document to ticket
// TODO : comment maybe not used
// if($image['tickets_id'] != $this->fields['id']){
// $docitem = new Document_Item();
// $docitem->add(array('documents_id' => $image['id'],
// '_do_notif' => false,
// '_disablenotif' => true,
// 'itemtype' => $this->getType(),
// 'items_id' => $this->fields['id']));
// }
} else {
// Remove tag
$content_text = preg_replace('/' . Document::getImageTag($image['tag']) . '/', '', $content_text);
}
}
}
}
if ($force_update) {
$this->fields['content'] = $content_text;
$this->updateInDB(array('content'));
}
return $content_text;
}