public function sendMessage()
 {
     $encoded_values = Settings::where('key', 'chat')->first();
     $decoded_values = json_decode($encoded_values->value);
     $v_data = ["thread_id" => Input::get('thread_id'), "user_id" => Input::get('user_id'), "message" => Input::get('message'), "attachment" => Input::hasFile('attachment') ? \Str::lower(Input::file('attachment')->getClientOriginalExtension()) : ""];
     $v_rules = ["thread_id" => 'required', "user_id" => 'required', "message" => 'required', "attachment" => 'in:' . $decoded_values->chat_file_types];
     $v = Validator::make($v_data, $v_rules);
     if ($v->passes() && Input::get("user_id") > 0 && Input::get("thread_id") > 0) {
         $thread_message = new ThreadMessages();
         $thread_message->thread_id = Input::get('thread_id');
         $thread_message->sender_id = Input::get('user_id');
         $thread_message->message = Input::get('message');
         $thread_message->save();
         if (Input::hasFile('attachment') && Input::file('attachment')->getSize() <= $decoded_values->max_file_size * 1024 * 1024) {
             $ticket_attachment = new TicketAttachments();
             $ticket_attachment->thread_id = Input::get('thread_id');
             $ticket_attachment->message_id = $thread_message->id;
             $ticket_attachment->has_attachment = Input::hasFile('attachment');
             $ticket_attachment->attachment_path = Input::hasFile('attachment') ? Utils::fileUpload(Input::file('attachment'), 'attachments') : '';
             $ticket_attachment->save();
         }
         return json_encode(["result" => 1]);
     } else {
         return json_encode(["result" => 0]);
     }
 }
 public function convertToTicket($thread_id, $msg_id, $user, $subject, $message, $department_id, $company_id)
 {
     $ticket = new Tickets();
     $ticket->thread_id = $thread_id;
     $ticket->customer_id = $user->id;
     $ticket->priority = Tickets::PRIORITY_MEDIUM;
     $ticket->company_id = $company_id;
     $ticket->department_id = $department_id;
     $ticket->subject = $subject;
     $ticket->description = $message;
     $ticket->status = Tickets::TICKET_NEW;
     $ticket->requested_on = \Carbon\Carbon::now();
     $ticket->save();
     $ticket_attachment = new TicketAttachments();
     $ticket_attachment->thread_id = $thread_id;
     $ticket_attachment->message_id = $msg_id;
     $ticket_attachment->has_attachment = Input::hasFile('attachment');
     $ticket_attachment->attachment_path = Input::hasFile('attachment') ? Utils::fileUpload(Input::file('attachment'), 'attachments') : '';
     $ticket_attachment->save();
     $customer = User::find($ticket->customer_id);
     $ticketMailer = new \KodeInfo\Mailers\TicketsMailer();
     $mailer_extra = Tickets::getCreatedFields(false, $ticket->id, $msg_id);
     $ticketMailer->created($customer->email, $customer->name, $mailer_extra);
 }
 static function getUpdatedFields($is_fake = false, $ticket_id = 0, $msg_id = 0)
 {
     if (!$is_fake) {
         $ticket = Tickets::where('id', $ticket_id)->first();
         $thread_message = ThreadMessages::where('id', $msg_id)->first();
         $ticket_attachment = TicketAttachments::where('message_id', $msg_id)->first();
         $customer = User::where('id', $ticket->customer_id)->first();
         $operator = User::where('id', $ticket->operator_id)->first();
         $company = Company::where('id', $ticket->company_id)->first();
         $department = Department::where('id', $ticket->department_id)->first();
         $receiver = Input::get('user_id') == $ticket->operator_id ? $customer : $operator;
         $mailer_extra = ['ticket_id' => $ticket->id, 'ticket_subject' => $ticket->subject, 'ticket_description' => $ticket->description, 'ticket_status' => $ticket->status, 'ticket_status_txt' => self::resolveStatus($ticket->status), 'ticket_priority' => $ticket->priority, 'ticket_priority_txt' => self::resolveStatus($ticket->priority), 'company_name' => $company->name, 'company_description' => $company->description, 'company_domain' => $company->domain, 'company_logo' => $company->logo, 'department_name' => $department->name, 'has_attachment' => $ticket_attachment->has_attachment, 'attachment_path' => $ticket_attachment->attachment_path, 'updated_message' => $thread_message->message, 'receiver_name' => $receiver->name, 'receiver_email' => $receiver->email];
     } else {
         $mailer_extra = ['ticket_id' => 1, 'ticket_subject' => "How can i use contact us form", 'ticket_description' => "Hi , Sir how can i use contact us form", 'ticket_status' => 1, 'ticket_status_txt' => self::resolveStatus(1), 'ticket_priority' => 1, 'ticket_priority_txt' => self::resolveStatus(1), 'company_name' => "KODEINFO", 'company_description' => "We are a small and dedicated team of designers/developers. This is our web design and development focused blog.We focus on pushing the boundaries of standards based web technologies.", 'company_domain' => "http://www.kodeinfo.com", 'company_logo' => "http://kodeinfo.com/img/shortlogo.png", 'department_name' => "General Queries", 'has_attachment' => false, 'attachment_path' => "", 'updated_message' => "This is a updated message from customer/operator", 'receiver_name' => "Imran", 'receiver_email' => "*****@*****.**"];
     }
     return $mailer_extra;
 }
 public static function getTicketMessages($thread_id, $last_message_id)
 {
     ThreadMessages::where('thread_id', $thread_id)->where('sender_id', 0)->update(['sender_id' => Auth::user()->id]);
     $thread = MessageThread::find($thread_id);
     if ($last_message_id > 0) {
         $messages = ThreadMessages::where('thread_id', $thread_id)->where('id', '>', $last_message_id)->get();
     } else {
         $messages = ThreadMessages::where('thread_id', $thread_id)->get();
     }
     foreach ($messages as $message) {
         $message->user = User::find($message->sender_id);
         $attachment = TicketAttachments::where('message_id', $message->id)->where('thread_id', $thread_id)->first();
         $message->attachment = $attachment;
     }
     $message_str = View::make('tickets.stub-ticket-message', ["messages" => $messages, "thread" => $thread])->render();
     if (sizeof($messages) > 0) {
         $last_message_id = $messages[sizeof($messages) - 1]->id;
     } else {
         $last_message_id = $last_message_id;
     }
     return ["messages_arr" => $messages, 'last_message_id' => $last_message_id, "messages_str" => $message_str];
 }
 public function delete($user_id)
 {
     $tickets = Tickets::where('customer_id', $user_id)->get();
     //Delete tickets
     foreach ($tickets as $ticket) {
         TicketAttachments::where('thread_id', $ticket->id)->delete();
         MessageThread::where('id', $ticket->thread_id)->delete();
         ThreadMessages::where('thread_id', $ticket->thread_id)->delete();
     }
     Tickets::where('customer_id', $user_id)->delete();
     //Delete Chat and Conversations
     $online_users = OnlineUsers::where('user_id', $user_id)->get();
     foreach ($online_users as $online_user) {
         MessageThread::where('id', $online_user->thread_id)->delete();
         ThreadMessages::where('thread_id', $online_user->thread_id)->delete();
     }
     OnlineUsers::where('user_id', $user_id)->delete();
     $closed_conversations = ClosedConversations::where('user_id', $user_id)->get();
     foreach ($closed_conversations as $closed_conversation) {
         MessageThread::where('id', $closed_conversation->thread_id)->delete();
         ThreadMessages::where('thread_id', $closed_conversation->thread_id)->delete();
     }
     ClosedConversations::where('user_id', $user_id)->delete();
     UsersGroups::where('user_id', $user_id)->delete();
     CompanyCustomers::where("customer_id", $user_id)->delete();
     User::where("id", $user_id)->delete();
     RecentActivities::createActivity("Customer deleted by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
     Session::flash('success_msg', trans('msgs.customer_deleted_success'));
     return Redirect::to('/customers/all');
 }
 public function delete($company_id)
 {
     $departments = Department::where('company_id', $company_id)->get();
     if (Config::get('site-config.is_demo') && $company_id == 1) {
         Session::flash('error_msg', 'Demo : Feature is disabled');
         return Redirect::to('/dashboard');
     }
     foreach ($departments as $department) {
         if (!empty($department)) {
             $tickets = Tickets::where('department_id', $department->id)->get();
             //Delete tickets
             foreach ($tickets as $ticket) {
                 TicketAttachments::where('thread_id', $ticket->id)->delete();
                 MessageThread::where('id', $ticket->thread_id)->delete();
                 ThreadMessages::where('thread_id', $ticket->thread_id)->delete();
             }
             Tickets::where('department_id', $department->id)->delete();
             //Delete Chat and Conversations
             $online_users = OnlineUsers::where('department_id', $department->id)->get();
             foreach ($online_users as $online_user) {
                 MessageThread::where('id', $online_user->thread_id)->delete();
                 ThreadMessages::where('thread_id', $online_user->thread_id)->delete();
             }
             OnlineUsers::where('department_id', $department->id)->delete();
             $closed_conversations = ClosedConversations::where('department_id', $department->id)->get();
             foreach ($closed_conversations as $closed_conversation) {
                 MessageThread::where('id', $closed_conversation->thread_id)->delete();
                 ThreadMessages::where('thread_id', $closed_conversation->thread_id)->delete();
             }
             ClosedConversations::where('department_id', $department->id)->delete();
             $operators = OperatorsDepartment::where('department_id', $department->id)->lists('user_id');
             if (sizeof($operators) > 0) {
                 User::whereIn('id', $operators)->delete();
                 UsersGroups::whereIn('user_id', $operators)->delete();
             }
             OperatorsDepartment::where('department_id', $department->id)->delete();
             $department_admin = DepartmentAdmins::where('department_id', $department->id)->first();
             if (!empty($department_admin)) {
                 UsersGroups::where('user_id', $department_admin->user_id)->delete();
                 User::where("id", $department_admin->user_id)->delete();
                 CompanyDepartmentAdmins::where("user_id", $department_admin->user_id)->delete();
                 CannedMessages::where('operator_id', $operators)->delete();
             }
         }
         DepartmentAdmins::where('department_id', $department->id)->delete();
         Department::where('id', $department->id)->delete();
     }
     $company = Company::where('id', $company_id)->first();
     RecentActivities::createActivity("Company <a href='/companies/all'>" . $company->name . "</a> deleted by User Name " . Auth::user()->name . " User ID " . Auth::user()->id);
     Company::where('id', $company_id)->delete();
     Session::flash('success_msg', trans('msgs.company_deleted_success'));
     return Redirect::to('/companies/all');
 }
 public function delete($thread_id)
 {
     Tickets::where('thread_id', $thread_id)->delete();
     TicketAttachments::where('thread_id', $thread_id)->delete();
     MessageThread::where('id', $thread_id)->delete();
     ThreadMessages::where('thread_id', $thread_id)->delete();
     ThreadGeoInfo::where('thread_id', $thread_id)->delete();
     RecentActivities::createActivity("Ticket deleted by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
     Session::flash('success_msg', trans('msgs.ticket_deleted_success'));
     return Redirect::to('/tickets/all');
 }