toutpratique/modules/cmsps/ajax_voting.php

54 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2015-07-31 11:37:25 +02:00
<?php
require_once '../../config/config.inc.php';
require_once '../../init.php';
require_once dirname(__FILE__).'/classes/CmsPsPost.php';
$id_article = Tools::getValue('id_post');
$cms_post = new CmsPsPost($id_article);
$result = array();
if (Validate::isLoadedObject($cms_post)) {
$result['errors'] = false;
if (!checkCookie($cms_post->id)) {
$cms_post->note = $cms_post->note + 1;
$cms_post->save();
$result['nb_vote'] = $cms_post->note;
$result['already_vote'] = false;
} else {
$result['nb_vote'] = $cms_post->note;
$result['already_vote'] = true;
}
die(Tools::jsonEncode($result));
} else {
$result['errors'] = true;
die(Tools::jsonEncode($result));
}
function checkCookie($id_post) {
if (isset($_COOKIE['voting'])) {
$cookie = utf8_decode($_COOKIE['voting']);
if ($cookie != 0) {
$already_vote = explode(',', $cookie);
if (in_array($id_post, $already_vote)) {
return true;
} else {
$vote = '';
for ($i=0; $i < (sizeof($already_vote)-1) ; $i++) {
2015-08-12 12:04:17 +02:00
$vote.= $already_vote[$i].',';
2015-07-31 11:37:25 +02:00
}
2015-08-12 12:04:17 +02:00
$vote.=$id_post.',';
2015-07-31 11:37:25 +02:00
setcookie("voting", utf8_encode($vote), time()+(60*60*24*300), '/');
return false;
}
} else {
$vote = $id_post.',';
setcookie("voting", utf8_encode($vote), time()+(60*60*24*300), '/');
return false;
}
} else {
setcookie("voting", utf8_encode('0'), time()+(60*60*24*300), '/');
return false;
}
}