Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataCollection: Refactor cast properties on image upload field #7903

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ public function __construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $f

/**
* @param array|int $value
* @return array|string
* @throws ilException
* @throws ilFileUtilsException
* @throws ilMediaObjectsException
*/
public function parseValue($value)
{
if ($value === -1) { //marked for deletion.
if ($value === -1) {
return null;
}

Expand All @@ -50,11 +47,7 @@ public function parseValue($value)
$is_confirmed = $this->http->wrapper()->post()->has('save_confirmed');
$has_save_confirmation = ($this->getRecord()->getTable()->getSaveConfirmation() && !$has_record_id);

if (is_array($media)
&& isset($media['tmp_name'])
&& $media['tmp_name'] !== ""
&& (!$has_save_confirmation || $is_confirmed)
) {
if (($media['tmp_name'] ?? '') !== '' && (!$has_save_confirmation || $is_confirmed)) {
$mob = new ilObjMediaObject();
$mob->setTitle($media['name']);
$mob->create();
Expand All @@ -64,10 +57,10 @@ public function parseValue($value)
}
$media_item = new ilMediaItem();
$mob->addMediaItem($media_item);
$media_item->setPurpose("Standard");
$media_item->setPurpose('Standard');
$file_name = ilFileUtils::getASCIIFilename($media['name']);
$file_name = str_replace(" ", "_", $file_name);
$target_file_path = $mob_dir . "/" . $file_name;
$file_name = str_replace(' ', '_', $file_name);
$target_file_path = $mob_dir . '/' . $file_name;
$location = $file_name;

if ($has_save_confirmation) {
Expand All @@ -79,15 +72,15 @@ public function parseValue($value)
$move_file = ilDclPropertyFormGUI::getTempFilename(
$ilfilehash,
'field_' . $this->getField()->getId(),
$media["name"],
$media["type"]
$media['name'],
$media['type']
);
} else {
if (false === $this->upload->hasBeenProcessed()) {
if (!$this->upload->hasBeenProcessed()) {
$this->upload->process();
}

if (false === $this->upload->hasUploads()) {
if (!$this->upload->hasUploads()) {
throw new ilException($this->lng->txt('upload_error_file_not_found'));
}
$move_file = $media['tmp_name'];
Expand All @@ -96,66 +89,57 @@ public function parseValue($value)
ilFileUtils::rename($move_file, $target_file_path);
ilFileUtils::renameExecutables($mob_dir);

// Check image/video
$format = ilObjMediaObject::getMimeType($target_file_path);

if ($format == 'image/jpeg') {
if ($format === 'image/jpeg') {
list($width, $height, $type, $attr) = getimagesize($target_file_path);
$field = $this->getField();
$new_width = $field->getProperty(ilDclBaseFieldModel::PROP_WIDTH);
$new_height = $field->getProperty(ilDclBaseFieldModel::PROP_HEIGHT);
if ($new_width || $new_height) {
//only resize if it is bigger, not if it is smaller
$new_width = (int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_WIDTH);
$new_height = (int) $this->getField()->getProperty(ilDclBaseFieldModel::PROP_HEIGHT);
if ($new_width > 0 || $new_height > 0) {
if ($new_height < $height && $new_width < $width) {
//resize proportional
if (!$new_height || !$new_width) {
$format = ilObjMediaObject::getMimeType($target_file_path);
$wh
= ilObjMediaObject::_determineWidthHeight(
$format,
"File",
$target_file_path,
"",
true,
false,
$field->getProperty(ilDclBaseFieldModel::PROP_WIDTH),
(int) $field->getProperty(ilDclBaseFieldModel::PROP_HEIGHT)
);
} else {
$wh['width'] = (int) $field->getProperty(ilDclBaseFieldModel::PROP_WIDTH);
$wh['height'] = (int) $field->getProperty(ilDclBaseFieldModel::PROP_HEIGHT);
$wh['width'] = $new_width;
$wh['height'] = $new_height;
if ($new_height === 0 || $new_width === 0) {
$wh = ilObjMediaObject::_determineWidthHeight(
$format,
'File',
$target_file_path,
'',
true,
false,
$new_width,
$new_height
);
}

$location = ilObjMediaObject::_resizeImage($target_file_path, $wh['width'], $wh['height']);
}
}
}

ilObjMediaObject::_saveUsage(
$mob->getId(),
"dcl:html",
'dcl:html',
$this->getRecord()->getTable()->getCollectionObject()->getId()
);
$media_item->setFormat($format);
$media_item->setLocation($location);
$media_item->setLocationType("LocalFile");
$media_item->setLocationType('LocalFile');

if (ilFFmpeg::enabled() && ilFFmpeg::supportsImageExtraction($format)) {
$med = $mob->getMediaItem("Standard");
$mob_file = ilObjMediaObject::_getDirectory($mob->getId()) . "/" . $med->getLocation();
$med = $mob->getMediaItem('Standard');
$mob_file = ilObjMediaObject::_getDirectory($mob->getId()) . '/' . $med->getLocation();
$a_target_dir = ilObjMediaObject::_getDirectory($mob->getId());
try {
ilFFmpeg::extractImage($mob_file, "mob_vpreview.png", $a_target_dir);
ilFFmpeg::extractImage($mob_file, 'mob_vpreview.png', $a_target_dir);
} catch (Exception $e) {
$this->main_tpl->setOnScreenMessage('failure', $e->getMessage(), true);
}
}

$mob->update();
$return = $mob->getId();
// handover for save-confirmation
} else {
if (is_array($media) && isset($media['tmp_name']) && $media['tmp_name'] != '') {
if (($media['tmp_name'] ?? '') !== '') {
$return = $media;
} else {
$return = $this->getValue();
Expand Down
Loading