Skip to content

Commit

Permalink
Fixed clone and copy handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gvollbach committed Jan 3, 2024
1 parent 767c2f6 commit 2b4f4c0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 73 deletions.
10 changes: 7 additions & 3 deletions Services/Badge/classes/class.ilBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ public function copy(
$this->setId(0);
$this->create();
if($this->getImageRid()) {
$img = $this->getImageRid();
$this->setImageRid($img);
$this->update();
} else {
$img = $this->getImagePath();
if ($img) {
Expand Down Expand Up @@ -475,7 +474,12 @@ public function delete(): void
if (file_exists($this->getImagePath())) {
unlink($this->getImagePath());
} else {
$this->resource_storage->manage()->remove($this->getImageRid(), new ilBadgeFileStakeholder());
if($this->getImageRid() !== null) {
try{
$this->resource_storage->manage()->remove($this->getImageRid(), new ilBadgeFileStakeholder());
} catch (Exception $e) {
}
}
}

$this->deleteStaticFiles();
Expand Down
7 changes: 5 additions & 2 deletions Services/Badge/classes/class.ilBadgeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class ilBadgeImage
private ?FileUpload $upload_service;
private ?ilGlobalTemplateInterface $main_template;

public function __construct(Services $resourceStorage, FileUpload $uploadService, ilGlobalTemplateInterface $main_template)
{
public function __construct(
Services $resourceStorage,
FileUpload $uploadService,
ilGlobalTemplateInterface $main_template
) {
$this->resource_storage = $resourceStorage;
$this->upload_service = $uploadService;
$this->main_template = $main_template;
Expand Down
39 changes: 32 additions & 7 deletions Services/Badge/classes/class.ilBadgeImageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,19 @@ public function uploadImage(array $a_upload_meta): void
public function processImageUpload( $badge) : void
{
try {
$this->upload_service->process();
$array_result = $this->upload_service->getResults();
$array_result = array_pop($array_result);
$stakeholder = new ilBadgeFileStakeholder();
$identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
$badge->setImageRid($identification);
$badge->update();
if (!$this->upload_service->hasBeenProcessed()) {
$this->upload_service->process();
}
if($this->upload_service->hasUploads()) {
$array_result = $this->upload_service->getResults();
$array_result = array_pop($array_result);
if($array_result->getName() !== '') {
$stakeholder = new ilBadgeFileStakeholder();
$identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
$badge->setImageRid($identification);
$badge->update();
}
}
} catch (IllegalStateException $e) {
$this->main_template->setOnScreenMessage('failure', $e->getMessage(), true);
}
Expand Down Expand Up @@ -361,4 +367,23 @@ public function setImageRid(?string $image_rid = null) : void
{
$this->image_rid = $image_rid;
}

public function getImageFromResourceId(?string $image_rid, int $badge_id = null) : string
{
$image_src = '';

if ($image_rid !== null) {
$identification = $this->resource_storage->manage()->find($image_rid);
if ($identification !== null) {
$image_src = $this->resource_storage->consume()->src($identification)->getSrc();
}
} else {
if($badge_id !== null) {
$badge = new ilBadge($badge_id);
$image_src = $badge->getImage();
}
}

return $image_src;
}
}
53 changes: 17 additions & 36 deletions Services/Badge/classes/class.ilBadgeImageTemplateTableGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct(
$this->setFormAction($ilCtrl->getFormAction($a_parent_obj));
$this->setRowTemplate("tpl.template_row.html", "Services/Badge");
$this->setDefaultOrderField("title");
$this->setExternalSorting(true);

$this->getItems();
}
Expand All @@ -73,64 +74,44 @@ public function getItems(): void
$data = array();

foreach (ilBadgeImageTemplate::getInstances() as $template) {
$data[] = array(
"id" => $template->getId(),
"title" => $template->getTitle(),
"path" => $template->getImagePath(),
"file" => $template->getImage(),
"file_rid" => $template->getImageRid()
);

if($template->getId() !== null) {
$data[] = $template;
}
}

$this->setData($data);
}

protected function fillRow(array $a_set): void
protected function fillRow($badge_image_template): void
{
$lng = $this->lng;
$ilCtrl = $this->ctrl;

/**
* @var ilBadgeImageTemplate $badge_image_template
*/
if ($this->has_write) {
$this->tpl->setVariable("VAL_ID", $a_set["id"]);
$this->tpl->setVariable("VAL_ID", $badge_image_template->getId());
}

$this->tpl->setVariable("TXT_TITLE", $a_set["title"]);
if($a_set['file_rid']) {
$this->tpl->setVariable("TXT_IMG", $a_set["file_rid"]);
$img = $this->getImageFromResourceId($a_set["file_rid"]);
$this->tpl->setVariable("TXT_TITLE", $badge_image_template->getTitle());
if($badge_image_template->getImageRid()) {
$this->tpl->setVariable("TXT_IMG", $badge_image_template->getImageRid());
$img = $badge_image_template->getImageFromResourceId($badge_image_template->getImageRid());
$this->tpl->setVariable("VAL_IMG", $img);
} else {
$this->tpl->setVariable("VAL_IMG", ilWACSignedPath::signFile($a_set["path"]));
$this->tpl->setVariable("TXT_IMG", $a_set["file"]);
$this->tpl->setVariable("VAL_IMG", ilWACSignedPath::signFile($badge_image_template->getImagePath()));
$this->tpl->setVariable("TXT_IMG", $badge_image_template->getImage());
}


if ($this->has_write) {
$ilCtrl->setParameter($this->getParentObject(), "tid", $a_set["id"]);
$ilCtrl->setParameter($this->getParentObject(), "tid", $badge_image_template->getId());
$url = $ilCtrl->getLinkTarget($this->getParentObject(), "editImageTemplate");
$ilCtrl->setParameter($this->getParentObject(), "tid", "");

$this->tpl->setVariable("TXT_EDIT", $lng->txt("edit"));
$this->tpl->setVariable("URL_EDIT", $url);
}
}

public function getImageFromResourceId(?string $image_rid, int $badge_id = null) : string
{
$image_src = '';

if ($image_rid !== null) {
$identification = $this->resource_storage->manage()->find($image_rid);
if ($identification !== null) {
$image_src = $this->resource_storage->consume()->src($identification)->getSrc();
}
} else {
if($badge_id !== null) {
$badge = new ilBadge($badge_id);
$image_src = $badge->getImage();
}
}

return $image_src;
}
}
21 changes: 18 additions & 3 deletions Services/Badge/classes/class.ilBadgeManagementGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ protected function saveBadge(): void
$this->badge_image_service->processImageUpload($badge);
} else {
$tmpl = new ilBadgeImageTemplate($form->getInput('tmpl'));
$badge->importImage($tmpl->getImage(), $tmpl->getImagePath());
if($tmpl->getImageRid() !== null) {
$badge->setImageRid($tmpl->getImageRid());
$badge->update();
}
}

$this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
Expand Down Expand Up @@ -500,13 +503,25 @@ protected function updateBadge(): void
$badge->setDescription($form->getInput('desc'));
$badge->setCriteria($form->getInput('crit'));
$badge->setValid($form->getInput('valid'));
$image = $form->getInput('img');
if(isset($image['name']) && $image['name'] !== '') {
$this->badge_image_service->processImageUpload($badge);
}

$badge->update();
if ($custom) {
$badge->setConfiguration($custom->getConfigFromForm($form));
}

$badge->update();
$this->badge_image_service->processImageUpload($badge);
$tmpl_id = $form->getInput('tmpl');
if($tmpl_id !== '') {
$tmpl = new ilBadgeImageTemplate($tmpl_id);
if($tmpl->getImageRid() !== '') {
$badge->setImageRid($tmpl->getImageRid());
$this->badge_image_service->processImageUpload($badge);
$badge->update();
}
}

$this->tpl->setOnScreenMessage('success', $lng->txt('settings_saved'), true);
$ilCtrl->redirect($this, 'listBadges');
Expand Down
22 changes: 1 addition & 21 deletions Services/Badge/classes/class.ilObjBadgeAdministrationGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ protected function saveImageTemplate(): void
$tmpl->setTypes($form->getInput("type"));
$tmpl->create();

#$tmpl->uploadImage($_FILES["img"]);
$tmpl->processImageUpload($tmpl);

$this->tpl->setOnScreenMessage('success', $lng->txt("settings_saved"), true);
Expand Down Expand Up @@ -416,7 +415,7 @@ protected function setImageTemplateFormValues(
): void {
$a_form->getItemByPostVar("title")->setValue($a_tmpl->getTitle());
if($a_tmpl->getImageRid() !== null) {
$img = $this->getImageFromResourceId($a_tmpl->getImageRid());
$img = $a_tmpl->getImageFromResourceId($a_tmpl->getImageRid());
$a_form->getItemByPostVar("img")->setImage($img);
$a_form->getItemByPostVar("img")->setValue($a_tmpl->getImageRid());
} else {
Expand Down Expand Up @@ -460,7 +459,6 @@ protected function updateImageTemplate(): void

$tmpl->update();

#$tmpl->uploadImage($_FILES["img"]);
$tmpl->processImageUpload($tmpl);

$this->tpl->setOnScreenMessage('success', $lng->txt("settings_saved"), true);
Expand Down Expand Up @@ -723,22 +721,4 @@ protected function deleteObjectBadges(): void
$ilCtrl->redirect($this, "listObjectBadges");
}

public function getImageFromResourceId(?string $image_rid, int $badge_id = null) : string
{
$image_src = '';

if ($image_rid !== null) {
$identification = $this->resource_storage->manage()->find($image_rid);
if ($identification !== null) {
$image_src = $this->resource_storage->consume()->src($identification)->getSrc();
}
} else {
if($badge_id !== null) {
$badge = new ilBadge($badge_id);
$image_src = $badge->getImage();
}
}

return $image_src;
}
}
1 change: 0 additions & 1 deletion src/FileUpload/FileUploadImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ public function hasUploads(): bool
if ($this->moved) {
return false;
}

$uploadedFiles = $this->flattenUploadedFiles($this->globalHttpState->request()->getUploadedFiles());

return ($uploadedFiles !== []);
Expand Down

0 comments on commit 2b4f4c0

Please sign in to comment.