sNews Ajax Polls mod now available
filed under: sNews CMS / Hacks & Mods
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.
49 comments
Add a new comment »Categories
Recent Entries
Recent Comments
- Redbeard (I managed to get Vampire: The Masquerade - Bloodlines ...)
- Tina (Installed this today and working like a charm :) Thanks!)
- Tina (Thanks for this great mod :) Working on my site for quite a ...)
- konga (Hi Matt, if you plan to update it, please have a look into ...)
- David (Yo Matt, I have a problem, when I use this mod. ...)
- Dave (Have a Toshiba NB305. Win7 starter would not do screen ...)
- Daichisan (Howdy Matt, I dont really get it, whats ...)
- Matt (Mine is just customized further, that's all :) It just spits ...)
- Matt (David, Do you own or admin the server? Do you have exec ...)
Popular Entries
- Compact archives for sNews 1.7 (5/5)
- Light-weight related articles mod for sNews 1.7 (4.78/5)
- SEF / SEO search for your sNews website (4.75/5)
- 1024x600 netbook wallpapers of Evangeline Lilly (4.67/5)
- An improved tag cloud for sNews 1.7 (4.67/5)
- Gravatar mod for sNews 1.7 (4.67/5)
- Image / math hybrid captcha version 2, vastly improved (4.64/5)
- Related Articles mod for sNews CMS, public beta release (4.6/5)
- An easy mod to create custom break titles for your sNews articles (4.6/5)
May 7th, 2009 at 3:41 pm
Let me be the first to say, WELL DONE (caps was needed) can't wait to try it out.
May 8th, 2009 at 3:31 am
Great MOD Matt!! I wish i use snews 1.7 but i m for now stuck with 1.6..I know people going to use this MOD a lot .. Great :)
May 8th, 2009 at 8:12 am
Thanks guys. Sasha, that's too bad, I remember looking at sNews several years ago, it was like 1.3 then or something, but I didn't start using it until 1.7, so I'm kinda of clueless about the differences with 1.6, though I know there were quite a few changes... enough to break most of the existing mods anyways.
May 12th, 2009 at 8:39 am
Thanks for this great mod!
I installed it on my 1.6 MU version and realized midway the process that it was for 1.7 so I changed the necessary parts and now it works like a charm but it took some more time than the 5-10 minutes you predicted :D
May 12th, 2009 at 8:50 am
Gnilp, that is cool, was it very difficult to change it to work with sNews 1.6?
I've never used 1.6, but if it's not too much work, I will consider installing it on my test machine and back-porting the mod.
Also, I'll update the article to note it's intended for sNews 1.7 :)
May 17th, 2009 at 6:36 pm
I'm a newbie, but really enjoying the ride. I seem to have problems following the language sometimes in the instructions, so I know I've made the mistake.
Anyway, when I click, Create a Poll, I get, "Error Error, poll does not exist."
I got a feeling it's something that I didn't see or clearly understand in the early phpMyAdmin phase.
Am I correct, and if so, how can I solve this?
Peace.
May 17th, 2009 at 8:21 pm
Hi Homer, welcome to the fold. If you're getting that error when creating a poll, then it's definitely a problem with inserting into the database tables.
Do you have a default first poll listed on the manage polls page? If not, then the database tables definitely weren't loaded properly.
Let me know, if you have trouble installing the database tables with the .sql file and phpMyAdmin, then drop me a help request on my contact form and I'll do my best to walk you through it over email or yahoo messenger.
Jun 11th, 2009 at 4:41 am
Everything went well.
Except one thing.
The total vote is empty(Total votes:).
(I made only 1 vote which is correctly registered).
A bug?
Jun 11th, 2009 at 6:55 am
Hi Sven,
Yep, as usual, it's the table prefix, lol :)
Find line 65 of poll_functions.php and and this highlighted bit;
$query = mysql_query("SELECT COUNT(id) AS total_votes FROM "._PRE."poll_answers WHERE pid='".$pid."'");Damn, I thought I had all the prefixes this time too! :)
I will update the package as well.
Jun 11th, 2009 at 7:20 am
Those bloody snoozers and their table prefixes!!!
Now it works!
Thanks Matt.
Jun 11th, 2009 at 7:31 am
lol, I'll learn to add the prefixes from the start one day!
What I usually do is create these mods for my own site here, and then I'll clean them up and install them on my test site before a public release, I'm going to add prefixes on my test site's tables before the next mod.
The mod_poll package has been updated to 1.0.1, no need to update if you changed line 65 in poll_functions.php, there were no other changes.
Jun 11th, 2009 at 8:12 am
Lol too.
But no lol there:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Is it the server or me?
Jun 11th, 2009 at 8:13 am
I think it's me... waitee... waitee...
Jun 11th, 2009 at 8:23 am
Now you can vote for your favorite cheesecake :
http://carnet.hiseo.fr/blog-en-vrac/raquel-ou-ursula-ursula-ou-raquel-/
On my side it doesn't work still.
Jun 11th, 2009 at 8:35 am
Hmmm, Sven, did you set the database settings & prefix in poll_process.php?
Jun 11th, 2009 at 8:38 am
Hum... I made an error with the syntax.
It's now corrected.
Jun 11th, 2009 at 8:40 am
... and, tada! it works!
Really a great Mod Matt!
Once again bravo and thanks!
Jun 11th, 2009 at 8:42 am
Cool, looks good to me, now I just have to choose between Raquel Welch or Ursula Andress, in their primes, not easy! I think the years have been much kinder to Raquel however, she still looks good :)
Jun 11th, 2009 at 8:48 am
Thanks for your vote. I made the same.
Ursula was just a starlette but Raquel was an actress.
It makes a lotta difference.
Jul 6th, 2009 at 6:14 am
uh oh...
a little issue with the accented chars.
If there are some of those **** foreign chars right after the vote there are not displayed:
try it out here: http://carnet.hiseo.fr/qulture/plagiat-ou-simple-coincidence-/
Page has to be refreshed to display it.
Jul 6th, 2009 at 7:01 am
Hmmm, I see what you're saying, though I'm not able to duplicate the problem.
I just created the same poll on my site and it seems to be working fine.
Have a look here; http://mdj.us/polltest/
Is it working for you?
Try adding this just above the other header calls in poll_process.php
header("Content-Type: text/html; charset=utf-8");Jul 6th, 2009 at 7:11 am
Hi Matt...
it's workin for you and not for me. Weid.
Jul 6th, 2009 at 7:30 am
Sven, did you change any encodings with the DB when you set-up sNews?
Jul 6th, 2009 at 7:52 am
No: all is in UTF-8/utf8_unicode_ci
Jul 6th, 2009 at 8:01 am
As it's only when outputting from the ajax called PHP function, it's gotta be something simple.
Did you try adding the extra header yet in poll_process.php?
Jul 6th, 2009 at 8:10 am
I just put: setlocale(LC_ALL, 'fr_FR.utf8'); with no results.
Jul 6th, 2009 at 8:17 am
Try adding this just above the other header calls in poll_process.php
header("Content-Type: text/html; charset=utf-8");Jul 6th, 2009 at 8:18 am
By putting header("Content-type: text/html; charset=UTF-8"); it gives : Anouk Aim�nbsp;
Jul 6th, 2009 at 8:25 am
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 ?
Jul 6th, 2009 at 8:34 am
Hmmm, what about
header("Content-Type: text/html; charset=iso-8859-1");
Jul 6th, 2009 at 8:41 am
There it works.
I didn't know we can manage 2 differente encoding within the same page.
Jul 6th, 2009 at 8:48 am
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.
Jul 6th, 2009 at 8:51 am
Okay. Got it.
Thanks to Ajax and thanks to you too!
Jul 6th, 2009 at 9:46 am
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
Aug 22nd, 2009 at 9:23 pm
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.
Aug 23rd, 2009 at 7:29 am
I just had an idea, would it be possible to make a timeframe, I mean, set a time for the poll to end?
Aug 23rd, 2009 at 10:30 am
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 :)
Aug 23rd, 2009 at 1:14 pm
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.
Nov 17th, 2009 at 3:14 pm
Fantastic mod, works flawlessly on my site. You should be hired to work on the new release of sNews!
Nov 17th, 2009 at 4:59 pm
Thanks Martin, I appreciate the feedback! Let me know if you have any problems with this or any other mod.
Nov 21st, 2009 at 10:52 pm
Nice will do. I will test it right now
Dec 26th, 2009 at 7:06 am
Seems like a great mod, but where is the poll.sql? It doesn't appear to be inside the zip file. Please check it.
Dec 26th, 2009 at 11:17 am
Whoops, looks I left it out of 1.0.2, I have added it back in the zip file, thanks selo!
Feb 3rd, 2010 at 5:25 am
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
But the poll is not displayed. What could be the reason?
Thank you.
Feb 3rd, 2010 at 7:42 am
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 mod_poll_1.0.3.zip, 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;
↓↓↓↓ <?php echo show_poll(3,0); ?>Sorry about that!
Feb 3rd, 2010 at 10:06 am
Thank you very much
Mar 29th, 2010 at 12:25 pm
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!
Mar 29th, 2010 at 12:28 pm
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.
Apr 6th, 2010 at 6:28 am
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.