Skip to Content Skip to Menu

CB Gallery file upload validation

  • activha
  • activha
  • OFFLINE
  • Posts: 2326
  • Thanks: 117
  • Karma: 13
2 months 4 days ago - 2 months 3 days ago #338913 by activha
CB Gallery file upload validation was created by activha
Hi Kyle

I use CB Gallery to allow upload of images and files.

It works fine but I would like to add a validation step for some files llke CSV which are sometimes badly formatted by users. In this case, our AI fails in integrating them in our neuralDB

So I was thinking at a validation step before upload.

Is there a trigger that I could use with CB Gallery for this ?

The kind of code I was thinking about is 
Code:
function validateCSV($filePath, $expectedColumns = 10) { // Ouvrir le fichier en mode lecture if (($handle = fopen($filePath, "r")) !== FALSE) { $lineNumber = 0; while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {$lineNumber++; // Vérifier le nombre de colonnes if (count($data) != $expectedColumns) { fclose($handle); return ['status' => false, 'message' => "Le fichier CSV est mal formaté. Ligne $lineNumber a un nombre incorrect de colonnes." ]; } // Vérifier l'encodage (si nécessaire) foreach ($data as $field) { if (mb_detect_encoding($field, 'UTF-8', true) === false) { fclose($handle); return [ 'status' => false, 'message' => "Le fichier CSV contient des caractères non UTF-8 à la ligne $lineNumber." ]; } } } fclose($handle); return [ 'status' => true, 'message' => "Le fichier CSV est bien formaté." ]; } else {return [ 'status' => false, 'message' => "Impossible d'ouvrir le fichier." ]; } }

I guess that a code autoaction could do it but do not really know how to set it ?


On a second point :
Is there a trigger to change the display of the footer in a CB Gallery thumbnail item display ?
I'd like to make a DB check to see if there exists our AI source_id for the gallery_item_id displayed and display a green or red icon accordingly


Thanks for your help
Jean
Last edit: 2 months 3 days ago by activha.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48419
  • Thanks: 8274
  • Karma: 1443
2 months 3 days ago - 2 months 3 days ago #338919 by krileon
Replied by krileon on topic CB Gallery file upload validation

So I was thinking at a validation step before upload.

Is there a trigger that I could use with CB Gallery for this ?

Custom validation can be done on the gallery_onBeforeCreateGalleryItem trigger. Its variables are as follows.

Code:
$_PLUGINS->trigger( 'gallery_onBeforeCreateGalleryItem', array( $gallery, $source, &$row ) );


var1 being the gallery object of the gallery being uploaded to
var2 being the source object for the gallery (profile object, groupjive group object, etc..)
var3 being the new gallery media object

You'd have to check the files input data for the uploaded file and apply whatever parsing you need from there. The below should work and assumes you're using a Code action with PHP.

Code:
$upload = $variables['var3']->getRaw( '_files' )->subTree( 'upload' );


That should give you an array of the file data. You can then open it from php tmp and do whatever validation you need to do. If you need to throw an error you can do it with the following.

Code:
$variables['var3']->setError( 'ERROR_MESSAGE_HERE' );

Is there a trigger to change the display of the footer in a CB Gallery thumbnail item display ?
I'd like to make a DB check to see if there exists our AI source_id for the gallery_item_id displayed and display a green or red icon accordingly

Yes, there's a trigger for that as well. gallery_onDisplayItem is fired during container displays and any content returned during that trigger is inserted into the footer. Its variables are as follows.

Code:
$_PLUGINS->trigger( 'gallery_onDisplayItem', array( &$row, &$menu, $gallery ) );


var1 being the gallery media object
var2 being an array of drop menu items
var3 being the gallery object for that media


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
Last edit: 2 months 3 days ago by krileon.

Please Log in or Create an account to join the conversation.

  • activha
  • activha
  • OFFLINE
  • Posts: 2326
  • Thanks: 117
  • Karma: 13
2 months 3 days ago #338921 by activha
Replied by activha on topic CB Gallery file upload validation
Thanks a lot that will do :-)

Last question : I use gallery_onAfterCreateItem and gallery_onAfterUpdateItem in a code autoaction with a PHP code sending the item to our AI DB, the code returns a json like :
return $output = json_encode( [
'success' => 'true',
'sourceID' => 'registered',
'error' => 'none',
] );

Is it possible to display something in the ajax window used to save the item ?
I mean can I add something to the validation message when the user saves the item.

As it's on ItemTable I cannot find the idea

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48419
  • Thanks: 8274
  • Karma: 1443
2 months 3 days ago #338922 by krileon
Replied by krileon on topic CB Gallery file upload validation
Only way to do that would be to act on gallery_onBeforeItemEditMini and return whatever message you're wanting. That trigger is used for the mini edit display during the upload process that's shown after an upload completes (where you can change folder, title, description, etc..). Its variables are as follows.
Code:
$_PLUGINS->trigger( 'gallery_onBeforeItemEditMini', array( &$row, &$input, $viewer, $gallery ) );

var1 being the gallery media object
var2 being the http request input object
var3 being the viewing user object
var4 being the gallery object for that media

Anything returned during that trigger will show at the top while gallery_onAfterItemEditMini (same variables as above) would show at the bottom.

I assume that's where you mean to add this? Otherwise you'll need to be more specific as to what view you want this displayed on.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • activha
  • activha
  • OFFLINE
  • Posts: 2326
  • Thanks: 117
  • Karma: 13
2 months 3 days ago #338923 by activha
Replied by activha on topic CB Gallery file upload validation
Yes I think that gallery_onAfterItemEditMini should be the one to use as it's the area where the save message is displayed.

However I cannot see how to link my two autoactions.

The first one uses gallery_onAfterCreateItem and gallery_onAfterUpdateItem to trigger the sending of the file to the AI DB. This one works fine and the PHP code returns the json stated above.

But how can I use this return (or any other else) to change the display of the save message with gallery_onAfterItemEditMini ?

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48419
  • Thanks: 8274
  • Karma: 1443
2 months 3 days ago #338924 by krileon
Replied by krileon on topic CB Gallery file upload validation
The media object is passed by reference. So you can add new properties to it using property getter/setter functions. Example as follows.

Set
Code:
$variables['var1']->set( '_myMessage', 'Hello!' );
Get
Code:
$customMessage = $variables['var1']->get( '_myMessage' );

The output of the mini edit is not a redirect so you can pass data through to it using this method and then use that data in your display.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: activha

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum