Page 1 of 2
Pass param to Custom Field by udt in LISE
Posted: Mon Sep 13, 2021 2:28 pm
by pierrepercee
Hello,
I use Imagik in a udt to generate a jpg based on the first page from the pdf file i upload.
Code: Select all
$im = new Imagick();
$im->setResolution(72,72);
$im->setBackgroundColor('white');
$im->readImage($params['name']);
I must pass path from the generate pdf to my UDT.
In summary template i can for example use :
Code: Select all
{assign var=cheminpdf value=$fielddef->GetImagePath(true)|cat:"/":$fielddef.value scope='global'}
Please, can you telle me how can i pass this param to a Custom Field by udt ?
maybe i'm wrong...
Thanks !
Re: Pass param to Custom Field by udt in LISE
Posted: Mon Sep 13, 2021 3:37 pm
by velden
Not sure I understand what you mean but passing a parameter to a UDT is quite simple:
{myudt myparam='myvalue'}
Inside the UDT:
That said, as you are generating a preview which probably should be a one-time event per item, you might consider using the built-in CMSMS Event Manager (under Extensions). It allows you to connect a specific event (in this case the PostItemSave event of your LISE instance) to a UDT.
If you're interested me or perhaps another user can post some example code using it. But I'm not sure I have something laying around using the imagepath.
Re: Pass param to Custom Field by udt in LISE
Posted: Mon Sep 13, 2021 4:11 pm
by pierrepercee
Many thanks Velden,
I have tested with Event Manager. It works but I don't know how to get smarty value in my UDT.
Code: Select all
$im = new Imagick();
$im->setResolution(72,72);
$im->setBackgroundColor('white');
$im->readImage(xxxx);
For readImage (last line) I must get the complet path for the pdf file I have posted.
In Lise summary template, it's easy :
Code: Select all
{$fielddefs.fichierpub->GetImagePath(true)/$fielddefs.fichierpub.value}
but I do not know how to get this value from my UDT.
Re: Pass param to Custom Field by udt in LISE
Posted: Mon Sep 13, 2021 6:18 pm
by velden
Had to test but this seems to work:
Code: Select all
$obj = $params['item_object'];
$fd = $obj->fielddefs['fileupload'];
echo 'path: ' . $fd->GetImagePath(false) . '/' . $fd->value;
die();
Would be nice if you could share your final code when it works.
Re: Pass param to Custom Field by udt in LISE
Posted: Tue Sep 14, 2021 6:59 am
by pierrepercee
Velden,
It works like a charm...
There is unexpected behavior. If I use event manager ->PostItemSave then the pdf file (my custom field (send file)in Lise) is not uploaded.
My UDT:
Code: Select all
$im = new Imagick();
$obj = $params['item_object'];
$fd = $obj->fielddefs['sendfile'];
$ft = 'uploads/batimac/'.$fd->value;
$im->readImage($ft);
$im->setResolution(72,72);
$im->setBackgroundColor('white');
$compression_type = Imagick::COMPRESSION_JPEG;
$im->setImageCompression($compression_type);
$im->setImageCompressionQuality(19);
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$im->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);
$im->setIteratorIndex(0);
$debtamps = bin2hex(random_bytes(5));
$afinalname = "slide-".$debtamps;
$finalname=uniqid($afinalname).".jpg";
$im->setGravity(\Imagick::GRAVITY_CENTER);
$geo=$im->getImageGeometry();
$sizex=$geo['width'];
$sizey=$geo['height'];
$ratio=$sizex/$sizey;
if ($sizex>600){
if ($ratio>1) {$im->scaleImage(480,560,true);$im->cropThumbnailImage(240,310);}
else {$im->scaleImage(240,320,true);}}
else {echo "<span class='careful'> to small pdf</span>";}
Then I get an error "
"Fatal error: Uncaught ImagickException: unable to open image 'uploads/batimac/test5.pdf'".
I dont understand. It seems that the user tag is executed before the saving of the item and that it prevents the sending of the file.
The line
seems to generate the error.
If i use my udt in Lise summary template
and I use
, it works...
That's not all, if I succeed in making this work, I will have to manage to retrieve the url of the generated image and make sure that a custom field of type inputtext in Lise takes this value. .
Suffice to say that we will surely have to play with the database ... The 12 labors of Hercule, for me ..
Re: Pass param to Custom Field by udt in LISE
Posted: Tue Sep 14, 2021 7:16 am
by velden
I note you're not using the GetImagePath(false) method in your code. That may be the reason it isn't working for you.
Writing a value to a custom field in LISE is possible too. It may even be easier to do that in the PREItemSave event though I'm not sure if the pdf file will then already be in the right place. You could start testing after you get this path-issue solved.
Re: Pass param to Custom Field by udt in LISE
Posted: Tue Sep 14, 2021 7:59 am
by pierrepercee
Velden,
Same thing... The file is not uploaded. I get the correct path (verified using echo in udt).
If I try with a minimalist udt
Then I can use Event Manager.There's no problem, the file is well uploaded.
I'm lost....

