Umstellung auf eigene Ajax-Verarbeitung über Route "rateit"

This commit is contained in:
2018-02-15 06:14:26 +01:00
parent 107e32c3be
commit 1dd2979664
8 changed files with 149 additions and 138 deletions

View File

@ -31,7 +31,6 @@
namespace cgoIT\rateit;
use cgoIT\rateit\RateItFrontend;
use SimpleAjax\Event\SimpleAjax;
use Symfony\Component\HttpFoundation\JsonResponse;
class RateIt extends \Frontend {
@ -65,127 +64,108 @@ class RateIt extends \Frontend {
* @param integer id - The id of key to register a rating for.
* @param integer percent - The rating in percentages.
*/
public function doVote(SimpleAjax $event) {
$input = $event->getEnvironment()->getInputProvider();
public function doVote() {
$ip = $_SERVER['REMOTE_ADDR'];
if ((true === $input->hasParameter('do'))
&& ('rateit' === $input->getParameter('do'))
) {
$ip = $_SERVER['REMOTE_ADDR'];
$rkey = $this->Input->post('id');
$percent = $this->Input->post('vote');
$type = $this->Input->post('type');
$rkey = $input->getParameter('id');
$percent = $input->getParameter('vote');
$type = $input->getParameter('type');
//Make sure that the ratable ID is a number and not something crazy.
if (strstr($rkey, '|')) {
$arrRkey = explode('|', $rkey);
foreach ($arrRkey as $key) {
if (!is_numeric($key)) {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['invalid_rating']];
$response = new JsonResponse($return);
$event->setResponse($response);
return $event;
}
$id = $rkey;
}
} else {
if (is_numeric($rkey)) {
$id = $rkey;
} else {
//Make sure that the ratable ID is a number and not something crazy.
if (strstr($rkey, '|')) {
$arrRkey = explode('|', $rkey);
foreach ($arrRkey as $key) {
if (!is_numeric($key)) {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['invalid_rating']];
$response = new JsonResponse($return);
$event->setResponse($response);
return $event;
return new JsonResponse($return);
}
$id = $rkey;
}
//Make sure the percent is a number and under 100.
if (is_numeric($percent) && $percent < 101) {
$rating = $percent;
} else {
if (is_numeric($rkey)) {
$id = $rkey;
} else {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['invalid_rating']];
$response = new JsonResponse($return);
$event->setResponse($response);
return $event;
return new JsonResponse($return);
}
//Make sure that the ratable type is 'page' or 'ce' or 'module'
if (!($type === 'page' || $type === 'article' || $type === 'ce' || $type === 'module' || $type === 'news' || $type === 'faq' || $type === 'galpic' || $type === 'news4ward')) {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['invalid_type']];
$response = new JsonResponse($return);
$event->setResponse($response);
return $event;
}
$strHash = sha1(session_id() . (!$GLOBALS['TL_CONFIG']['disableIpCheck'] ? \Environment::get('ip') : '') . 'FE_USER_AUTH');
// FrontendUser auslesen
if (FE_USER_LOGGED_IN) {
$objUser = $this->Database->prepare("SELECT pid FROM tl_session WHERE hash=?")
->limit(1)
->execute($strHash);
if ($objUser->numRows) {
$userId = $objUser->pid;
}
}
$ratableKeyId = $this->Database->prepare('SELECT id FROM tl_rateit_items WHERE rkey=? and typ=?')
->execute($id, $type)
->fetchAssoc();
$canVote = false;
if (isset($userId)) {
$countUser = $this->Database->prepare('SELECT * FROM tl_rateit_ratings WHERE pid=? and memberid=?')
->execute($ratableKeyId['id'], $userId)
->count();
}
$countIp = $this->Database->prepare('SELECT * FROM tl_rateit_ratings WHERE pid=? and ip_address=?')
->execute($ratableKeyId['id'], $ip)
->count();
// Die with an error if the insert fails (duplicate IP or duplicate member id for a vote).
if ((!$this->allowDuplicatesForMembers && (isset($countUser) ? $countUser == 0 : false)) || ($this->allowDuplicatesForMembers && isset($userId))) {
// Insert the data.
$arrSet = array('pid' => $ratableKeyId['id'],
'tstamp' => time(),
'ip_address' => $ip,
'memberid' => isset($userId) ? $userId : null,
'rating' => $rating,
'createdat'=> time()
);
$this->Database->prepare('INSERT INTO tl_rateit_ratings %s')
->set($arrSet)
->execute();
} elseif (!isset($countUser) && ((!$this->allowDuplicates && $countIp == 0) || $this->allowDuplicates)) {
// Insert the data.
$arrSet = array('pid' => $ratableKeyId['id'],
'tstamp' => time(),
'ip_address' => $ip,
'memberid' => isset($userId) ? $userId : null,
'rating' => $rating,
'createdat'=> time()
);
$this->Database->prepare('INSERT INTO tl_rateit_ratings %s')
->set($arrSet)
->execute();
} else {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['duplicate_vote']];
$response = new JsonResponse($return);
$event->setResponse($response);
return $event;
}
$rating = $this->rateItFrontend->loadRating($id, $type);
$return = [$this->rateItFrontend->getStarMessage($rating)];
$response = new JsonResponse($return);
$event->setResponse($response);
return $event;
}
}
//Make sure the percent is a number and under 100.
if (is_numeric($percent) && $percent < 101) {
$rating = $percent;
} else {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['invalid_rating']];
return new JsonResponse($return);
}
//Make sure that the ratable type is 'page' or 'ce' or 'module'
if (!($type === 'page' || $type === 'article' || $type === 'ce' || $type === 'module' || $type === 'news' || $type === 'faq' || $type === 'galpic' || $type === 'news4ward')) {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['invalid_type']];
return new JsonResponse($return);
}
$strHash = sha1(session_id() . (!$GLOBALS['TL_CONFIG']['disableIpCheck'] ? \Environment::get('ip') : '') . 'FE_USER_AUTH');
// FrontendUser auslesen
if (FE_USER_LOGGED_IN) {
$objUser = $this->Database->prepare("SELECT pid FROM tl_session WHERE hash=?")
->limit(1)
->execute($strHash);
if ($objUser->numRows) {
$userId = $objUser->pid;
}
}
$ratableKeyId = $this->Database->prepare('SELECT id FROM tl_rateit_items WHERE rkey=? and typ=?')
->execute($id, $type)
->fetchAssoc();
$canVote = false;
if (isset($userId)) {
$countUser = $this->Database->prepare('SELECT * FROM tl_rateit_ratings WHERE pid=? and memberid=?')
->execute($ratableKeyId['id'], $userId)
->count();
}
$countIp = $this->Database->prepare('SELECT * FROM tl_rateit_ratings WHERE pid=? and ip_address=?')
->execute($ratableKeyId['id'], $ip)
->count();
// Die with an error if the insert fails (duplicate IP or duplicate member id for a vote).
if ((!$this->allowDuplicatesForMembers && (isset($countUser) ? $countUser == 0 : false)) || ($this->allowDuplicatesForMembers && isset($userId))) {
// Insert the data.
$arrSet = array('pid' => $ratableKeyId['id'],
'tstamp' => time(),
'ip_address' => $ip,
'memberid' => isset($userId) ? $userId : null,
'rating' => $rating,
'createdat'=> time()
);
$this->Database->prepare('INSERT INTO tl_rateit_ratings %s')
->set($arrSet)
->execute();
} elseif (!isset($countUser) && ((!$this->allowDuplicates && $countIp == 0) || $this->allowDuplicates)) {
// Insert the data.
$arrSet = array('pid' => $ratableKeyId['id'],
'tstamp' => time(),
'ip_address' => $ip,
'memberid' => isset($userId) ? $userId : null,
'rating' => $rating,
'createdat'=> time()
);
$this->Database->prepare('INSERT INTO tl_rateit_ratings %s')
->set($arrSet)
->execute();
} else {
$return = [$GLOBALS['TL_LANG']['rateit']['error']['duplicate_vote']];
return new JsonResponse($return);
}
$rating = $this->rateItFrontend->loadRating($id, $type);
$return = [$this->rateItFrontend->getStarMessage($rating)];
return new JsonResponse($return);
}
}
?>