Skip to content

Commit

Permalink
#964 [MediaGallery] fix: wrong behavior on JS/action with global object
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Jun 4, 2024
1 parent 255954f commit 59004a7
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions core/tpl/medias/medias_gallery_modal.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,14 @@
}

if ($subaction == 'addFiles') {
global $object;

$data = json_decode(file_get_contents('php://input'), true);

$objectType = $data['objectType'];
$objectId = $data['objectId'];

if ($objectId != $object->id && $objectType != $object->element) {
$className = $objectType;
$object = new $className($db);
$object->fetch($objectId);
}
$className = $objectType;
$object = new $className($db);
$object->fetch($objectId);

$pathToECMImg = $conf->ecm->multidir_output[$conf->entity] . '/'. $moduleNameLowerCase .'/medias';
if (!dol_is_dir($pathToECMImg)) {
Expand Down Expand Up @@ -219,8 +215,6 @@
}

if ($subaction == 'unlinkFile') {
global $object;

$data = json_decode(file_get_contents('php://input'), true);

$fullPath = $data['filepath'] . '/' . $data['filename'];
Expand All @@ -235,28 +229,38 @@
}
}

if (property_exists($object, $data['objectSubtype'])) {
if ($object->{$data['objectSubtype']} == $data['filename']) {
$fileArray = dol_dir_list($data['filepath'], 'files');
if (count($fileArray) > 0) {
$firstFileName = array_shift($fileArray);
$object->{$data['objectSubtype']} = $firstFileName['name'];
} else {
$object->{$data['objectSubtype']} = '';
if ($data['objectId'] > 0) {
$className = $data['objectType'];
$object = new $className($db);
$object->fetch($data['objectId']);

if (property_exists($object, $data['objectSubtype'])) {
if ($object->{$data['objectSubtype']} == $data['filename']) {
$fileArray = dol_dir_list($data['filepath'], 'files');
if (count($fileArray) > 0) {
$firstFileName = array_shift($fileArray);
$object->{$data['objectSubtype']} = $firstFileName['name'];
} else {
$object->{$data['objectSubtype']} = '';
}
$object->setValueFrom($data['objectSubtype'], $object->{$data['objectSubtype']}, '', '', 'text', '', $user);
}
$object->setValueFrom($data['objectSubtype'], $object->{$data['objectSubtype']}, '', '', 'text', '', $user);
}
}
}

if ($subaction == 'addToFavorite') {
global $object;

$data = json_decode(file_get_contents('php://input'), true);

if (property_exists($object, $data['objectSubtype'])) {
$object->{$data['objectSubtype']} = $data['filename'];
$object->setValueFrom($data['objectSubtype'], $object->{$data['objectSubtype']}, '', '', 'text', '', $user);
if ($data['objectId'] > 0) {
$className = $data['objectType'];
$object = new $className($db);
$object->fetch($data['objectId']);

if (property_exists($object, $data['objectSubtype'])) {
$object->{$data['objectSubtype']} = $data['filename'];
$object->setValueFrom($data['objectSubtype'], $object->{$data['objectSubtype']}, '', '', 'text', '', $user);
}
}
}

Expand Down

0 comments on commit 59004a7

Please sign in to comment.