Search code examples
phpcodeignitergrocery-crud

GroceryCRUD - how to set variables in a callback? (I upload a file, want to resize it 3 times and put all 3 in the db - how do i save that data)?


I am using codeigniter + grocerycrud. I have a callback for image uploads.

I upload one image in the form, but want to do this:

copy it 3 times

resize each one a certain size

then save each one in the database.

any ideas how to do this? The callback only seems to let me edit the image (and do things like that) - not change the varaiables of the submitted data so we can save the manipulated data.

looking at their callback tutorial it looks like most call backs do something like this:

function callback_for_this_field($posted_data) {

$posted_data = $posted_data .= "append me";

return $posted_data;
}

(ie returning the modified data)

but the upload callback just returns true


Solution

  • You can simply do it with a callback_before_insert and callback_before_update. When a file is uploaded in grocery CRUD then a hidden field with value as the name of the file is inserted. So for example let's say you have:

    $crud->set_field_upload('image_url','assets/uploads/images');
    

    You can simply do something like this:

    $crud->callback_before_insert(array($this,'_append_uploaded_file'));
    $crud->callback_before_update(array($this,'_append_uploaded_file'));
    

    and in your Controller add something like:

    public function _append_uploaded_file($post_array) 
    {
        if (!empty($post_array['image_url'])) {
            $post_array['image_url'] =  "append-me-".$post_array['image_url'];
        }
    
        //You can add or insert to other tables too
    
        return $post_array;
    }
    

    Of course you can do the same thing with callback_after_insert and callback_after_update without any problem.