Re: Pass param to Custom Field by udt in LISE
Posted: Tue Sep 14, 2021 8:17 am
by velden
I would expect the file to be there once the PostItemSave event is called. But to be sure you could use some basic PHP test the check if that's true.
Another approach can be to check your custom value for the generated image, and if empty call the UDT (from the summary/detail template).
That would generate the image on the first front end request.
I'd then recommend to use the PREItemSave event to clear the value of the custom value. In case an editor uploads a new PDF file. You then want to generate a new image too.
You may end up with many ghosted image files then btw. Something to think about.
Re: Pass param to Custom Field by udt in LISE
Posted: Tue Sep 14, 2021 8:58 am
by pierrepercee
Velden,
I'm sure in my udt I test
Code: Select all
$filename = $ft;
if (file_exists($filename))
{$im->readImage($ft);}
else {echo "No uploads";}
It returns "No uploads"....
This is not the expected behavior.
Record the item then call the udt.
Obviously there, the correct recording of the item is disturbed by the call of the udt.
It looks like a bug ...
Re: Pass param to Custom Field by udt in LISE
Posted: Tue Sep 14, 2021 10:14 am
by velden
I think you may be right there and the file is moved after calling this event.
You can file a bug report of course and in the meanwhile choose another approach (e.g. what I suggested above).
Re: Pass param to Custom Field by udt in LISE
Posted: Tue Sep 14, 2021 2:09 pm
by pierrepercee
Velden,
don't think it's a good idea to generate the thumbnails with loading front-end page for many reasons. If there's 200 items (pdf>8mo) and somebody forget to load front page anytime he creates an item...
I think it's more efficient to create a plugin dealing with the database :
-Add an inputtext field item to Lise.
- plugin tests if this field is empty
- If it is the case generate a thumbs from the path of the pdf file
- Write the path of this thumbs as the value of this field
Where to call this plugin, don't know...
Re: Pass param to Custom Field by udt in LISE
Posted: Wed Sep 15, 2021 8:55 am
by pierrepercee
Velden,
Really many thanks....
I'm almost done...
Last little difficulty I try to get 'item_id' in my UDT. I try hundred solution, nothing seems to work....
In summary template it's trivial :
but in UDT...
Code: Select all
$obj = $params['item_object'];
$fd = $obj->fielddefs['sendpdf'];
$fd = $fd->GetImagePath(false) . '/' . $fd->value;
In my UDT I update value of an input field with the path generated. It works very well, and relation between item and path of the thumbnails generated is 'solid'.

Re: Pass param to Custom Field by udt in LISE
Posted: Wed Sep 15, 2021 10:48 am
by pierrepercee
Velden,
I think it's a LISE bug. When used with event handler you can't get item_id param.

Re: Pass param to Custom Field by udt in LISE
Posted: Wed Sep 15, 2021 11:08 am
by velden
Trivial (though possibly not for a PRESave... event):
Code: Select all
$obj = $params['item_object'];
echo $obj->item_id . '<br>';
Re: Pass param to Custom Field by udt in LISE
Posted: Wed Sep 15, 2021 3:13 pm
by pierrepercee
Velden,
I report a bug... You can get params like name, alias etc... but not item_id when you use an event handler.
I am tired, I think. A little rest, no doubt...Every time I solve a problem, it's to meet another one that is even more difficult
Last, in my UDT I try to get item_id via sql request:
Code: Select all
$querya = "SELECT item_id FROM cms_module_lisebontruc_item WHERE alias='test88'";
$dbr = $db->getAll($querya);
The output is :
Code: Select all
Array ( [0] => Array ( [item_id] => 24 ) )
H ow the heck do i get item_id as a string. An array within an array, I don't know how to browse. I know, developer is a job
