This topic is locked
[SOLVED]

Edit file JSON to remove or keep data

4/22/2022 1:13:39 PM
PHPRunner General questions
D
david22585 author

I was wondering if there was a way using the events on the list page (Before or After Record Process) AND view page (Process Record Values) to edit the contents of the JSON array?

Take for example the following JSON string:

[
{"name":"\/home\/site\/site.com\/files\/data\/B22DACA1-A281-4B3A-9B92-DDE9CD39EADE_jz9b1fzj.jpeg","usrName":"B22DACA1-A281-4B3A-9B92-DDE9CD39EADE.jpeg","size":1094035,"type":"image\/jpeg","thumbnail":"\/home\/site\/site.com\/files\/data\/thB22DACA1-A281-4B3A-9B92-DDE9CD39EADE_kiq52jur.jpeg","thumbnail_type":"image\/jpeg","thumbnail_size":16255,"searchStr":"B22DACA1-A281-4B3A-9B92-DDE9CD39EADE.jpeg,!DB703970-F9F7-4173-A26A-DC6024FC59BB.jpeg,!9085A129-A0D6-41F6-90B4-93D54361CA58.jpeg,!CD06BDBB-61F5-4F9F-8E1E-8346296C807D.jpeg,!E2CBACA7-ADFF-4E8A-A2D8-4B94000C4E78.jpeg,!Inv_1015_2940.pdf,!:sStrEnd"},

{"name":"\/home\/site\/site.com\/files\/data\/DB703970-F9F7-4173-A26A-DC6024FC59BB_4d7tqgho.jpeg","usrName":"DB703970-F9F7-4173-A26A-DC6024FC59BB.jpeg","size":2034658,"type":"image\/jpeg","thumbnail":"\/home\/site\/site.com\/files\/data\/thDB703970-F9F7-4173-A26A-DC6024FC59BB_htgmdt4z.jpeg","thumbnail_type":"image\/jpeg","thumbnail_size":19939},

{"name":"\/home\/site\/site.com\/files\/data\/9085A129-A0D6-41F6-90B4-93D54361CA58_mhzqdso6.jpeg","usrName":"9085A129-A0D6-41F6-90B4-93D54361CA58.jpeg","size":2454851,"type":"image\/jpeg","thumbnail":"\/home\/site\/site.com\/files\/data\/th9085A129-A0D6-41F6-90B4-93D54361CA58_zg728q61.jpeg","thumbnail_type":"image\/jpeg","thumbnail_size":9889},

{"name":"\/home\/site\/site.com\/files\/data/Inv_1015_2940_9f70m3j1.pdf","usrName":"Inv_1015_2940.pdf","size":131518,"type":"application\/pdf"}
]

I want to do 2 queries with this, 1 being an alias to replicate this field twice on the page. In the first, I want to remove everything that is not a TYPE image/jpeg, so there only images left. This way I can use that as a lightbox view for images. And then do it a second time on the alias field and remove everything that is TYPE image/jpeg so I'm left with PDF, DOC, or anything that is not an image file so that can be viewed as a file list.

This way all images uploaded are lightbox viewable (IMAGE with View As), while the files are downloadable (FILE as View As). I'm hoping this is possible, or at least a future review of PHPRunner will have a way to do this nativly.

Admin 4/22/2022

Check this article. It explains how to convert JSON to PHP array, work with that array and then convert it back to JSON.

D
david22585 author 5/31/2022

Just providing an update. Got this to work on the view page, but haven't tried the list page yet.

Case: I have a form that submits work orders. Submission has 1 file upload field, and the users can upload pictures, videos, or documents. I wanted the documents/videos to be View As as "File" and the pictures to be View As "Image" with lightbox view.

I created an alias field:

workorders.files,
workorders.files AS images,

On the events, View page, Process Record Value, I have the following:

// Create PHP array
$fileArray = my_json_decode($values["files"]);

// Images only
$search_image = 'image';
$imageArray = array_filter($fileArray, function($el) use ($search_image) {
return ( strpos($el['type'], $search_image) !== false );
});
$imgArray = array_values($imageArray);
$values["images"] = my_json_encode($imgArray);

// Everything but images
$search_document = 'image';
$documentArray = array_filter($fileArray, function($el) use ($search_document) {
return ( strpos($el['type'], $search_document) === false );
});
$docarr = array_values($documentArray);
$values["files"] = my_json_encode($docarr);

Breaking it down is as follows:

// Create PHP array
$fileArray = my_json_decode($values["files"]);

This takes the JSON array and converts it into a PHP array to work with.

// Images only
$search_image = 'image';
$imageArray = array_filter($fileArray, function($el) use ($search_image) {
return ( strpos($el['type'], $search_image) !== false );
});
$imgArray = array_values($imageArray);
$values["images"] = my_json_encode($imgArray);

This will search the array, and extract everything that is an image, removing all the other data that is not images. It then creates a new array, recreates the JSON, and leaves only images to be seen as a lightbox view on the page.

// Everything but images
$search_document = 'image';
$documentArray = array_filter($fileArray, function($el) use ($search_document) {
return ( strpos($el['type'], $search_document) === false );
});
$docarr = array_values($documentArray);
$values["files"] = my_json_encode($docarr);

The same as above, but leaves everything that is not an image, and removes all the images from the array.

D
david22585 author 5/31/2022

This is how it looks on the view page. Also works on list pages as well. I have 1 field that stores all files, and you can see how images are in lightbox view while all other files are viewable as File type.
img alt

D
david22585 author 6/17/2022

Just a quick update. Prior to PHP 8.1, this works on the list page. After going to 8.1,, the code fails on the list page, but works on the view page. It throw the following error on the list page with 8.1:

Fatal error: Uncaught TypeError: array_filter(): Argument #1 ($array) must be of type array, int given in

Not sure why this would work on the view page, and prior to 8.11111 on the list page, but not now. If anyone has any ideas, that would be great.