Question about event hooks and how they work

Discussion corner for Developers of Serendipity.
Post Reply
eburrows
Regular
Posts: 19
Joined: Fri May 05, 2006 11:28 pm

Question about event hooks and how they work

Post by eburrows »

I was going through some of the code to modify it to my needs and have not been able to figure out where the actual event hooks reside. I was looking to edit the Edit entries display to include additional information and got stuck while looking through the functions_entries_admin.inc.php code. Currently, only certain users have user accounts but we receive potential entries from other users.. so we wanted to at least give credit to those users by mentioning there name. So within the advanced options section of the edit entry page I was thinking about adding two regular textbox where a guest can submit his name and organizational association. Going through the code I discovered that this section is printed by the event hook:

serendipity_plugin_api::hook_event('backend_display', $entry);

I did a grep for the backend_display but maybe I did it wrong because I wasn't able to figure out where this code actually resides.. Does Serendipity have a standard place where these event hooks are stored? Or is it just random to a particular php where they are addressed?

Any help would be appreciated. Once I find out where that code is I can update it and then create a few extra fields in the db.

Thanks!
Evan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Question about event hooks and how they work

Post by garvinhicking »

Hi!

Thanks a lot for asking! I'm always happy if people ask about how to extend serendipity, because this way we can get new code contributors. :)

How, the event stuff works like this:

Inside the code the plugin API calls an event via the "hook_event" method. This is the method you already spotted. This says that the event "backend_display" now happens, and then the plugin API goes through all event plugins and asks them "Hey, do you want to do something on this event?"

So, inside the include/plugin_api.inc.php, the hook_event method is explained. Every event plugin needs to implement the "event_hook" (note the different function name!) method, which is called with the name of the hook as an argument, plus some variables.

If you look at a plugin like plugins/serendipity_event_entryproperties.php you will see how inside that event_hook method it is checked if the currently fired event is called "backend_display", and if so, the plugin emits its output.

Actually, the "entryproperties" plugin also has a config option that allows you to created "Custom Fields". You can thus create a "username" custom field, fill it, and then you can also display it in your template. You might want to look at blog.s9y.org, I think I once wrote an intense article about custom fields.. :)

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/
eburrows
Regular
Posts: 19
Joined: Fri May 05, 2006 11:28 pm

Post by eburrows »

Garvin,

Thanks for the quick reply! You know.. sometimes I surprise myself with how easily my mind scatters. I have already used the extended properties plugin to add additional fields.. I don't know why that option just didn't click in my head!

Anyway, I guess my problem is trying to figure out how to use a default guest account. The serendipity site that I stood up is being used as an interface to allow people to submit entries to the site for review and possible publishing. We kind of want to stay away from user accounts so I was thinking about guest priveleges but wasn't too sure how they work. I created a guest account but it doesn't make much sense to have to distribute this username and password to everyone that might want to contribute. Currently, we has a few content editors who will review the submissions coming in from external parties and determine if they are relevant for posting.

So as I said before I kind of wanted to stay away from having to manage user accounts except for when we get to know a entry submitter and trust them. At that point I would probably create a user account with publishing priveleges for that person. But until then is there a way to have a guest account logged in once the user accesses the site? My current guest account allows users to view RSS entries and submit new articles in draft form so that they can be reviewed by our editor(s). I notice that you guys do it on s9y's frontpage but I wasn't to sure how or where to address it in the code. Would this be something that I would address in index.php?

Thanks again for all the help!
Evan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Ah, I see. I guess what you want to use is the "Suggest entry" plugin. I developed this commercially some time back, and the version in Spartacus is not ready to be used on other sites.

But I'll try to polish it up a bit so that you can use it. Will post here once I've done that.

That plugin will allow anonymous users to send an entry that is stored in your interface for reviewing, and then you don't need to create any accounts for thos people.

Best 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/
eburrows
Regular
Posts: 19
Joined: Fri May 05, 2006 11:28 pm

Post by eburrows »

Garvin,

Thanks.. Yes it is sort of like a suggest entry, but that is not the only part. We are using the RSS aggregator plugin to combine feeds that content contributors look at when looking for articles/entries to submit to the site. So the goal was to allow users access to view the compiled RSS feeds, which currently requires the user to have an account so they can view and filter the feeds in the admin console. I thought about making a sidebar plugin to do some of this but it seemed to make more sense just to use Serendipity's built in editing functions.

