Laravel controller for a commenting system











up vote
3
down vote

favorite












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 AppHttpControllers;

use AppHttpControllersController;
use IlluminateSupportFacadesDB;
use Cookie;
use Response;
use IlluminateSupportFacadesInput;

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'));
}

}
}









share|improve this question
















bumped to the homepage by Community 8 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    3
    down vote

    favorite












    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 AppHttpControllers;

    use AppHttpControllersController;
    use IlluminateSupportFacadesDB;
    use Cookie;
    use Response;
    use IlluminateSupportFacadesInput;

    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'));
    }

    }
    }









    share|improve this question
















    bumped to the homepage by Community 8 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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 AppHttpControllers;

      use AppHttpControllersController;
      use IlluminateSupportFacadesDB;
      use Cookie;
      use Response;
      use IlluminateSupportFacadesInput;

      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'));
      }

      }
      }









      share|improve this question















      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 AppHttpControllers;

      use AppHttpControllersController;
      use IlluminateSupportFacadesDB;
      use Cookie;
      use Response;
      use IlluminateSupportFacadesInput;

      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'));
      }

      }
      }






      object-oriented mvc ajax php5 laravel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 27 '15 at 17:37









      200_success

      128k15149412




      128k15149412










      asked Feb 27 '15 at 16:54









      Vladimir

      1162




      1162





      bumped to the homepage by Community 8 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 8 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          First of all; your controller does too many things.



          You may need to move your database related queries out of the controller. Repositories maybe? Also use Eloquent it is one of the most powerful way of Laravel.



          methods checkHash, unique, make should not be in the controller.



          Instead of facades you may inject IlluminateHttpRequest into the constructor and use request instead of input facades.



          Move your business logic related functionalities into Services and inject these services into your controller. (method on constructor injection)



          Instead of Response::make(json_encode($abc)) you may use response()->json($abc)



          You need to follow a naming convention for your controller methods, variables and functions.



          You need to indent your code.






          share|improve this answer























            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f82756%2flaravel-controller-for-a-commenting-system%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            First of all; your controller does too many things.



            You may need to move your database related queries out of the controller. Repositories maybe? Also use Eloquent it is one of the most powerful way of Laravel.



            methods checkHash, unique, make should not be in the controller.



            Instead of facades you may inject IlluminateHttpRequest into the constructor and use request instead of input facades.



            Move your business logic related functionalities into Services and inject these services into your controller. (method on constructor injection)



            Instead of Response::make(json_encode($abc)) you may use response()->json($abc)



            You need to follow a naming convention for your controller methods, variables and functions.



            You need to indent your code.






            share|improve this answer



























              up vote
              0
              down vote













              First of all; your controller does too many things.



              You may need to move your database related queries out of the controller. Repositories maybe? Also use Eloquent it is one of the most powerful way of Laravel.



              methods checkHash, unique, make should not be in the controller.



              Instead of facades you may inject IlluminateHttpRequest into the constructor and use request instead of input facades.



              Move your business logic related functionalities into Services and inject these services into your controller. (method on constructor injection)



              Instead of Response::make(json_encode($abc)) you may use response()->json($abc)



              You need to follow a naming convention for your controller methods, variables and functions.



              You need to indent your code.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                First of all; your controller does too many things.



                You may need to move your database related queries out of the controller. Repositories maybe? Also use Eloquent it is one of the most powerful way of Laravel.



                methods checkHash, unique, make should not be in the controller.



                Instead of facades you may inject IlluminateHttpRequest into the constructor and use request instead of input facades.



                Move your business logic related functionalities into Services and inject these services into your controller. (method on constructor injection)



                Instead of Response::make(json_encode($abc)) you may use response()->json($abc)



                You need to follow a naming convention for your controller methods, variables and functions.



                You need to indent your code.






                share|improve this answer














                First of all; your controller does too many things.



                You may need to move your database related queries out of the controller. Repositories maybe? Also use Eloquent it is one of the most powerful way of Laravel.



                methods checkHash, unique, make should not be in the controller.



                Instead of facades you may inject IlluminateHttpRequest into the constructor and use request instead of input facades.



                Move your business logic related functionalities into Services and inject these services into your controller. (method on constructor injection)



                Instead of Response::make(json_encode($abc)) you may use response()->json($abc)



                You need to follow a naming convention for your controller methods, variables and functions.



                You need to indent your code.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 16 at 23:53









                Sᴀᴍ Onᴇᴌᴀ

                8,12861751




                8,12861751










                answered Jan 16 at 23:31









                Bold P.

                111




                111






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f82756%2flaravel-controller-for-a-commenting-system%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'