Textpattern Forum

You are not logged in. Register | Login | Help

#1 2012-02-16 14:17:16

geoff777
Plugin Author
From: Benarrabá Andalucía Spain
Registered: 2008-02-19
Posts: 271
Website

First Plugin ... Help with Public side needed

Hi

I’m trying to write a voting plugin.

I have the Admin side up and working. – Lots of adapting by looking at other plugins. – Mainly smd_bio.
Two tables are created.
An Extension Tab added from which you can input Voting titles and options.
You can Save – Edit & Delete.
Amazingly this all works.

I’m completely stuck on how to start with the public tag.

The idea is to have only one tag that took no attributes … <txp:geo_vote /> (if geo is available?)
So it’s easy to add to articles and all configuration is from the admin tab.

1. I’m not sure how to handle the event of the vote button being pressed.

2. There are some voting fraud prevention checks to do – cookie – ip – logged in? How do I handle setting and reading cookies, is there a preferred method?

I’m sure I’ll need a lot more help but getting answers to these 2 issues will be a start :-)

Thanks
Geoff

Last edited by geoff777 (2012-02-16 14:23:16)


There are 10 types of people in the world: those who understand binary, and those who don’t.

Offline

#2 2012-02-16 14:47:06

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,909
Website

Re: First Plugin ... Help with Public side needed

geoff777 wrote:

(if geo is available?)

Currently yes — grab it while you can.

how to handle the event of the vote button being pressed.

Before Txp 4.4.0 I’d have said AJAX is easiest by adding some jQuery to hook into your button like this:

jQuery(function() {
   jQuery(".vote-button-class").click(function() {
      jQuery.post(
          'http://site.com/textpattern/index.php?event=geo_vote', {
             step: "count_vote",
             vote_for: "some_unique_ref"
          },
          function(data) {
             // Parse the returned data block and do something
             // to tell people their vote has been counted (or not)
             // and disable / remove the vote button
          }
      );
   });
});

But now that Txp requires a token to be passed on the admin side, I’m not sure if that approach is feasible any more. Requires thought. Does the code from the latest voting plugin cbe_helpful help at all?

How do I handle cookies?

Txp has the cs() function (“cookie set”) to read them in.

$cookie_val = cs('my_cookie_name');
if ($cookie_val) {
   // Yay! Cookie has been set to value $cookie_val
}

PHP has setcookie().

I need to grab the article_id

Pretty much everything you need will be in the globals $thisarticle and $pretext. If you dmp($thisarticle) in your public-side code you can see what’s available when you’re viewing an individual article. But defend against missing array entries in your code by using isset() first and/or testing if $thisarticle is NULL (which it will be when not in article context).

Hope that helps.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2012-02-16 15:25:05

geoff777
Plugin Author
From: Benarrabá Andalucía Spain
Registered: 2008-02-19
Posts: 271
Website

Re: First Plugin ... Help with Public side needed

Thanks Stef

The button event is on the public-side.
When someone votes.
Ajax would be cool but I’m pretty clueless about using it.

I haven’t looked at cbe_helpful – I’ll check it out

$thisarticle[‘thisid’] should be what I need.

Thanks


There are 10 types of people in the world: those who understand binary, and those who don’t.

Offline

#4 2012-02-16 15:33:46

Gocom
Developer
Registered: 2006-07-14
Posts: 4,476
Website

Re: First Plugin ... Help with Public side needed

Bloke wrote:

But now that Txp requires a token to be passed on the admin side, I’m not sure if that approach is feasible any more. Requires thought. Does the code from the latest voting plugin cbe_helpful help at all?

But isn’t the voting for public-side, not for admin-side? If it was for admin-side, the token shouldn’t be a problem.

  • it’s available via textpattern._txp_token in JavaScript.
  • There is always jquery.serialize() (normal HTML forms can be used to build the data).
  • And Textpattern’s dev branch ships with postForm() and sendAsyncEvent() JavaScript functions.