So although your plugin would be very useful the problem that we were facing was trying to give users access to view those RSS feeds without having to create an account for them. More eyes on the feeds will hopefully produce more submissions and better articles rather than having to pin all of it on one of two content editors on our end looking and sorting through potential articles. This way a guest user searches through the feeds that are pulled in through the aggregator plugin, they can flag it in the article title that it is being reviewed and once they craft an entry based on the information in that rss article they can submit it in draft form to the site (if they don't have publishing rights) and our editors can review the submission and choose whether or not to publish it to the site. Granted it would make more sense to have an automated way of letting other users know that someone is viewing and editing that RSS entry (so people don't step on on another's toes and submit entries related to the same RSS feed article) but for the time being I can live with that.

So is the actual code for the anonymous/guest user part simple enough that I can just snag that? Because that is all I am really looking for. Just a way to give any user enough rights to get into the admin section to view those feeds and submit an entry to draft. Since I already have that account it is just a matter of figuring out how to log the visitor of the site into the guest account automatically once they come to the site. I am new to this stuff so I am not sure if you can have one guest account and all users that hit the site can be logged into it (I don't know if there are conflicts with only one session being associated with an account at any particular point in time). I am assuming that there should not be, but I don't know anything about this stuff.

So to recap, I just want to find a way to log each incoming user into my guest account when they visit the site so that they can have limited access to some of the admin functionality.

Sorry for being such a pain but I really appreciate your responses.
Evan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

I just committed the updated "suggest entry" plugin variant. Hope it can be of help to you, it'll be in CVS shortly I think.

I must admit I'm not that fully understanding your RSS feed issue, it sure sounds complicated. I'll try to read through your entry when my head's not spinning. *g*
So to recap, I just want to find a way to log each incoming user into my guest account when they visit the site so that they can have limited access to some of the admin functionality.
I think there once was a plugin for that, which logged in people based on their IP address automatically to profiles. It must be here in the forum somewhere, maybe the "ldap" keyword or "session login" might be a use for you? It was actually only 5-10 lines of plugin code to write...

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/
eburrows
Regular
Posts: 19
Joined: Fri May 05, 2006 11:28 pm

Post by eburrows »

Thanks again..

Yeah I looked at the IP-based account login which was cool and would fit the bill if we were in a static environment.. unforunately we have to deal with DHCP :x, so the plugin would just end up creating way too many user accounts that weren't distinguishable. I will keep my eye out for the plugin you posted and continue looking for a way to have a user logged in automatically to the guest account.

I appologize if i am confusing you, I am not having an issue with the RSS stuff, that stuff works great!

I was looking through the ip-based login code that you mentioned and saw this function that I thought might be of use..

if (serendipity_authenticate_author($_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_ADDR'], false, true)) {
// If the function returns true, we are authenticated. Hooray for Boobies.
return true;
}

I just created an event plugin using the code above and subsitituted the IP stuff with my guest username and password and it seems to work.

Going back to one of my previous questions I understood how to track down the event hooks that are described in event plugins but functions_entries_admin.inc.php is not an event plugin and makes no other reference to backend_display which it calls in serendipity_plugin_api::hook_event('backend_display', $entry). You mentioned serendipity_event_entryproperties as an example but that is an event plugin and uses backend_display as a case option, that is not the case in functions_entries_admin.inc.php in makes no other mention of backend_display. So where is it grabbing that information to print the Advanced Options section then?

I am just trying to piece this whole picture together and your responses have been very helpful

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

Post by garvinhicking »

Hi!

Okay, let me know how the IP-authentication stuff works out for you :)
Going back to one of my previous questions I understood how to track down the event hooks that are described in event plugins but functions_entries_admin.inc.php is not an event plugin and makes no other reference to backend_display which it calls in serendipity_plugin_api::hook_event('backend_display', $entry). You mentioned serendipity_event_entryproperties as an example but that is an event plugin and uses backend_display as a case option, that is not the case in functions_entries_admin.inc.php in makes no other mention of backend_display. So where is it grabbing that information to print the Advanced Options section then?
As I said -- the Event hook queries all installed plugins, and the plugins are responsible for what gets outputted there! The event takes place in functions_entries, but the executed code is in the plugins.

So a plugin like the entryproperties plugin is responible to push the information to the event call. That's why it's called a "hook" :)

Does that help you anything, or am I confusing? :)

Best 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