sNews Ajax Polls mod now available

OK folks, here it is, the initial release of my sNews Polls mod for sNews 1.7. Ready to get started? Good, it should only take about 5-10 minutes for the average sNews user to install. Go ahead and download the mod zip file;


VERSION history
[1.0.3] - fixed show_poll function to display when called directly via PHP for people NOT using the cache mod
[1.0.2] - prepared show_poll function to work with caching mod
[1.0.1] - fixed missing DB prefix
[1.0.0] - initial release

Step 1) Now extract the archive and familiarize yourself with the files, you should have a folder titled mod_poll, with all the relevant content inside. You'll need to edit a few items now, open poll_process.php and modify the language file name (if not English) and database settings;

include_once('../lang/EN.php');

$server = 'localhost';
$dbuser = 'database_username';
$dbpass = 'database_password';
$dbname = 'database_name';
$dbprefix = '';

Now save the poll_process.php file and close it.

Step 2) Now use the poll.sql file and phpMyAdmin (or your preferred DB tool) to add the database tables. You can delete the poll.sql file if you like now, once the tables have been added.

Step 3) Now upload the entire mod_poll folder and contents to the SAME directory as your snews.php, like in this example;

/home/mysite/public_html/snews.php
/home/mysite/public_html/mod_poll/jscolor/<jscolor files>
/home/mysite/public_html/mod_poll/delete.png
/home/mysite/public_html/mod_poll/loading.gif
/home/mysite/public_html/mod_poll/poll.gif
/home/mysite/public_html/mod_poll/poll.js
/home/mysite/public_html/mod_poll/poll_admin.php
/home/mysite/public_html/mod_poll/poll_functions.php
/home/mysite/public_html/mod_poll/poll_process.php
/home/mysite/public_html/mod_poll/poll_styles.css

Step 4) Next we need to gather the sNews files needed to pull the whole thing together... unfortunately sNews has no plug-in hooks at all, so we have to get our hands a little dirty, but I have done my best to make this part as easy as possible for folks to do. So create a new folder on your desktop titled SNEWS-POLL, now download the following files into the folder;

index.php
snews.php
lang/EN.php (or your local language file)
js/admin.js

Now copy the SNEWS-POLL folder and save that copy as SNEWS-POLL.orig, now you have a back-up, if your site breaks, just upload the contents of the .orig folder and that will recover your site.

Step 5) Let's edit the index.php file now, make sure you're working in the SNEWS-POLL folder, not the back-up ;)... now add the following just BELOW the snews.php include in the document head;

<?php include("mod_poll/poll_functions.php"); ?>

then add the following just below the title() function call in the document head;

<script type="text/javascript" src="mod_poll/poll.js"></script>
<link rel="stylesheet" href="mod_poll/poll_styles.css" type="text/css" />

Save and close index.php

Step 6) Open EN.php (or whatever language file you use) and add the following code;

    # Ajax Polls mod
    $l['polls'] = 'Manage polls';
    $l['snews_polls'] = 'Manage Ajax polls';
    $l['admin_poll'] = 'Edit Ajax poll';
    $l['poll_not_found'] = 'Error, poll does not exist';
    $l['poll_reset_msg'] = 'All votes will be removed, this will NOT remove cookies set for people who may have already voted, are you sure you want to reset total to 0?';
    $l['poll_delete_msg'] = 'The poll and all votes will be removed, are you sure you want to delete?';
    $l['js_poll1'] = 'Enter the poll ID.';
    $l['poll'] = 'Poll';
    $l['poll_value'] = 'Add Poll';
    $l['poll_question'] = 'Add your poll question here.';
    $l['poll_create'] = 'Create a new poll';
    $l['poll_manage'] = 'Manage current polls';
    $l['no_polls'] = 'No polls created yet.';
    $l['poll_overview_question'] = 'Poll question';
    $l['poll_overview_color'] = 'Poll bar color';
    $l['poll_id'] = 'ID';
    $l['poll_edit_id'] = 'Edit poll ID #';
    $l['poll_answers'] = 'Poll answers';
    $l['poll_add_answer'] = 'Add another answer';
    $l['poll_save'] = 'Save';
    $l['poll_delete'] = 'Delete';
    $l['poll_reset'] = 'Reset votes';
    $l['poll_embed'] = 'Embed codes'; 
    $l['poll_embed_snews'] = 'sNews code:';
    $l['poll_embed_php'] = 'PHP code:';
    $l['poll_preview'] = 'Preview poll';
    $l['poll_votes_singular'] = 'vote';
    $l['poll_votes_plural'] = 'votes';
    $l['poll_total_votes'] = 'Total votes: ';
    $l['poll_back_to'] = 'Back to poll';
    $l['poll_vote_button'] = 'Vote';
    $l['poll_view_results'] = 'View current results';

