Duplicate File plugin

Hello everyone,sorry for my bad english. right now, i'm duplicating File plugin for some reason. I named it Mediafile plugin . What I expect are:

go to File plugin:

  • file
  • access id

((Succesful-> file was uploaded with the access))

 

go to Mediafile plugin

  • title
  • description
  • tag
  • accessid

((Failed-> there is no new item appear in Mediafile plugin page))

Can you help me ?

this is my "upload.php"

<?php
/**
 * Elgg file uploader/edit action
 *
 * @package ElggFile
 */

// Get variables
$title = htmlspecialchars(get_input('title', '', false), ENT_QUOTES,

'UTF-8');
$desc = get_input("description");
$access_id = (int) get_input("access_id");
$container_guid = (int) get_input('container_guid', 0);
$guid = (int) get_input('file_guid');
$tags = get_input("tags");

if ($container_guid == 0) {
    $container_guid = elgg_get_logged_in_user_guid();
}

elgg_make_sticky_form('file');

// check if upload attempted and failed
if (!empty($_FILES['upload']['name']) && $_FILES['upload']['error']

!= 0) {
    $error = elgg_get_friendly_upload_error($_FILES['upload']

['error']);

    register_error($error);
    forward(REFERER);
}

// check whether this is a new file or an edit
$new_file = true;
if ($guid > 0) {
    $new_file = false;
}

if ($new_file) {
    // must have a file if a new file upload
    if (empty($_FILES['upload']['name'])) {
        $error = elgg_echo('file:nofile');
        register_error($error);
        forward(REFERER);
    }

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

    // if no title on new upload, grab filename
    if (empty($title)) {
        $title = htmlspecialchars($_FILES['upload']['name'],

ENT_QUOTES, 'UTF-8');
    }

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

    // user must be able to edit file
    if (!$file->canEdit()) {
        register_error(elgg_echo('file:noaccess'));
        forward(REFERER);
    }

    if (!$title) {
        // user blanked title, but we need one
        $title = $file->title;
    }
}


$mediafile->title = $title;
$mediafile->description = $desc;
$mediafile->access_id = $access_id;
$mediafile->container_guid = $container_guid;
$mediafile->tags = string_to_tag_array($tags);
$title = htmlspecialchars($_FILES['upload']['name'], ENT_QUOTES,

'UTF-8');
$file->title = $title;
$file->access_id = 2;
// we have a file upload, so process it
if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']

['name'])) {

    $prefix = "file/";

    // if previous file, delete it
    if ($new_file == false) {
        $filename = $file->getFilenameOnFilestore();
        if (file_exists($filename)) {
            unlink($filename);
        }

        // use same filename on the disk - ensures

thumbnails are overwritten
        $filestorename = $file->getFilename();
        $filestorename = elgg_substr($filestorename,

elgg_strlen($prefix));
    } else {
        $filestorename = elgg_strtolower(time().$_FILES

['upload']['name']);
    }

    $file->setFilename($prefix . $filestorename);
    $file->originalfilename = $_FILES['upload']['name'];
    $mime_type = $file->detectMimeType($_FILES['upload']

['tmp_name'], $_FILES['upload']['type']);

    $file->setMimeType($mime_type);
    $file->simpletype = elgg_get_file_simple_type($mime_type);

    // Open the file to guarantee the directory exists
    $file->open("write");
    $file->close();
    move_uploaded_file($_FILES['upload']['tmp_name'], $file-

>getFilenameOnFilestore());

    $guid = $file->save();


    // if image, we need to create thumbnails (this should be

moved into a function)
    if ($guid && $file->simpletype == "image") {
        $file->icontime = time();
        
        $thumbnail = get_resized_image_from_existing_file

($file->getFilenameOnFilestore(), 60, 60, true);
        if ($thumbnail) {
            $thumb = new ElggFile();
            $thumb->setMimeType($_FILES['upload']

['type']);

            $thumb->setFilename($prefix."thumb".

$filestorename);
            $thumb->open("write");
            $thumb->write($thumbnail);
            $thumb->close();

            $file->thumbnail = $prefix."thumb".

$filestorename;
            unset($thumbnail);
        }

        $thumbsmall = get_resized_image_from_existing_file

($file->getFilenameOnFilestore(), 153, 153, true);
        if ($thumbsmall) {
            $thumb->setFilename($prefix."smallthumb".

$filestorename);
            $thumb->open("write");
            $thumb->write($thumbsmall);
            $thumb->close();
            $file->smallthumb = $prefix."smallthumb".

$filestorename;
            unset($thumbsmall);
        }

        $thumblarge = get_resized_image_from_existing_file

($file->getFilenameOnFilestore(), 600, 600, false);
        if ($thumblarge) {
            $thumb->setFilename($prefix."largethumb".

$filestorename);
            $thumb->open("write");
            $thumb->write($thumblarge);
            $thumb->close();
            $file->largethumb = $prefix."largethumb".

$filestorename;
            unset($thumblarge);
        }
    } elseif ($file->icontime) {
        // if it is not an image, we do not need thumbnails
        unset($file->icontime);
        
        $thumb = new ElggFile();
        
        $thumb->setFilename($prefix . "thumb" .

$filestorename);
        $thumb->delete();
        unset($file->thumbnail);
        
        $thumb->setFilename($prefix . "smallthumb" .

$filestorename);
        $thumb->delete();
        unset($file->smallthumb);
        
        $thumb->setFilename($prefix . "largethumb" .

$filestorename);
        $thumb->delete();
        unset($file->largethumb);
    }
} else {
    // not saving a file but still need to save the entity to

push attributes to database
    $file->save();
    $mediafile->save();
    
}

