Besoin d'aide ?

Ordre message conversation

  • Avatar de jeanpierre99
    Membre depuis :
    01/07/2017
    Messages :
    2

    Bonjour voici ou je bloque sur le systeme de messagerie.

    Quand un utlisateur recois un message l'ordre dans la boite de reception est trié par date de création.

    mais si je repond à un message dans x jours, le message ne se met plus dans l'ordre, la boite de réception garde la date de création du premier message et non de la dernière réponse faite.

    moi je voudrai que quand on répond sa met à jour l'ordre dans la boite de réception grâce à la date du dernier message dans la conversation.

    voici mon code :

    <?php
    class MessageController extends BaseController {
    public function manageMessages(){
    $user = User::find(Auth::user()->id);

    $messages=Message::leftjoin('users as u_sender','messages.from','=','u_sender.id')
    ->leftjoin('users as u_receiver','messages.to','=','u_receiver.id')
    ->leftjoin('message_conversation','messages.id','=','message_conversation.message_id');

    $filter = trim(Input::get('filter'));
    if($filter!=''){
    $messages->where('messages.message','LIKE',"%$filter%");
    }

    $messages->where(function ($query) use ($user) {
    $query->orwhere(function ($query_1) use ($user) {
    $query_1->where('to',$user->id)->where('deleted_receiver',0);
    });

    //get messeages sent that has reply
    $allconversation = MessageConversation::select('message_id')->distinct()->orderby('created_at','desc')->get();
    $allconversation = arrayflatten($allconversation->toArray());
    $query->orwhere(function ($query
    ) use ($user,$allconversation) {
    $query_->where('from',$user->id)->where('deleted_sender',0)->whereIn('messages.id',$allconversation);
    });

    });

    $messages = $messages->select(array('messages.','u_sender.username as sender','u_receiver.username as receiver', DB::raw('COUNT(message_conversation.id) as count_message')))
    ->orderby('created_at','desc')
    ->groupby('messages.id')
    ->orderby('message_conversation.created_at','desc')
    ->get();

    $this->getReplyLastest($messages);
    return View::make('dashboard.messages.manage_message',array('messages'=>$messages,'user'=>$user));
    }

    public function manageMessagesSent(){
    $user = User::find(Auth::user()->id);
    $messages=Message::leftjoin('users','messages.to','=','users.id')
    ->leftjoin('message_conversation','messages.id','=','message_conversation.message_id')
    ->where('from',$user->id)
    ->where('deleted_sender',0)
    ->select(array('messages.
    ','users.username as receiver', DB::raw('COUNT(message_conversation.id) as count_message')));
    $filter = trim(Input::get('filter'));
    if($filter!=''){
    $messages->where('messages.message','LIKE',"%$filter%");
    }
    $messages = $messages->groupby('messages.id')
    ->orderby('messages.created_at','desc')
    ->orderby('message_conversation.created_at','desc')
    ->get();

    $this->getReplyLastest($messages);

    return View::make('dashboard.messages.manage_message_sent',array('messages'=>$messages,'user'=>$user));
    }

    public function manageMessagesStarred(){
    $user = User::find(Auth::user()->id);

    $messages=Message::leftjoin('users as u_sender','messages.from','=','u_sender.id')
    ->leftjoin('users as u_receiver','messages.to','=','u_receiver.id')
    ->leftjoin('message_conversation','messages.id','=','message_conversation.message_id');

    $filter = trim(Input::get('filter'));
    if($filter!=''){
    $messages->where('messages.message','LIKE',"%$filter%");
    }
    $messages->where(function ($query) use ($user) {
    $query->orwhere(function ($query_1) use ($user) {
    $query_1->where('to',$user->id)->where('deleted_receiver',0)->where('starreceiver',1);
    });
    $query->orwhere(function ($query
    ) use ($user) {
    $query_->where('from',$user->id)->where('deleted_sender',0)->where('star_sender',1);
    });

    });

    $messages = $messages->select(array('messages.*','u_receiver.username as receiver','u_sender.username as sender', DB::raw('COUNT(message_conversation.id) as count_message')))
    ->groupby('messages.id')
    ->orderby('created_at','desc')
    ->orderby('message_conversation.created_at','desc')
    ->get();
    $this->getReplyLastest($messages);
    return View::make('dashboard.messages.manage_message_starred',array('messages'=>$messages,'user'=>$user));
    }

    public function getReplyLastest(&$messages){
    foreach ($messages as &$message) {
    $message_conversation = MessageConversation::where('message_id',$message->id)
    ->orderby('created_at','desc')->first();
    if(isset($message_conversation->reply)) $message->reply = $message_conversation->reply;
    else $message->reply = '';
    }
    }

    public function doActionSelected(){
    $user = User::find(Auth::user()->id);
    $input = Input::all();
    if(isset($input['message']))
    $messages = $input['message'];
    else $messages = $input['message_all'];

    if(isset($input['mark_unread'])){
    $messages = Message::wherein('id',$messages)->get();
    foreach ($messages as $message) {
    if($message->from == $user->id){
    Message::where('id',$message->id)->where('read_sender',1)->update(array('read_sender'=>0));
    }elseif($message->to == $user->id){
    Message::where('id',$message->id)->where('read_receiver',1)->update(array('read_receiver'=>0));
    }
    }
    }

    if(isset($input['mark_read'])){
    $messages = Message::wherein('id',$messages)->get();
    foreach ($messages as $message) {
    if($message->from == $user->id){
    Message::where('id',$message->id)->where('read_sender',0)->update(array('read_sender'=>1));
    }elseif($message->to == $user->id){
    Message::where('id',$message->id)->where('read_receiver',0)->update(array('read_receiver'=>1));
    }
    }
    }

    if(isset($input['delete'])) {
    $messages = Message::wherein('id',$messages)->get();
    foreach ($messages as $message) {
    if($message->from == $user->id){
    Message::where('id',$message->id)->where('deleted_sender',0)->update(array('deleted_sender'=>1));
    }elseif($message->to == $user->id){
    Message::where('id',$message->id)->where('deleted_receiver',0)->update(array('deleted_receiver'=>1));
    }
    }
    }

    if(isset($input['star_add'])) {
    $messages = Message::wherein('id',$messages)->get();
    foreach ($messages as $message) {
    if($message->from == $user->id){
    Message::where('id',$message->id)->where('star_sender',0)->update(array('star_sender'=>1));
    }elseif($message->to == $user->id){
    Message::where('id',$message->id)->where('star_receiver',0)->update(array('star_receiver'=>1));
    }
    }
    }

    if(isset($input['star_remove'])) {
    $messages = Message::wherein('id',$messages)->get();
    foreach ($messages as $message) {
    if($message->from == $user->id){
    Message::where('id',$message->id)->where('star_sender',1)->update(array('star_sender'=>0));
    }elseif($message->to == $user->id){
    Message::where('id',$message->id)->where('star_receiver',1)->update(array('star_receiver'=>0));
    }
    }
    }

    return Redirect::back()->with('success','<strong>Success!</strong> Bulk action performed.');
    }

    public function createMessages(){
    $to = Input::get('to');
    $params = array();
    if(!empty($to)){
    $user_to = User::find($to);
    $user_to = User::where('username',$to)->first();
    $params['user_to'] = $user_to;
    }
    $user = User::find(Auth::user()->id);
    $captcha = BaseController::createCaptcha();
    $params['captcha'] = $captcha;
    $params['user'] = $user;

    return View::make('dashboard.messages.create_message',$params);
    }

    public function doCreateMessages(){
    $user = User::find(Auth::user()->id);
    $username = Input::get('username');
    $subject = Input::get('subject');
    $text_message = Input::get('message');

    $rules = array(
    'username' => 'required',
    'subject' => 'required',
    'message' => 'required',
    );
    $validator = Validator::make(Input::all(), $rules);
    if ($validator->fails())
    {
    return Redirect::back()->withInput()->withErrors($validator);
    }
    if(Input::get('captcha') != Session::get('captcha_string')){
    $error = "<strong>Error!</strong> Your answer is incorrect!. Please try again";
    return Redirect::back()->withInput()->with('error', $error);
    }

    $user_to = User::where('username',$username)->first();
    if(!isset($user_to->id))
    return Redirect::back()->withInput()->with('error','<strong>Error!</strong> This user could not be found.');
    elseif($user_to->id == $user->id)
    return Redirect::back()->withInput()->with('error','<strong>Error!</strong> You cannot send messages to yourself.');

    $message = new Message();
    $message->from = $user->id;
    $message->to = $user_to->id;
    $message->subject = $subject;
    $message->message = $text_message;
    $message->star_sender = 0;
    $message->star_receiver = 0;
    $message->deleted_sender = 0;
    $message->deleted_receiver = 0;
    $message->read_sender = 1;
    $message->read_receiver = 0;
    $message->save();
    if($message->id){
    $url_message = URL::to('messages').'/view/'.$message->id;
    return Redirect::to('messages')->with('success','<strong>Success!</strong> Message sent. <a href="'.$url_message.'" class="alert-link">View your message.</a>');
    }else return Redirect::back()->with('error','<strong>Error!</strong> Can not send message.');
    }

    public function starOffMessagesInbox($message_id){
    $user = User::find(Auth::user()->id);
    $message = Message::find($message_id);
    if(!isset($message->id) || $message->to != $user->id) return Redirect::back()->with('error','<strong>Error!</strong> Message does not exist.');
    $message->star_receiver = 0;
    if($message->save()) return Redirect::back()->with('success','<strong>Success!</strong> Message was removed star.');
    else return Redirect::back()->with('error','<strong>Error!</strong> There are some errors.');
    }

    public function starOnMessagesInbox($message_id){
    $user = User::find(Auth::user()->id);
    $message = Message::find($message_id);
    if(!isset($message->id) || $message->to != $user->id) return Redirect::back()->with('error','<strong>Error!</strong> Message does not exist.');
    $message->star_receiver = 1;
    if($message->save()) return Redirect::back()->with('success','<strong>Success!</strong> Message was marked star.');
    else return Redirect::back()->with('error','<strong>Error!</strong> There are some errors.');
    }

    public function starOffMessagesSent($message_id){
    $user = User::find(Auth::user()->id);
    $message = Message::find($message_id);
    if(!isset($message->id) || $message->from != $user->id) return Redirect::back()->with('error','<strong>Error!</strong> Message does not exist.');
    $message->star_sender = 0;
    if($message->save()) return Redirect::back()->with('success','<strong>Success!</strong> Message was removed star.');
    else return Redirect::back()->with('error','<strong>Error!</strong> There are some errors.');
    }

    public function starOnMessagesSent($message_id){
    $user = User::find(Auth::user()->id);
    $message = Message::find($message_id);
    if(!isset($message->id) || $message->from != $user->id) return Redirect::back()->with('error','<strong>Error!</strong> Message does not exist.');
    $message->star_sender = 1;
    if($message->save()) return Redirect::back()->with('success','<strong>Success!</strong> Message was marked star.');
    else return Redirect::back()->with('error','<strong>Error!</strong> There are some errors.');
    }

    public function viewMessages($message_id){
    $user = User::find(Auth::user()->id);
    $message=Message::leftjoin('users as u_sender','messages.from','=','u_sender.id')
    ->leftjoin('users as u_receiver','messages.to','=','u_receiver.id')
    ->where('messages.id',$message_id)
    ->select('messages.','u_receiver.username as receiver','u_receiver.id as receiver_id','u_sender.username as sender','u_sender.id as sender_id','u_sender.role as sender_role')->orderby('created_at','desc')
    ->first();
    if(!isset($message->id) || ($message->from != $user->id && $message->to != $user->id)) return Redirect::to('messages')->with('error','<strong>Error!</strong> Message does not exist.');
    $conversations = MessageConversation::leftjoin('users','message_conversation.user_id','=','users.id')->where('message_conversation.message_id',$message->id)->select('users.
    ','message_conversation.*')->orderby('message_conversation.created_at','desc')->get();

    $return_to = Input::get('return_to');
    $text_back = 'Inbox';
    $url_back = URL::to('messages');
    if($return_to == 'sent') {
    $url_back = URL::to('messages/sent');
    $text_back = "Sent";
    }
    elseif($return_to == 'starred') {
    $url_back = URL::to('messages/starred');
    $text_back = "Starred";
    }

    // Set this message to read for the user who read it.
    if ($message->from == $user->id) {
    Message::where('id',$message_id)->update(array('read_sender'=>1));
    } elseif ($message->to == $user->id) {
    Message::where('id',$message_id)->update(array('read_receiver'=>1));
    }
    return View::make('dashboard.messages.view_message',array( 'user'=>$user ,'message'=>$message, 'conversations'=>$conversations,'url_back'=>$url_back,'text_back'=>$text_back));
    }

    public function doReplyMessages($message_id){
    $user = User::find(Auth::user()->id);
    $message_reply = Input::get('message');
    $rules = array('message' =>'required');
    $validator = Validator::make(Input::all(), $rules);
    if ($validator->fails())
    return Redirect::back()->withInput()->withErrors($validator);
    $message_conversation = new MessageConversation();
    $message_conversation->message_id = $message_id;
    $message_conversation->user_id = $user->id;
    $message_conversation->reply = $message_reply;
    $message_conversation->save();
    if($message_conversation->id){
    // Set this message to unread for the other user.
    $message = Message::find($message_id);
    if ($user->id == $message->from) {
    Message::where('id',$message_id)->update(array('read_receiver'=>0));
    Message::where('id',$message_id)->update(array('deleted_receiver'=>0));
    } elseif ($user->id == $message->to) {
    Message::where('id',$message_id)->update(array('read_sender'=>0));
    Message::where('id',$message_id)->update(array('deleted_sender'=>0));
    }
    $url_message = URL::to('messages').'/view/'.$message_id;
    return Redirect::to('messages')->with('success','<strong>Success!</strong> Message sent. <a href="'.$url_message.'" class="alert-link">View your message.</a>');
    }else return Redirect::back()->with('error','<strong>Error!</strong> Can not send message.');
    }
    }
  • Avatar de CoxinetMaroc
    Membre depuis :
    12/07/2017
    Messages :
    2

    lors de la récuperation des message, le tri est fait via "message_conversation.created_at", je pense que ça serait mieux si vous utilisez "messages.created_at", comme ça la derniere conversation en haut

    je travaille aussi sur une application pour moi, est ce que c'est possible de partager avec moi votre code ?
    mon email est coxinet.ma@gmail.com

Vous ne pouvez pas répondre à ce sujet.