1084

Picture Uploader

Comments for “Picture Uploader”
 

Posted by DaddyJohn on Thursday 21st April 2022 at 15:09 GMT

Hi, more of a suggestion here.

When we use the single picture uploader, the image does not delete when we delete the post entry.

For example, if I create a blog post with a picture uploader. I am able to delete the Post and the image stays in the directory. Given that I wont need that image anymore, shouldn't the image delete as well.

I can delete the image first and then the Post second and all is good, but would seem to make more sense to delete image if Post is deleted too.

Also, when we delete a Post and Image, the pic directory remains. If you had a blog with many posts that you create and delete, then we would eventually end up with a lot of empty picture directories.

If there is a logic to this I have missed, please let me know.

thanks
Early Adopter

DaddyJohn

User Level: Early Adopter

Date Joined: 23/01/2022

Posted by djnordeen on Thursday 21st April 2022 at 20:37 GMT

Hello,
You would have to have separate functions(methods) to delete the various items.
I think I would have all pictures in an image folder under the main module.
image/
Then have a sub-category for image size
image/sm
image/med
image/lg
This seems more easy to manage.
They are 2 different deletes, that is why you would need a separate delete method
To delete the image then create a function to get the image from the database and delete it.

You call the first method to delete the post and then call the method to delete the image.


Also, when we delete a Post and Image, the pic directory remains. If you had a blog with many posts that you create and delete, then we would eventually end up with a lot of empty picture directories.
For this I would have to see how you are create your image structure.

Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by DaFa on Friday 22nd April 2022 at 00:07 GMT

Hi John,
This was a known issue I raised a while back. And thought DC had addressed it when he added pictures to the same module and choice to have them put in the public folder.

I'll look at the code once more and post a solution when I'm back on front of the computer. From memory it was a simple fix.

Cheers,
Simon
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by DaddyJohn on Friday 22nd April 2022 at 07:47 GMT

Thanks for the comments.

I have now created the code myself so I can have a Large, Medium, Small image which I have added to the upload and ditch methods.

However, I do agree that one directory for all Post images (large, medium, small) within the module would be easier to manage.

Hope you are all well and happy.

Best
Early Adopter

DaddyJohn

User Level: Early Adopter

Date Joined: 23/01/2022

Posted by Davcon on Friday 22nd April 2022 at 22:47 GMT

It's one for the ol' to do list. Hopefully it won't be long.
Founding Member

Davcon

User Level: Founding Member

Date Joined: 3/11/2018

Posted by Davcon on Friday 22nd April 2022 at 23:08 GMT

By the way, having thought about this for a solid half hour I would agree it's fairly urgent. So, I really will do my best to get that nailed as soon as I can.
Founding Member

Davcon

User Level: Founding Member

Date Joined: 3/11/2018

Posted by DaFa on Monday 25th April 2022 at 06:14 GMT

Here is a quick solution to removing the remnant single picture uploader directories. Modify your module to look like this or wait for DC to modify the Desktop app's donor code for submit_delete() and ditch_picture() methods.

Modified submit_delete() method for a module called 'users' which has had a single picture uploader added:
    function submit_delete() {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        $submit = post('submit');
        $params['update_id'] = segment(3);

        if (($submit == 'Yes - Delete Now') && (is_numeric($params['update_id']))) {

            //check if picture directory exists, if so delete it
            $this->ditch_picture($params['update_id'], true);

            //delete all of the comments associated with this record
            $sql = 'delete from trongate_comments where target_table = :module and update_id = :update_id';
            $params['module'] = 'users';
            $this->model->query_bind($sql, $params);

            //delete the record
            $this->model->delete($params['update_id'], 'users');

            //set the flashdata
            $flash_msg = 'The record was successfully deleted';
            set_flashdata($flash_msg);

            //redirect to the manage page
            redirect('users/manage');
        }
    }


Then change ditch_picture() donor code to look like this:
    function ditch_picture($update_id, $del_item = NULL) {

        if (!is_numeric($update_id)) {
            redirect($_SERVER['HTTP_REFERER']);
        }

        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        $result = $this->model->get_where($update_id);

        if ($result == false) {
            redirect($_SERVER['HTTP_REFERER']);
        }

        $picture_settings = $this->_init_picture_settings();
        $target_column_name = $picture_settings['target_column_name'];
        $picture_name = $result->$target_column_name;

        if ($picture_settings['upload_to_module'] == true) {
            $picture_path = APPPATH . 'modules/' . segment(1) . '/assets/' . $picture_settings['destination'] . '/' . $update_id;
        } else {
            $picture_path = APPPATH . 'public/' . $picture_settings['destination'] . '/' . $update_id;
        }

        $picture_path = str_replace('\\', '/', $picture_path);

        if (file_exists($picture_path)) {
            $handle = opendir($picture_path);
            unlink($picture_path . '/' . $picture_name);
            closedir($handle);
            rmdir($picture_path);
        }

        if (isset($picture_settings['thumbnail_dir'])) {
            $ditch = $picture_settings['destination'] . '/' . $update_id;
            $replace = $picture_settings['thumbnail_dir'] . '/' . $update_id;
            $thumbnail_path = str_replace($ditch, $replace, $picture_path);

            if (file_exists($thumbnail_path)) {
                $handle = opendir($thumbnail_path);
                unlink($thumbnail_path . '/' . $picture_name);
                closedir($handle);
                rmdir($thumbnail_path);
            }
        }

        $data[$target_column_name] = '';
        $this->model->update($update_id, $data);

        if ($del_item == true) {
            return true;
        } else {
            $flash_msg = 'The picture was successfully deleted';
            set_flashdata($flash_msg);
            redirect($_SERVER['HTTP_REFERER']);
        }
    }


Hope the above makes sense.

This comment was edited by DaFa on Monday 25th April 2022 at 06:29 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by DaddyJohn on Monday 25th April 2022 at 14:36 GMT

Many thanks,

works perfectly.
Early Adopter

DaddyJohn

User Level: Early Adopter

Date Joined: 23/01/2022

×