// file saved so clear sticky form
elgg_clear_sticky_form('file');


// handle results differently for new files and file updates
if ($new_file) {
    if ($guid) {
        $message = elgg_echo("file:saved");
        system_message($message);
        elgg_create_river_item(array(
            'view' => 'river/object/file/create',
            'action_type' => 'create',
            'subject_guid' =>

elgg_get_logged_in_user_guid(),
            'subject_guid' =>

elgg_get_logged_in_user_guid(),
            'object_guid' => $file->guid,
        ));
    } else {
        // failed to save file object - nothing we can do

about this
        $error = elgg_echo("file:uploadfailed");
        register_error($error);
    }

    $container = get_entity($container_guid);
    if (elgg_instanceof($container, 'group')) {
        forward("mediafile/group/$container->guid/all");
    } else {
        forward("mediafile/owner/$container->username");
    }

} else {
    if ($guid) {
        system_message(elgg_echo("file:saved"));
    } else {
        register_error(elgg_echo("file:uploadfailed"));
    }

    forward($file->getURL());
}

 

 

  • Do you want to install File and your Mediafile both on site?

    Do you plan to save 'file' suntype, duplicate it only (create a new 'mediafile' subtype?

    Finally, maybe you want to rename File, pagehandler 'file', 'file/all' etc with Mediafile, 'mediafile', 'mediafile/all' etc.. only ?

     

    About your code:

     $file = new FilePluginFile($guid);
        if (!$file) {
            register_error(elgg_echo('file:cannotload'));
            forward(REFERER);
        }
    
        // user must be able to edit file
        if (!$file->canEdit()) {
            register_error(elgg_echo('file:noaccess'));
            forward(REFERER);
        }
    
        if (!$title) {
            // user blanked title, but we need one
            $title = $file->title;
        }
    }
    
    $mediafile->title = $title;
    $mediafile->description = $desc;
    $mediafile->access_id = $access_id;
    $mediafile->container_guid = $container_guid;
    $mediafile->tags = string_to_tag_array($tags);
    $title = htmlspecialchars($_FILES['upload']['name'], ENT_QUOTES,
    
    'UTF-8');
    $file->title = $title;
    $file->access_id = 2;
    // we have a file upload, so process it
    if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']

    See at the bolded strings ($file $mediafile): you must to replace (edit) it everywhere....

  • That's my upload.php in Mediafiles plugin.This is my plan:

    User fill Mediafiles form (title,browse file, description, etc).
    The file will be uploaded in Files plugin
    The system will make new Mediafiles and Files item.

    Conclusion: one program, two action

  • I don't understand your plan...

    Which your goal? 1 type with 2 separate subtypes? 1 file with 2 diferrent 'names'??

    Did you tried File Tools and/or similar Elgg plugins?

    Perhaps, better is make a custom form/action for your 'mediafile' then duplicate the bundled File plugin?

    Also, think about embedding and customization your newest 'mediafiles' in future on your Elgg website.

  • okay , i make the goal easier than before , i just want to rename "File" plugin to "Mediafile" plugin. I'm successful to rename it . The form , the link , etc they are normal.


    but the problem is in action of uploading . if I replace all text "file" to "mediafile" , white screen appear.when i click "save" for uploading a file

    Sorry for my bad english ,

  • okay , i make the goal easier than before , i just want to rename "File" plugin to "Mediafile" plugin. I'm successful to rename it . The form , the link , etc they are normal.


    but the problem is in action of uploading . if I replace all text "file" to "mediafile" , white screen appear.when i click "save" for uploading a file.

    This is the code ,  i have replaced all "file" to "mediafile" , i don't know what's the problem.

    Please correct my code

    Sorry for my bad english ,

    <?php
    /**
     * Elgg mediafile uploader/edit action
     *
     * @package ElggMediafile
     */

    // Get variables
    $title = htmlspecialchars(get_input('title', '', false), ENT_QUOTES, 'UTF-8');
    $desc = get_input("description");
    $access_id = (int) get_input("access_id");
    $container_guid = (int) get_input('container_guid', 0);
    $guid = (int) get_input('mediafile_guid');
    $tags = get_input("tags");

    if ($container_guid == 0) {
        $container_guid = elgg_get_logged_in_user_guid();
    }

    elgg_make_sticky_form('mediafile');

    // check if upload attempted and failed
    if (!empty($_FILES['upload']['name']) && $_FILES['upload']['error'] != 0) {
        $error = elgg_get_friendly_upload_error($_FILES['upload']['error']);

        register_error($error);
        forward(REFERER);
    }

    // check whether this is a new mediafile or an edit
    $new_mediafile = true;
    if ($guid > 0) {
        $new_mediafile = false;
    }

    if ($new_mediafile) {
        // must have a mediafile if a new mediafile upload
        if (empty($_FILES['upload']['name'])) {
            $error = elgg_echo('mediafile:nomediafile');
            register_error($error);
            forward(REFERER);
        }

        $mediafile = new MediafilePluginMediafile();
        $mediafile->subtype = "mediafile";

        // if no title on new upload, grab mediafilename
        if (empty($title)) {
            $title = htmlspecialchars($_FILES['upload']['name'], ENT_QUOTES, 'UTF-8');
        }

    } else {
        // load original mediafile object
        $mediafile = new MediafilePluginMediafile($guid);
        if (!$mediafile) {
            register_error(elgg_echo('mediafile:cannotload'));
            forward(REFERER);
        }

        // user must be able to edit mediafile
        if (!$mediafile->canEdit()) {
            register_error(elgg_echo('mediafile:noaccess'));
            forward(REFERER);
        }

        if (!$title) {
            // user blanked title, but we need one
            $title = $mediafile->title;
        }
    }

    $mediafile->title = $title;
    $mediafile->description = $desc;
    $mediafile->access_id = $access_id;
    $mediafile->container_guid = $container_guid;
    $mediafile->tags = string_to_tag_array($tags);

    // we have a mediafile upload, so process it
    if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) {

        $prefix = "mediafile/";

        // if previous mediafile, delete it
        if ($new_mediafile == false) {
            $mediafilename = $mediafile->getMediafilenameOnMediafilestore();
            if (mediafile_exists($mediafilename)) {
                unlink($mediafilename);
            }

            // use same mediafilename on the disk - ensures thumbnails are overwritten
            $mediafilestorename = $mediafile->getMediafilename();
            $mediafilestorename = elgg_substr($mediafilestorename, elgg_strlen($prefix));
        } else {
            $mediafilestorename = elgg_strtolower(time().$_FILES['upload']['name']);
        }

        $mediafile->setMediafilename($prefix . $mediafilestorename);
        $mediafile->originalmediafilename = $_FILES['upload']['name'];
        $mime_type = $mediafile->detectMimeType($_FILES['upload']['tmp_name'], $_FILES['upload']['type']);

        $mediafile->setMimeType($mime_type);
        $mediafile->simpletype = elgg_get_mediafile_simple_type($mime_type);

        // Open the mediafile to guarantee the directory exists
        $mediafile->open("write");
        $mediafile->close();
        move_uploaded_mediafile($_FILES['upload']['tmp_name'], $mediafile->getMediafilenameOnMediafilestore());

        $guid = $mediafile->save();

        // if image, we need to create thumbnails (this should be moved into a function)
        if ($guid && $mediafile->simpletype == "image") {
            $mediafile->icontime = time();
            
            $thumbnail = get_resized_image_from_existing_mediafile($mediafile->getMediafilenameOnMediafilestore(), 60, 60, true);
            if ($thumbnail) {
                $thumb = new ElggMediafile();
                $thumb->setMimeType($_FILES['upload']['type']);

                $thumb->setMediafilename($prefix."thumb".$mediafilestorename);
                $thumb->open("write");
                $thumb->write($thumbnail);
                $thumb->close();

                $mediafile->thumbnail = $prefix."thumb".$mediafilestorename;
                unset($thumbnail);
            }

            $thumbsmall = get_resized_image_from_existing_mediafile($mediafile->getMediafilenameOnMediafilestore(), 153, 153, true);
            if ($thumbsmall) {
                $thumb->setMediafilename($prefix."smallthumb".$mediafilestorename);
                $thumb->open("write");
                $thumb->write($thumbsmall);
                $thumb->close();
                $mediafile->smallthumb = $prefix."smallthumb".$mediafilestorename;
                unset($thumbsmall);
            }

            $thumblarge = get_resized_image_from_existing_mediafile($mediafile->getMediafilenameOnMediafilestore(), 600, 600, false);
            if ($thumblarge) {
                $thumb->setMediafilename($prefix."largethumb".$mediafilestorename);
                $thumb->open("write");
                $thumb->write($thumblarge);
                $thumb->close();
                $mediafile->largethumb = $prefix."largethumb".$mediafilestorename;
                unset($thumblarge);
            }
        } elseif ($mediafile->icontime) {
            // if it is not an image, we do not need thumbnails
            unset($mediafile->icontime);
            
            $thumb = new ElggMediafile();
            
            $thumb->setMediafilename($prefix . "thumb" . $mediafilestorename);
            $thumb->delete();
            unset($mediafile->thumbnail);
            
            $thumb->setMediafilename($prefix . "smallthumb" . $mediafilestorename);
            $thumb->delete();
            unset($mediafile->smallthumb);
            
            $thumb->setMediafilename($prefix . "largethumb" . $mediafilestorename);
            $thumb->delete();
            unset($mediafile->largethumb);
        }
    } else {
        // not saving a mediafile but still need to save the entity to push attributes to database
        $mediafile->save();
    }

    // mediafile saved so clear sticky form
    elgg_clear_sticky_form('mediafile');


    // handle results differently for new mediafiles and mediafile updates
    if ($new_mediafile) {
        if ($guid) {
            $message = elgg_echo("mediafile:saved");
            system_message($message);
            elgg_create_river_item(array(
                'view' => 'river/object/mediafile/create',
                'action_type' => 'create',
                'subject_guid' => elgg_get_logged_in_user_guid(),
                'object_guid' => $mediafile->guid,
            ));
        } else {
            // failed to save mediafile object - nothing we can do about this
            $error = elgg_echo("mediafile:uploadfailed");
            register_error($error);
        }

        $container = get_entity($container_guid);
        if (elgg_instanceof($container, 'group')) {
            forward("mediafile/group/$container->guid/all");
        } else {
            forward("mediafile/owner/$container->username");
        }

    } else {
        if ($guid) {
            system_message(elgg_echo("mediafile:saved"));
        } else {
            register_error(elgg_echo("mediafile:uploadfailed"));
        }

        forward($mediafile->getURL());
    }

     

  • Please, don't try to write the code if you don't know the basics.

    Read Elgg Docs