/**
  * reply
  * @param type $value 
  * @return type view
  */
 public function reply($id, Request $request)
 {
     $comment = $request->input('comment');
     if ($comment != null) {
         $tickets = Tickets::where('id', '=', $id)->first();
         $tickets->closed_at = null;
         $tickets->closed = 0;
         $tickets->reopened_at = date('Y-m-d H:i:s');
         $tickets->reopened = 1;
         $threads = new Ticket_Thread();
         $threads->user_id = Auth::user()->id;
         $threads->ticket_id = $tickets->id;
         $threads->poster = "client";
         $threads->body = $comment;
         try {
             $threads->save();
             $tickets->save();
             return \Redirect::back()->with('success1', 'Successfully replied');
         } catch (Exception $e) {
             return \Redirect::back()->with('fails1', $e->errorInfo[2]);
         }
     } else {
         return \Redirect::back()->with('fails1', 'Please fill some data!');
     }
 }
 /**
  * reply
  * @param type $value 
  * @return type view
  */
 public function reply($id, Request $request)
 {
     $comment = $request->input('comment');
     if ($comment != null) {
         $tickets = Tickets::where('id', '=', $id)->first();
         $threads = new Ticket_Thread();
         $threads->user_id = Auth::user()->id;
         $threads->ticket_id = $tickets->id;
         $threads->poster = "client";
         $threads->body = $comment;
         $threads->save();
         return \Redirect::back()->with('success1', 'Successfully replied');
     } else {
         return \Redirect::back()->with('fails1', 'Please fill some data!');
     }
 }
 /**
  * lock
  * @param type $id 
  * @return type null
  */
 public function lock($id)
 {
     $ticket = Tickets::where('id', '=', $id)->first();
     $ticket->lock_by = Auth::user()->id;
     $ticket->lock_at = date('Y-m-d H:i:s');
     $ticket->save();
 }
 /**
  * Post Check ticket
  * @param type CheckTicket $request
  * @param type User $user
  * @param type Tickets $ticket
  * @param type Ticket_Thread $thread
  * @return type Response
  */
 public function PostCheckTicket()
 {
     $Email = \Input::get('email');
     $Ticket_number = \Input::get('ticket_number');
     $ticket = Tickets::where('ticket_number', '=', $Ticket_number)->first();
     if ($ticket == null) {
         return \Redirect::route('form')->with('fails', 'There is no such Ticket Number');
     } else {
         $userId = $ticket->user_id;
         $user = User::where('id', '=', $userId)->first();
         if ($user->role == 'user') {
             $username = $user->user_name;
         } else {
             $username = $user->first_name . " " . $user->last_name;
         }
         if ($user->email != $Email) {
             return \Redirect::route('form')->with('fails', "Email didn't match with Ticket Number");
         } else {
             $code = $ticket->id;
             $code = \Crypt::encrypt($code);
             $company = $this->company();
             \Mail::send('emails.check_ticket', array('link' => \URL::route('check_ticket', $code), 'user' => $username, 'from' => $company), function ($message) use($user, $username, $Ticket_number) {
                 $message->to($user->email, $username)->subject('Ticket link Request [' . $Ticket_number . ']');
             });
             return \Redirect::back()->with('success', 'We have sent you a link by Email. Please click on that link to view ticket');
         }
     }
 }
 /**
  * Reademails
  * @return type
  */
 public function readmails(Emails $emails, Email $settings_email, System $system)
 {
     // $path_url = $system->first()->url;
     if ($settings_email->first()->email_fetching == 1) {
         if ($settings_email->first()->all_emails == 1) {
             // $helptopic = $this->TicketController->default_helptopic();
             // $sla = $this->TicketController->default_sla();
             $email = $emails->get();
             foreach ($email as $e_mail) {
                 $helptopic = $e_mail->help_topic;
                 $get_helptopic = Help_topic::where('id', '=', $helptopic)->first();
                 $sla = $get_helptopic->sla_plan;
                 $dept = $e_mail->department;
                 $host = $e_mail->fetching_host;
                 $port = $e_mail->fetching_port;
                 $protocol = $e_mail->mailbox_protocol;
                 $get_mailboxprotocol = MailboxProtocol::where('id', '=', $protocol)->first();
                 $protocol = $get_mailboxprotocol->value;
                 $imap_config = '{' . $host . ':' . $port . $protocol . '}INBOX';
                 $password = Crypt::decrypt($e_mail->password);
                 $mailbox = new ImapMailbox($imap_config, $e_mail->email_address, $password, __DIR__);
                 $mails = array();
                 $mailsIds = $mailbox->searchMailBox('SINCE ' . date('d-M-Y', strtotime("-1 day")));
                 if (!$mailsIds) {
                     die('Mailbox is empty');
                 }
                 foreach ($mailsIds as $mailId) {
                     $overview = $mailbox->get_overview($mailId);
                     $var = $overview[0]->seen ? 'read' : 'unread';
                     if ($var == 'unread') {
                         $mail = $mailbox->getMail($mailId);
                         if ($settings_email->first()->email_collaborator == 1) {
                             $collaborator = $mail->cc;
                         } else {
                             $collaborator = null;
                         }
                         $body = $mail->textHtml;
                         if ($body == null) {
                             $body = $mailbox->backup_getmail($mailId);
                             $body = str_replace('\\r\\n', '<br/>', $body);
                             // var_dump($body);
                         }
                         $date = $mail->date;
                         $datetime = $overview[0]->date;
                         $date_time = explode(" ", $datetime);
                         $date = $date_time[1] . "-" . $date_time[2] . "-" . $date_time[3] . " " . $date_time[4];
                         $date = date('Y-m-d H:i:s', strtotime($date));
                         // dd($date);
                         if (isset($mail->subject)) {
                             $subject = $mail->subject;
                         } else {
                             $subject = "No Subject";
                         }
                         // dd($subject);
                         $fromname = $mail->fromName;
                         $fromaddress = $mail->fromAddress;
                         $ticket_source = Ticket_source::where('name', '=', 'email')->first();
                         $source = $ticket_source->id;
                         $phone = "";
                         $priority = $get_helptopic->priority;
                         // Ticket_Priority::where('')
                         $assign = $get_helptopic->auto_assign;
                         $form_data = null;
                         $result = $this->TicketController->create_user($fromaddress, $fromname, $subject, $body, $phone, $helptopic, $sla, $priority, $source, $collaborator, $dept, $assign, $form_data);
                         // dd($result);
                         if ($result[1] == true) {
                             $ticket_table = Tickets::where('ticket_number', '=', $result[0])->first();
                             $thread_id = Ticket_Thread::where('ticket_id', '=', $ticket_table->id)->max('id');
                             // $thread_id = Ticket_Thread::whereRaw('id = (select max(`id`) from ticket_thread)')->first();
                             $thread_id = $thread_id;
                             foreach ($mail->getAttachments() as $attachment) {
                                 $support = "support";
                                 // echo $_SERVER['DOCUMENT_ROOT'];
                                 $dir_img_paths = __DIR__;
                                 $dir_img_path = explode('/code', $dir_img_paths);
                                 // dd($attachment->filePath);
                                 $filepath = explode('../../../../../public', $attachment->filePath);
                                 // var_dump($attachment->filePath);
                                 // dd($filepath);
                                 // $path = $dir_img_path[0]."/code/public/".$filepath[1];
                                 $path = public_path() . $filepath[1];
                                 // dd($path);
                                 $filesize = filesize($path);
                                 $file_data = file_get_contents($path);
                                 $ext = pathinfo($attachment->filePath, PATHINFO_EXTENSION);
                                 $imageid = $attachment->id;
                                 $string = str_replace('-', '', $attachment->name);
                                 $filename = explode('src', $attachment->filePath);
                                 $filename = str_replace('\\', '', $filename);
                                 $body = str_replace("cid:" . $imageid, $filepath[1], $body);
                                 $pos = strpos($body, $filepath[1]);
                                 if ($pos == false) {
                                     if ($settings_email->first()->attachment == 1) {
                                         $upload = new Ticket_attachments();
                                         $upload->file = $file_data;
                                         $upload->thread_id = $thread_id;
                                         $upload->name = $filepath[1];
                                         $upload->type = $ext;
                                         $upload->size = $filesize;
                                         $upload->poster = "ATTACHMENT";
                                         $upload->save();
                                     }
                                 } else {
                                     $upload = new Ticket_attachments();
                                     $upload->file = $file_data;
                                     $upload->thread_id = $thread_id;
                                     $upload->name = $filepath[1];
                                     $upload->type = $ext;
                                     $upload->size = $filesize;
                                     $upload->poster = "INLINE";
                                     $upload->save();
                                 }
                                 unlink($path);
                             }
                             $body = Encoding::fixUTF8($body);
                             $thread = Ticket_Thread::where('id', '=', $thread_id)->first();
                             $thread->body = $this->separate_reply($body);
                             $thread->save();
                         }
                     }
                 }
             }
         }
     }
 }
 public function ratingReply($id, $rating)
 {
     Tickets::where('id', $id)->update(array('ratingreply' => $rating));
     return redirect()->back()->with('Success', 'Thank you for your rating!');
 }
 /**
  * function to assign ticket
  * @param type $id
  * @return type bool
  */
 public function assign($id)
 {
     try {
         $UserEmail = Input::get('user');
         //dd($id);
         // $UserEmail = '*****@*****.**';
         $user = User::where('email', '=', $UserEmail)->first();
         $user_id = $user->id;
         $ticket = Tickets::where('id', '=', $id)->first();
         $ticket_number = $ticket->ticket_number;
         $ticket->assigned_to = $user_id;
         $ticket->save();
         $ticket_thread = Ticket_Thread::where('ticket_id', '=', $id)->first();
         $ticket_subject = $ticket_thread->title;
         $thread = new Ticket_Thread();
         $thread->ticket_id = $ticket->id;
         $thread->user_id = Auth::user()->id;
         $thread->is_internal = 1;
         $thread->body = "This Ticket has been assigned to " . $user->first_name . " " . $user->last_name;
         $thread->save();
         $company = $this->company();
         $system = $this->system();
         $agent = $user->first_name;
         $agent_email = $user->email;
         $master = Auth::user()->first_name . " " . Auth::user()->last_name;
         if (Alert::first()->internal_status == 1 || Alert::first()->internal_assigned_agent == 1) {
             // ticket assigned send mail
             Mail::send('emails.Ticket_assign', ['agent' => $agent, 'ticket_number' => $ticket_number, 'from' => $company, 'master' => $master, 'system' => $system], function ($message) use($agent_email, $agent, $ticket_number, $ticket_subject) {
                 $message->to($agent_email, $agent)->subject($ticket_subject . '[#' . $ticket_number . ']');
             });
         }
         return 1;
     } catch (\Exception $e) {
         return $e->getMessage();
     }
 }