Recent Topics

AJAX in a plugin

Started by on Dec 05, 2005 – Contents updated: Dec 05, 2005

Dec 05, 2005 17:03    

I'm about 60% through porting the [url=http://blog.jalenack.com/archives/democracy/]Wordpress Democracy poll plugin[/url] to a b2evolution plugin. The backoffice part of my version is done. It installs the tables and lets you add, edit, delete, activate and deactivate polls, all from the Tools tab. And I've got a skinTag that writes to the head and body sections of a blog, so it can display the question form. But it uses some AJAX to send the vote and fetch the results without reloading the page. This is where I'm getting stuck.

The JavaScript tries to send the vote to the php file (plugins/_poll.plugin.php) and get the results back. I've tried this several ways, but I run into problems every time. The plugin file doesn't like being accessed directly like that. I tried making a template that sits in the plugin folder and only has a skintag that tries to catch the results and send back the results. I tried just creating a file that includes what it needs to make the plugin file do it's thing. I need some way to get into the b2evolution program so it has access to the tables and settings, but only does this one thing. I'm probably not explaining this very well.

If anyone wants to help I can zip up what I have so far, and try to do some quick documentation so you can take a look. We could share credit for the port.

Dec 05, 2005 19:22

Zip it up, I'd like to take a look and help.

Dec 05, 2005 20:38

Here's the zip: http://www.brendoman.com/dev/blogs/plugins/pollport.zip

It's pretty messy at this point. Here are some things you might want to know.

1. Most of what was the main plugin file (democracy/democracy.php) has been moved to _poll.plugin.php. democracy.php is now a small file that attempts to respond to the AJAX code when someone votes. old-democracy.php is the file from the Wordpress version.

2. I've commented out all of the user permission checks, since they're the WP way they don't work. They can be added back in later.

3. Installing: Drop the _poll.plugin.php file and the democracy directory into the plugins directory. Then add this to the skin in the head section:

        <?php
                // -------- POLL INCLUDED HERE ---------------
                // Call the Poll plugin head section:
                $Plugins->call_by_code( 'evo_poll', array(
                        'display'=>'head'  ) );
                // -------- END OF POLL -----------------
        ?>

And this in the sidebar:

        <?php
                // -------------- POLL INCLUDED HERE -----------------
                // Call the Poll plugin sidebar section:
                $Plugins->call_by_code( 'evo_poll', array(      // Add parameters below:
                        ) );
                // --------------- END OF POLL -------------------------
        ?>

4. Once this is working we'll want to add in multiblog support. Basically we can just add a column to the question table called blog_id. And then fix up the functions so they respect that an allow each blog to have one active poll. The backoffice can display the blogs that the use has permission to edit along the top of the page, just like in the 'Write' tab.

5. I'm getting more comfortable with PHP, but OOP and AJAX are still over my head (as you'll probably be able to tell from the code).

Thanks for your help,

Danny

Dec 06, 2005 00:34

Your main problem (from looking at the code) seems to be that the democracy.php file (which answers the JS calls!?) will give fatal error: call to undefined function, because there you call functions, but have transformed the WP plugin functions into methods of your plugin.

functions: global/static.
methods: functions of an object.

You'll have to change your plugins source to make the democracy functions functions again (currently they are methods [of your Plugin]).
That should work.
To debug it, you could call democracy.php like the JS does it directly in your browser probably.

I like the idea/port and hope you'll get it to work.. :)

Dec 06, 2005 03:38

That's what I'll have to do. Move those functions out of the object. The only problem is that I need to be able to access the database to record votes and fetch results. I can include the _config.php file and use the built in php mysql functions rather than $DB->query(). I'll give that a try.

Dec 06, 2005 21:35

You could also require/include /evocore/_main.inc.php - that should init anything, but it's probably overkill.

Perhaps it would be a good idea, to have a /evocore/_connect.inc.php that could be required/included to have just a DB connection.

Anyway, take a look where $DB gets instantiated in _main.inc.php, you could do it the same. It's a lot more fun to work with the DB class than with mysql_* functions.

Dec 07, 2005 02:51

Great advice, blueyed. I got past that problem. It's able to send a vote and fetch results with Ajax now.

Dec 12, 2005 22:04

I am so excited to see this... I was hoping someone would put one of these together... please post when it is ready for prime time! And thanks in advance!! :D

Dec 12, 2005 22:17

personman, you're probably happy to here, that I've created a call_plugin.php file in /htsrv/ (just locally for now), which allows to easily link to plugin methods that get executed through http (htsrv).

I'd be interested in issues you have with implementing your plugin and suggestions how to address those..

Dec 13, 2005 02:59

That sounds great. That should make my plugin a lot easier to make.

Jan 09, 2006 22:43

Will call_plugin.php make it into version 1.7?

Jan 10, 2006 01:08

I'd like to, of course. I'll try. A problem is of course to provide also a call_plugin_light.php file that does not get the whole _main.inc.php file down etc.

Jan 31, 2006 22:22

I see that call_plugin.php has made it into CVS. I read your comments and I think I see how this will work when it's done. Let me know if I can do anything to help (like writing a howto).

Feb 01, 2006 00:59

It should be done, like it is. Actually we have a code freeze, so there won't happen much until the beta..

The procedure is quite simple:
- define a get_htsrv_methods() method in your plugin that returns the method names you want to make accessible through call_plugin.php (as array)
- define those methods in your plugin (prefixed with "htsrv_").
- use $this->get_htsrv_url() in your Plugin to create an URL that links/calls your method.

E.g.:


class my_plugin extends Plugin
{
  function get_htsrv_methods()
  {
     return array( 'js_callback' );
  }

  function htsrv_js_callback( & $params )
  {
    // $params will be what you've given in $this->get_call_url()
  }

  function DisplayOnSomeEvent( & $params )
  {
    echo '...'.$this->get_htsrv_url( 'js_callback', array( 'some' => 'params', 'which' => 'get passed to your htsrv_* method' ).'...';
  }
}

Documentation would be good, of course.

A problem of this is, that it's quite slow, because it does an whole init of the system.. therefor I want to add (after the beta) call_plugin_static.php which just loads the config and the Plugin class and instantiates the Plugin, while providing static methods to connect to the DB for example, if you want to use that.

Feb 01, 2006 03:44

I guess I didn't understand it as well as I thought I did. Thanks for the example. I tried it out successfully. Now back to that poll plugin . . .

Keep up the good work. I'm looking forward to the beta.

Feb 27, 2006 23:19

wow I just randomly came across your site with the wp poll on it which made me search the b2evolution forums and voila here you are working on a b2evolution version, thanks alot! :>

let me just make sure you're the same guy, is the original plugin this one? http://blog.jalenack.com/archives/democracy/

Feb 27, 2006 23:44

Yep, that's the plugin I ported. It's all done now. I'm just waiting for 1.7-beta to come out, then I'll release the plugin.

Feb 28, 2006 00:36

You're a star

out of curiousity why did you port it to b2evolution, do you use b2evolution as well as wordpress?

personman wrote:

Yep, that's the plugin I ported. It's all done now. I'm just waiting for 1.7-beta to come out, then I'll release the plugin.

Feb 28, 2006 01:21

I don't use Wordpress at all. I have an install that I use to test out plugins and stuff, but my main site is powered by b2evolution. Note that I didn't write the original plugin. That was Andrew Jalenack. I just ported it to b2evolution.

Feb 28, 2006 05:24

ahh, that makes sense, sorry for the mixup...

you should do the shoutbox too ;)
http://blog.jalenack.com/ajax/

personman wrote:

I don't use Wordpress at all. I have an install that I use to test out plugins and stuff, but my main site is powered by b2evolution. Note that I didn't write the original plugin. That was Andrew Jalenack. I just ported it to b2evolution.

Mar 27, 2006 20:27

Couldn't you release it even now, please? The current 1.6 Alpha is from Nov 05, at it may take a while before 1.7 comes out. Or, will it be tight to new features not present in 1.6?

personman wrote:

Yep, that's the plugin I ported. It's all done now. I'm just waiting for 1.7-beta to come out, then I'll release the plugin.

Mar 27, 2006 21:17

The plugin does use hooks that aren't in version 1.6. And even if you installed from CVS, the code has changed a lot since I wrote this, so it probably wouldn't work. When the beta is released I'll make sure it's working and then release it. Sorry to tease you with it. :)

Jul 22, 2006 11:03

Is this plugin 1.8 compatable? If not could you please make it so if you have some sparetime. Would be much appriciated from my side :)

Jul 22, 2006 16:02

Yes, I'm using it on my site now and I run 1.8-beta. I'll try to release it soon.

Aug 10, 2006 02:05

you couldn't send the version you use for beta testing ;)

Aug 11, 2006 01:31

yeah, a release would be nice :P even if its beta

:oops:

Shanti

Sep 07, 2006 06:08

I'm looking for that plugin too. I saw wp-polls.php running on a WP blog and it looked like just the thing to help people get started to partcipate.

I'm posting this so I'll get auto mail replies when it becomes available.

Sep 18, 2006 23:12

Hello guys,

i´ve searched for a poll plugin. I need it till a blog have to go online tomorrow. The Link http://www.brendoman.com/dev/b...llport.zip is down, or has moved?

@Bredoman: Can you sent me the latest version? I´m using b2e version 1.8.1-RC

Thanks a lot.

Mikih

Oct 13, 2006 21:27

I gave this a shot, and while it works, it's not what I was looking for. Any way to integrate this poll into a specific post rather than into the skin?

Feb 11, 2008 06:08

Hi guys,

First off, thanks to the developer for this plugin, an awesome addition to my site.

I have but one issue/questions.

Can this be configured to create poll posts also?
I have a blog category that is assigned to polls only and I would love to use this to create the poll posts.

Please advise.

Feb 11, 2008 14:17

That's not possible in the current version, but it would be a nice feature.

Feb 11, 2008 15:46

Personman,

It would be an awesome feature and I can see it adding much value and functionality to many blogs.

The simple fact of having this poll organized in a category on your site with the charts, priceless.

Would this be hard to code/design?

Feb 11, 2008 19:02

Personman,

Here's the error i'm getting on my Blog when viewing the main page,

";s:7:"after_q";s:9:"";s:4:"sort

After you vote this appears,

";s:7:"after_q";s:9:"";s:4:"sort";b:1;s:4:"blog";s:1:"1";s:12:"archive_link";b:0;s:3:"gft";b:1;s:3:"gct";s:4:"#06c";s:3:"gcb";s:4:"#05a";s:7:"poll_id";i:0;}' />
Notice: unserialize() [function.unserialize]: Error at offset 1017 of 1027 bytes in /titssn/blogs/plugins/democracy_plugin/_democracy.plugin.php on line 697

Any ideas as to how to fix this?

Feb 11, 2008 19:09

I've never seen that error. Can you paste in the question and choices of your poll? You may have some characters that need to be escaped.

Feb 11, 2008 19:28

Personman,

Thanks for the assistance with this issue.

Here's the info you requested.

Question: Are you concerned about your online identity?

Choices:
1. Yes
2. No
3. I don't have one.
4. Never thought about it.

It would be nice if a bar chart option was available :-).

Thanks for making this awesome plugin available.

Feb 11, 2008 19:43

Yabba,

Thanks for the feedback and sugegstion.

That didn't help. It's still doing the same thing. Take a look at the site. Unless I added the \ at the wrong place.

Feb 13, 2008 20:55

Hey guys,

Any help with this?

Please advise as I am now having the problem on multiple pages.

Feb 13, 2008 21:20

TITSSN,
I'm using the plugin on b2evolution 2.4 and not having the problem. There must be something different in the way you're set up. Can you make sure you have the most current version of the plugin? Are you adding the poll to the page using a widget, or did you add code to your skin?

Feb 13, 2008 21:46

The last params are without html codes like &quot;.

s="" polls="" ;s:16:="" widget_css_class="" ;s:9:="" widget_id="" ;s:0:="" ;s:5:="" title="" ;s:4:="" poll="" ;s:8:="" before_q="" ;s:27:="" type="hidden">
<strong id="poll-question">";s:7:"after_q";s:9:"</strong>";s:4:"sort";b:1;s:4:"blog";s:1:"1";s:12:"archive_link";b:0;s:3:"gft";b:1;s:3:"gct";s:4:"#06c";s:3:"gcb";
s:4:"#05a";s:7:"poll_id";i:0;}

It missed to htmlspecialchars() the params. May be a bug, but I have only seen it in the TITSSN blog.

TITSSN, I don't have the error on my testblog. Do try a fresh copy of the plugin as personman suggested.

Good luck

Feb 13, 2008 22:25

Personman/Afwas,

Thank you for the feedback and response. I uninstalled and reinstalled it and now it works :-).

I think I figured out what the problem was. When I installed it I wanted it to say TITSSN Polls in the sidebar and so I changed the name in the plugins section.

So, it's working fine now :-). I still think this is an awesome compliment to by site and would love to see the posts version. I have all faith in you that this will happen and I have a category just waiting for it :-)

Feb 13, 2008 22:36

TITSSN, great to hear that it's working. Sorry it took so long.

Afwas, thanks for your help.


Form is loading...

CMS software – This forum is powered by b2evolution CMS, a complete engine for your website.