Skip to content

Commit

Permalink
chore(file): Removes the class FilePluginFile
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
If you use the class FilePluginFile in your plugin, replace this usage
with ElggFile (for construction). Load files objects with get_entity().

Fixes #7763
  • Loading branch information
mrclay committed May 22, 2015
1 parent e986719 commit 6be0f97
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 45 deletions.
5 changes: 5 additions & 0 deletions docs/guides/upgrading.rst
Expand Up @@ -133,6 +133,11 @@ Plugin Messages

Messages will no longer get the metadata 'msg' for newly created messages. This means you can not rely on that metadata to exist.

Removed Classes
---------------

- ``FilePluginFile``: replace with ``ElggFile`` (or load with ``get_entity()``)

Removed Functions
-----------------

Expand Down
5 changes: 3 additions & 2 deletions mod/file/actions/file/delete.php
Expand Up @@ -7,11 +7,12 @@

$guid = (int) get_input('guid');

$file = new FilePluginFile($guid);
if (!$file->guid) {
$file = get_entity($guid);
if (!$file instanceof ElggFile) {
register_error(elgg_echo("file:deletefailed"));
forward('file/all');
}
/* @var ElggFile $file */

if (!$file->canEdit()) {
register_error(elgg_echo("file:deletefailed"));
Expand Down
7 changes: 4 additions & 3 deletions mod/file/actions/file/upload.php
Expand Up @@ -41,7 +41,7 @@
forward(REFERER);
}

$file = new FilePluginFile();
$file = new ElggFile();
$file->subtype = "file";

// if no title on new upload, grab filename
Expand All @@ -51,11 +51,12 @@

} else {
// load original file object
$file = new FilePluginFile($guid);
if (!$file) {
$file = get_entity($guid);
if (!$file instanceof ElggFile) {
register_error(elgg_echo('file:cannotload'));
forward(REFERER);
}
/* @var ElggFile $file */

// user must be able to edit file
if (!$file->canEdit()) {
Expand Down
45 changes: 9 additions & 36 deletions mod/file/classes/FilePluginFile.php
@@ -1,42 +1,15 @@
<?php

/**
* Override the ElggFile
*
* @note When extending an ElggEntity, one should always register a unique type/subtype
* combination. We failed to do so here, so get_entity() and friends will always return
* these objects as ElggFile. We leave it this way just for BC.
* Removed subclass of ElggFile. See https://github.com/Elgg/Elgg/issues/7763
*/
class FilePluginFile extends ElggFile {
protected function initializeAttributes() {
parent::initializeAttributes();

// This should have been a unique subtype (see above)
$this->attributes['subtype'] = "file";
}

public function __construct($guid = null) {
if ($guid && !is_object($guid)) {
// Loading entities via __construct(GUID) is deprecated, so we give it the entity row and the
// attribute loader will finish the job. This is necessary due to not using a custom
// subtype (see above).
$guid = get_entity_as_row($guid);
}
parent::__construct($guid);
}

public function delete() {

$thumbnails = array($this->thumbnail, $this->smallthumb, $this->largethumb);
foreach ($thumbnails as $thumbnail) {
if ($thumbnail) {
$delfile = new ElggFile();
$delfile->owner_guid = $this->owner_guid;
$delfile->setFilename($thumbnail);
$delfile->delete();
}
}

return parent::delete();
class FilePluginFile {
/**
* Constructor
*
* @throws APIException
*/
public function __construct() {
throw new APIException(__CLASS__ . ' is no longer available. Use ElggFile or get_entity()');
}
}
2 changes: 1 addition & 1 deletion mod/file/lib/file.php
Expand Up @@ -8,7 +8,7 @@
/**
* Prepare the upload/edit form variables
*
* @param FilePluginFile $file
* @param ElggFile $file
* @return array
*/
function file_prepare_form_vars($file = null) {
Expand Down
7 changes: 5 additions & 2 deletions mod/file/pages/file/edit.php
Expand Up @@ -10,10 +10,13 @@
elgg_gatekeeper();

$file_guid = (int) get_input('guid');
$file = new FilePluginFile($file_guid);
if (!$file) {

$file = get_entity($file_guid);
if (!$file instanceof ElggFile) {
forward();
}
/* @var ElggFile $file */

if (!$file->canEdit()) {
forward();
}
Expand Down
32 changes: 32 additions & 0 deletions mod/file/start.php
Expand Up @@ -58,6 +58,10 @@ function file_init() {
// temporary - see #2010
elgg_register_action("file/download", "$action_path/download.php");

// cleanup thumbnails on delete. high priority because we want to try to make sure the
// deletion will actually occur before we go through with this.
elgg_register_event_handler('delete', 'object', 'file_handle_object_delete', 999);

// embed support
$item = ElggMenuItem::factory(array(
'name' => 'file',
Expand Down Expand Up @@ -417,3 +421,31 @@ function file_set_icon_url($hook, $type, $url, $params) {
return $url;
}
}

/**
* Handle an object being deleted
*
* @param string $event Event name
* @param string $type Event type
* @param ElggObject $file The object deleted
* @return void
*/
function file_handle_object_delete($event, $type, ElggObject $file) {
if (!$file instanceof ElggFile) {
return;
}
if (!$file->guid) {
// this is an ElggFile used as temporary API
return;
}

$thumbnails = array($file->thumbnail, $file->smallthumb, $file->largethumb);
foreach ($thumbnails as $thumbnail) {
if ($thumbnail) {
$delfile = new ElggFile();
$delfile->owner_guid = $file->owner_guid;
$delfile->setFilename($thumbnail);
$delfile->delete();
}
}
}
2 changes: 1 addition & 1 deletion mod/file/views/default/file/theme_sandbox/icons/files.php
@@ -1,6 +1,6 @@
<?php

$file = new FilePluginFile();
$file = new ElggFile();

$mapping = array(
'general' => 'general',
Expand Down

0 comments on commit 6be0f97

Please sign in to comment.