Save and close EN.php

Step 7) Open your admin.js file, add the poll button code between the func and include button code, highlighted below;

// function use end
case 'poll':// adding function insertion url = prompt("<?php echo l('js_poll1'); ?>", ''); start = url !=null ? '[func]show_poll:|:'+url+',0[\/func]' : ''; end = ''; break;
case 'include':

Save and close admin.js

Step 8) Open snews.php and add the two hard-coded pages to the $l['cat_listSEF'], like highlighted below (scroll over if necessary, it's there);

if (_ADMIN) {$l['cat_listSEF'] .= ',administration,admin_category,admin_article,admin_poll,snews_polls,article_new,extra_new,page_new,snews_categories,

now find the function center and add the following highlighted code;

case 'snews_files':
    files(); return; break;
case 'admin_poll': polls('edit'); return; break; case 'snews_polls': polls('home'); return; break;
case 'process':
    processing(); return; break;

now find the function administration and add the following highlighted code;

echo '<p><a href="snews_settings/">'.l('settings').'</a></p>
    <p><a href="snews_files/">'.l('files').'</a></p>
    <p><a href="snews_polls/">'.l('polls').'</a></p>

now find the function button, and insert the highlighted code to make the article poll button;

$insert = array('img', 'link', 'include', 'func','intro','poll');

and lastly, add the following function to the bottom of your snews.php file, just above the closing php tag;

// Ajax POLLS
function polls($option) {
    if (file_exists('mod_poll/poll_admin.php')) {
        include_once('mod_poll/poll_admin.php');
        do_polls($option);
    } else {
        echo "<h2>Oops!</h2><p>I couldn't find the mod file!</p>";
    }
}

Step 9) Now upload your index.php, snews.php, admin.js, and EN.php files and you should be all set. Log-in to your admin interface and you should have the option to manage polls now. Enjoy!

A couple of things to note, if you're deleting a poll answer that has already been voted on, those votes remain, unless you reset all votes. Your best bet is to make sure the poll is the way you want it before making it publicly available, or edit the answers, you can always add new answers too. To add a poll to an article, simply click the embed sNews code box and copy the code into your article.

As usual, this stuff is provided without warranty, use at your own risk, etc. If I missed a step or something doesn't appear correct, please let me know as soon as possible and I'll do what I can to fixes any issues.

Share this post:
Share of Facebook Tweet this post Share this post on Google+ Email this post


Comments

RSS Comments Feed


Sven - Philippe's Avatar

Sven - Philippe

I just put: setlocale(LC_ALL, 'fr_FR.utf8'); with no results.

Matt's Avatar

Matt

Try adding this just above the other header calls in poll_process.php
header("Content-Type: text/html; charset=utf-8");

Sven - Philippe's Avatar

Sven - Philippe

By putting header("Content-type: text/html; charset=UTF-8"); it gives : Anouk Aim�nbsp;

Sven - Philippe's Avatar

Sven - Philippe

oops! cross-posting!
It's worst: Maurice Binder a t-il copi� l'id�e de la vision du personnage de James Bond au travers d'un canon de pistolet ?

Matt's Avatar

Matt

Hmmm, what about


header("Content-Type: text/html; charset=iso-8859-1");

Sven - Philippe's Avatar

Sven - Philippe

There it works.
I didn't know we can manage 2 differente encoding within the same page.

Matt's Avatar

Matt

Well, when the show_poll function is called via ajax (an XMLHttpRequest) it's a separate "page" with it's own headers.

As for why UTF-8 encodings for the polls are misbehaving on your site, I'm not sure :/... that's definitely not my strong suit.

Sven - Philippe's Avatar

Sven - Philippe

Okay. Got it.
Thanks to Ajax and thanks to you too!

Matt's Avatar

Matt

Cool, now I have to fix the word filter function to match only whole words, charset should not be censored because "arse" is inside!

lol... I should have that done today

slemborg's Avatar

slemborg

Finally after a few moths, I took the time to test this, and it works flawlessly, I just made a test poll on my site (in danish) but it looks good.


Thanks again for another great mod.

slemborg's Avatar

slemborg

I just had an idea, would it be possible to make a timeframe, I mean, set a time for the poll to end?

Matt's Avatar

Matt

Slemborg, that wouldn't be too difficult to add a close date actually. I'll add it to my growing list of "to-dos" once work slows down a little :)

