Pass param to Custom Field by udt in LISE

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Pass param to Custom Field by udt in LISE

Post 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 !
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3365
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Pass param to Custom Field by udt in LISE

Post 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:

Code: Select all

$myparam = $params['myparam'];
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.
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post 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.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3365
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Pass param to Custom Field by udt in LISE

Post 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.
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post 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

Code: Select all

$im->readImage($ft);
seems to generate the error.
If i use my udt in Lise summary template

Code: Select all

{myudt name=$path}
and I use

Code: Select all

$im->readImage($params[name]);
, 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 ..
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3365
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Pass param to Custom Field by udt in LISE

Post 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.
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post by pierrepercee »

Velden,

Code: Select all

$fd->GetImagePath(false)
Same thing... The file is not uploaded. I get the correct path (verified using echo in udt).
If I try with a minimalist udt

Code: Select all

echo "youpi!";

Then I can use Event Manager.There's no problem, the file is well uploaded.
I'm lost.... :(
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3365
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Pass param to Custom Field by udt in LISE

Post 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.
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post 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 ...
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3365
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Pass param to Custom Field by udt in LISE

Post 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).
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post 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...
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post 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 :

Code: Select all

{$item->item_id}

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'. :)
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post by pierrepercee »

Velden,

I think it's a LISE bug. When used with event handler you can't get item_id param. :(
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3365
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: Pass param to Custom Field by udt in LISE

Post by velden »

Trivial (though possibly not for a PRESave... event):

Code: Select all

$obj = $params['item_object'];
echo $obj->item_id . '<br>';
pierrepercee
Forum Members
Forum Members
Posts: 71
Joined: Thu Jan 10, 2013 8:02 am

Re: Pass param to Custom Field by udt in LISE

Post 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 :)
Post Reply

Return to “Modules/Add-Ons”