But defend against missing array entries in your code by using isset() first and/or testing if $thisarticle is NULL (which it will be when not in article context).

Batm… umm assert_article() to the rescue. Gives out those useful error messages too when deemed needed.


Rah-plugins | What? I’m a little confused… again :-) <txp:is_god />

Offline

#5 2012-02-16 15:34:47

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,909
Website

Re: First Plugin ... Help with Public side needed

geoff777 wrote:

The button event is on the public-side.

Yes, you’re right I’m talking crap. Gah, I need sleep / chocolate.

In addition to what Gocom suggests, you can hook your plugin into the public-side callbacks pretext or pretext_end. If you wrap the vote button(s) in a regular HTML form that passes some vars via method="post" then when you click the button and it submits the page, your plugin can read the posted values ($val = ps('my_posted_input_name');) and act accordingly, then let the page continue to render.

Downside: page refresh, which might look ugly.

(EDIT: aren’t postForm() and sendAsyncEvent() admin-side functions? Unless you include textpattern.js of course? Or have I missed something?)

Last edited by Bloke (2012-02-16 15:39:52)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#6 2012-02-16 15:57:23

uli
Moderator
From: Cologne/Köln
Registered: 2006-08-15
Posts: 3,084

Re: First Plugin ... Help with Public side needed

geoff777 wrote:

<txp:geo_vote /> (if geo is available?)

Not any longer ;)
It’s me who’s to blame.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#7 2012-02-16 16:23:18

geoff777
Plugin Author
From: Benarrabá Andalucía Spain
Registered: 2008-02-19
Posts: 271
Website

Re: First Plugin ... Help with Public side needed

uli wrote:

Not any longer ;)
It’s me who’s to blame.

Thank you … Now the pressures on to get this finished … :-)

Last edited by geoff777 (2012-02-16 16:25:05)


There are 10 types of people in the world: those who understand binary, and those who don’t.

Offline

#8 2012-02-19 15:06:09

geoff777
Plugin Author
From: Benarrabá Andalucía Spain
Registered: 2008-02-19
Posts: 271
Website

Re: First Plugin ... Help with Public side needed

Is there a best practice for outputting text on the public side?

I’ve got a voting form working with echo(); and a lot of escaping apostrophes.
It seems an ugly way of doing it, but it works.

I have to output a results table should I use hed(), startTable(), tr() fLabelCell() & td()?
Do I define $out to be a concatenated string of these functions then call another function to output $out?

I’ve been trying to find out about the core defined functions but the wiki pages for them are all empty.

Thanks


There are 10 types of people in the world: those who understand binary, and those who don’t.

Offline

#9 2012-02-21 12:34:17

geoff777
Plugin Author
From: Benarrabá Andalucía Spain
Registered: 2008-02-19
Posts: 271
Website

Re: First Plugin ... Help with Public side needed

Still plodding on with this … I have it working but the output is appearing before the <!DOCTYPE html tag at the top of the page and not as part of the article body or excerpt.

Does anyone know how to correct this?

Thanks


There are 10 types of people in the world: those who understand binary, and those who don’t.

Offline

#10 2012-02-21 12:39:03

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,909
Website

Re: First Plugin ... Help with Public side needed

geoff777 wrote:

output is appearing before the <!DOCTYPE html tag at the top of the page and not as part of the article body or excerpt.

Instead of using echo $out in your tag, use return $out at the very end, which will then put the contents where the tag is in the page flow.

In answer to your previous question above, I usually concatenate them into an array ($out[] = 'first bit'; ... $out[] = 'second bit';...) and then join('', $out) for display. Alternatively, look at the core’s doWrap() function which takes a $out array and wraptag, break, class, etc and does it for you.

btw, sorry I didn’t reply to your e-mail. I’ll try and get back to you when I get a moment.

Last edited by Bloke (2012-02-21 12:42:55)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB