How to bind uploaded file with ADAM to entity field correctly in 2sxc module? - dotnetnuke

I need to create entity object programmatically and fill it with data. One field needs to be of a file type. So I managed to create entity without file, upload file in ADAM using this sample of code. However it seems that I didn't bind it as it is binded when file is uploaded manually.
When a file is uploaded to entity field manually, you can see content like file:421 .../asdf.docx. However when I repeat code sample from link above, field contains that file available to choose and already uploaded, but field value is null. IFile.Url seems to write correct data via App.Data.Update method, but no id is displayed in admin panel.
Dictionary<string, object> fileDict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase) {
{ "File", file.Url }
}; // file is ToSic.Sxc.Adam.IFile, returned by SaveInAdam
App.Data.Update(entityObj.EntityId, fileDict); // entityObj is ToSic.Eav.Data.IEntity, returned by App.Data.Create
I wonder if that's going to have some bad conscequences if it has no binding like manual upload and how to do that correctly?

To add files, you actually add them to a field (so that the UI can then offer them as if they were added by a normal user). Check out https://docs.2sxc.org/api/dot-net/ToSic.Sxc.Dnn.Web.IDynamicWebApi.html#ToSic_Sxc_Dnn_Web_IDynamicWebApi_SaveInAdam_System_String_System_IO_Stream_System_String_System_String_System_Nullable_System_Guid__System_String_System_String_
BTW: Best check out MobiusForms to see how it's done.
And I forgot to mention: here the docs https://docs.2sxc.org/web-api/custom-web-api-dotnet-saveinadam.html
To further explain:
SaveInAdam will place the file in the ADAM folder of the item. Since it could be that many files are added, it's just assuming that the field will be the library type - which doesn't store anything in the entity, it just automatically finds the folder that belongs to the field.
If you wish to not use the library feature but actually just the single-field with link, then you must also save the term like "file:74" into the value of the field.

Related

How do I access certain entity associated files from another portal in 2sxc module?

I want to access multiple files from single field of certain entity, which is at different portal. The problem is that when I try to use this example or this one, its field of type Hyperlink contains empty string and method AsAdam(entityObj, "FieldWithFiles").Files as IEnumerable<dynamic> returns empty IEnumerable<dynamic>.
So is there a way to get files data from another portal in module 2sxc?
Environment used:
DNN v.9.6.1
2sxc v.11.5.0
This may fix itself in v11.11, but the general method is as follows:
Get the real file id (instead of the url) by accessing the raw APIs first - kind of like this
var entity = AsEntity(Content);
var realLink = entity.GetBestValue<string>("FieldNameWhichHasTheLink");
// now you have something like file:74 in realLink
// now continue with the DNN API to figure out what file 74 is, and if the permissions allow access

Does removing all data from a Sencha store also remove associated data?

I have a store and a model with a hasMany association.
If I call Ext.getStore('SessionStore').removeAll() does it also remove any associated data with the records in the store?
If not, how would I do this?
if you console log a record in SessionStore you should have a property with the name of the association plus the suffix Store
e.g. if the name of the association is personalInformation there should be a new property in the record called personalInformationStore.
So the answer would be yes by removing a record or all from a store the associated data will also be removed as this data is part of the parent object.
Perhaps Ext.StoreManager.getCount() can help validate the existence of the data / objects.
I also recommend this chrome extension: https://chrome.google.com/webstore/detail/app-inspector-for-sencha/pbeapidedgdpniokbedbfbaacglkceae?hl=en

Send file to server through iText PDF?

I'm dynamically creating some PDF files through my application, using iText, and I need to use several components (TextField, CheckBox, RadioButtons etc), and then submit the values to server. But, one of the requirements says that the user needs to be able to select and send files along with the other values. I did not find a specific component to this, and so I'm asking for some help with this situation.
Is there a way to create some kind of Input File, File Chooser, etc, and attach it on the generated PDF file? And then send this selected file to server?
Thanks
This is explained in the official documentation, more specifically in the example FdfServlet of chapter 9 of my book. However, in this example, we add a file selection field to an existing PDF, so I've made you an example that explains how to create such a field when creating a document from scratch: FileSelectionExample
A file selection field is created just like any other text field, except that you have to set a flag using the setOptions() method. If you want a file chooser to appear, you also have to add a JavaScript action:
TextField file = new TextField(writer, new Rectangle(36, 788, 559, 806), "myfile");
file.setOptions(TextField.FILE_SELECTION);
PdfFormField upload = file.getTextField();
upload.setAdditionalActions(PdfName.U, PdfAction.javaScript(
"this.getField('myfile').browseForFileToSubmit();", writer));
writer.addAnnotation(upload);
In the full example, I also added a second field and after selecting the file with the browseForFileToSubmit() method, I set the focus to that other field. I'm doing this because the file name only becomes visible in the file selection field after that field loses focus.

Get View Output In CakePHP 2.x Model

Context: I'm generating a PDF in a model callback (afterSave). I found a library I'm comfortable with called FPDF. I'm adding the FPDF class as a vendor.
So, without going into too much detail, essentially, once all checks have been completed for a particular contract application, the app needs to prepopulate a PDF file and attach it to an email.
I can figure everything out except how to generate the PDF in the model. I want to use a view to pass view vars to so that I can populate the template file and use the FPDF class to save a PDF file.
This file will in turn be attached the automated email and sent to the applicant.
So the flow is:
Once all checks have been complete (via crons and a CakePHP Shell), we trigger a function inside the model afterSave callback.
The function performs some logic and determines whether a declined or approved email should be sent.
If approved, then a PDF is generated using a view file and saved in /Views/pdf/
This file is attached to CakeEmail object and sent.
It's just the view rendering part here that I'm stuck with:
3. If approved, then a PDF is generated using a view file and saved in /Views/pdf/
How can I populate a view file with view variables and return the result into a variable?
For example, think of how the CakeEmail class does it with the CakeEmail->template('example') function......
Any ideas?
The answer is to construct your view class manually:
$view = new View(null, false);
$view->set(compact('variable1', 'variable2'));
$view->viewPath = 'ViewFolder';
$output = $view->render('view_file', 'layout');

Associating an uploaded File (id) with another table

I'm using Filestore to make uploads in a form.
It's a great implementation but I've a doubt about associating that file uploaded with a register in a table.
When I run the filestore.001.sql, it add 4 tables in Database (filestore_image, filestore_file, filestore_volume, filestore_extension).
Then in anoter table I have:
id
field1
idfile
I was thinking in how to do that association when I submit the form.
Maybe a dsql() and then selecting the last inserted file, but I don't know if this is the best option.
Thanks
Alejandro
When you add a image field to a form, it will return you ID (or list of uploaded file id's) on submission. So theoretically user can upload image and never submit form, hence it wouldn't be associated.
You can have the following table:
id
name
picture_id
and define picture as type image, there should be examples on the site - it will automatically receive ID from the filestore_file table.
There is another way you can do and it's exactly how Filestore_Image is being built. You can make your own model, and link it with necessary image and use that for your image field. Since it's your model, you can now control what happens before inserting, you can override beforeInsert and afterInsert events to build the proper associations.
Both methods have their own uses, pick the one which you like.

Resources