slemborg's Avatar

slemborg

Damn, looks like I was very tired, cause moth aint what I meant (you surely know) hehe, and also made a typo in my web addy under Website URI: in the comments, ohh well, to much php and not enough sleep, makes you type in strange ways.

Martin's Avatar

Martin

Fantastic mod, works flawlessly on my site. You should be hired to work on the new release of sNews!

Matt's Avatar

Matt

Thanks Martin, I appreciate the feedback! Let me know if you have any problems with this or any other mod.

Pico RG's Avatar

Pico RG

Nice will do. I will test it right now

selo's Avatar

selo

Seems like a great mod, but where is the poll.sql? It doesn't appear to be inside the zip file. Please check it.

Matt's Avatar

Matt

Whoops, looks I left it out of 1.0.2, I have added it back in the zip file, thanks selo!

salut's Avatar

salut

Hello! Sorry for my English. Thank you for the addition to Snews. I did everything as written in the instructions, then created a poll and at his page, put the function call
<?php show_poll (3,0);?>
But the poll is not displayed. What could be the reason?
Thank you.

Matt's Avatar

Matt

Thanks for the note salut. I just realized there is a problem with returning the poll via PHP in version 1.0.2, if you're not using my caching mod.

I have just updated the file, please download <a href="http://mdj.us/media/mod_poll_1.0.3.zip">mod_poll_1.0.3.zip</a>, and replace the poll_functions.php file and you should be all set.

Or you could just make sure to echo the show_poll function when calling via PHP;
&darr;&darr;&darr;&darr;
<?php echo show_poll(3,0); ?>
Sorry about that!

salut's Avatar

salut

Thank you very much

Tatsu's Avatar

Tatsu

Howdy,

once again i have a question. This mod is fantastic! But I have a idea...how could you make, that the mod will run allways at (example index.php below the cat)
That the poll will automaticly update for a new poll, or it would type or say, the poll is closed! VOTING ISNT ALLOWED ANY MORE! how would i make that? :D

tnx matt!

Matt's Avatar

Matt

Hi Tatsu,

If you want to "close" a poll, right now, the only way to do that is to set the "novote" option to 1 when displaying the poll.

Like this;
↓
<?php echo show_poll(3,1); ?>
As for displaying the latest poll, you would have to write a small function to do such. Simply select the poll form the DB with the highest ID number and pass that to the show_poll function.

As for making the poll always show in X location, I use an extra within the admin interface to display on the page/categories I want.

logon's Avatar

logon

I took the time to test this, and it works flawlessly, I just made a test poll on my site (in danish) but it looks good.

Different's Avatar

Different

Hi, Matt!
For UTF-8 you need add mysql_query("SET NAMES 'utf8'") in file poll_process.php after line $conn = mysql_connect($server,$dbuser,$dbpass) or die(mysql_error())



(optional, not publicly displayed) (optional)

Site News

About Matt

Privacy Policy | About Me
Copyright © 2002 - 2012 Matt Jones
HTML5/CSS3
Back to Top Back to top