54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
<?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++) {
|
|
$vote.= $already_vote[$i].',';
|
|
}
|
|
$vote.=$id_post.',';
|
|
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;
|
|
}
|
|
}
|