A Simple Plugin? Adding to the entryproperties table.

Creating and modifying plugins.
Post Reply
dakotaroar
Regular
Posts: 21
Joined: Thu Nov 08, 2007 3:30 pm

A Simple Plugin? Adding to the entryproperties table.

Post by dakotaroar »

I am having a hard time getting the hang of how all these plugins work. Basically, I want to modify the Extended Properties plugin to actually do something with the data that is input (and then I can strip out the rest of the code that I don't need. So say I create some custom fields (Field1, Field2, Field3), and then I want to use the information in those fields to interact with a second application, and then I want to get information back from that application and put it in one of those fields. For example:

1.User posts "A Post" and enters "2" in a Field1.

2.User hits "Save" and the "2" from Field1 is sent to another application (lets say an SMF forum), where it is used immediately to create a new thread posted by the user with ID = "2") This is all done from the plugin, so all variables are present.

3.Second application (SMF) returns the URL (in a variable) of the topic that was just created to Serendipity.

4.Plugin edits the entry just created and adds URL in the body of the entry.

5.Serendipity plugin places this URL into Field2 associated with the entry that was just created and this goes into the entryproperties table to be called later.

Now, I can DO 1-4 by screwing with functions_entries.inc.php, which I know is the wrong way to go about it, and I would like to make it work correctly as a plugin.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: A Simple Plugin? Adding to the entryproperties table.

Post by garvinhicking »

Hi!

Actually, you should be able to do all of this inside the "backend_save" / "backend_publish" hooks.

There you can access the entryproperties (either read them from the DB, or check the $_POST array - maybe even $eventData holds this) and send it to your second application, and the returned URL you can update the body with (using a database query). If you want to do without the DB query, you could use the "backend_presave" hook instead of backend_save/backend_publish.

Entryproperties you can also simply insert into the serendipity_entryproperties database, if you want to store additional ones.

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/
dakotaroar
Regular
Posts: 21
Joined: Thu Nov 08, 2007 3:30 pm

Post by dakotaroar »

Is there a good tutorial on hooks toward which you could direct me? I'm really a beginner at this.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Sadly there's little writeup for this, only some texts on this forum, the technical API on www.s9y.org and the plugins as a starting example.

The API docs and the plugin examples are usually sufficient for PHP developers; of course it would be nice to write a full "howto" for people who are no Php-Professionals, but no one has volunteered for that as of yet.

The german s9y book I'm writing covers some ground rules, but this also actually expects plugin developers to know some PHP so that the API documentation is usually sufficient for those people. So I'm afraid, I can't really point you to things other than the API docs and existing plugins, plus searching this forum. :(

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/
dakotaroar
Regular
Posts: 21
Joined: Thu Nov 08, 2007 3:30 pm

Re: A Simple Plugin? Adding to the entryproperties table.

Post by dakotaroar »

garvinhicking wrote:Hi!

Actually, you should be able to do all of this inside the "backend_save" / "backend_publish" hooks.

There you can access the entryproperties (either read them from the DB, or check the $_POST array
Yup, I can get things out of $_POST.
- maybe even $eventData holds this) and send it to your second application, and the returned URL you can update the body with (using a database query).
The DB query makes sense.
If you want to do without the DB query, you could use the "backend_presave" hook instead of backend_save/backend_publish.

Entryproperties you can also simply insert into the serendipity_entryproperties database, if you want to store additional ones.
Okay, is there some weird way of doing this that I was unaware of? When I have attempted inserting things into the database, I have run into problems. Is this the correct syntax to use to insert a new record in the entrypropertiestable?

Code: Select all

serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}entryproperties (entryid, property, value) VALUES ($entry['id'], $property, $value)");
Thanks for the help. I'm trying to go through and learn the API stuff by looking at the plugins.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: A Simple Plugin? Adding to the entryproperties table.

Post by garvinhicking »

Hi!

In your SQL query you are missing quotes! It should be:

Code: Select all

serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}entryproperties (entryid, property, value) VALUES ($entry['id'], '$property', '$value')");
instead?

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/
dakotaroar
Regular
Posts: 21
Joined: Thu Nov 08, 2007 3:30 pm

Post by dakotaroar »

I think I found my problem.

Say the entryproprties table looks like this:
23 prop1 val23
24 prop2 val24
25 prop3 val25

And say I have the following custom fields:
prop1
prop2
prop3

Now, if I try to use this query:

Code: Select all

serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}entryproperties (entryid, property, value) VALUES ($entry[id], 'prop3', '$val')");
it will NOT be added to the table (EVEN if that field is empty for that entry)! If, however, I use a property name that is not already in use by a custom field, it works fine, and I can still call it with smarty by using, e.g., {$entry.properties.ep_prop4}.

I just never knew that these custom field names were reserved like this. Weird.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

I'd guess your SQL query might simply be wrong.

Try this:

Code: Select all

$q = "INSERT INTO {$serendipity['dbPrefix']}entryproperties (entryid, property, value) VALUES ({$entry['id']}, 'prop3', '$val')";
$result = serendipity_db_query($q);
echo $q . "<br />\n" . $result;
I think that the INSERT fails, because there's a unique entry restriction on entryid, property - so you need to UPDATE the table, if such a field already exists:

Code: Select all

$q = "UPDATE {$serendipity['dbPrefix']}entryproperties SET value = '$val' WHERE entryid = {$entry['id']} AND property = 'prop3'";
$result = serendipity_db_query($q);
echo $q . "<br />\n" . $result;

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/
dakotaroar
Regular
Posts: 21
Joined: Thu Nov 08, 2007 3:30 pm

Post by dakotaroar »

The thing is, the field doesn't exist in that table with that entry id as far as I am aware, which is why I was using INSERT instead of UPDATE.
Post Reply