upload issues

edited August 2014 in Modules
I want to have a multiple upload for a page called "product"

so i modify the product model with

function form_fields($values = array(), $related = array())
{
$fields = parent::form_fields($values, $related);

$fields['image']=array('type'=>'asset', 'class'=>'multifile'); // i also tried to add multiple=>true
$fields['image']['folder']='images/products';

return $fields;

}

The image field is parsed to show "upload asset" and "select image" buttons.

1. clicking the "upload asset" button, i uploaded a file, then the "image" field is populated with the uploaded file name, but when i upload another, the "image" field is overwritten with the new one, i dont think this is the right way of multiple upload, at least both uploaded file name must be present. i cant find ways for me to do multiple upload

i used on_after_post to check the values, no values is detected..

function on_after_post($values){

$CI =& get_instance();

$data = $this->fuel->assets->uploaded_data(); // also tried $data=$CI->upload->data();

var_dump($data);
exit;
}

2. i tried quite a while and managed to upload few images, as i can see by clicking the "select images" button, newly uploaded images are shown.
with $fields['image']=array('type'=>'asset', 'class'=>'multifile', 'multiple'=>true);
by select images from drop down one by one, i can see the "image" field now is populated by selected image name separated with comma. but after i save, using on_after_post and vardump the $values['image'], i got a strange value of image values such as string(43) "["ayase-haruka_1.jpg","ayase-haruka_5.jpg"]" . This is not an array, i cant loop through it.

Question 1, how can i do multiple upload correctly? i searched, it says use "multifile" but apparaently it is not working at the moment
Question 2, anyway to multiple select images from the pop up modal of "select image" maybe by checkbox instead of using dropdown select which is quite tedious.
Question 3, when i am selecting using "select images", how can i get the array of images so i can loop.


Thanks

Comments

  • edited 9:33PM
    The multiple image upload for the "asset" field type not adding a comma is a bug that I just pushed a fix for in the develop branch on FUEL:
    https://github.com/daylightstudio/FUEL-CMS/commit/6d3fed52fed35902fc41a13c61c9f8529a9af52d
    When that field type is set to "multiple" it json_encodes the data as an array (which is what you were seeing) so you should set the field you are saving it in as a serialized field for your model so that it will properly decode the data upon retrieval:
    public $serialized_fields = array('image');
    You can also use the field type "file" and set the parameter "multiple" => TRUE. Then in your model, you can use on_after_post hooks to do any further processing of the images. The uploaded image data is saved under the $this->fuel->assets->uploaded_data().
  • edited 9:33PM
    Hi so in order to retrieve the image, i just need to do json_decode($this->fuel->assets->uploaded_data())?
  • edited 9:33PM
    It depends on where you are wanting to retrieve the image information. If you are saving it to your model, the value will get saved as a JSON value to the field name "image" and can be accessed on the record as such:
    $my_record->image; // will return an array of image names if serialized_fields is set for "image" field
    If you use a "file" field type, you can access the uploaded image information in your model to do further processing on save (e.g. change image dimensions, concatenate information and save into a different field, etc), you can grab all the uploaded image info from the $this->fuel->assets->uploaded_data(). To see it in action, add a "file" field type in your model's form_fields method. Then create an on_after_post method on your model:
    function on_after_post($values) { print_r($this->fuel->assets->uploaded_data()); exit(); }
  • edited August 2014
    but when i am selecting image, i dont think i can access $this->fuel->assets->uploaded_data() to get the data? i checked, seem it is not working. Also if i am saving it in another table using on_after_post, when i edit the multip-upload page, which function i can use to populate the image field with all the name saved in image table
  • edited 9:33PM
    The uploaded_data is only on save and is not available on select. I guess I'm not clear as to what you are trying to do, in particular, Question 3, "when i am selecting using "select images", how can i get the array of images so i can loop."
  • edited 9:33PM
    if i set the field type to "file" there are two buttons appear beside the field when viewing the form. One is upload the asset and another is select image.

    Because this product form has other fields ( not only to do upload), so if i am editing the products, i am able to select the images for this product if the images already uploaded using "Select Images" field without the need to upload again. So when i use this button, i am able to select a list of images separated by comma in the image field, i am not able to use $this->fuel->assets->uploaded_data() because there will not be any upload, so how can i do in this case.
  • edited 9:33PM
    Because the field name is "image" it will automatically assume the "asset" field type unless you set the "ignore_representative" => TRUE parameter on the field. Add that and you should see the field change to file upload fields.
  • edited 9:33PM
    yes, but i want it to have select image abilities, otherwise users are forced to upload the product images everytime even the images already exist in the assets, how can i let the image field to handle both upload and select image in this case?
  • edited 9:33PM
    Then in your case you want the "asset" field type since the file field type does not give you that ability to select an image. Using the "asset" field type doesn't give you the ability to do any further manipulation of images upon upload with model hooks since it happens in a modal iframe window outside of the context of the module. So to be clear, in your case, are just wanting the same comma separated list for uploaded images the same as for selected images?
  • edited August 2014
    yes, basically want the asset field type to handle multiple images upload too, and i need to do some parsing on this field, as i need to store the images selected in a seperate table
  • edited 9:33PM
    So the multiple image upload issue is a bug as mentioned above and I posted a fix to it in the develop branch. If you need to store the images into separate tables as well, then you will have access to the files names field value in a model hook such as on_before_save, but it will be in a JSON encoded format so you will need to use json_decode on the value.
  • edited 9:33PM
    Thanks, a side topic, i just realized that while i am on this forum page, when u reply my post, there is a pop up saying " you have just made a comment", is this forum using node.js or other technologies to do this thing?
  • edited 9:33PM
    there is another problem:

    The multiple image upload for the "asset" field type not adding a comma is a bug that I just pushed a fix for in the develop branch on FUEL:
    https://github.com/daylightstudio/FUEL-CMS/commit/6d3fed52fed35902fc41a13c61c9f8529a9af52d


    when uploading, there still some problems:

    1. when the field is empty and do a first time upload of ONE image, there is a comma in front of the image such as ",abc.jpg"

    2. On the pop up window to do upload, if we select multiple images, then click upload, in the end, only first image is append to the current field values

    e.g. image field already got value of "abc.jpg"
    then click upload assets, in the pop up window select "abc2.jpg" , "abc3.jpg", click upload

    the field only displays "abc.jpg,abc2.jpg"

    i checked js, seem no problem, but no sure why the problem happens.
  • edited 9:33PM
    Thanks for the report. I've pushed a couple more fixes to help with that issue into the develop branch.
  • edited 9:33PM
    sorry, may i know where can i pull these changes
  • edited 9:33PM
    Here it is:
    https://github.com/daylightstudio/FUEL-CMS/tree/develop

    The easiest way to make updates is to use Git and add the following as a remote repository to pull from:
    https://github.com/daylightstudio/FUEL-CMS.git

    Then you can pull from the develop branch.
  • edited August 2014
    working thanks
Sign In or Register to comment.