Can someone review my Controller? I should follow these guidelines:
Code should be written with MVC pattern and to use OOP.
The code now works fine, but I need to improve it. Also, should I use more Ajax Model?
This code is used to receive data from a jQuery script and send it back. Almost every method does the same.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Cookie;
use Response;
use Illuminate\Support\Facades\Input;
class AjaxController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
if(Input::get('task') && Input::get('task') == 'comment_insert'){
$userID = Input::get('userID');
$comment = nl2br(Input::get('comment'));
$name = Input::get('name');
$date = Input::get('date');
if(Input::get('picture')){
$img = Input::get('picture');
}else{
$img = 'http://thecontentwrangler.com/wp-content/uploads/2011/08/User.png';
}
$qv = DB::table('comments')
->insert(array(
'name' => $name,
'comment' => $comment,
'current_date' => $date,
'avatar' => $img
));
$id = DB::getPdo()->lastInsertId();
if($qv){
$out = array('date' => $date,
'userID' => $userID,
'comment' => $comment,
'comment_id' => $id,
'userName' => $name,
'photo_img' => $img);
return Response::make(json_encode($out));
}
}
}
public function reply()
{
if(Input::get('task') && Input::get('task') == 'reply_insert'){
$commentID = Input::get('commentID');
$comment = nl2br(Input::get('comment'));
$name = Input::get('name');
$date = Input::get('date');
$level = Input::get('levelBR');
$order= Input::get('order');
$id_rep = Input::get('id_rep');
$nametorep = Input::get('nametorep');
$level += 1;
if(Input::get('picture')){
$img = Input::get('picture');
}else{
$img = 'http://thecontentwrangler.com/wp-content/uploads/2011/08/User.png';
}
if($order == 'NULL'){
$order = NULL;
}
$qv = DB::table('replays')
->insert(array(
'name' => $name,
'comment' => $comment,
'comment_integer' => $commentID,
'level' => $level,
'nametorep' => $nametorep,
'current_date' => $date,
'order' => $order,
'avatar' => $img
));
$id = DB::getPdo()->lastInsertId();
$out = array(
'date' => $date,
'order' => $order,
'nametorep' => $nametorep,
'commentID' => $commentID,
'comment' => $comment,
'replyID' => $id,
'name' => $name,
'levelBR' => $level,
'photo_img' => $img
);
return Response::make(json_encode($out));
}
}
public function checkHash($table,$colCol,$colRep,$comment,$reply){
if($reply == 'NULL'){
$hashs = DB::select("SELECT * FROM $table WHERE $colRep IS NULL && $colCol =?", array($comment));
}else{
$hashs = DB::select("SELECT * FROM $table WHERE $colRep = ? && $colCol =?", array($reply,$comment ));
}
foreach ($hashs as $h){
if(Cookie::get($h->hash)){
return true;
}
}
}
public static function make($string,$salt=''){
return hash('sha256',$string . $salt);
}
public static function unique(){
return self::make(uniqid());
}
public function upVote(){
if(Input::get('task') && Input::get('task') == true){
$reply = Input::get('reply');
$comment = Input::get('comment');
$vote = Input::get('vote');
$voteCom = Input::get('voteCom');
intval($vote);
intval($voteCom);
if (self::checkHash('hashs','comment_integer','reply_integer',$comment,$reply)) {
$out1= array('voted' => 'You have already voted.');
return json_encode($out1);
} else {
$hash = self::unique();
$cookie = Cookie::forever($hash, 7);
if($reply == 'NULL'){
$reply = NULL;
DB::table('comments')->where(array('id' => $comment ))->increment('vote_up', 1);
}else{
DB::table('replays')->where(array('id' => $reply,'comment_integer' => $comment ))->increment('vote_up', 1);
}
DB::table('hashs')->insert(array('comment_integer' => $comment, 'reply_integer' => $reply,'hash' => $hash));
$vote = $vote + 1;
$voteCom = $voteCom +1;
$out = array('vote' => $vote,'voteCom' => $voteCom);
return Response::make(json_encode($out))-> withCookie($cookie);
}
}
}
public function downVote(){
if(Input::get('task')){
$reply = Input::get('reply');
$comment = Input::get('comment');
$vote = Input::get('vote');
$voteCom = Input::get('voteCom');
intval($vote);
intval($voteCom);
if (self::checkHash('hashs','comment_integer','reply_integer',$comment,$reply)) {
$out1= array('voted' => 'You have already voted.');
return json_encode($out1);
} else {
$hash = self::unique();
$cookie = Cookie::forever($hash, 7);
if($reply == 'NULL'){
$reply = NULL;
DB::table('comments')->where(array('id' => $comment ))->increment('vote_down', 1);
}else{
DB::table('replays')->where(array('id' => $reply,'comment_integer' => $comment ))->increment('vote_down', 1);
}
DB::table('hashs')->insert(array('comment_integer' => $comment, 'reply_integer' => $reply,'hash' => $hash));
$vote = $vote + 1;
$voteCom = $voteCom +1;
$out = array('vote' => $vote,'voteCom' => $voteCom);
return Response::make(json_encode($out))-> withCookie($cookie);
}
}
}
public function report(){
if(Input::get('task')){
$commentID = Input::get('comment');
$replyID =Input::get('reply');
if(self::checkHash('hashreport','comment_report_id','reply_report_id',$commentID,$replyID)){
$out1= array('reported' => 'Comment already reported.');
return json_encode($out1);
} else {
$hash = self::unique();
$cookie = Cookie::forever($hash, 7);
if($replyID == 'NULL'){
$replyID = NULL;
DB::table('comments')->where(array('id' => $commentID ))->increment('report', 1);
}else{
DB::table('replays')->where(array('id' => $replyID,'comment_integer' => $commentID ))->increment('report', 1);
}
DB::table('hashreport')->insert(array('comment_report_id' => $commentID, 'reply_report_id' => $replyID,'hash' => $hash));
$out = array('report' => 'This comment is reported. Thanks!');
return Response::make(json_encode($out))-> withCookie($cookie);
}
}
}
private function getComments($order){
$comments = DB::select("SELECT avatar,name, vote_up,vote_down,report,id,comment,`current_date`,comment_integer,level FROM ( SELECT avatar,name, vote_up,vote_down,report,id,comment,`current_date`,comment_integer,level FROM comments UNION ALL SELECT avatar,name, vote_up,vote_down,report,id,comment,`current_date`,comment_integer,level FROM replays ) T ORDER BY $order DESC");
return $comments;
}
public function bylikes(){
//SELECT name, vote_up FROM ( SELECT name, vote_up FROM comments UNION ALL SELECT name, vote_up FROM replays ) T ORDER BY vote_up DESC
if(Input::get('task')){
return json_encode(self::getComments('vote_up'));
}
}
public function bydiss(){
//SELECT name, vote_up FROM ( SELECT avatar,name, vote_up FROM comments UNION ALL SELECT name, vote_up FROM replays ) T ORDER BY vote_up DESC
if(Input::get('task')){
return json_encode(self::getComments('vote_down'));
}
}
}