need help with counter in plugin_contactform

Creating and modifying plugins.
Post Reply
konus
Regular
Posts: 334
Joined: Mon Jun 16, 2008 1:57 pm
Location: Dresden, Germany
Contact:

need help with counter in plugin_contactform

Post by konus »

Hi,
modifying the contactform using a dynamic template with the setting "custom" is very easy and convenient.

I now would like to add a hidden field with a counter (as ticket#) to the contactform and write the field to the mail. The counter should be increased after each submit of the form.

Is there a easy way to implement such funktion?

Greetings Konrad
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by garvinhicking »

Hi!
Is there a easy way to implement such funktion?
That depends on your reception of "easy". I would make use of the fact, that the contactform allows you to call the frontend_comment event hook (for spamblock integration, mostly). So I'd create an event plugin that simply listens on frontend_comments, emits the hidden input field and per each submit (event frontend_saveComment) increase a database-counter for one.

For someone that knows the s9y API that might be work of 1 or 2 hours tops, and actually this is a pretty simple and great task for anyone that wants to try using the s9y Event-API for the first time.... :-)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
konus
Regular
Posts: 334
Joined: Mon Jun 16, 2008 1:57 pm
Location: Dresden, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by konus »

Garvin, thank you for your very fast answer!

Wouldn'd a plugin whitch is generally listening to the frontend_comment event hook, start every time the hook is called (also increase the counter during normal commenting too) and not only after calling from plugin_contactform?

I am wondering why are you suggesting a new plugin? I was thinking it would be "easier" to enhance the serendipity_event_contactform plugin to accept the new field? The only drawback I see, that it would be necessary to update the field right bevor the moment of sending, and not during page creation to ensure having an unique ID.

I imagine that I could try to change the plungin in this way, but since I don't know php very well I have no idea of how to read and write to a custom database field. Are there any examples in one of the plugins already?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by garvinhicking »

Hi!
Wouldn'd a plugin whitch is generally listening to the frontend_comment event hook, start every time the hook is called (also increase the counter during normal commenting too) and not only after calling from plugin_contactform?
Sure, the plugin would need to check for specific contactform variables, to make sure that it only fires up when a contactform is called. I think that should be doable through pagetitle or special contactform template variables.
I am wondering why are you suggesting a new plugin? I was thinking it would be "easier" to enhance the serendipity_event_contactform plugin to accept the new field? The only drawback I see, that it would be necessary to update the field right bevor the moment of sending, and not during page creation to ensure having an unique ID.
Its much easier to create a new small plugin for that, than to make the whole thing customizable with config options etc. And patching the contactform plugin should never be an option, as you can then no longer easily upgrade to new versions of the plugin.
I imagine that I could try to change the plungin in this way, but since I don't know php very well I have no idea of how to read and write to a custom database field. Are there any examples in one of the plugins already?
That can be done easily within the plugin:

Code: Select all

$this->set_config('counter', $this->get_config('counter')+1);
Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
konus
Regular
Posts: 334
Joined: Mon Jun 16, 2008 1:57 pm
Location: Dresden, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by konus »

garvinhicking wrote: Sure, the plugin would need to check for specific contactform variables, to make sure that it only fires up when a contactform is called. I think that should be doable through pagetitle or special contactform template variables.
Do you even have an example for this?

Sorry for asking again. I would like to try it by myself and learn it, but I feel like a pig looking into a watch.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by garvinhicking »

Hi!
Do you even have an example for this?
Sadly not out of the top of my head, no. Maybe you could check to use $serendipity['smarty']->get_template_vars('commentform_action') - and then check if that variable contains anything. It should only be set, when the contactform is being executed.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
konus
Regular
Posts: 334
Joined: Mon Jun 16, 2008 1:57 pm
Location: Dresden, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by konus »

Garvin, thank you for your fast answer!

Sorry for bothering again with questions but I am a beginner and really would like to learn it by myself. I now need to solve the next problem. Garmin wrote
So I'd create an event plugin that simply listens on frontend_comments, emits the hidden input field and...


How could the plugin emit a input field witch would by send by mail afterwards? I found out, that serendipity_event_contactform calls the hook with

Code: Select all

serendipity_plugin_api::hook_event('frontend_saveComment', $ca, $commentInfo);
$commentInfo ['comment'] is filled with $comment and consists all the information which are send by mail afterwards. My idea was to modify $commentInfo to add the desired information. But after some testing, it looks like that serendipity_event_contactform does not use the returned value. The Mail is created with the original variable $comment instead.

So how could I add a field to the mail without changing the serendipity_event_contactform-plugin? Is there a way to change the value of $comment directly?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by garvinhicking »

Hi!

Hm, I see, it could be that you cannot modify $commentInfo because that variable is not passed by reference. You would need to use an "earlier" event hook to overwrite the contents of $serendipity['POST']['comment'] to append your variable that has been emitted using frontend_comment.

"frontend_config" is one of the earlist event hooks, you could try that. Use a check to see if $serendipity['POST']['yourhiddenfieldname'] is set.

So if you use frontend_comment with

Code: Select all

echo '<input type="hidden" name="serendipity[contactcounter]" value="' . $this->get_Config('counter') . '" />';
you could use this on frontend_config:

Code: Select all

if (isset($serendipity['POST']['contactcounter'])) {
  $serendipity['POST']['comment'] = 'Counter: ' . $serendipity['POST']['contactcounter'] . "\n" . $serendipity['POST']['comment'];
  $this->set_config('counter', $this->get_Config('counter')+1);
}
HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
konus
Regular
Posts: 334
Joined: Mon Jun 16, 2008 1:57 pm
Location: Dresden, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by konus »

Sorry, I have two new problems:
1. setting $serendipity['POST']['comment'] as suggested does not help. Even $serendipity['POST']['comment'] = 'Blahblah' has no effect.

2. The suggested method also counted normal comments. To distict between normal comments and the contactform-comments I checked the page-title with

Code: Select all

if "my nice pagetitle" == $serendipity['smarty']->get_template_vars('head_title') )
Within contactform-comments it works nice, but within article-comments it doesn't. After sending the article-comment (and calling the abouve if-statement) the resulting page is empty, no feedback.

I even tested $test = $serendipity['smarty']->get_template_vars('head_title') , but with no luck - empty page after article-comment sending.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: need help with counter in plugin_contactform

Post by garvinhicking »

Hi!
konus wrote:Sorry, I have two new problems:
1. setting $serendipity['POST']['comment'] as suggested does not help. Even $serendipity['POST']['comment'] = 'Blahblah' has no effect.
Did you try $GLOBALS['serendipity']['POST']['comment'] instead?
2. The suggested method also counted normal comments. To distict between normal comments and the contactform-comments I checked the page-title with

Code: Select all

if "my nice pagetitle" == $serendipity['smarty']->get_template_vars('head_title') )
Within contactform-comments it works nice, but within article-comments it doesn't. After sending the article-comment (and calling the abouve if-statement) the resulting page is empty, no feedback.
Maybe instead only issue the hidden input field when

Code: Select all

if ($serendipity['smarty']->get_template_vars('commentform_action') != '') {
 echo ...
}
applies?

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply