Skip to content

Commit 4c8de56

Browse files
authored
[4.0] Media Manager Events correctly triggered (#28886)
1 parent 9ede7af commit 4c8de56

File tree

1 file changed

+82
-79
lines changed

1 file changed

+82
-79
lines changed

administrator/components/com_media/src/Model/ApiModel.php

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,24 @@ public function createFolder($adapter, $name, $path, $override)
228228
throw new FileExistsException;
229229
}
230230

231-
$object = $this->triggerEvent(
232-
$adapter,
233-
$name,
234-
$path,
235-
0,
236-
function ($object)
237-
{
238-
$object->name = $this->getAdapter($object->adapter)->createFolder($object->name, $object->path);
239-
}
240-
);
231+
$app = Factory::getApplication();
232+
$object = new CMSObject;
233+
$object->adapter = $adapter;
234+
$object->name = $name;
235+
$object->path = $path;
236+
237+
PluginHelper::importPlugin('content');
238+
239+
$result = $app->triggerEvent('onContentBeforeSave', ['com_media.folder', $object, true, $object]);
240+
241+
if (in_array(false, $result, true))
242+
{
243+
throw new \Exception($object->getError());
244+
}
245+
246+
$object->name = $this->getAdapter($object->adapter)->createFolder($object->name, $object->path);
247+
248+
$app->triggerEvent('onContentAfterSave', ['com_media.folder', $object, true, $object]);
241249

242250
return $object->name;
243251
}
@@ -281,16 +289,29 @@ public function createFile($adapter, $name, $path, $data, $override)
281289
throw new InvalidPathException;
282290
}
283291

284-
$object = $this->triggerEvent(
285-
$adapter,
286-
$name,
287-
$path,
288-
$data,
289-
function ($object)
290-
{
291-
$object->name = $this->getAdapter($object->adapter)->createFile($object->name, $object->path, $object->data);
292-
}
293-
);
292+
$app = Factory::getApplication();
293+
$object = new CMSObject;
294+
$object->adapter = $adapter;
295+
$object->name = $name;
296+
$object->path = $path;
297+
$object->data = $data;
298+
$object->extension = strtolower(File::getExt($name));
299+
300+
PluginHelper::importPlugin('content');
301+
302+
// Also include the filesystem plugins, perhaps they support batch processing too
303+
PluginHelper::importPlugin('media-action');
304+
305+
$result = $app->triggerEvent('onContentBeforeSave', ['com_media.file', $object, true, $object]);
306+
307+
if (in_array(false, $result, true))
308+
{
309+
throw new \Exception($object->getError());
310+
}
311+
312+
$object->name = $this->getAdapter($object->adapter)->createFile($object->name, $object->path, $object->data);
313+
314+
$app->triggerEvent('onContentAfterSave', ['com_media.file', $object, true, $object]);
294315

295316
return $object->name;
296317
}
@@ -318,16 +339,29 @@ public function updateFile($adapter, $name, $path, $data)
318339
throw new InvalidPathException;
319340
}
320341

321-
$this->triggerEvent(
322-
$adapter,
323-
$name,
324-
$path,
325-
$data,
326-
function ($object)
327-
{
328-
$this->getAdapter($object->adapter)->updateFile($object->name, $object->path, $object->data);
329-
}
330-
);
342+
$app = Factory::getApplication();
343+
$object = new CMSObject;
344+
$object->adapter = $adapter;
345+
$object->name = $name;
346+
$object->path = $path;
347+
$object->data = $data;
348+
$object->extension = strtolower(File::getExt($name));
349+
350+
PluginHelper::importPlugin('content');
351+
352+
// Also include the filesystem plugins, perhaps they support batch processing too
353+
PluginHelper::importPlugin('media-action');
354+
355+
$result = $app->triggerEvent('onContentBeforeSave', ['com_media.file', $object, false, $object]);
356+
357+
if (in_array(false, $result, true))
358+
{
359+
throw new \Exception($object->getError());
360+
}
361+
362+
$this->getAdapter($object->adapter)->updateFile($object->name, $object->path, $object->data);
363+
364+
$app->triggerEvent('onContentAfterSave', ['com_media.file', $object, false, $object]);
331365
}
332366

333367
/**
@@ -353,7 +387,24 @@ public function delete($adapter, $path)
353387
throw new InvalidPathException;
354388
}
355389

356-
$this->getAdapter($adapter)->delete($path);
390+
$type = $file->type === 'file' ? 'file' : 'folder';
391+
$app = Factory::getApplication();
392+
$object = new CMSObject;
393+
$object->adapter = $adapter;
394+
$object->path = $path;
395+
396+
PluginHelper::importPlugin('content');
397+
398+
$result = $app->triggerEvent('onContentBeforeDelete', ['com_media.' . $type, $object]);
399+
400+
if (in_array(false, $result, true))
401+
{
402+
throw new \Exception($object->getError());
403+
}
404+
405+
$this->getAdapter($object->adapter)->delete($object->path);
406+
407+
$app->triggerEvent('onContentAfterDelete', ['com_media.' . $type, $object]);
357408
}
358409

359410
/**
@@ -494,52 +545,4 @@ private function isMediaFile($path)
494545
// Check if the extension exists in the allowed extensions
495546
return in_array($extension, $this->allowedExtensions);
496547
}
497-
498-
/**
499-
* Triggers the onContentBeforeSave and onContentAfterSave event when calling the
500-
* given callable.
501-
*
502-
* If the onContentBeforeSave contains false, the operation will be aborted and an exception thrown.
503-
*
504-
* The object will be returned which got sent as part of the event.
505-
*
506-
* @param string $adapter The adapter
507-
* @param string $name The name
508-
* @param string $path The path
509-
* @param binary $data The binary data
510-
* @param callable $callback The callback
511-
*
512-
* @return CMSObject
513-
*
514-
* @throws \Exception
515-
* @since 4.0.0
516-
*/
517-
private function triggerEvent(string $adapter, string $name, string $path, $data, callable $callback)
518-
{
519-
$app = Factory::getApplication();
520-
521-
$object = new CMSObject;
522-
$object->adapter = $adapter;
523-
$object->name = $name;
524-
$object->path = $path;
525-
$object->data = $data;
526-
$object->extension = strtolower(File::getExt($name));
527-
$object->type = $object->extension ? 'file' : 'dir';
528-
529-
// Also include the filesystem plugins, perhaps they support batch processing too
530-
PluginHelper::importPlugin('media-action');
531-
532-
$result = $app->triggerEvent('onContentBeforeSave', ['com_media.' . $object->type, $object, true]);
533-
534-
if (in_array(false, $result, true))
535-
{
536-
throw new \Exception($object->getError());
537-
}
538-
539-
$callback($object);
540-
541-
$app->triggerEvent('onContentAfterSave', ['com_media.' . $object->type, $object, true]);
542-
543-
return $object;
544-
}
545548
}

0 commit comments

Comments
 (0)