From b0b5bd8637c1a9287a01c3e9adda45b3c8b82dd2 Mon Sep 17 00:00:00 2001 From: Vijay Sebastian Date: Thu, 27 Jul 2017 16:38:21 +0530 Subject: [PATCH] api updates --- .../Agent/helpdesk/TicketController.php | 27 +- app/Http/Controllers/Api/v1/ApiController.php | 1067 +++++++++++------ .../Controllers/Api/v1/TicketController.php | 551 +++++---- .../Api/v1/TokenAuthController.php | 2 +- app/Http/Controllers/Auth/AuthController.php | 502 +++++--- app/Http/routes.php | 4 +- app/Model/helpdesk/Utility/Priority.php | 2 +- 7 files changed, 1363 insertions(+), 792 deletions(-) diff --git a/app/Http/Controllers/Agent/helpdesk/TicketController.php b/app/Http/Controllers/Agent/helpdesk/TicketController.php index 82245c848..732837cc6 100644 --- a/app/Http/Controllers/Agent/helpdesk/TicketController.php +++ b/app/Http/Controllers/Agent/helpdesk/TicketController.php @@ -284,29 +284,34 @@ class TicketController extends Controller if ($result[1]) { $status = $this->checkUserVerificationStatus(); if ($status == 1) { - if ($api != false) { - return Lang::get('lang.Ticket-created-successfully'); + if ($api != false) + { + $ticket = Tickets::where('ticket_number', '=', $result[0])->select('id')->first(); + return ['ticket_id' => $ticket->id, 'message' => Lang::get('lang.Ticket-created-successfully')]; } return Redirect('newticket')->with('success', Lang::get('lang.Ticket-created-successfully')); } else { - if ($api != false) { - return Lang::get('lang.Ticket-created-successfully'); + if ($api != false) + { + return response()->json(['success' => Lang::get('lang.Ticket-created-successfully')]); } return Redirect('newticket')->with('success', Lang::get('lang.Ticket-created-successfully2')); } } else { - if ($api != false) { - return Lang::get('lang.failed-to-create-user-tcket-as-mobile-has-been-taken'); + if ($api != false) + { + return response()->json(['error' => Lang::get('lang.failed-to-create-user-tcket-as-mobile-has-been-taken')], 500); } return Redirect('newticket')->with('fails', Lang::get('lang.failed-to-create-user-tcket-as-mobile-has-been-taken'))->withInput($request->except('password')); } } catch (Exception $e) { - // dd($e); - if ($api != false) { - return $e->getMessage(); + dd($e); + if ($api != false) + { + return response()->json(['error' => $e->getMessage()], 500); } return Redirect()->back()->with('fails', '
  • '.$e->getMessage().'
  • '); @@ -879,7 +884,7 @@ class TicketController extends Controller } $set_mails = []; - if (Alert::first()->ticket_status == 1 || Alert::first()->ticket_admin_email == 1) { + if (Alert::first() && (Alert::first()->ticket_status == 1 || Alert::first()->ticket_admin_email == 1)) { // send email to admin $admins = User::where('role', '=', 'admin')->get(); foreach ($admins as $admin) { @@ -891,7 +896,7 @@ class TicketController extends Controller } if ($is_reply == 0) { - if (Alert::first()->ticket_status == 1 || Alert::first()->ticket_department_member == 1) { + if (Alert::first() && (Alert::first()->ticket_status == 1 || Alert::first()->ticket_department_member == 1)) { // send email to agents $agents = User::where('role', '=', 'agent')->get(); foreach ($agents as $agent) { diff --git a/app/Http/Controllers/Api/v1/ApiController.php b/app/Http/Controllers/Api/v1/ApiController.php index 6878c510c..fa69a36b7 100644 --- a/app/Http/Controllers/Api/v1/ApiController.php +++ b/app/Http/Controllers/Api/v1/ApiController.php @@ -36,6 +36,7 @@ use Illuminate\Support\Collection; */ class ApiController extends Controller { + public $user; public $request; public $ticket; @@ -61,53 +62,60 @@ class ApiController extends Controller $this->middleware('jwt.auth'); $this->middleware('api', ['except' => 'GenerateApiKey']); - try { - $user = \JWTAuth::parseToken()->authenticate(); + try + { + $user = \JWTAuth::parseToken()->authenticate(); $this->user = $user; - } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) { - } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) { + } + catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) + { + + } + catch (\Tymon\JWTAuth\Exceptions\JWTException $e) + { + } - $ticket = new TicketController(); + $ticket = new TicketController(); $this->ticket = $ticket; - $model = new Tickets(); + $model = new Tickets(); $this->model = $model; - $thread = new Ticket_Thread(); + $thread = new Ticket_Thread(); $this->thread = $thread; - $attach = new Ticket_attachments(); + $attach = new Ticket_attachments(); $this->attach = $attach; - $ticketRequest = new TicketRequest(); + $ticketRequest = new TicketRequest(); $this->ticketRequest = $ticketRequest; - $faveoUser = new User(); + $faveoUser = new User(); $this->faveoUser = $faveoUser; - $faveoUser = new User(); + $faveoUser = new User(); $this->user = $faveoUser; - $team = new Teams(); + $team = new Teams(); $this->team = $team; - $setting = new System(); + $setting = new System(); $this->setting = $setting; - $helptopic = new Help_topic(); + $helptopic = new Help_topic(); $this->helptopic = $helptopic; - $slaPlan = new Sla_plan(); + $slaPlan = new Sla_plan(); $this->slaPlan = $slaPlan; - $priority = new Priority(); + $priority = new Priority(); $this->priority = $priority; - $department = new Department(); + $department = new Department(); $this->department = $department; - $source = new Ticket_source(); + $source = new Ticket_source(); $this->source = $source; } @@ -122,33 +130,37 @@ class ApiController extends Controller */ public function createTicket(\App\Http\Requests\helpdesk\CreateTicketRequest $request, \App\Model\helpdesk\Utility\CountryCode $code) { - try { + try + { $user_id = $this->request->input('user_id'); - $subject = $this->request->input('subject'); - $body = $this->request->input('body'); + $subject = $this->request->input('subject'); + $body = $this->request->input('body'); $helptopic = $this->request->input('helptopic'); - $sla = $this->request->input('sla'); - $priority = $this->request->input('priority'); - $header = $this->request->input('cc'); - $dept = $this->request->input('dept'); + $sla = $this->request->input('sla'); + $priority = $this->request->input('priority'); + $header = $this->request->input('cc'); + $dept = $this->request->input('dept'); - $assignto = $this->request->input('assignto'); + $assignto = $this->request->input('assignto'); $form_data = $this->request->input('form_data'); - $source = $this->request->input('source'); - $attach = $this->request->input('attachments'); - $headers = []; - if ($header) { + $source = $this->request->input('source'); + $attach = $this->request->input('attachments'); + $headers = []; + if ($header) + { $headers = explode(',', $header); } //return $headers; /* * return s ticket number */ - $PhpMailController = new \App\Http\Controllers\Common\PhpMailController(); + $PhpMailController = new \App\Http\Controllers\Common\PhpMailController(); $NotificationController = new \App\Http\Controllers\Common\NotificationController(); - $core = new CoreTicketController($PhpMailController, $NotificationController); - $response = $core->post_newticket($request, $code, true); + $core = new CoreTicketController($PhpMailController, $NotificationController); + $this->request->merge(['body' => preg_replace('/[ ](?=[^>]*(?:<|$))/', ' ', nl2br($this->request->get('body')))]); + $request->replace($this->request->except('token', 'api_key')); + $response = $core->post_newticket($request, $code, true); //$response = $this->ticket->createTicket($user_id, $subject, $body, $helptopic, $sla, $priority, $source, $headers, $dept, $assignto, $form_data, $attach); //return $response; /* @@ -158,13 +170,17 @@ class ApiController extends Controller //$result = $this->thread->where('id', $response)->first(); //$result = $this->attach($result->id,$file); return response()->json(compact('response')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')) @@ -182,12 +198,14 @@ class ApiController extends Controller public function ticketReply() { //dd($this->request->all()); - try { + try + { $v = \Validator::make($this->request->all(), [ 'ticket_ID' => 'required|exists:tickets,id', 'reply_content' => 'required', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); @@ -196,16 +214,21 @@ class ApiController extends Controller $result = $this->ticket->reply($this->thread, $this->request, $this->attach, $attach); $result = $result->join('users', 'ticket_thread.user_id', '=', 'users.id') ->select('ticket_thread.*', 'users.first_name as first_name') + ->orderBy('ticket_thread.id', 'desc') ->first(); return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -219,7 +242,8 @@ class ApiController extends Controller */ public function editTicket() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'ticket_id' => 'required|exists:tickets,id', 'subject' => 'required', @@ -228,22 +252,25 @@ class ApiController extends Controller 'ticket_source' => 'required|exists:ticket_source,id', 'ticket_priority' => 'required|exists:ticket_priority,priority_id', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $ticket_id = $this->request->input('ticket_id'); - $result = $this->ticket->ticketEditPost($ticket_id, $this->thread, $this->model); - - return response()->json(compact('result')); - } catch (\Exception $e) { + return $this->ticket->ticketEditPost($ticket_id, $this->thread, $this->model); + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -257,27 +284,33 @@ class ApiController extends Controller */ public function deleteTicket() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'ticket_id' => 'required|exists:tickets,id', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $id = $this->request->input('ticket_id'); - $result = $this->ticket->delete($id, $this->model); + $result = $this->ticket->delete(explode(',', $id), $this->model); return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -291,20 +324,23 @@ class ApiController extends Controller */ public function openedTickets() { - try { + try + { // $result = $this->model->where('status', '=', 1)->where('isanswered', '=', 0)->where('assigned_to', '=', null)->orderBy('id', 'DESC')->get(); // return response()->json(compact('result')); - $result = $this->user->join('tickets', function ($join) { - $join->on('users.id', '=', 'tickets.user_id') + $result = $this->user->join('tickets', function ($join) + { + $join->on('users.id', '=', 'tickets.user_id') ->where('isanswered', '=', 0)->where('status', '=', 1)->whereNull('assigned_to'); - }) + }) ->join('department', 'department.id', '=', 'tickets.dept_id') ->join('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') ->join('sla_plan', 'sla_plan.id', '=', 'tickets.sla') ->join('help_topic', 'help_topic.id', '=', 'tickets.help_topic_id') ->join('ticket_status', 'ticket_status.id', '=', 'tickets.status') - ->join('ticket_thread', function ($join) { + ->join('ticket_thread', function ($join) + { $join->on('tickets.id', '=', 'ticket_thread.ticket_id') ->whereNotNull('title'); }) @@ -316,13 +352,17 @@ class ApiController extends Controller ->toJson(); return $result; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -336,30 +376,38 @@ class ApiController extends Controller */ public function unassignedTickets() { - try { + try + { //dd('sdhjbc'); // $result = $this->model->where('assigned_to', '=', null)->where('status', '1')->orderBy('id', 'DESC')->get(); // return response()->json(compact('result')); - $user = \JWTAuth::parseToken()->authenticate(); - $unassigned = $this->user->join('tickets', function ($join) { - $join->on('users.id', '=', 'tickets.user_id') + $user = \JWTAuth::parseToken()->authenticate(); + $unassigned = $this->user->join('tickets', function ($join) + { + $join->on('users.id', '=', 'tickets.user_id') ->whereNull('assigned_to')->where('status', '=', 1); - }) + }) ->join('department', 'department.id', '=', 'tickets.dept_id') ->join('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') ->join('sla_plan', 'sla_plan.id', '=', 'tickets.sla') ->join('help_topic', 'help_topic.id', '=', 'tickets.help_topic_id') ->join('ticket_status', 'ticket_status.id', '=', 'tickets.status') - ->join('ticket_thread', function ($join) { - $join->on('tickets.id', '=', 'ticket_thread.ticket_id') - ->whereNotNull('title'); - }) - ->select(\DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'title', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') - ->where(function ($query) use ($user) { - if ($user->role != 'admin') { - $query->where('tickets.dept_id', '=', $user->primary_dpt); - } + ->leftJoin('ticket_thread', function ($join) + { + $join->on('tickets.id', '=', 'ticket_thread.ticket_id'); }) + ->leftJoin('ticket_attachment', 'ticket_attachment.thread_id', '=', 'ticket_thread.id'); + if ($user->role == 'agent') + { + $id = $user->id; + $dept = \DB::table('department_assign_agents')->where('agent_id', '=', $id)->pluck('department_id')->toArray(); + $unassigned = $unassigned->where(function ($query) use ($dept, $id) + { + $query->whereIn('tickets.dept_id', $dept) + ->orWhere('assigned_to', '=', $id); + }); + } + $unassigned = $unassigned->select('ticket_priority.priority_color as priority_color', \DB::raw('substring_index(group_concat(ticket_thread.title order by ticket_thread.id asc) , ",", 1) as title'), 'tickets.duedate as overdue_date', \DB::raw('count(ticket_attachment.id) as attachment'), \DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') ->orderBy('updated_at', 'desc') ->groupby('tickets.id') ->distinct() @@ -367,13 +415,17 @@ class ApiController extends Controller ->toJson(); return $unassigned; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -387,29 +439,37 @@ class ApiController extends Controller */ public function closeTickets() { - try { + try + { // $result = $this->model->where('status', '>', 1)->where('status', '<', 4)->orderBy('id', 'DESC')->get(); // return response()->json(compact('result')); - $user = \JWTAuth::parseToken()->authenticate(); - $result = $this->user->join('tickets', function ($join) { - $join->on('users.id', '=', 'tickets.user_id') + $user = \JWTAuth::parseToken()->authenticate(); + $result = $this->user->join('tickets', function ($join) + { + $join->on('users.id', '=', 'tickets.user_id') ->where('status', '=', 3)->orWhere('status', '=', 2); - }) + }) ->join('department', 'department.id', '=', 'tickets.dept_id') ->join('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') ->join('sla_plan', 'sla_plan.id', '=', 'tickets.sla') ->join('help_topic', 'help_topic.id', '=', 'tickets.help_topic_id') ->join('ticket_status', 'ticket_status.id', '=', 'tickets.status') - ->join('ticket_thread', function ($join) { - $join->on('tickets.id', '=', 'ticket_thread.ticket_id') - ->whereNotNull('title'); - }) - ->select(\DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'title', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') - ->where(function ($query) use ($user) { - if ($user->role != 'admin') { - $query->where('tickets.dept_id', '=', $user->primary_dpt); - } + ->leftJoin('ticket_thread', function ($join) + { + $join->on('tickets.id', '=', 'ticket_thread.ticket_id'); }) + ->leftJoin('ticket_attachment', 'ticket_attachment.thread_id', '=', 'ticket_thread.id'); + if ($user->role == 'agent') + { + $id = $user->id; + $dept = \DB::table('department_assign_agents')->where('agent_id', '=', $id)->pluck('department_id')->toArray(); + $result = $result->where(function ($query) use ($dept, $id) + { + $query->whereIn('tickets.dept_id', $dept) + ->orWhere('assigned_to', '=', $id); + }); + } + $result = $result->select('tickets.duedate as overdue_date', 'ticket_priority.priority_color as priority_color', \DB::raw('substring_index(group_concat(ticket_thread.title order by ticket_thread.id asc) , ",", 1) as title'), \DB::raw('count(ticket_attachment.id) as attachment'), \DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') ->orderBy('updated_at', 'desc') ->groupby('tickets.id') ->distinct() @@ -417,13 +477,17 @@ class ApiController extends Controller ->toJson(); return $result; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -437,17 +501,22 @@ class ApiController extends Controller */ public function getAgents() { - try { + try + { $result = $this->faveoUser->where('role', 'agent')->orWhere('role', 'admin')->where('active', 1)->get(); return response()->json(compact('result')); - } catch (Exception $e) { + } + catch (Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -461,17 +530,22 @@ class ApiController extends Controller */ public function getTeams() { - try { + try + { $result = $this->team->get(); return response()->json(compact('result')); - } catch (Exception $e) { + } + catch (Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -485,32 +559,41 @@ class ApiController extends Controller */ public function assignTicket() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'ticket_id' => 'required', 'user' => 'required', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } - $id = $this->request->input('ticket_id'); + $id = $this->request->input('ticket_id'); $response = $this->ticket->assign($id); - if ($response == 1) { + if ($response == 1) + { $result = 'success'; return response()->json(compact('result')); - } else { + } + else + { return response()->json(compact('response')); } - } catch (Exception $e) { + } + catch (Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -524,27 +607,33 @@ class ApiController extends Controller */ public function getCustomers() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'search' => 'required', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $search = $this->request->input('search'); - $result = $this->faveoUser->where('first_name', 'like', '%'.$search.'%')->orWhere('last_name', 'like', '%'.$search.'%')->orWhere('user_name', 'like', '%'.$search.'%')->orWhere('email', 'like', '%'.$search.'%')->get(); + $result = $this->faveoUser->where('first_name', 'like', '%' . $search . '%')->orWhere('last_name', 'like', '%' . $search . '%')->orWhere('user_name', 'like', '%' . $search . '%')->orWhere('email', 'like', '%' . $search . '%')->get(); return response()->json(compact('result')) ->header('X-Header-One', 'Header Value'); - } catch (Exception $e) { + } + catch (Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')) @@ -559,24 +648,29 @@ class ApiController extends Controller */ public function getCustomersWith() { - try { + try + { $users = $this->user ->leftJoin('user_assign_organization', 'user_assign_organization.user_id', '=', 'users.id') ->leftJoin('organization', 'organization.id', '=', 'user_assign_organization.org_id') - ->where('role', 'user') - ->select('users.id', 'user_name', 'first_name', 'last_name', 'email', 'phone_number', 'users.profile_pic', 'organization.name AS company', 'users.active') + ->where('users.role', 'user') + ->select('users.id', 'users.user_name', 'users.first_name', 'users.last_name', 'users.email', 'users.phone_number', 'users.profile_pic', 'organization.name AS company', 'users.active', 'users.ext as telephone_extension', 'users.mobile', 'users.phone_number as telephone', 'users.country_code as mobile_code') ->paginate(10) ->toJson(); //dd($users); return $users; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')) @@ -591,26 +685,32 @@ class ApiController extends Controller */ public function getCustomer() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'user_id' => 'required', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } - $id = $this->request->input('user_id'); + $id = $this->request->input('user_id'); $result = $this->faveoUser->where('id', $id)->where('role', 'user')->first(); return response()->json(compact('result')); - } catch (Exception $e) { + } + catch (Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -624,26 +724,32 @@ class ApiController extends Controller */ public function searchTicket() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'search' => 'required', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $search = $this->request->input('search'); - $result = $this->thread->select('ticket_id')->where('title', 'like', '%'.$search.'%')->orWhere('body', 'like', '%'.$search.'%')->get(); + $result = $this->thread->select('ticket_id')->where('title', 'like', '%' . $search . '%')->orWhere('body', 'like', '%' . $search . '%')->get(); return response()->json(compact('result')); - } catch (Exception $e) { + } + catch (Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -657,31 +763,38 @@ class ApiController extends Controller */ public function ticketThreads() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'id' => 'required', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } - $id = $this->request->input('id'); + $id = $this->request->input('id'); $result = $this->user - ->leftjoin('ticket_thread', 'ticket_thread.user_id', '=', 'users.id') + ->rightjoin('ticket_thread', 'ticket_thread.user_id', '=', 'users.id') ->select('ticket_thread.id', 'ticket_id', 'user_id', 'poster', 'source', 'title', 'body', 'is_internal', 'format', 'ip_address', 'ticket_thread.created_at', 'ticket_thread.updated_at', 'users.first_name', 'users.last_name', 'users.user_name', 'users.email', 'users.profile_pic') ->where('ticket_id', $id) + ->orderBy('ticket_thread.id', 'desc') ->get() ->toJson(); return $result; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -696,30 +809,37 @@ class ApiController extends Controller public function checkUrl() { //dd($this->request); - try { + try + { $v = \Validator::make($this->request->all(), [ 'url' => 'required|url', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $url = $this->request->input('url'); - if (!str_is('*/', $url)) { + if (!str_is('*/', $url)) + { $url = str_finish($url, '/'); } - $url = $url.'/api/v1/helpdesk/check-url?api_key='.$this->request->input('api_key').'&token='.\Config::get('app.token'); + $url = $url . '/api/v1/helpdesk/check-url?api_key=' . $this->request->input('api_key') . '&token=' . \Config::get('app.token'); $result = $this->CallGetApi($url); //dd($result); return response()->json(compact('result')); - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { $error = $e->getMessage(); return response()->json(compact('error')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -745,15 +865,16 @@ class ApiController extends Controller */ public function callGetApi($url) { - $curl = curl_init($url); + $curl = curl_init($url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); - if (curl_errno($curl)) { - echo 'error:'.curl_error($curl); + if (curl_errno($curl)) + { + echo 'error:' . curl_error($curl); } return $response; @@ -770,7 +891,7 @@ class ApiController extends Controller */ public function callPostApi($url, $data) { - $curl = curl_init($url); + $curl = curl_init($url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); @@ -778,8 +899,9 @@ class ApiController extends Controller curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $response = curl_exec($curl); - if (curl_errno($curl)) { - echo 'error:'.curl_error($curl); + if (curl_errno($curl)) + { + echo 'error:' . curl_error($curl); } return $response; @@ -793,28 +915,36 @@ class ApiController extends Controller */ public function generateApiKey() { - try { + try + { $set = $this->setting->where('id', '1')->first(); //dd($set); - if ($set->api_enable == 1) { - $key = str_random(32); + if ($set->api_enable == 1) + { + $key = str_random(32); $set->api_key = $key; $set->save(); - $result = $set->api_key; + $result = $set->api_key; return response()->json(compact('result')); - } else { + } + else + { $result = 'please enable api'; return response()->json(compact('result')); } - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -828,17 +958,22 @@ class ApiController extends Controller */ public function getHelpTopic() { - try { + try + { $result = $this->helptopic->get(); return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -852,17 +987,22 @@ class ApiController extends Controller */ public function getSlaPlan() { - try { + try + { $result = $this->slaPlan->get(); return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -876,17 +1016,22 @@ class ApiController extends Controller */ public function getPriority() { - try { - $result = $this->priority->get(); + try + { + $result = $this->priority->select('priority as name', 'priority_id as id')->get(); return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -900,17 +1045,22 @@ class ApiController extends Controller */ public function getDepartment() { - try { + try + { $result = $this->department->get(); return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -924,18 +1074,23 @@ class ApiController extends Controller */ public function getTickets() { - try { + try + { $tickets = $this->model->orderBy('created_at', 'desc')->paginate(10); $tickets->toJson(); return $tickets; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -949,27 +1104,36 @@ class ApiController extends Controller */ public function inbox() { - try { - $user = \JWTAuth::parseToken()->authenticate(); - $inbox = $this->user->join('tickets', function ($join) { - $join->on('users.id', '=', 'tickets.user_id') + try + { + $user = \JWTAuth::parseToken()->authenticate(); + $inbox = $this->user->join('tickets', function ($join) + { + $join->on('users.id', '=', 'tickets.user_id') ->where('status', '=', 1); - }) + }) ->join('department', 'department.id', '=', 'tickets.dept_id') ->join('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') ->join('sla_plan', 'sla_plan.id', '=', 'tickets.sla') ->join('help_topic', 'help_topic.id', '=', 'tickets.help_topic_id') ->join('ticket_status', 'ticket_status.id', '=', 'tickets.status') - ->join('ticket_thread', function ($join) { - $join->on('tickets.id', '=', 'ticket_thread.ticket_id') - ->whereNotNull('ticket_thread.title'); - }) - ->select(\DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'ticket_thread.title', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name', 'department.id as department_id', 'users.primary_dpt as user_dpt') - ->where(function ($query) use ($user) { - if ($user->role != 'admin') { - $query->where('tickets.dept_id', '=', $user->primary_dpt); - } + ->leftJoin('ticket_thread', function ($join) + { + $join->on('tickets.id', '=', 'ticket_thread.ticket_id'); }) + ->leftJoin('ticket_attachment', 'ticket_attachment.thread_id', '=', 'ticket_thread.id') + ->where('ticket_status.name', 'Open'); + if ($user->role == 'agent') + { + $id = $user->id; + $dept = \DB::table('department_assign_agents')->where('agent_id', '=', $id)->pluck('department_id')->toArray(); + $inbox = $inbox->where(function ($query) use ($dept, $id) + { + $query->whereIn('tickets.dept_id', $dept) + ->orWhere('assigned_to', '=', $id); + }); + } + $inbox = $inbox->select(\DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', \DB::raw('substring_index(group_concat(ticket_thread.title order by ticket_thread.id asc) , ",", 1) as title'), 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'ticket_priority.priority_color as priority_color', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name', 'department.id as department_id', 'users.primary_dpt as user_dpt', \DB::raw('count(ticket_attachment.id) as attachment'), 'tickets.duedate as overdue_date') ->orderBy('updated_at', 'desc') ->groupby('tickets.id') ->distinct() @@ -977,13 +1141,17 @@ class ApiController extends Controller ->toJson(); return $inbox; - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { $error = $ex->getMessage(); - $line = $ex->getLine(); - $file = $ex->getFile(); + $line = $ex->getLine(); + $file = $ex->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -997,31 +1165,37 @@ class ApiController extends Controller */ public function internalNote() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'userid' => 'required|exists:users,id', 'ticketid' => 'required|exists:tickets,id', 'body' => 'required', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } - $userid = $this->request->input('userid'); + $userid = $this->request->input('userid'); $ticketid = $this->request->input('ticketid'); - $body = $this->request->input('body'); + $body = preg_replace('/[ ](?=[^>]*(?:<|$))/', ' ', nl2br($this->request->input('body'))); $thread = $this->thread->create(['ticket_id' => $ticketid, 'user_id' => $userid, 'is_internal' => 1, 'body' => $body]); return response()->json(compact('thread')); - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -1030,27 +1204,35 @@ class ApiController extends Controller public function getTrash() { - try { - $user = \JWTAuth::parseToken()->authenticate(); - $trash = $this->user->join('tickets', function ($join) { - $join->on('users.id', '=', 'tickets.user_id') + try + { + $user = \JWTAuth::parseToken()->authenticate(); + $trash = $this->user->join('tickets', function ($join) + { + $join->on('users.id', '=', 'tickets.user_id') ->where('status', '=', 5); - }) + }) ->join('department', 'department.id', '=', 'tickets.dept_id') ->join('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') ->join('sla_plan', 'sla_plan.id', '=', 'tickets.sla') ->join('help_topic', 'help_topic.id', '=', 'tickets.help_topic_id') ->join('ticket_status', 'ticket_status.id', '=', 'tickets.status') - ->join('ticket_thread', function ($join) { - $join->on('tickets.id', '=', 'ticket_thread.ticket_id') - ->whereNotNull('title'); - }) - ->select(\DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'title', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') - ->where(function ($query) use ($user) { - if ($user->role != 'admin') { - $query->where('tickets.dept_id', '=', $user->primary_dpt); - } + ->leftJoin('ticket_thread', function ($join) + { + $join->on('tickets.id', '=', 'ticket_thread.ticket_id'); }) + ->leftJoin('ticket_attachment', 'ticket_attachment.thread_id', '=', 'ticket_thread.id'); + if ($user->role == 'agent') + { + $id = $user->id; + $dept = \DB::table('department_assign_agents')->where('agent_id', '=', $id)->pluck('department_id')->toArray(); + $trash = $trash->where(function ($query) use ($dept, $id) + { + $query->whereIn('tickets.dept_id', $dept) + ->orWhere('assigned_to', '=', $id); + }); + } + $trash = $trash->select('ticket_priority.priority_color as priority_color', \DB::raw('substring_index(group_concat(ticket_thread.title order by ticket_thread.id asc) , ",", 1) as title'), 'tickets.duedate as overdue_date', \DB::raw('count(ticket_attachment.id) as attachment'), \DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') ->orderBy('updated_at', 'desc') ->groupby('tickets.id') ->distinct() @@ -1058,13 +1240,17 @@ class ApiController extends Controller ->toJson(); return $trash; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -1073,43 +1259,46 @@ class ApiController extends Controller public function getMyTicketsAgent() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'user_id' => 'required|exists:users,id', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $id = $this->request->input('user_id'); - if ($this->user->where('id', $id)->first()->role == 'user') { + if ($this->user->where('id', $id)->first()->role == 'user') + { $error = 'This user is not an Agent or Admin'; return response()->json(compact('error')); } //$user = \JWTAuth::parseToken()->authenticate(); - $result = $this->user->join('tickets', function ($join) use ($id) { - $join->on('users.id', '=', 'tickets.assigned_to') + $result = $this->user->join('tickets', function ($join) use ($id) + { + $join->on('users.id', '=', 'tickets.assigned_to') ->where('status', '=', 1); //->where('user_id', '=', $id); - }) + }) + ->join('users as client', 'tickets.user_id', '=', 'client.id') ->join('department', 'department.id', '=', 'tickets.dept_id') ->join('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') ->join('sla_plan', 'sla_plan.id', '=', 'tickets.sla') ->join('help_topic', 'help_topic.id', '=', 'tickets.help_topic_id') ->join('ticket_status', 'ticket_status.id', '=', 'tickets.status') - ->join('ticket_thread', function ($join) { - $join->on('tickets.id', '=', 'ticket_thread.ticket_id') - ->whereNotNull('title'); + ->leftJoin('ticket_thread', function ($join) + { + $join->on('tickets.id', '=', 'ticket_thread.ticket_id'); }) + ->leftJoin('ticket_attachment', 'ticket_attachment.thread_id', '=', 'ticket_thread.id') ->where('users.id', $id) - ->select(\DB::raw('max(ticket_thread.updated_at) as updated_at'), 'user_name', 'first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'title', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') -// ->where(function($query) use($user) { -// if ($user->role != 'admin') { -// $query->where('tickets.dept_id', '=', $user->primary_dpt); -// } -// }) + ->select( + 'ticket_priority.priority_color as priority_color', \DB::raw('substring_index(group_concat(ticket_thread.title order by ticket_thread.id asc) , ",", 1) as title'), 'tickets.duedate as overdue_date', \DB::raw('count(ticket_attachment.id) as attachment'), \DB::raw('max(ticket_thread.updated_at) as updated_at'), 'client.user_name', 'client.first_name', 'client.last_name', 'client.email', 'client.profile_pic', 'ticket_number', 'tickets.id', 'tickets.created_at', 'department.name as department_name', 'ticket_priority.priority as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name' + ) ->orderBy('updated_at', 'desc') ->groupby('tickets.id') ->distinct() @@ -1117,13 +1306,17 @@ class ApiController extends Controller ->toJson(); return $result; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -1132,31 +1325,36 @@ class ApiController extends Controller public function getMyTicketsUser() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'user_id' => 'required|exists:users,id', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $id = $this->request->input('user_id'); - if ($this->user->where('id', $id)->first()->role == 'admin' || $this->user->where('id', $id)->first()->role == 'agent') { + if ($this->user->where('id', $id)->first()->role == 'admin' || $this->user->where('id', $id)->first()->role == 'agent') + { $error = 'This is not a client'; return response()->json(compact('error')); } - $result = $this->user->join('tickets', function ($join) use ($id) { - $join->on('users.id', '=', 'tickets.user_id') + $result = $this->user->join('tickets', function ($join) use ($id) + { + $join->on('users.id', '=', 'tickets.user_id') ->where('user_id', '=', $id); - }) + }) ->join('department', 'department.id', '=', 'tickets.dept_id') ->join('ticket_priority', 'ticket_priority.priority_id', '=', 'tickets.priority_id') ->join('sla_plan', 'sla_plan.id', '=', 'tickets.sla') ->join('help_topic', 'help_topic.id', '=', 'tickets.help_topic_id') ->join('ticket_status', 'ticket_status.id', '=', 'tickets.status') - ->join('ticket_thread', function ($join) { + ->join('ticket_thread', function ($join) + { $join->on('tickets.id', '=', 'ticket_thread.ticket_id') ->whereNotNull('title'); }) @@ -1169,13 +1367,17 @@ class ApiController extends Controller ->toJson(); return $result; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -1184,72 +1386,53 @@ class ApiController extends Controller public function getTicketById() { - try { + try + { $v = \Validator::make($this->request->all(), [ 'id' => 'required|exists:tickets,id', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } $id = $this->request->input('id'); - if (!$this->model->where('id', $id)->first()) { - $error = 'There is no Ticket as ticket id: '.$id; + if (!$this->model->where('id', $id)->first()) + { + $error = 'There is no Ticket as ticket id: ' . $id; return response()->json(compact('error')); } - $query = $this->user->join('tickets', function ($join) use ($id) { + $query = $this->user->join('tickets', function ($join) use ($id) + { $join->on('users.id', '=', 'tickets.user_id') ->where('tickets.id', '=', $id); }); $response = $this->differenciateHelpTopic($query) - ->leftJoin('department', 'tickets.dept_id', '=', 'department.id') - ->leftJoin('ticket_priority', 'tickets.priority_id', '=', 'ticket_priority.priority_id') - ->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id') - ->leftJoin('sla_plan', 'tickets.sla', '=', 'sla_plan.id') - ->leftJoin('ticket_source', 'tickets.source', '=', 'ticket_source.id'); + ->leftJoin('department', 'tickets.dept_id', '=', 'department.id') + ->leftJoin('ticket_priority', 'tickets.priority_id', '=', 'ticket_priority.priority_id') + ->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id') + ->leftJoin('sla_plan', 'tickets.sla', '=', 'sla_plan.id') + ->leftJoin('ticket_source', 'tickets.source', '=', 'ticket_source.id'); //$select = 'users.email','users.user_name','users.first_name','users.last_name','tickets.id','ticket_number','num_sequence','user_id','priority_id','sla','max_open_ticket','captcha','status','lock_by','lock_at','source','isoverdue','reopened','isanswered','is_deleted', 'closed','is_transfer','transfer_at','reopened_at','duedate','closed_at','last_message_at'; $result = $response->addSelect( - 'users.email', - 'users.user_name', - 'users.first_name', - 'users.last_name', - 'tickets.id', - 'ticket_number', - 'user_id', - 'ticket_priority.priority_id', - 'ticket_priority.priority as priority_name', - 'department.name as dept_name', - 'ticket_status.name as status_name', - 'sla_plan.name as sla_name', - 'ticket_source.name as source_name', - 'sla_plan.id as sla', - 'ticket_status.id as status', - 'lock_by', - 'lock_at', - 'ticket_source.id as source', - 'isoverdue', - 'reopened', - 'isanswered', - 'is_deleted', - 'closed', - 'reopened_at', - 'duedate', - 'closed_at', - 'tickets.created_at', - 'tickets.updated_at')->first(); + 'users.email', 'users.user_name', 'users.first_name', 'users.last_name', 'tickets.id', 'ticket_number', 'user_id', 'ticket_priority.priority_id', 'ticket_priority.priority as priority_name', 'department.name as dept_name', 'ticket_status.name as status_name', 'sla_plan.name as sla_name', 'ticket_source.name as source_name', 'sla_plan.id as sla', 'ticket_status.id as status', 'lock_by', 'lock_at', 'ticket_source.id as source', 'isoverdue', 'reopened', 'isanswered', 'is_deleted', 'closed', 'reopened_at', 'duedate', 'closed_at', 'tickets.created_at', 'tickets.updated_at')->first(); return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -1258,7 +1441,8 @@ class ApiController extends Controller public function createPagination($array, $perPage) { - try { + try + { //Get current page form url e.g. &page=6 $currentPage = LengthAwarePaginator::resolveCurrentPage(); @@ -1272,13 +1456,17 @@ class ApiController extends Controller $paginatedResults = new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage); return $paginatedResults; - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (\TokenExpiredException $e) { + } + catch (\TokenExpiredException $e) + { $error = $e->getMessage(); return response()->json(compact('error')); @@ -1288,26 +1476,31 @@ class ApiController extends Controller public function collaboratorSearch() { $this->validate($this->request, ['term' => 'required']); - try { + try + { $emails = $this->ticket->autosearch(); //return $emails; - $user = new User(); - if (count($emails) > 0) { - foreach ($emails as $key => $email) { - $user_model = $user->where('email', $email)->first(); + $user = new User(); + if (count($emails) > 0) + { + foreach ($emails as $key => $email) + { + $user_model = $user->where('email', $email)->first(); //return $user_model; - $users[$key]['name'] = $user_model->first_name.' '.$user_model->last_name; - $users[$key]['email'] = $email; + $users[$key]['name'] = $user_model->first_name . ' ' . $user_model->last_name; + $users[$key]['email'] = $email; $users[$key]['avatar'] = $this->avatarUrl($email); } } //return $users; return response()->json(compact('users')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); } @@ -1315,17 +1508,23 @@ class ApiController extends Controller public function avatarUrl($email) { - try { + try + { $user = new User(); $user = $user->where('email', $email)->first(); - if ($user->profile_pic) { - $url = url('uploads/profilepic/'.$user->profile_pic); - } else { + if ($user->profile_pic) + { + $url = url('uploads/profilepic/' . $user->profile_pic); + } + else + { $url = \Gravatar::src($email); } return $url; - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { //return $ex->getMessage(); throw new \Exception($ex->getMessage()); } @@ -1333,13 +1532,15 @@ class ApiController extends Controller public function addCollaboratorForTicket() { - try { + try + { $v = \Validator::make(\Input::get(), [ 'email' => 'required|email|unique:users', 'ticket_id' => 'required', ] ); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->messages(); return response()->json(compact('error')); @@ -1347,16 +1548,20 @@ class ApiController extends Controller $collaborator = $this->ticket->useradd(); return response()->json(compact('collaborator')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $ex) { + } + catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $ex) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); } @@ -1364,12 +1569,14 @@ class ApiController extends Controller public function getCollaboratorForTicket() { - try { + try + { $v = \Validator::make(\Input::get(), [ 'ticket_id' => 'required', ] ); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->messages(); return response()->json(compact('error')); @@ -1377,16 +1584,20 @@ class ApiController extends Controller $collaborator = $this->ticket->getCollaboratorForTicket(); return response()->json(compact('collaborator')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); - } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $ex) { + } + catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $ex) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); } @@ -1394,13 +1605,15 @@ class ApiController extends Controller public function deleteCollaborator() { - try { + try + { $v = \Validator::make(\Input::get(), [ 'ticketid' => 'required', 'email' => 'required', ] ); - if ($v->fails()) { + if ($v->fails()) + { $result = $v->messages(); return response()->json(compact('result')); @@ -1408,10 +1621,12 @@ class ApiController extends Controller $collaborator = $this->ticket->userremove(); return response()->json(compact('collaborator')); - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); } @@ -1419,23 +1634,26 @@ class ApiController extends Controller public function dependency() { - try { + try + { $department = $this->department->select('name', 'id')->get()->toArray(); - $sla = $this->slaPlan->select('name', 'id')->get()->toArray(); - $staff = $this->user->where('role', 'agent')->select('email', 'id')->get()->toArray(); - $team = $this->team->select('name', 'id')->get()->toArray(); - $priority = \DB::table('ticket_priority')->select('priority', 'priority_id')->get(); - $helptopic = $this->helptopic->select('topic', 'id')->get()->toArray(); - $status = \DB::table('ticket_status')->select('name', 'id')->get(); - $source = \DB::table('ticket_source')->select('name', 'id')->get(); - $result = ['departments' => $department, 'sla' => $sla, 'staffs' => $staff, 'teams' => $team, - 'priorities' => $priority, 'helptopics' => $helptopic, 'status' => $status, 'sources' => $source, ]; + $sla = $this->slaPlan->select('name', 'id')->get()->toArray(); + $staff = $this->user->where('role', 'agent')->select('email', 'id')->get()->toArray(); + $team = $this->team->select('name', 'id')->get()->toArray(); + $priority = \DB::table('ticket_priority')->select('priority', 'priority_id')->get(); + $helptopic = $this->helptopic->select('topic', 'id')->get()->toArray(); + $status = \DB::table('ticket_status')->select('name', 'id')->get(); + $source = \DB::table('ticket_source')->select('name', 'id')->get(); + $result = ['departments' => $department, 'sla' => $sla, 'staffs' => $staff, 'teams' => $team, + 'priorities' => $priority, 'helptopics' => $helptopic, 'status' => $status, 'sources' => $source,]; return response()->json(compact('result')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); - $line = $e->getLine(); - $file = $e->getFile(); + $line = $e->getLine(); + $file = $e->getFile(); return response()->json(compact('error', 'file', 'line')); } @@ -1444,15 +1662,19 @@ class ApiController extends Controller public function differenciateHelpTopic($query) { $ticket = $query->first(); - $check = 'department'; - if ($ticket) { - if ($ticket->dept_id && $ticket->help_topic_id) { + $check = 'department'; + if ($ticket) + { + if ($ticket->dept_id && $ticket->help_topic_id) + { return $this->getSystem($check, $query); } - if (!$ticket->dept_id && $ticket->help_topic_id) { + if (!$ticket->dept_id && $ticket->help_topic_id) + { return $query->select('tickets.help_topic_id'); } - if ($ticket->dept_id && !$ticket->help_topic_id) { + if ($ticket->dept_id && !$ticket->help_topic_id) + { return $query->select('tickets.dept_id'); } } @@ -1462,7 +1684,8 @@ class ApiController extends Controller public function getSystem($check, $query) { - switch ($check) { + switch ($check) + { case 'department': return $query->select('tickets.dept_id'); case 'helpTopic': @@ -1481,36 +1704,94 @@ class ApiController extends Controller */ public function register(Request $request) { - try { + try + { $v = \Validator::make($request->all(), [ 'email' => 'required|email|unique:users', 'password' => 'required|min:6', ]); - if ($v->fails()) { + if ($v->fails()) + { $error = $v->errors(); return response()->json(compact('error')); } - $auth = $this->user; - $email = $request->input('email'); + $auth = $this->user; + $email = $request->input('email'); $username = $request->input('email'); $password = \Hash::make($request->input('password')); - $role = $request->input('role'); - if ($auth->role == 'agent') { + $role = $request->input('role'); + if ($auth->role == 'agent') + { $role = 'user'; } - $user = new User(); - $user->password = $password; + $user = new User(); + $user->password = $password; $user->user_name = $username; - $user->email = $email; - $user->role = $role; + $user->email = $email; + $user->role = $role; $user->save(); return response()->json(compact('user')); - } catch (\Exception $e) { + } + catch (\Exception $e) + { $error = $e->getMessage(); return response()->json(compact('error')); } } + + public function createUser() + { + try + { + $v = \Validator::make( + $this->request->all(), [ + 'username' => 'required|unique:users,user_name', + 'first_name' => 'required', + ] + ); + if ($v->fails()) + { + $error = $v->messages(); + return response()->json(compact('error')); + } + $str = str_random(8); + $array = ['password' => $str, 'password_confirmation' => $str, 'email' => $this->request->input('email'), 'full_name' => $this->request->input('first_name')]; + $all = $this->request->input(); + $merged = $array + $all; + $request = new \App\Http\Requests\helpdesk\RegisterRequest(); + $request->replace($merged); + if ($request->has('username')) + { + $request->merge(['user_name' => $request->get('username')]); + } + \Route::dispatch($request); + $auth = new \App\Http\Controllers\Auth\AuthController(); + $user = new User(); + $register = $auth->postRegister($user, $request, true); + if ($register) + { + return response()->json(compact('register')); + } + } + catch (\Exception $e) + { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + + return response()->json(compact('error', 'file', 'line')); + } + catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) + { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + + return response()->json(compact('error', 'file', 'line')); + } + } + } diff --git a/app/Http/Controllers/Api/v1/TicketController.php b/app/Http/Controllers/Api/v1/TicketController.php index b9e3bfc30..24c5281f5 100644 --- a/app/Http/Controllers/Api/v1/TicketController.php +++ b/app/Http/Controllers/Api/v1/TicketController.php @@ -32,6 +32,7 @@ use Mail; */ class TicketController extends Controller { + /** * Create a new controller instance. * @@ -39,7 +40,7 @@ class TicketController extends Controller */ public function __construct() { - $PhpMailController = new PhpMailController(); + $PhpMailController = new PhpMailController(); $this->PhpMailController = $PhpMailController; } @@ -57,42 +58,51 @@ class TicketController extends Controller */ public function createTicket($user_id, $subject, $body, $helptopic, $sla, $priority, $source, $headers, $dept, $assignto, $form_data, $attach = '') { - try { + try + { //return $headers; $max_number = Tickets::whereRaw('id = (select max(`id`) from tickets)')->first(); //dd($max_number); - if ($max_number == null) { + if ($max_number == null) + { $ticket_number = 'AAAA-9999-9999999'; - } else { - foreach ($max_number as $number) { + } + else + { + foreach ($max_number as $number) + { $ticket_number = $max_number->ticket_number; } } - $ticket = new Tickets(); + $ticket = new Tickets(); $ticket->ticket_number = $this->ticketNumber($ticket_number); //dd($this->ticketNumber($ticket_number)); - $ticket->user_id = $user_id; - $ticket->dept_id = $dept; + $ticket->user_id = $user_id; + $ticket->dept_id = $dept; $ticket->help_topic_id = $helptopic; - $ticket->sla = $sla; - $ticket->assigned_to = $assignto; - $ticket->status = '1'; - $ticket->priority_id = $priority; - $ticket->source = $source; + $ticket->sla = $sla; + $ticket->assigned_to = $assignto; + $ticket->status = '1'; + $ticket->priority_id = $priority; + $ticket->source = $source; $ticket->save(); //dd($ticket); - $ticket_number = $ticket->ticket_number; - $id = $ticket->id; - if ($form_data != null) { + $ticket_number = $ticket->ticket_number; + $id = $ticket->id; + if ($form_data != null) + { $help_topic = Help_topic::where('id', '=', $helptopic)->first(); - $forms = Fields::where('forms_id', '=', $help_topic->custom_form)->get(); - foreach ($form_data as $key => $form_details) { - foreach ($forms as $from) { - if ($from->name == $key) { - $form_value = new Ticket_Form_Data(); + $forms = Fields::where('forms_id', '=', $help_topic->custom_form)->get(); + foreach ($form_data as $key => $form_details) + { + foreach ($forms as $from) + { + if ($from->name == $key) + { + $form_value = new Ticket_Form_Data(); $form_value->ticket_id = $id; - $form_value->title = $from->label; - $form_value->content = $form_details; + $form_value->title = $from->label; + $form_value->content = $form_details; $form_value->save(); } } @@ -102,12 +112,15 @@ class TicketController extends Controller $this->storeCollaborators($headers, $id); $thread = $this->ticketThread($subject, $body, $id, $user_id); - if (!empty($attach)) { + if (!empty($attach)) + { $this->attach($thread, $attach); } return $thread; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -121,44 +134,52 @@ class TicketController extends Controller */ public function storeCollaborators($headers, $id) { - try { + try + { //return $headers; $company = $this->company(); - if (isset($headers)) { - foreach ($headers as $email) { - $name = $email; + if (isset($headers)) + { + foreach ($headers as $email) + { + $name = $email; $email = $email; - if ($this->checkEmail($email) == false) { - $create_user = new User(); + if ($this->checkEmail($email) == false) + { + $create_user = new User(); $create_user->user_name = $name; - $create_user->email = $email; - $create_user->active = 1; - $create_user->role = 'user'; - $password = $this->generateRandomString(); - $create_user->password = Hash::make($password); + $create_user->email = $email; + $create_user->active = 1; + $create_user->role = 'user'; + $password = $this->generateRandomString(); + $create_user->password = Hash::make($password); $create_user->save(); - $user_id = $create_user->id; + $user_id = $create_user->id; // Mail::send('emails.pass', ['password' => $password, 'name' => $name, 'from' => $company, 'emailadd' => $email], function ($message) use ($email, $name) { // $message->to($email, $name)->subject('password'); // }); - $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('1', '0'), $to = ['name' => $name, 'email' => $email], $message = ['subject' => 'password', 'scenario' => 'registration-notification'], $template_variables = ['user' => $name, 'email_address' => $email, 'user_password' => $password]); - } else { - $user = $this->checkEmail($email); + $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('1', '0'), $to = ['name' => $name, 'email' => $email], $message = ['subject' => 'password', 'scenario' => 'registration-notification'], $template_variables = ['user' => $name, 'email_address' => $email, 'user_password' => $password]); + } + else + { + $user = $this->checkEmail($email); $user_id = $user->id; } //return $user_id; - $collaborator_store = new Ticket_Collaborator(); - $collaborator_store->isactive = 1; + $collaborator_store = new Ticket_Collaborator(); + $collaborator_store->isactive = 1; $collaborator_store->ticket_id = $id; - $collaborator_store->user_id = $user_id; - $collaborator_store->role = 'ccc'; + $collaborator_store->user_id = $user_id; + $collaborator_store->role = 'ccc'; $collaborator_store->save(); } } return true; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -175,17 +196,20 @@ class TicketController extends Controller */ public function ticketThread($subject, $body, $id, $user_id) { - try { - $thread = new Ticket_Thread(); - $thread->user_id = $user_id; + try + { + $thread = new Ticket_Thread(); + $thread->user_id = $user_id; $thread->ticket_id = $id; - $thread->poster = 'client'; - $thread->title = $subject; - $thread->body = $body; + $thread->poster = 'client'; + $thread->title = $subject; + $thread->body = $body; $thread->save(); return $thread->id; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -199,20 +223,24 @@ class TicketController extends Controller */ public function ticketNumber($ticket_number) { - try { + try + { //dd($ticket_number); - $number = $ticket_number; - $number = explode('-', $number); + $number = $ticket_number; + $number = explode('-', $number); $number1 = $number[0]; - if ($number1 == 'ZZZZ') { + if ($number1 == 'ZZZZ') + { $number1 = 'AAAA'; } $number2 = $number[1]; - if ($number2 == '9999') { + if ($number2 == '9999') + { $number2 = '0000'; } $number3 = $number[2]; - if ($number3 == '9999999') { + if ($number3 == '9999999') + { $number3 = '0000000'; } $number1++; @@ -220,11 +248,13 @@ class TicketController extends Controller $number3++; $number2 = sprintf('%04s', $number2); $number3 = sprintf('%07s', $number3); - $array = [$number1, $number2, $number3]; - $number = implode('-', $array); + $array = [$number1, $number2, $number3]; + $number = implode('-', $array); return $number; - } catch (\Exception $e) { + } + catch (\Exception $e) + { dd($e); return $e->getMessage(); @@ -240,16 +270,20 @@ class TicketController extends Controller */ public function generateRandomString($length = 10) { - try { - $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + try + { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); - $randomString = ''; - for ($i = 0; $i < $length; $i++) { + $randomString = ''; + for ($i = 0; $i < $length; $i++) + { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -264,64 +298,71 @@ class TicketController extends Controller */ public function reply($thread, $request, $ta, $attach = '') { - try { + try + { $check_attachment = null; - $eventthread = $thread->where('ticket_id', $request->input('ticket_ID'))->first(); + $eventthread = $thread->where('ticket_id', $request->input('ticket_ID'))->first(); //dd($request->input('ticket_ID')); //dd($eventthread); - $eventuserid = $eventthread->user_id; - $emailadd = User::where('id', $eventuserid)->first()->email; + $eventuserid = $eventthread->user_id; + $emailadd = User::where('id', $eventuserid)->first()->email; //dd($emailadd); - $source = $eventthread->source; + $source = $eventthread->source; - $form_data = $request->except('reply_content', 'ticket_ID', 'attachment'); + $form_data = $request->except('reply_content', 'ticket_ID', 'attachment'); \Event::fire(new \App\Events\ClientTicketFormPost($form_data, $emailadd, $source)); //dd('yes'); - $reply_content = $request->input('reply_content'); - $thread->ticket_id = $request->input('ticket_ID'); - $thread->poster = 'support'; - $thread->body = $request->input('reply_content'); - $thread->user_id = Auth::user()->id; - $ticket_id = $request->input('ticket_ID'); - $tickets = Tickets::where('id', '=', $ticket_id)->first(); + $reply_content = $request->input('reply_content'); + $thread->ticket_id = $request->input('ticket_ID'); + $thread->poster = 'support'; + $thread->body = $request->input('reply_content'); + $thread->user_id = Auth::user()->id; + $ticket_id = $request->input('ticket_ID'); + $tickets = Tickets::where('id', '=', $ticket_id)->first(); $tickets->isanswered = '1'; $tickets->save(); $ticket_user = User::where('id', '=', $tickets->user_id)->first(); - if ($tickets->assigned_to == 0) { + if ($tickets->assigned_to == 0) + { $tickets->assigned_to = Auth::user()->id; $tickets->save(); - $thread2 = new Ticket_Thread(); - $thread2->ticket_id = $thread->ticket_id; - $thread2->user_id = Auth::user()->id; + $thread2 = new Ticket_Thread(); + $thread2->ticket_id = $thread->ticket_id; + $thread2->user_id = Auth::user()->id; $thread2->is_internal = 1; - $thread2->body = 'This Ticket have been assigned to '.Auth::user()->first_name.' '.Auth::user()->last_name; + $thread2->body = 'This Ticket have been assigned to ' . Auth::user()->first_name . ' ' . Auth::user()->last_name; $thread2->save(); } - if ($tickets->status > 1) { - $tickets->status = '1'; + if ($tickets->status > 1) + { + $tickets->status = '1'; $tickets->isanswered = '1'; $tickets->save(); } $thread->save(); - if (!empty($attach)) { + if (!empty($attach)) + { $check_attachment = $this->attach($thread->id, $attach); } - $thread1 = Ticket_Thread::where('ticket_id', '=', $ticket_id)->first(); + $thread1 = Ticket_Thread::where('ticket_id', '=', $ticket_id)->first(); $ticket_subject = $thread1->title; - $user_id = $tickets->user_id; - $user = User::where('id', '=', $user_id)->first(); - $email = $user->email; - $user_name = $user->user_name; - $ticket_number = $tickets->ticket_number; - $company = $this->company(); - $username = $ticket_user->user_name; - if (!empty(Auth::user()->agent_sign)) { + $user_id = $tickets->user_id; + $user = User::where('id', '=', $user_id)->first(); + $email = $user->email; + $user_name = $user->user_name; + $ticket_number = $tickets->ticket_number; + $company = $this->company(); + $username = $ticket_user->user_name; + if (!empty(Auth::user()->agent_sign)) + { $agentsign = Auth::user()->agent_sign; - } else { + } + else + { $agentsign = null; } \Event::fire(new \App\Events\FaveoAfterReply($reply_content, $user->phone_number, $request, $tickets)); @@ -341,23 +382,30 @@ class TicketController extends Controller * Getting the subject of the thread */ //dd($eventthread); - try { - $re = $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('0', $tickets->dept_id), $to = ['name' => $user_name, 'email' => $email], $message = ['subject' => $eventthread->title, 'scenario' => 'create-ticket-by-agent', 'body' => $thread->body], $template_variables = ['agent_sign' => Auth::user()->agent_sign, 'ticket_number' => $tickets->number]); + try + { + $re = $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('0', $tickets->dept_id), $to = ['name' => $user_name, 'email' => $email], $message = ['subject' => $eventthread->title, 'scenario' => 'create-ticket-by-agent', 'body' => $thread->body], $template_variables = ['agent_sign' => Auth::user()->agent_sign, 'ticket_number' => $tickets->number]); //dd($re); - } catch (\Exception $e) { - throw new \Exception($e->getMessage()); + } + catch (\Exception $e) + { + //throw new \Exception($e->getMessage()); } $collaborators = Ticket_Collaborator::where('ticket_id', '=', $ticket_id)->get(); - foreach ($collaborators as $collaborator) { + foreach ($collaborators as $collaborator) + { //mail to collaborators $collab_user_id = $collaborator->user_id; $user_id_collab = User::where('id', '=', $collab_user_id)->first(); - $collab_email = $user_id_collab->email; - if ($user_id_collab->role == 'user') { + $collab_email = $user_id_collab->email; + if ($user_id_collab->role == 'user') + { $collab_user_name = $user_id_collab->user_name; - } else { - $collab_user_name = $user_id_collab->first_name.' '.$user_id_collab->last_name; + } + else + { + $collab_user_name = $user_id_collab->first_name . ' ' . $user_id_collab->last_name; } // Mail::send('emails.ticket_re-reply', ['content' => $reply_content, 'ticket_number' => $ticket_number, 'From' => $company, 'name' => $collab_user_name, 'Agent_Signature' => $agentsign], function ($message) use ($collab_email, $collab_user_name, $ticket_number, $ticket_subject, $check_attachment) { // $message->to($collab_email, $collab_user_name)->subject($ticket_subject . '[#' . $ticket_number . ']'); @@ -369,14 +417,20 @@ class TicketController extends Controller // // } // }, true); - try { - $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('0', $ticketdata->dept_id), $to = ['user' => $admin_user, 'email' => $admin_email], $message = ['subject' => $updated_subject, 'body' => $body, 'scenario' => $mail], $template_variables = ['ticket_agent_name' => $admin_user, 'ticket_client_name' => $username, 'ticket_client_email' => $emailadd, 'user' => $admin_user, 'ticket_number' => $ticket_number2, 'email_address' => $emailadd, 'name' => $ticket_creator]); - } catch (\Exception $e) { + try + { + $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('0', $ticketdata->dept_id), $to = ['user' => $admin_user, 'email' => $admin_email], $message = ['subject' => $updated_subject, 'body' => $body, 'scenario' => $mail], $template_variables = ['ticket_agent_name' => $admin_user, 'ticket_client_name' => $username, 'ticket_client_email' => $emailadd, 'user' => $admin_user, 'ticket_number' => $ticket_number2, 'email_address' => $emailadd, 'name' => $ticket_creator]); + } + catch (\Exception $e) + { + } } return $thread; - } catch (\Exception $e) { + } + catch (\Exception $e) + { //dd($e); return $e->getMessage(); } @@ -389,16 +443,22 @@ class TicketController extends Controller */ public function company() { - try { + try + { $company = Company::Where('id', '=', '1')->first(); - if ($company->company_name == null) { + if ($company->company_name == null) + { $company = 'Support Center'; - } else { + } + else + { $company = $company->company_name; } return $company; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -413,24 +473,28 @@ class TicketController extends Controller */ public function ticketEditPost($ticket_id, $thread, $ticket) { - try { + try + { $ticket = $ticket->where('id', '=', $ticket_id)->first(); - $ticket->sla = Input::get('sla_plan'); + $ticket->sla = Input::get('sla_plan'); $ticket->help_topic_id = Input::get('help_topic'); - $ticket->source = Input::get('ticket_source'); - $ticket->priority_id = Input::get('ticket_priority'); - $ticket->status = Input::get('status'); + $ticket->source = Input::get('ticket_source'); + $ticket->priority_id = Input::get('ticket_priority'); + $ticket->status = Input::get('status'); $ticket->save(); - $threads = $thread->where('ticket_id', '=', $ticket_id)->first(); + $threads = $thread->where('ticket_id', '=', $ticket_id)->first(); $threads->title = Input::get('subject'); $threads->save(); - - return $threads; - } catch (\Exception $e) { - return $e->getMessage(); } + catch (\Exception $ex) + { + $result = $ex->getMessage(); + return response()->json(compact('result'), 500); + } + $result = ["success" => "Edited successfully"]; + return response()->json(compact('result')); } /** @@ -442,56 +506,66 @@ class TicketController extends Controller */ public function assign($id) { - try { + try + { $UserEmail = Input::get('user'); //dd($UserEmail); // $UserEmail = 'sujitprasad12@yahoo.in'; - $user = User::where('email', '=', $UserEmail)->first(); - if (!$user) { + $user = User::where('email', '=', $UserEmail)->first(); + if (!$user) + { return ['error' => 'No agent not found']; } $user_id = $user->id; - $ticket = Tickets::where('id', '=', $id)->first(); - if (!$ticket) { + $ticket = Tickets::where('id', '=', $id)->first(); + if (!$ticket) + { return ['error' => 'No ticket not found']; } - $ticket_number = $ticket->ticket_number; + $ticket_number = $ticket->ticket_number; $ticket->assigned_to = $user_id; $ticket->save(); - $ticket_thread = Ticket_Thread::where('ticket_id', '=', $id)->first(); - if (!$ticket_thread) { + $ticket_thread = Ticket_Thread::where('ticket_id', '=', $id)->first(); + if (!$ticket_thread) + { return ['error' => 'No thread not found']; } - $ticket_subject = $ticket_thread->title; - $thread = new Ticket_Thread(); - $thread->ticket_id = $ticket->id; - $thread->user_id = Auth::user()->id; + $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->body = 'This Ticket has been assigned to ' . $user->first_name . ' ' . $user->last_name; $thread->save(); $company = $this->company(); - $system = $this->system(); + $system = $this->system(); - $agent = $user->first_name; + $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) { + $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 . ']'); // }); - try { - $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('0', $ticket->dept_id), $to = ['name' => $agent, 'email' => $agent_email], $message = ['subject' => $ticket_subject.'[#'.$ticket_number.']', 'scenario' => 'assign-ticket'], $template_variables = ['ticket_agent_name' => $agent, 'ticket_number' => $ticket_number, 'ticket_assigner' => $master]); - } catch (\Exception $e) { + try + { + $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('0', $ticket->dept_id), $to = ['name' => $agent, 'email' => $agent_email], $message = ['subject' => $ticket_subject . '[#' . $ticket_number . ']', 'scenario' => 'assign-ticket'], $template_variables = ['ticket_agent_name' => $agent, 'ticket_number' => $ticket_number, 'ticket_assigner' => $master]); + } + catch (\Exception $e) + { return 0; } } return 1; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -506,47 +580,63 @@ class TicketController extends Controller */ public function delete($ids, $ticket) { - try { - foreach ($ids as $id) { + try + { + foreach ($ids as $id) + { $ticket_delete = $ticket->where('id', '=', $id)->first(); - if ($ticket_delete) { - if ($ticket_delete->status == 5) { + if ($ticket_delete) + { + if ($ticket_delete->status == 5) + { $ticket_delete->delete(); $ticket_threads = Ticket_Thread::where('ticket_id', '=', $id)->get(); - if ($ticket_threads) { - foreach ($ticket_threads as $ticket_thread) { - if ($ticket_thread) { + if ($ticket_threads) + { + foreach ($ticket_threads as $ticket_thread) + { + if ($ticket_thread) + { $ticket_thread->delete(); } } } $ticket_attachments = Ticket_attachments::where('thread_id', '=', $id)->get(); - if ($ticket_attachments) { - foreach ($ticket_attachments as $ticket_attachment) { - if ($ticket_attachment) { + if ($ticket_attachments) + { + foreach ($ticket_attachments as $ticket_attachment) + { + if ($ticket_attachment) + { $ticket_attachment->delete(); } } } - } else { + } + else + { $ticket_delete->is_deleted = 0; - $ticket_delete->status = 5; + $ticket_delete->status = 5; $ticket_delete->save(); - $ticket_status_message = Ticket_Status::where('id', '=', $ticket_delete->status)->first(); - $thread = new Ticket_Thread(); - $thread->ticket_id = $ticket_delete->id; - $thread->user_id = Auth::user()->id; - $thread->is_internal = 1; - $thread->body = $ticket_status_message->message.' '.Auth::user()->first_name.' '.Auth::user()->last_name; + $ticket_status_message = Ticket_Status::where('id', '=', $ticket_delete->status)->first(); + $thread = new Ticket_Thread(); + $thread->ticket_id = $ticket_delete->id; + $thread->user_id = Auth::user()->id; + $thread->is_internal = 1; + $thread->body = $ticket_status_message->message . ' ' . Auth::user()->first_name . ' ' . Auth::user()->last_name; $thread->save(); } - } else { + } + else + { return 'ticket not found'; } } return 'your tickets has been deleted'; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -560,14 +650,20 @@ class TicketController extends Controller */ public function checkEmail($email) { - try { + try + { $check = User::where('email', '=', $email)->first(); - if ($check) { + if ($check) + { return $check; - } else { + } + else + { return false; } - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -579,16 +675,22 @@ class TicketController extends Controller */ public function system() { - try { + try + { $system = System::Where('id', '=', '1')->first(); - if ($system->name == null) { + if ($system->name == null) + { $system = 'Support Center'; - } else { + } + else + { $system = $system->name; } return $system; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -603,15 +705,19 @@ class TicketController extends Controller */ public function attach($thread, $attach) { - try { + try + { $ta = new Ticket_attachments(); - foreach ($attach as $file) { + foreach ($attach as $file) + { $ta->create(['thread_id' => $thread, 'name' => $file['name'], 'size' => $file['size'], 'type' => $file['type'], 'file' => $file['file'], 'poster' => 'ATTACHMENT']); } $ta->create(['thread_id' => $thread, 'name' => $name, 'size' => $size, 'type' => $type, 'file' => $file, 'poster' => 'ATTACHMENT']); return 1; - } catch (\Exception $e) { + } + catch (\Exception $e) + { return $e->getMessage(); } } @@ -624,7 +730,7 @@ class TicketController extends Controller public function autosearch() { $term = \Input::get('term'); - $user = \App\User::where('email', 'LIKE', '%'.$term.'%')->orWhere('first_name', 'LIKE', '%'.$term.'%')->orWhere('last_name', 'LIKE', '%'.$term.'%')->orWhere('user_name', 'LIKE', '%'.$term.'%')->lists('email'); + $user = \App\User::where('email', 'LIKE', '%' . $term . '%')->orWhere('first_name', 'LIKE', '%' . $term . '%')->orWhere('last_name', 'LIKE', '%' . $term . '%')->orWhere('user_name', 'LIKE', '%' . $term . '%')->lists('email'); return $user; } @@ -638,26 +744,27 @@ class TicketController extends Controller */ public function useradd() { - $email = Input::get('email'); - $ticket_id = Input::get('ticket_id'); - $company = $this->company(); - $user = new User(); + $email = Input::get('email'); + $ticket_id = Input::get('ticket_id'); + $company = $this->company(); + $user = new User(); $user->user_name = $email; - $user->email = $email; - $password = $this->generateRandomString(); - $user->password = \Hash::make($password); - $user->role = 'user'; - $user->active = 1; - if ($user->save()) { - $user_id = $user->id; - $php_mailer = new PhpMailController(); - $php_mailer->sendmail($from = $php_mailer->mailfrom('1', '0'), $to = ['name' => $email, 'email' => $email], $message = ['subject' => 'Password', 'scenario' => 'registration-notification'], $template_variables = ['user' => $email, 'email_address' => $email, 'user_password' => $password]); + $user->email = $email; + $password = $this->generateRandomString(); + $user->password = \Hash::make($password); + $user->role = 'user'; + $user->active = 1; + if ($user->save()) + { + $user_id = $user->id; + $php_mailer = new PhpMailController(); + $php_mailer->sendmail($from = $php_mailer->mailfrom('1', '0'), $to = ['name' => $email, 'email' => $email], $message = ['subject' => 'Password', 'scenario' => 'registration-notification'], $template_variables = ['user' => $email, 'email_address' => $email, 'user_password' => $password]); } - $ticket_collaborator = new Ticket_Collaborator(); - $ticket_collaborator->isactive = 1; + $ticket_collaborator = new Ticket_Collaborator(); + $ticket_collaborator->isactive = 1; $ticket_collaborator->ticket_id = $ticket_id; - $ticket_collaborator->user_id = $user->id; - $ticket_collaborator->role = 'ccc'; + $ticket_collaborator->user_id = $user->id; + $ticket_collaborator->role = 'ccc'; $ticket_collaborator->save(); $result = [$user->user_name => $user->email]; @@ -672,46 +779,57 @@ class TicketController extends Controller */ public function userremove() { - $email = Input::get('email'); - $ticketid = Input::get('ticketid'); - $user = new User(); - $user = $user->where('email', $email)->first(); + $email = Input::get('email'); + $ticketid = Input::get('ticketid'); + $user = new User(); + $user = $user->where('email', $email)->first(); $ticket_collaborator = Ticket_Collaborator::where('ticket_id', '=', $ticketid) ->where('user_id', $user->id) ->first(); - if ($ticket_collaborator) { + if ($ticket_collaborator) + { $ticket_collaborator->delete(); return 'deleted successfully'; - } else { + } + else + { return 'not found'; } } public function getCollaboratorForTicket() { - try { + try + { $ticketid = Input::get('ticket_id'); $ticket_collaborator = \DB::table('users') - ->join('ticket_collaborator', function ($join) use ($ticketid) { + ->join('ticket_collaborator', function ($join) use ($ticketid) + { $join->on('users.id', '=', 'ticket_collaborator.user_id') ->where('ticket_collaborator.ticket_id', '=', $ticketid); }) ->select('users.email', 'users.user_name') ->get(); - if (count($ticket_collaborator) > 0) { - foreach ($ticket_collaborator as $key => $collaborator) { - $collab[$key]['email'] = $collaborator->email; + if (count($ticket_collaborator) > 0) + { + foreach ($ticket_collaborator as $key => $collaborator) + { + $collab[$key]['email'] = $collaborator->email; $collab[$key]['user_name'] = $collaborator->user_name; - $collab[$key]['avatar'] = $this->avatarUrl($collaborator->email); + $collab[$key]['avatar'] = $this->avatarUrl($collaborator->email); } - } else { + } + else + { $collab = $ticket_collaborator; } return $collab; - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { return $ex->getMessage(); throw new \Exception('get collaborator for ticket fails'); } @@ -719,19 +837,26 @@ class TicketController extends Controller public function avatarUrl($email) { - try { + try + { $user = new User(); $user = $user->where('email', $email)->first(); - if ($user->profile_pic) { - $url = url('uploads/profilepic/'.$user->profile_pic); - } else { + if ($user->profile_pic) + { + $url = url('uploads/profilepic/' . $user->profile_pic); + } + else + { $url = \Gravatar::src($email); } return $url; - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { //return $ex->getMessage(); throw new \Exception($ex->getMessage()); } } + } diff --git a/app/Http/Controllers/Api/v1/TokenAuthController.php b/app/Http/Controllers/Api/v1/TokenAuthController.php index f08d0346d..f5c38b722 100644 --- a/app/Http/Controllers/Api/v1/TokenAuthController.php +++ b/app/Http/Controllers/Api/v1/TokenAuthController.php @@ -61,7 +61,7 @@ class TokenAuthController extends Controller return response()->json(compact('error')); } - $user_id = \Auth::user()->id; + $user_id = \Auth::user(); // if no errors are encountered we can return a JWT return response()->json(compact('token', 'user_id')); } diff --git a/app/Http/Controllers/Auth/AuthController.php b/app/Http/Controllers/Auth/AuthController.php index f6f00dba7..cbba80e92 100644 --- a/app/Http/Controllers/Auth/AuthController.php +++ b/app/Http/Controllers/Auth/AuthController.php @@ -36,18 +36,19 @@ use Socialite; * * @author Ladybird */ -class AuthController extends Controller { +class AuthController extends Controller +{ use AuthenticatesAndRegistersUsers; /* to redirect after login */ // if auth is agent - protected $redirectTo = '/dashboard'; + protected $redirectTo = '/dashboard'; // if auth is user - protected $redirectToUser = '/profile'; + protected $redirectToUser = '/profile'; /* Direct After Logout */ protected $redirectAfterLogout = '/'; - protected $loginPath = '/auth/login'; + protected $loginPath = '/auth/login'; protected $social; /** @@ -58,14 +59,18 @@ class AuthController extends Controller { * * @return void */ - public function __construct(PhpMailController $PhpMailController, SocialMediaController $social) { - $this->PhpMailController = $PhpMailController; + public function __construct() + { + $this->PhpMailController = new PhpMailController(); + $social = new SocialMediaController(); $social->configService(); $this->middleware('guest', ['except' => ['getLogout', 'verifyOTP', 'redirectToProvider']]); } - public function redirectToProvider($provider, $redirect = '') { - if ($redirect !== '') { + public function redirectToProvider($provider, $redirect = '') + { + if ($redirect !== '') + { $this->setSession($provider, $redirect); } //dd(\Config::get('services')); @@ -74,41 +79,50 @@ class AuthController extends Controller { return $s; } - public function handleProviderCallback($provider) { - try { + public function handleProviderCallback($provider) + { + try + { //notice we are not doing any validation, you should do it $this->changeRedirect(); $user = Socialite::driver($provider)->user(); - if ($user) { + if ($user) + { // stroing data to our use table and logging them in - $username = $user->getEmail(); + $username = $user->getEmail(); $first_name = $user->getName(); - if ($user->nickname) { + if ($user->nickname) + { $username = $user->nickname; } - if (!$first_name) { + if (!$first_name) + { $first_name = $username; } $data = [ 'first_name' => $first_name, - 'email' => $user->getEmail(), - 'user_name' => $username, - 'role' => 'user', - 'active' => 1, + 'email' => $user->getEmail(), + 'user_name' => $username, + 'role' => 'user', + 'active' => 1, ]; $user = User::where('email', $data['email'])->first(); - if (!$user) { + if (!$user) + { $user = User::where('user_name', $data['user_name'])->first(); } - if (!$user) { + if (!$user) + { $user = User::firstOrCreate($data); } Auth::login($user); } //after login redirecting to home page return redirect('/'); - } catch (\Exception $ex) { + } + catch (\Exception $ex) + { return redirect()->back()->with('fails', $ex->getMessage()); } } @@ -118,19 +132,26 @@ class AuthController extends Controller { * * @return type Response */ - public function getRegister(CommonSettings $settings) { + public function getRegister(CommonSettings $settings) + { // Event for login - $settings = $settings->select('status')->where('option_name', '=', 'send_otp')->first(); + $settings = $settings->select('status')->where('option_name', '=', 'send_otp')->first(); $email_mandatory = $settings->select('status')->where('option_name', '=', 'email_mandatory')->first(); //dd($settings->status); \Event::fire(new \App\Events\FormRegisterEvent()); - if (Auth::user()) { - if (Auth::user()->role == 'admin' || Auth::user()->role == 'agent') { + if (Auth::user()) + { + if (Auth::user()->role == 'admin' || Auth::user()->role == 'agent') + { return \Redirect::route('dashboard'); - } elseif (Auth::user()->role == 'user') { + } + elseif (Auth::user()->role == 'user') + { // return view('auth.register'); } - } else { + } + else + { return view('auth.register', compact('settings', 'email_mandatory')); } } @@ -143,65 +164,102 @@ class AuthController extends Controller { * * @return type Response */ - public function postRegister(User $user, RegisterRequest $request) { - try { - $request_array = $request->input(); - $password = Hash::make($request->input('password')); - $user->password = $password; - $name = $request->input('full_name'); + public function postRegister(User $user, RegisterRequest $request, $api = false) + { + //dd($request->all()); + try + { + $request_array = $request->input(); + $password = Hash::make($request->input('password')); + $user->password = $password; + $name = $request->input('full_name'); $user->first_name = $name; - if ($request_array['email'] == '') { + if ($request_array['email'] == '') + { $user->email = null; - } else { + } + else + { $user->email = $request->input('email'); } - if ($request_array['mobile'] == '') { + if (!checkArray('mobile', $request_array)) + { $user->mobile = null; - } else { + } + else + { $user->mobile = $request->input('mobile'); } - if ($request_array['code'] == '') { + if (!checkArray('code', $request_array)) + { $user->country_code = 0; - } else { + } + else + { $user->country_code = $request->input('code'); } - if ($request_array['email'] != '') { - $user->user_name = $request->input('email'); - } else { - $user->user_name = $request->input('mobile'); + if (checkArray('username', $request_array)) + { + $user->user_name = checkArray('username', $request_array); } - $user->role = 'user'; - $code = str_random(60); + else + { + $user->user_name = $request->input('email'); + } + $user->role = 'user'; + $code = str_random(60); $user->remember_token = $code; $user->save(); - $message12 = ''; - $settings = CommonSettings::select('status')->where('option_name', '=', 'send_otp')->first(); - $sms = Plugin::select('status')->where('name', '=', 'SMS')->first(); + $message12 = ''; + $settings = CommonSettings::select('status')->where('option_name', '=', 'send_otp')->first(); + $sms = Plugin::select('status')->where('name', '=', 'SMS')->first(); // Event for login \Event::fire(new \App\Events\LoginEvent($request)); - if ($request->input('email') !== '') { - $var = $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('1', '0'), $to = ['name' => $name, 'email' => $request->input('email')], $message = ['subject' => null, 'scenario' => 'registration'], $template_variables = ['user' => $name, 'email_address' => $request->input('email'), 'password_reset_link' => url('account/activate/' . $code)]); + if ($request->input('email') !== '') + { + $var = $this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('1', '0'), $to = ['name' => $name, 'email' => $request->input('email')], $message = ['subject' => null, 'scenario' => 'registration'], $template_variables = ['user' => $name, 'email_address' => $request->input('email'), 'password_reset_link' => url('account/activate/' . $code)]); } - if ($settings->status == 1 || $settings->status == '1') { - if (count($sms) > 0) { - if ($sms->status == 1 || $sms->status == '1') { + if ($settings->status == 1 || $settings->status == '1') + { + if (count($sms) > 0) + { + if ($sms->status == 1 || $sms->status == '1') + { $message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail_and_moble'); - } else { + } + else + { $message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail_sms_plugin_inactive_or_not_setup'); } - } else { - if ($request->input('email') !== '') { + } + else + { + if ($request->input('email') !== '') + { $message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail'); - } else { + } + else + { $message12 = Lang::get('lang.account-created-contact-admin-as-we-were-not-able-to-send-opt'); } } - } else { + } + else + { $message12 = Lang::get('lang.activate_your_account_click_on_Link_that_send_to_your_mail'); } - + if ($api == true) + { + return ['message' => $message12, 'user' => $user->toArray()]; + } return redirect('home')->with('success', $message12); - } catch (\Exception $e) { + } + catch (\Exception $e) + { + if ($api == true) + { + throw new \Exception($e->getMessage()); + } return redirect()->back()->with('fails', $e->getMessage()); } } @@ -213,16 +271,20 @@ class AuthController extends Controller { * * @return type redirect */ - public function accountActivate($token) { + public function accountActivate($token) + { $user = User::where('remember_token', '=', $token)->first(); - if ($user) { - $user->active = 1; + if ($user) + { + $user->active = 1; $user->remember_token = null; $user->save(); $this->openTicketAfterVerification($user->id); return redirect('/auth/login')->with('status', 'Acount activated. Login to start'); - } else { + } + else + { return redirect('/auth/login')->with('fails', 'Invalid Token'); } } @@ -235,14 +297,18 @@ class AuthController extends Controller { * * @return type Response */ - public function getMail($token, User $user) { + public function getMail($token, User $user) + { $user = $user->where('remember_token', $token)->where('active', 0)->first(); - if ($user) { + if ($user) + { $user->active = 1; $user->save(); return redirect('auth/login'); - } else { + } + else + { return redirect('auth/login'); } } @@ -252,19 +318,29 @@ class AuthController extends Controller { * * @return type Response */ - public function getLogin() { + public function getLogin() + { $directory = base_path(); - if (file_exists($directory . DIRECTORY_SEPARATOR . '.env')) { - if (Auth::user()) { - if (Auth::user()->role == 'admin' || Auth::user()->role == 'agent') { + if (file_exists($directory . DIRECTORY_SEPARATOR . '.env')) + { + if (Auth::user()) + { + if (Auth::user()->role == 'admin' || Auth::user()->role == 'agent') + { return \Redirect::route('dashboard'); - } elseif (Auth::user()->role == 'user') { + } + elseif (Auth::user()->role == 'user') + { return \Redirect::route('home'); } - } else { + } + else + { return view('auth.login'); } - } else { + } + else + { return Redirect::route('licence'); } } @@ -276,120 +352,155 @@ class AuthController extends Controller { * * @return type Response */ - public function postLogin(LoginRequest $request) { - try { + public function postLogin(LoginRequest $request) + { + try + { // dd($request->input()); \Event::fire('auth.login.event', []); //added 5/5/2016 // Set login attempts and login time - $value = $_SERVER['REMOTE_ADDR']; + $value = $_SERVER['REMOTE_ADDR']; $usernameinput = $request->input('email'); - $password = $request->input('password'); - if ($request->input('referer')) { + $password = $request->input('password'); + if ($request->input('referer')) + { $referer = 'form'; - } else { + } + else + { $referer = '/'; } - $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name'; + $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name'; $result = $this->confirmIPAddress($value, $usernameinput); // If attempts > 3 and time < 30 minutes $security = Security::whereId('1')->first(); - if ($result == 1) { + if ($result == 1) + { return redirect()->back()->withErrors('email', 'Incorrect details')->with(['error' => $security->lockout_message, 'referer' => $referer]); } $check_active = User::where('email', '=', $request->input('email'))->orwhere('user_name', '=', $request->input('email'))->first(); - if (!$check_active) { //check if user exists or not + if (!$check_active) + { //check if user exists or not //if user deos not exist then return back with error that user is not registered return redirect()->back() ->withInput($request->only('email', 'remember')) ->withErrors([ - 'email' => $this->getFailedLoginMessage(), + 'email' => $this->getFailedLoginMessage(), 'password' => $this->getFailedLoginMessage(), - ])->with(['error' => Lang::get('lang.not-registered'), + ])->with(['error' => Lang::get('lang.not-registered'), 'referer' => $referer,]); } //if user exists $settings = CommonSettings::select('status')->where('option_name', '=', 'send_otp')->first(); - if ($settings->status == '1' || $settings->status == 1) { // check for otp verification setting + if ($settings->status == '1' || $settings->status == 1) + { // check for otp verification setting // setting is enabled $sms = Plugin::select('status')->where('name', '=', 'SMS')->first(); - if ($sms) { //check sms plugin installed or not + if ($sms) + { //check sms plugin installed or not // plugin is installed - if ($sms->status == 1 || $sms->status === '1') { //check plugin is active or not + if ($sms->status == 1 || $sms->status === '1') + { //check plugin is active or not // plugin is active - if (!$check_active->active) { //check account is active or not + if (!$check_active->active) + { //check account is active or not // account is not active show verify otp window - if ($check_active->mobile) { //check user has mobile or not + if ($check_active->mobile) + { //check user has mobile or not // user has mobile number return verify OTP screen return \Redirect::route('otp-verification') ->withInput($request->input()) - ->with(['values' => $request->input(), + ->with(['values' => $request->input(), 'referer' => $referer, - 'name' => $check_active->first_name, - 'number' => $check_active->mobile, - 'code' => $check_active->country_code,]); - } else { + 'name' => $check_active->first_name, + 'number' => $check_active->mobile, + 'code' => $check_active->country_code,]); + } + else + { goto a; //attenmpt login (be careful while using goto statements) } - } else { + } + else + { goto a; //attenmpt login (be careful while using goto statements) } - } else { + } + else + { goto a; //attenmpt login (be careful while using goto statements) } - } else { + } + else + { goto a; //attenmpt login (be careful while using goto statements) } - } else { + } + else + { // setting is disabled - a: if (!$check_active->active) { //check account is active or not + a: if (!$check_active->active) + { //check account is active or not // if accoutn is not active return back with error message that account is inactive return redirect()->back() ->withInput($request->only('email', 'remember')) ->withErrors([ - 'email' => $this->getFailedLoginMessage(), + 'email' => $this->getFailedLoginMessage(), 'password' => $this->getFailedLoginMessage(), - ])->with(['error' => Lang::get('lang.this_account_is_currently_inactive'), + ])->with(['error' => Lang::get('lang.this_account_is_currently_inactive'), 'referer' => $referer,]); - } else { + } + else + { // try login $loginAttempts = 1; // If session has login attempts, retrieve attempts counter and attempts time - if (\Session::has('loginAttempts')) { - $loginAttempts = \Session::get('loginAttempts'); + if (\Session::has('loginAttempts')) + { + $loginAttempts = \Session::get('loginAttempts'); $loginAttemptTime = \Session::get('loginAttemptTime'); $this->addLoginAttempt($value, $usernameinput); // $credentials = $request->only('email', 'password'); - $usernameinput = $request->input('email'); - $password = $request->input('password'); - $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name'; + $usernameinput = $request->input('email'); + $password = $request->input('password'); + $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name'; // If attempts > 3 and time < 10 minutes - if ($loginAttempts > $security->backlist_threshold && (time() - $loginAttemptTime <= ($security->lockout_period * 60))) { + if ($loginAttempts > $security->backlist_threshold && (time() - $loginAttemptTime <= ($security->lockout_period * 60))) + { return redirect()->back()->withErrors('email', 'incorrect email')->with('error', $security->lockout_message); } // If time > 10 minutes, reset attempts counter and time in session - if (time() - $loginAttemptTime > ($security->lockout_period * 60)) { + if (time() - $loginAttemptTime > ($security->lockout_period * 60)) + { \Session::put('loginAttempts', 1); \Session::put('loginAttemptTime', time()); } - } else { // If no login attempts stored, init login attempts and time + } + else + { // If no login attempts stored, init login attempts and time \Session::put('loginAttempts', $loginAttempts); \Session::put('loginAttemptTime', time()); $this->clearLoginAttempts($value, $usernameinput); } // If auth ok, redirect to restricted area \Session::put('loginAttempts', $loginAttempts + 1); - if (Auth::Attempt([$field => $usernameinput, 'password' => $password], $request->has('remember'))) { - if (Auth::user()->role == 'user') { - if ($request->input('referer')) { + if (Auth::Attempt([$field => $usernameinput, 'password' => $password], $request->has('remember'))) + { + if (Auth::user()->role == 'user') + { + if ($request->input('referer')) + { return \Redirect::route($request->input('referer')); } return \Redirect::route('/'); - } else { + } + else + { return redirect()->intended($this->redirectPath()); } } @@ -399,12 +510,14 @@ class AuthController extends Controller { return redirect()->back() ->withInput($request->only('email', 'remember')) ->withErrors([ - 'email' => $this->getFailedLoginMessage(), + 'email' => $this->getFailedLoginMessage(), 'password' => $this->getFailedLoginMessage(), - ])->with(['error' => Lang::get('lang.invalid'), + ])->with(['error' => Lang::get('lang.invalid'), 'referer' => $referer,]); // Increment login attempts - } catch (\Exception $e) { + } + catch (\Exception $e) + { return redirect()->back()->with('fails', $e->getMessage()); } } @@ -416,21 +529,28 @@ class AuthController extends Controller { * * @return type Response */ - public function addLoginAttempt($value, $field) { - $result = DB::table('login_attempts')->where('IP', '=', $value)->first(); - $data = $result; + public function addLoginAttempt($value, $field) + { + $result = DB::table('login_attempts')->where('IP', '=', $value)->first(); + $data = $result; $security = Security::whereId('1')->first(); - $apt = $security->backlist_threshold; - if ($data) { + $apt = $security->backlist_threshold; + if ($data) + { $attempts = $data->Attempts + 1; - if ($attempts == $apt) { + if ($attempts == $apt) + { // $result = DB::select('UPDATE login_attempts SET Attempts='.$attempts.", LastLogin=NOW() WHERE IP = '$value' OR User = '$field'"); $result = DB::table('login_attempts')->where('IP', '=', $value)->orWhere('User', '=', $field)->update(['Attempts' => $attempts, 'LastLogin' => date('Y-m-d H:i:s')]); - } else { + } + else + { $result = DB::table('login_attempts')->where('IP', '=', $value)->orWhere('User', '=', $field)->update(['Attempts' => $attempts]); // $result = DB::select("UPDATE login_attempts SET Attempts=".$attempts." WHERE IP = '$value' OR User = '$field'"); } - } else { + } + else + { // $result = DB::select("INSERT INTO login_attempts (Attempts,User,IP,LastLogin) values (1,'$field','$value', NOW())"); $result = DB::table('login_attempts')->update(['Attempts' => 1, 'User' => $field, 'IP' => $value, 'LastLogin' => date('Y-m-d H:i:s')]); } @@ -443,7 +563,8 @@ class AuthController extends Controller { * * @return type Response */ - public function clearLoginAttempts($value, $field) { + public function clearLoginAttempts($value, $field) + { $data = DB::table('login_attempts')->where('IP', '=', $value)->orWhere('User', '=', $field)->update(['attempts' => '0']); return $data; @@ -456,22 +577,28 @@ class AuthController extends Controller { * * @return type Response */ - public function confirmIPAddress($value, $field) { - $security = Security::whereId('1')->first(); - $time = $security->lockout_period; + public function confirmIPAddress($value, $field) + { + $security = Security::whereId('1')->first(); + $time = $security->lockout_period; $max_attempts = $security->backlist_threshold; - $table = 'login_attempts'; - $result = DB::select('SELECT Attempts, (CASE when LastLogin is not NULL and DATE_ADD(LastLogin, INTERVAL ' . $time . ' MINUTE)>NOW() then 1 else 0 end) as Denied ' . + $table = 'login_attempts'; + $result = DB::select('SELECT Attempts, (CASE when LastLogin is not NULL and DATE_ADD(LastLogin, INTERVAL ' . $time . ' MINUTE)>NOW() then 1 else 0 end) as Denied ' . ' FROM ' . $table . " WHERE IP = '$value' OR User = '$field'"); - $data = $result; + $data = $result; //Verify that at least one login attempt is in database - if (!$data) { + if (!$data) + { return 0; } - if ($data[0]->Attempts >= $max_attempts) { - if ($data[0]->Denied == 1) { + if ($data[0]->Attempts >= $max_attempts) + { + if ($data[0]->Denied == 1) + { return 1; - } else { + } + else + { $this->clearLoginAttempts($value, $field); return 0; @@ -486,7 +613,8 @@ class AuthController extends Controller { * * @return type string */ - protected function getFailedLoginMessage() { + protected function getFailedLoginMessage() + { return Lang::get('lang.this_field_do_not_match_our_records'); } @@ -497,10 +625,14 @@ class AuthController extends Controller { * * @return response|view */ - public function getVerifyOTP() { - if (\Session::has('values')) { + public function getVerifyOTP() + { + if (\Session::has('values')) + { return view('auth.otp-verify'); - } else { + } + else + { return redirect('auth/login'); } } @@ -512,27 +644,37 @@ class AuthController extends Controller { * * @return int|string */ - public function verifyOTP(LoginRequest $request) { - $user = User::select('id', 'mobile', 'user_name')->where('email', '=', $request->input('email')) + public function verifyOTP(LoginRequest $request) + { + $user = User::select('id', 'mobile', 'user_name')->where('email', '=', $request->input('email')) ->orWhere('user_name', '=', $request->input('email'))->first(); $otp_length = strlen($request->input('otp')); - if (!\Schema::hasTable('user_verification')) { + if (!\Schema::hasTable('user_verification')) + { $message = Lang::get('lang.opt-can-not-be-verified'); - } else { + } + else + { $otp = Otp::select('otp', 'updated_at')->where('user_id', '=', $user->id) ->first(); - if ($otp != null) { - if (($otp_length == 6 && !preg_match('/[a-z]/i', $request->input('otp')))) { - $otp2 = Hash::make($request->input('otp')); - $date1 = date_format($otp->updated_at, 'Y-m-d h:i:sa'); - $date2 = date('Y-m-d h:i:sa'); - $time1 = new DateTime($date2); - $time2 = new DateTime($date1); + if ($otp != null) + { + if (($otp_length == 6 && !preg_match('/[a-z]/i', $request->input('otp')))) + { + $otp2 = Hash::make($request->input('otp')); + $date1 = date_format($otp->updated_at, 'Y-m-d h:i:sa'); + $date2 = date('Y-m-d h:i:sa'); + $time1 = new DateTime($date2); + $time2 = new DateTime($date1); $interval = $time1->diff($time2); - if ($interval->i > 30 || $interval->h > 0) { + if ($interval->i > 30 || $interval->h > 0) + { $message = Lang::get('lang.otp-expired'); - } else { - if (Hash::check($request->input('otp'), $otp->otp)) { + } + else + { + if (Hash::check($request->input('otp'), $otp->otp)) + { Otp::where('user_id', '=', $user->id) ->update(['otp' => '']); User::where('id', '=', $user->id) @@ -540,14 +682,20 @@ class AuthController extends Controller { $this->openTicketAfterVerification($user->id); return $this->postLogin($request); - } else { + } + else + { $message = Lang::get('lang.otp-not-matched'); } } - } else { + } + else + { $message = Lang::get('lang.otp-invalid'); } - } else { + } + else + { $message = Lang::get('lang.otp-not-matched'); } } @@ -556,22 +704,29 @@ class AuthController extends Controller { ->withInput($request->input()) ->with(['values' => $request->input(), 'number' => $user->mobile, - 'name' => $user->user_name, - 'fails' => $message,]); + 'name' => $user->user_name, + 'fails' => $message,]); } - public function resendOTP(OtpVerifyRequest $request) { - if (!\Schema::hasTable('user_verification') || !\Schema::hasTable('sms')) { + public function resendOTP(OtpVerifyRequest $request) + { + if (!\Schema::hasTable('user_verification') || !\Schema::hasTable('sms')) + { $message = Lang::get('lang.opt-can-not-be-verified'); return $message; - } else { + } + else + { $sms = DB::table('sms')->get(); - if (count($sms) > 0) { + if (count($sms) > 0) + { \Event::fire(new \App\Events\LoginEvent($request)); return 1; - } else { + } + else + { $message = Lang::get('lang.opt-can-not-be-verified'); return $message; @@ -588,15 +743,18 @@ class AuthController extends Controller { * * @author manish.verma@ladybirdweb.com */ - public function openTicketAfterVerification($id) { + public function openTicketAfterVerification($id) + { // dd($id); $ticket = Tickets::select('id') ->where(['user_id' => $id, 'status' => 6]) ->get(); Tickets::where(['user_id' => $id, 'status' => 6]) ->update(['status' => 1]); - if ($ticket != null) { - foreach ($ticket as $value) { + if ($ticket != null) + { + foreach ($ticket as $value) + { $ticket_id = $value->id; Ticket_Thread::where('ticket_id', '=', $ticket_id) ->update(['updated_at' => date('Y-m-d H:i:s')]); @@ -604,13 +762,15 @@ class AuthController extends Controller { } } - public function changeRedirect() { + public function changeRedirect() + { $provider = \Session::get('provider'); - $url = \Session::get($provider . 'redirect'); + $url = \Session::get($provider . 'redirect'); \Config::set("services.$provider.redirect", $url); } - public function setSession($provider, $redirect) { + public function setSession($provider, $redirect) + { $url = url($redirect); \Session::set('provider', $provider); \Session::set($provider . 'redirect', $url); diff --git a/app/Http/routes.php b/app/Http/routes.php index 131494175..304be246f 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -705,8 +705,7 @@ Route::group(['middleware' => ['web']], function () { * @author Vijay Sebastian * @name Faveo */ - Route::group(['prefix' => 'api/v1'], function () { - Route::post('register', 'Api\v1\ApiController@register'); + Route::group(['prefix' => 'api/v1'], function () { Route::post('authenticate', 'Api\v1\TokenAuthController@authenticate'); Route::get('authenticate/user', 'Api\v1\TokenAuthController@getAuthenticatedUser'); Route::get('/database-config', ['as' => 'database-config', 'uses' => 'Api\v1\InstallerApiController@config_database']); @@ -752,6 +751,7 @@ Route::group(['middleware' => ['web']], function () { Route::post('collaborator/get-ticket', 'Api\v1\ApiController@getCollaboratorForTicket'); Route::get('my-tickets-user', 'Api\v1\ApiController@getMyTicketsUser'); Route::get('dependency', 'Api\v1\ApiController@dependency'); + Route::post('register', 'Api\v1\ApiController@createUser'); }); /* diff --git a/app/Model/helpdesk/Utility/Priority.php b/app/Model/helpdesk/Utility/Priority.php index 051b6460a..92bb54ccb 100644 --- a/app/Model/helpdesk/Utility/Priority.php +++ b/app/Model/helpdesk/Utility/Priority.php @@ -7,7 +7,7 @@ use App\BaseModel; class Priority extends BaseModel { public $timestamps = false; - protected $table = 'priority'; + protected $table = 'ticket_priority'; protected $fillable = [ 'id', 'name', ];