ILIAS  release_8 Revision v8.23
class.ilDclMobRecordFieldModel.php
Go to the documentation of this file.
1 <?php
2 
20 {
21  private \ilGlobalTemplateInterface $main_tpl;
22  private \ILIAS\FileUpload\FileUpload $upload;
23 
25  {
26  parent::__construct($record, $field);
27  global $DIC;
28  $this->upload = $DIC->upload();
29  $this->main_tpl = $DIC->ui()->mainTemplate();
30  }
31 
39  public function parseValue($value)
40  {
41  if ($value === -1) { //marked for deletion.
42  return null;
43  }
44 
45  $media = $value;
46 
47  $has_record_id = $this->http->wrapper()->query()->has('record_id');
48  $is_confirmed = $this->http->wrapper()->post()->has('save_confirmed');
49  $has_save_confirmation = ($this->getRecord()->getTable()->getSaveConfirmation() && !$has_record_id);
50 
51  if (is_array($media)
52  && isset($media['tmp_name'])
53  && $media['tmp_name'] !== ""
54  && (!$has_save_confirmation || $is_confirmed)
55  ) {
56  $mob = new ilObjMediaObject();
57  $mob->setTitle($media['name']);
58  $mob->create();
59  $mob_dir = ilObjMediaObject::_getDirectory($mob->getId());
60  if (!is_dir($mob_dir)) {
61  $mob->createDirectory();
62  }
63  $media_item = new ilMediaItem();
64  $mob->addMediaItem($media_item);
65  $media_item->setPurpose("Standard");
66  $file_name = ilFileUtils::getASCIIFilename($media['name']);
67  $file_name = str_replace(" ", "_", $file_name);
68  $target_file_path = $mob_dir . "/" . $file_name;
69  $title = $file_name;
70  $location = $file_name;
71 
72  if ($has_save_confirmation) {
73  $ilfilehash = $this->http->wrapper()->post()->retrieve(
74  'ilfilehash',
75  $this->refinery->kindlyTo()->string()
76  );
77 
79  $ilfilehash,
80  'field_' . $this->getField()->getId(),
81  $media["name"],
82  $media["type"]
83  );
84  } else {
85  if (false === $this->upload->hasBeenProcessed()) {
86  $this->upload->process();
87  }
88 
89  if (false === $this->upload->hasUploads()) {
90  throw new ilException($this->lng->txt('upload_error_file_not_found'));
91  }
92  $move_file = $media['tmp_name'];
93  }
94 
95  ilFileUtils::rename($move_file, $target_file_path);
97 
98  // Check image/video
99  $format = ilObjMediaObject::getMimeType($target_file_path);
100 
101  if ($format == 'image/jpeg') {
102  list($width, $height, $type, $attr) = getimagesize($target_file_path);
103  $field = $this->getField();
106  if ($new_width || $new_height) {
107  //only resize if it is bigger, not if it is smaller
108  if ($new_height < $height && $new_width < $width) {
109  //resize proportional
110  if (!$new_height || !$new_width) {
111  $format = ilObjMediaObject::getMimeType($target_file_path);
112  $wh
114  $format,
115  "File",
116  $target_file_path,
117  "",
118  true,
119  false,
122  );
123  } else {
126  }
127 
128  $location = ilObjMediaObject::_resizeImage($target_file_path, $wh['width'], $wh['height']);
129  }
130  }
131  }
132 
134  $mob->getId(),
135  "dcl:html",
136  $this->getRecord()->getTable()->getCollectionObject()->getId()
137  );
138  $media_item->setFormat($format);
139  $media_item->setLocation($location);
140  $media_item->setLocationType("LocalFile");
141 
143  $med = $mob->getMediaItem("Standard");
144  $mob_file = ilObjMediaObject::_getDirectory($mob->getId()) . "/" . $med->getLocation();
145  $a_target_dir = ilObjMediaObject::_getDirectory($mob->getId());
146  try {
147  $new_file = ilFFmpeg::extractImage($mob_file, "mob_vpreview.png", $a_target_dir, 1);
148  } catch (Exception $e) {
149  $this->main_tpl->setOnScreenMessage('failure', $e->getMessage(), true);
150  }
151  }
152 
153  $mob->update();
154  $return = $mob->getId();
155  // handover for save-confirmation
156  } else {
157  if (is_array($media) && isset($media['tmp_name']) && $media['tmp_name'] != '') {
158  $return = $media;
159  } else {
160  $return = $this->getValue();
161  }
162  }
163 
164  return $return;
165  }
166 
172  public function parseExportValue($value)
173  {
174  $file = $value;
175  if (is_numeric($file)) {
176  $mob = new ilObjMediaObject($file);
177  $mob_name = $mob->getTitle();
178 
179  return $mob_name;
180  }
181 
182  return $file;
183  }
184 
185  public function addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation): void
186  {
187  if (is_array($this->getValue())) {
188  foreach ($this->getValue() as $key => $value) {
189  $confirmation->addHiddenItem('field_' . $this->field->getId() . '[' . $key . ']', $value);
190  }
191  }
192  }
193 
198  public function parseSortingValue($value, bool $link = true): string
199  {
200  $mob = new ilObjMediaObject($value);
201 
202  return $mob->getTitle();
203  }
204 
205  public function setValueFromForm(ilPropertyFormGUI $form): void
206  {
207  $value = $form->getInput("field_" . $this->getField()->getId());
208  if ($form->getItemByPostVar("field_" . $this->getField()->getId())->getDeletionFlag()) {
209  $value = -1;
210  }
211  $this->setValue($value);
212  }
213 
214  public function afterClone(): void
215  {
219 
220  if (!$record_field || !$record_field->getValue()) {
221  return;
222  }
223 
224  $mob_old = new ilObjMediaObject($record_field->getValue());
225  $mob_new = $mob_old->duplicate();
226 
227  $this->setValue($mob_new->getId(), true);
228  $this->doUpdate();
229  }
230 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _determineWidthHeight(string $a_format, string $a_type, string $a_file, string $a_reference, bool $a_constrain_proportions, bool $a_use_original, ?int $a_user_width=null, ?int $a_user_height=null)
$type
getItemByPostVar(string $a_post_var)
setValue($value, bool $omit_parsing=false)
Set value for record field.
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
static _resizeImage(string $a_file, int $a_width, int $a_height, bool $a_constrain_prop=false)
Resize image and return new image file ("_width_height" string appended)
addHiddenItem(string $a_post_var, string $a_value)
static extractImage(string $a_file, string $a_target_filename, string $a_target_dir="", int $a_sec=1)
Extract image from video file.
parseExportValue($value)
Function to parse incoming data from form input value $value.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
static getASCIIFilename(string $a_filename)
global $DIC
Definition: feed.php:28
static renameExecutables(string $a_dir)
static supportsImageExtraction(string $a_mime)
Check if mime type supports image extraction.
static _getDirectory(int $a_mob_id)
Get absolute directory.
static http()
Fetches the global http state from ILIAS.
static getMimeType(string $a_file, bool $a_external=false)
get mime type for file
doUpdate()
Update object in database.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193
$format
Definition: metadata.php:235
parseSortingValue($value, bool $link=true)
Returns sortable value for the specific field-types.
ILIAS FileUpload FileUpload $upload
getProperty(string $key)
Returns a certain property of a field.
__construct(Container $dic, ilPlugin $plugin)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getCloneOf(int $id, string $type)
addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
duplicate()
Duplicate media object, return new media object.
static rename(string $a_source, string $a_target)
static getRecordFieldCache(object $record, object $field)
__construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
static enabled()
Checks, whether FFmpeg support is enabled (path is set in the setup)
static getTempFilename(string $a_hash, string $a_field, string $a_name, string $a_type, ?string $a_index=null, ?string $a_sub_index=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setValueFromForm(ilPropertyFormGUI $form)