nadilhassan / humhub-news-module

This module allows you to publish news stories to any Space - you can upload a photo and select a layout.
15 stars 8 forks source link

File handling #4

Closed buddh4 closed 8 years ago

buddh4 commented 8 years ago

I'would suggest using the humhub file upload for your news images instead of handling it by yourself. Please check the following upload action of the custom pages module:

https://github.com/humhub/humhub-modules-custom-pages/blob/master/modules/template/controllers/UploadController.php#L27

In this example you forward your action to the /file/file/upload action, which will save your file and create an entry in the file table. You'll have to add the returned file guid to your form as hidden field. (Your files have to be set as $_FILES['files']).

When submitting your file you'll have to attach the file, which means it will be attached to your news activerecord and marked as persistent (otherwise it will be removed by a cronjob)

as here: https://github.com/humhub/humhub-modules-custom-pages/blob/master/modules/template/models/forms/TemplateElementForm.php#L114

(the attachPrecreated function expects a comma seperated string list of guids, which is in your case just one guid)

Part of the file activerecord: https://github.com/humhub/humhub/blob/master/protected/humhub/modules/file/models/File.php#L412

And something like this should be added to your news activerecord:

https://github.com/humhub/humhub-modules-custom-pages/blob/master/modules/template/models/TemplateContentActiveRecord.php#L152

buddh4 commented 8 years ago

You don't even have to forward your request, just upload your files to: /file/file/upload and submit the returned guid

nadilhassan commented 8 years ago

Yes I had a reason why I used manual file upload , because I already used the default humhub file upload but when I upload and save or update news module it prints the JSON output in the browser not appending to the stream. I requested helps from everyone but no response so I used this.

buddh4 commented 8 years ago

Ah ok, the file handling looks good anyways, so I think its ok for now.

nadilhassan commented 8 years ago

But If I am allowing multiple images to be added on a news post then I have to use the humhub file upload right? Whats that attaching a file with news activerecord and marked as persistent?

buddh4 commented 8 years ago

Attaching the news record to a file means setting the object_model and object_id of the file database entry, this is needed if you want to upload a file before saving your activerecord. All file entries without a given object_id/object_model are removed by a cronjob.

nadilhassan commented 8 years ago

In my case will that removed by the cronjob?

buddh4 commented 8 years ago

As I checked, there is a file db entry with object_model/object_id created for your uploaded images, but honestly I didn't really figured out where the file entry is created its a bit confusing since there are multiple upload functions.

nadilhassan commented 7 years ago

HI Julian

You advised me to attach the file on the controller where the form is handled.

This is the line where we save the image guid in news table - https://github.com/nadilhassan/newsmodule/blob/master/controllers/NewsController.php#L212

Is it OK if I add \humhub\modules\file\models\File::attachPrecreated($this->content, implode(',', $this->fileList)); this line below above code.

Meanwhile here I am getting only the image guid then how can I parse the parameters for the function attachPrecreated()? Can you please help me to fill these parameters.

buddh4 commented 7 years ago

the second argument of attachPrecreated is a comma seperated list of guids in your case i think you just have to set one guid. in version 1.2 we'll furthermore allow an array value.

You'll receive the guid after the upload of your file to 'file/file/upload'.

nadilhassan commented 7 years ago

Yes I am OK with that GUID of the image. what about the first argument, what is to be in our case? a news model($model) or anything else?

buddh4 commented 7 years ago

Your news active record.

nadilhassan commented 7 years ago

Thanks. I will to this 👍

nadilhassan commented 7 years ago

I have done with this. Thanks for your support!