/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, $ticket)
 {
     $this->validate($request, ['response' => 'required']);
     $response = TicketResponse::create(['user_id' => $this->my_id, 'response' => $request->response, 'source' => 'web', 'ticket_id' => $ticket]);
     if (isset($request->attachments)) {
         foreach ($request->attachments as $attachment) {
             // Check that the directory exists
             $uploadPath = storage_path() . '/attachments/' . $ticket;
             $fs = new Filesystem();
             if (!$fs->isDirectory($uploadPath)) {
                 // Create the directory
                 $fs->makeDirectory($uploadPath);
             }
             $attachment->move($uploadPath, $attachment->getClientOriginalName());
             $_attachment = Attachment::create(['user_id' => $this->my_id, 'name' => $attachment->getClientOriginalName(), 'ticket_response_id' => $response->id]);
         }
     }
     $_ticket = Ticket::where('id', $ticket)->first();
     Ticket::where('id', $ticket)->update(['status_id' => $request->status]);
     $this->dispatch(new EmailUpdatedTicket($_ticket, $response));
     return redirect('/ticket-response/' . $ticket);
 }
Beispiel #2
0
 public function response($id, Request $request)
 {
     $ticket = Ticket::find($id);
     if (!$ticket) {
         return redirect('/')->withErrors(config('constants.NA'));
     }
     $files = $request->file('file');
     $file_count = count($files);
     if ($request->hasFile('file') && $file_count > config('config.max_file_upload_at_once')) {
         return redirect()->back()->withErrors('Max ' . config('config.max_file_upload_at_once') . ' files are allowed to upload at once.');
     }
     $uploadcount = 0;
     $uploaded_file = array();
     if ($request->hasFile('file')) {
         foreach ($files as $file) {
             $filename = uniqid();
             $rules = array('file' => 'mimes:' . config('config.allowed_upload_file'));
             $validator = Validator::make(array('file' => $file), $rules);
             if ($validator->passes()) {
                 $destinationPath = 'uploads/response_attachment';
                 $name = $file->getClientOriginalName();
                 $extension = $file->getClientOriginalExtension();
                 $upload_success = $file->move($destinationPath, $filename . "." . $extension);
                 $uploaded_file[] = $filename . '.' . $extension;
                 $uploadcount++;
             }
         }
     }
     if ($file_count > 0 && $uploadcount == 0 && $request->hasFile('file')) {
         return redirect()->back()->withErrors('Only some files are allowed.');
     }
     $ticket_response = new TicketResponse();
     if ($uploadcount > 0) {
         $ticket_response->attachments = implode(',', $uploaded_file);
     }
     $title = $request->input('mail_subject');
     $content = $request->input('response_description');
     $ticket_response->response_description = $content;
     $ticket_response->user_id = Auth::user()->id;
     $ticket_response->ticket_id = $id;
     $ticket_response->set_response_time = $request->input('set_response_time') ? time() : null;
     $ticket_response->set_resolution_time = $request->input('set_resolution_time') ? time() : null;
     $ticket_response->save();
     if ($ticket_response->set_response_time) {
         $ticket->responded = 1;
     }
     if ($ticket_response->set_resolution_time) {
         $ticket->resolved = 1;
     }
     $ticket->ticket_status = $request->input('ticket_status');
     $ticket->save();
     if ($request->input('send_mail')) {
         Mail::send('template.mail', compact('content'), function ($message) use($ticket, $title) {
             $message->to($ticket->User->email)->subject($title);
         });
     }
     return redirect()->back()->withSuccess('Ticket reply sent.');
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $config = Config::get('mail');
     switch ($config['encryption']) {
         case 'ssl':
             $connection_string = '{' . $config['host'] . ':993/imap/ssl}INBOX';
             break;
         case 'tls':
             $connection_string = '{' . $config['host'] . ':143/imap/tls}INBOX';
             break;
         case null:
         default:
             $connection_string = '{' . $config['host'] . ':143/imap}INBOX';
             break;
     }
     $this->info($connection_string);
     $tmp_attachment_path = storage_path() . '/attachments/tmp';
     $mailbox = new Mailbox($connection_string, $config['username'], $config['password'], $tmp_attachment_path);
     if (!$mailbox) {
         $this->error('Unable to connect to the IMAP server');
     }
     $mailIds = $mailbox->searchMailbox('UNSEEN');
     $this->info('Found ' . count($mailIds) . ' new emails to check');
     if (!empty($mailIds)) {
         foreach ($mailIds as $mailId) {
             $msg = $mailbox->getMail($mailId);
             $subject = str_replace(array('Re: ', 'RE: '), '', $msg->subject);
             $email = $msg->fromAddress;
             if ($msg->textHtml == '') {
                 $response = $msg->textPlain;
                 $response = str_replace("\n\r", '<br>', $response);
                 $response = str_replace("\n", '<br>', $response);
                 $response = '<p>' . $response . '</p>';
             } else {
                 $response = $msg->textHtml;
             }
             $_subjectParts = explode(' - ', $subject);
             if (count($_subjectParts) > 1) {
                 $_subjectParts = explode('[', $_subjectParts[1]);
                 $this->info('Email details:');
                 $this->info('Subject: ' . $subject);
                 $this->info('Email : ' . $email);
                 $this->info('TrackID : ' . $_subjectParts[0]);
                 // Find the ticket
                 $ticket = Ticket::where('track_id', $_subjectParts[0])->first();
                 // Find the user
                 $user = User::where('email', $email)->first();
                 if (count($ticket) > 0 || count($user) > 0) {
                     $this->info('Email associated with ticket ' . $ticket->id);
                     $this->info('Email associated with user ' . $user->first_name . ' ' . $user->last_name);
                     $uploadPath = storage_path() . '/attachments/' . $ticket->id;
                     $response = TicketResponse::create(['user_id' => $user->id, 'response' => $response, 'source' => 'email', 'ticket_id' => $ticket->id]);
                     $attachments = $msg->getAttachments();
                     if (count($attachments) > 0) {
                         $fs = new Filesystem();
                         if (!$fs->isDirectory($uploadPath)) {
                             $fs->makeDirectory($uploadPath);
                         }
                     }
                     foreach ($attachments as $attachment) {
                         $tmpPath = $attachment->filePath;
                         $fileName = $attachment->name;
                         $this->info('Trying to move file from ' . $tmpPath . ' to ' . $uploadPath . '/' . $fileName);
                         File::move($tmpPath, $uploadPath . '/' . $fileName);
                         Attachment::create(['user_id' => $user->id, 'name' => $fileName, 'ticket_response_id' => $response->id]);
                     }
                     $this->dispatch(new EmailUpdatedTicket($ticket, $response));
                 } else {
                     $this->info('Ticket or User not found');
                 }
             } else {
                 $this->info('Not an email for us');
             }
         }
     }
 }