diff --git a/app/Http/Controllers/Api/v1/ApiController.php b/app/Http/Controllers/Api/v1/ApiController.php new file mode 100644 index 000000000..d87854b6f --- /dev/null +++ b/app/Http/Controllers/Api/v1/ApiController.php @@ -0,0 +1,759 @@ + + * @copyright (c) 2016, Ladybird Web Solution + * @name Faveo HELPDESK + * @version v1 + * + * + */ +class ApiController extends Controller { + + public $user; + public $request; + public $ticket; + public $model; + public $thread; + public $attach; + public $ticketRequest; + public $faveoUser; + public $team; + public $setting; + public $helptopic; + public $slaPlan; + public $department; + public $priority; + public $source; + + /** + * + * @param Request $request + */ + public function __construct(Request $request) { + + $this->request = $request; + + $this->middleware('jwt.auth'); + $this->middleware('api', ['except' => 'GenerateApiKey']); + + $user = \JWTAuth::parseToken()->authenticate(); + $this->user = $user; + + $ticket = new TicketController(); + $this->ticket = $ticket; + + $model = new Tickets(); + $this->model = $model; + + $thread = new Ticket_Thread(); + $this->thread = $thread; + + $attach = new Ticket_attachments(); + $this->attach = $attach; + + $ticketRequest = new TicketRequest(); + $this->ticketRequest = $ticketRequest; + + $faveoUser = new User(); + $this->faveoUser = $faveoUser; + + $team = new Teams(); + $this->team = $team; + + $setting = new Settings(); + $this->setting = $setting; + + $helptopic = new Help_topic(); + $this->helptopic = $helptopic; + + $slaPlan = new Sla_plan(); + $this->slaPlan = $slaPlan; + + $priority = new Priority(); + $this->priority = $priority; + + $department = new Department(); + $this->department = $department; + + $source = new Ticket_source(); + $this->source = $source; + } + + /** + * Create Tickets + * @method POST + * @param user_id,subject,body,helptopic,sla,priority,dept + * @return json + */ + public function CreateTicket() { + try { + $v = \Validator::make($this->request->all(), [ + 'user_id' => 'required|exists:users,id', + 'subject' => 'required', + 'body' => 'required', + 'helptopic' => 'required|exists:help_topic,id', + 'sla' => 'required|exists:sla_plan,id', + 'priority' => 'required|exists:ticket_priority,priority_id', + 'dept' => 'required|exists:department,id', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + + $user_id = $this->request->input('user_id'); + + + $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'); + $headers = $this->request->input('headers'); + $dept = $this->request->input('dept'); + + $assignto = $this->request->input('assignto'); + $form_data = $this->request->input('form_data'); + $source = $this->request->input('source'); + $attach = $this->request->input('attachments'); + /** + * return s ticket number + */ + $response = $this->ticket->create_ticket($user_id, $subject, $body, $helptopic, $sla, $priority, $source, $headers, $dept, $assignto, $form_data, $attach); + + /** + * return ticket details + */ + $result = $this->thread->where('id', $response)->first(); + //$result = $this->attach($result->id,$file); + return response()->json(compact('result')); + } catch (\Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Reply for the ticket + * @param TicketRequest $request + * @return json + */ + public function TicketReply() { + try { + $v = \Validator::make($this->request->all(), [ + 'ticket_ID' => 'required|exists:tickets,id', + 'ReplyContent' => 'required', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $attach = $this->request->input('attachments'); + $result = $this->ticket->reply($this->thread, $this->request, $this->attach, $attach); + return response()->json(compact('result')); + } catch (\Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Edit a ticket + * @return json + */ + public function EditTicket() { + try { + $v = \Validator::make($this->request->all(), [ + 'ticket_id' => 'required|exists:tickets,id', + 'subject' => 'required', + 'sla_plan' => 'required|exists:sla_plan,id', + 'help_topic' => 'required|exists:help_topic,id', + 'ticket_source' => 'required|exists:ticket_source,id', + 'ticket_priority' => 'required|exists:ticket_priority,priority_id', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $ticket_id = $this->request->input('ticket_id'); + $result = $this->ticket->ticket_edit_post($ticket_id, $this->thread, $this->model); + return response()->json(compact('result')); + } catch (\Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Delete The Ticket + * @return json + */ + public function DeleteTicket() { + try { + $v = \Validator::make($this->request->all(), [ + 'ticket_id' => 'required|exists:tickets,id', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $id = $this->request->input('ticket_id'); + + $result = $this->ticket->delete($id, $this->model); + return response()->json(compact('result')); + } catch (\Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get all opened tickets + * @return json + */ + public function OpenedTickets() { + try { + $result = $this->model->where('status', '=', 1)->where('isanswered', '=', 0)->where('assigned_to', '=', 0)->orderBy('id', 'DESC')->get(); + return response()->json(compact('result')); + } catch (\Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get Unsigned Tickets + * @return json + */ + public function UnassignedTickets() { + try { + $result = $this->model->where('assigned_to', '=', 0)->where('status', '1')->orderBy('id', 'DESC')->get(); + return response()->json(compact('result')); + } catch (\Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get closed Tickets + * @return json + */ + public function CloseTickets() { + try { + $result = $this->model->where('status', '>', 1)->where('status', '<', 4)->orderBy('id', 'DESC')->get(); + return response()->json(compact('result')); + } catch (\Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get All agents + * @return json + */ + public function GetAgents() { + try { + $result = $this->faveoUser->where('role', 'agent')->where('active', 1)->get(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get All Teams + * @return json + */ + public function GetTeams() { + try { + $result = $this->team->get(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * To assign a ticket + * @return json + */ + public function AssignTicket() { + try { + $v = \Validator::make($this->request->all(), [ + 'ticket_id' => 'required', + 'user' => 'required', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $id = $this->request->input('ticket_id'); + $response = $this->ticket->assign($id); + if ($response == 1) { + $result = 'success'; + return response()->json(compact('result')); + } else { + return response()->json(compact('response')); + } + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get all customers + * @return json + */ + public function GetCustomers() { + try { + $v = \Validator::make($this->request->all(), [ + 'search' => 'required', + ]); + 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(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get a customer by id + * @return json + */ + public function GetCustomer() { + try { + $v = \Validator::make($this->request->all(), [ + 'user_id' => 'required', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $id = $this->request->input('user_id'); + $result = $this->faveoUser->where('id', $id)->first(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Search tickets + * @return json + */ + public function SearchTicket() { + try { + $v = \Validator::make($this->request->all(), [ + 'search' => 'required', + ]); + 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(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get threads of a ticket + * @return json + */ + public function TicketThreads() { + try { + $v = \Validator::make($this->request->all(), [ + 'id' => 'required', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $id = $this->request->input('id'); + $result = $this->thread->where('ticket_id', $id)->get(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Check the url is valid or not + * @return json + */ + public function CheckUrl() { + //dd($this->request); + try { + $v = \Validator::make($this->request->all(), [ + 'url' => 'required|url' + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + + $url = $this->request->input('url'); + $url = $url . "/api/v1/helpdesk/check-url?token=" . \Config::get('app.token'); + $result = $this->CallGetApi($url); + return response()->json(compact('result')); + } catch (Exception $ex) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Success for currect url + * @return string + */ + public function UrlResult() { + return "success"; + } + + /** + * Call curl function for Get Method + * @param type $url + * @return type int|string|json + */ + public function CallGetApi($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); + } + + return $response; + curl_close($curl); + } + + /** + * Call curl function for POST Method + * @param type $url + * @param type $data + * @return type int|string|json + */ + public function CallPostApi($url, $data) { + + $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); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + $response = curl_exec($curl); + + if (curl_errno($curl)) { + echo 'error:' . curl_error($curl); + } + + return $response; + curl_close($curl); + } + + /** + * To generate api string + * + * @return type | json + */ + public function GenerateApiKey() { + try { + $set = $this->setting->where('id', '1')->first(); + if ($set->api_enable == 1) { + $key = str_random(32); + $set->api_key = $key; + $set->save(); + $result = $set->api_key; + return response()->json(compact('result')); + } else { + $result = 'please enable api'; + return response()->json(compact('result')); + } + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get help topics + * @return json + */ + public function GetHelpTopic() { + try { + $result = $this->helptopic->get(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get Sla plans + * @return json + */ + public function GetSlaPlan() { + try { + $result = $this->slaPlan->get(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get priorities + * @return json + */ + public function GetPriority() { + try { + $result = $this->priority->get(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Get departments + * @return json + */ + public function GetDepartment() { + try { + $result = $this->department->get(); + return response()->json(compact('result')); + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + /** + * Getting the tickets + * @return type json + */ + public function GetTickets() { + try { + $tickets = $this->model->paginate(10); + $tickets->toJson(); + return $tickets; + } catch (Exception $e) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } catch (\TokenExpiredException $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + + /** + * Fetching the Inbox details + * @return type json + */ + + public function Inbox() { + try { + $result = $this->user->join('tickets', 'users.id', '=', 'tickets.user_id') + ->join('department', 'department.id', '=', 'tickets.dept_id') + ->join('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('first_name', 'last_name', 'email', 'profile_pic', 'ticket_number', 'tickets.id', 'title', 'tickets.created_at', 'department.name as department_name', 'priority.name as priotity_name', 'sla_plan.name as sla_plan_name', 'help_topic.topic as help_topic_name', 'ticket_status.name as ticket_status_name') + ->groupby('tickets.id') + ->distinct() + ->paginate(10) + ->toJson(); + return $result; + } catch (Exception $ex) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } + } + /** + * Create internal note + * @return type json + */ + public function InternalNote() { + try { + $v = \Validator::make($this->request->all(), [ + 'userid' => 'required|exists:users,id', + 'ticketid' => 'required|exists:tickets,id', + 'body' => 'required', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $userid = $this->request->input('userid'); + $ticketid = $this->request->input('ticketid'); + + $body = $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) { + $error = $e->getMessage(); + $line = $e->getLine(); + $file = $e->getFile(); + return response()->json(compact('error', 'file', 'line')); + } + } + + + +} diff --git a/app/Http/Controllers/Api/v1/InstallerApiController.php b/app/Http/Controllers/Api/v1/InstallerApiController.php new file mode 100644 index 000000000..5a4f672f3 --- /dev/null +++ b/app/Http/Controllers/Api/v1/InstallerApiController.php @@ -0,0 +1,156 @@ + + * + */ +class InstallerApiController extends Controller { + + /** + * config_database + * This function is to configure the database and install the application via API call. + * @return type Json + */ + public function config_database(Request $request) { + error_reporting(E_ALL & ~E_NOTICE); + + // Check for pre install + if (\Config::get('database.install') == '%0%') { + + $default = $request->database; + $host = $request->host; + $database = $request->databasename; + $dbusername = $request->dbusername; + $dbpassword = $request->dbpassword; + $port = $request->port; + + if(isset($default) && isset($host) && isset($database) && isset($dbusername)){ + + // Setting environment values + $_ENV['DB_TYPE'] = $default; + $_ENV['DB_HOST'] = $host; + $_ENV['DB_PORT'] = $port; + $_ENV['DB_DATABASE'] = $database; + $_ENV['DB_USERNAME'] = $dbusername; + $_ENV['DB_PASSWORD'] = $dbpassword; + + $config = ''; + foreach ($_ENV as $key => $val) { + $config .= "{$key}={$val}\n"; + } + + // Write environment file + $fp = fopen(base_path()."/.env", 'w'); + fwrite($fp, $config); + fclose($fp); + + return ['response'=>'success','status'=>'1']; + } else { + return ['response'=>'fail','reason'=>'insufficient parameters','status'=>'0']; + } + } else { + return ['response'=>'fail','reason'=>'this system is already installed','status'=>'0']; + } + } + + /** + * config_database + * This function is to configure the database and install the application via API call. + * @return type Json + */ + public function config_system(Request $request) { + // Check for pre install + if (\Config::get('database.install') == '%0%') { + $firstname = $request->firstname; + $lastname = $request->lastname; + $email = $request->email; + $username = $request->username; + $password = $request->password; + $timezone = $request->timezone; + $datetime = $request->datetime; + + // Migrate database + Artisan::call('migrate', array('--force' => true)); + Artisan::call('db:seed', array('--force' => true)); + + // var_dump($datetime); + // $date_time_format = Date_time_format::where('format','=',$datetime)->first(); + // // dd($date_time_format->id); + // if($date_time_format->id){ + // $date_time = 1; + // } else { + // return 'date time format not supported'; + // } + + // Creating minum settings + $system = System::where('id','=','1')->first(); + $system->time_zone = 1; + $system->date_time_format = 1; + $system->save(); + + // Creating user + $user = User::create(array( + 'first_name' => $firstname, + 'last_name' => $lastname, + 'email' => $email, + 'user_name' => $username, + 'password' => Hash::make($password), + 'active' => 1, + 'role' => 'admin', + 'assign_group' => 1, + 'primary_dpt' => 1, + )); + + // Setting database installed status + $value = '1'; + $install = app_path('../config/database.php'); + $datacontent = File::get($install); + $datacontent = str_replace('%0%', $value, $datacontent); + File::put($install, $datacontent); + + // Applying email configuration on route + $smtpfilepath = "\App\Http\Controllers\Common\SettingsController::smtp()"; + $path22 = app_path('Http/routes.php'); + $content23 = File::get($path22); + $content23 = str_replace('"%smtplink%"', $smtpfilepath, $content23); + File::put($path22, $content23); + + // If user created return success + if($user){ + return ['response'=>'success','status'=>'1']; + } + } else { + return ['response'=>'fail','reason'=>'this system is already installed','status'=>'0']; + } + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Api/v1/TestController.php b/app/Http/Controllers/Api/v1/TestController.php new file mode 100644 index 000000000..0f96b7573 --- /dev/null +++ b/app/Http/Controllers/Api/v1/TestController.php @@ -0,0 +1,378 @@ +server = "http://" . $_SERVER['HTTP_HOST'] . $server['path'] . "/"; + } + + static function CallGetApi($url) { + //dd($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); + } + + return $response; + curl_close($curl); + } + + static function CallPostApi($url, $data) { + //dd($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); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + $response = curl_exec($curl); + + if (curl_errno($curl)) { + echo 'error:' . curl_error($curl); + } + + return $response; + curl_close($curl); + } + + public function TicketReply() { + + $file = file_get_contents(base_path() . '/../lb-faveo/Img/Ladybird.png'); + + $data = [ + + 'ticket_ID' => '81', + 'ReplyContent' => 'reply for the ticket id', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg', + 'attachments' => [ + [ + 'name' => 'ladybird', + 'size' => '26398', + 'type' => 'png', + 'file' => $file, + ], + [ + 'name' => 'ladybird', + 'size' => '26398', + 'type' => 'png', + 'file' => $file, + ] + ], + ]; + $data = http_build_query($data, '', '&'); + + $url = $this->server . "helpdesk/reply?token=" . \Config::get('app.token'); + + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + + public function CreateTicket() { + + $file = file_get_contents(base_path() . '/../lb-faveo/Img/Ladybird.png'); + + $data = [ + 'user_id' => '1', + 'subject' => 'Api create via faveo api', + 'body' => 'Test me when call api', + 'helptopic' => '1', + 'sla' => '1', + 'priority' => '1', + 'headers' => [0 => 'vijaycodename47@gmail.com'], + 'dept' => '1', + 'assignto' => '0', + 'source' => 'api', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg', + /** if attachment */ + 'attachments' => [ + [ + 'name' => 'ladybird', + 'size' => '26398', + 'type' => 'png', + 'file' => $file, + ], + [ + 'name' => 'ladybird', + 'size' => '26398', + 'type' => 'png', + 'file' => $file, + ] + ], + ]; + $data = http_build_query($data, '', '&'); + + $url = $this->server . "helpdesk/create?token=" . \Config::get('app.token'); + + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + + public function GenerateToken() { + $data = [ + //'email'=>'vijaycodename47@gmail.com', + 'username' => 'vijay', + 'password' => 'manjapra', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + + $data = http_build_query($data, '', '&'); + + $url = $this->server . "authenticate"; + //dd($url); + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + + public function CreateUser() { + $data = [ + 'email' => 'vijaycodename@gmail.com', + 'password' => 'manjapra', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + + $data = http_build_query($data, '', '&'); + $url = $this->server . "register"; + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + + public function GetAuthUser() { + + $url = $this->server . "authenticate/user?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function EditTicket() { + $data = [ + 'ticket_id' => '81', + 'subject' => 'Api editing ticket via faveo api', + 'sla_plan' => '2', + 'help_topic' => '2', + 'ticket_source' => '2', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg', + 'ticket_priority' => '2', + ]; + + $data = http_build_query($data, '', '&'); + $url = $this->server . "helpdesk/edit?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + + public function DeleteTicket() { + $data = [ + 'ticket_id' => [], + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + + $data = http_build_query($data, '', '&'); + $url = $this->server . "helpdesk/delete?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + + public function OpenedTickets() { + $url = $this->server . "helpdesk/open?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function UnassignedTickets() { + $url = $this->server . "helpdesk/unassigned?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function CloseTickets() { + $url = $this->server . "helpdesk/closed?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetAgents() { + $url = $this->server . "helpdesk/agents?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetTeams() { + $url = $this->server . "helpdesk/teams?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function AssignTicket() { + $data = [ + 'ticket_id' => '8', + 'user' => 'vijay.sebastian@ladybird.com', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + $data = http_build_query($data, '', '&'); + $url = $this->server . "helpdesk/assign?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + + public function GetCustomers() { + $search = [ + 'search' => 'vij', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + $data = http_build_query($search, '', '&'); + $url = $this->server . "helpdesk/customers?token=" . \Config::get('app.token'); + $url = $url . '&' . $data; + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetCustomer() { + $search = [ + 'user_id' => '1', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + $data = http_build_query($search, '', '&'); + $url = $this->server . "helpdesk/customer?token=" . \Config::get('app.token'); + $url = $url . '&' . $data; + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetSearch() { + $search = [ + 'search' => 'api', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + $data = http_build_query($search, '', '&'); + $url = $this->server . "helpdesk/ticket-search?token=" . \Config::get('app.token'); + $url = $url . '&' . $data; + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function TicketThreads() { + $search = [ + 'id' => '8', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + $data = http_build_query($search, '', '&'); + $url = $this->server . "helpdesk/ticket-thread?token=" . \Config::get('app.token'); + $url = $url . '&' . $data; + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function Url() { + $search = [ + 'url' => 'http://localhost/FaveoHELPDESK+KBLatests', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + $data = http_build_query($search, '', '&'); + $url = $this->server . "helpdesk/url?token=" . \Config::get('app.token'); + $url = $url . '&' . $data; + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GenerateApiKey() { + $url = $this->server . "helpdesk/api-key?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetHelpTopic() { + $url = $this->server . "helpdesk/help-topic?api-key=clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg&token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetSlaPlan() { + $url = $this->server . "helpdesk/sla-plan?api-key=clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg&token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetPriority() { + $url = $this->server . "helpdesk/priority?api-key=clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg&token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetDepartment() { + $url = $this->server . "helpdesk/department?api-key=clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg&token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function GetTickets() { + $url = $this->server . "helpdesk/tickets?api-key=clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg&token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function Inbox() { + $url = $this->server . "helpdesk/inbox?api-key=clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg&token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallGetApi($url); + return $respose; + } + + public function InternalNote() { + $data = [ + 'ticketid' => '23', + 'userid' => 1, + 'body' => 'Testing the api internal note', + 'api-key' => 'clYbe1g7BYVEJznBdvCEBR0xDCLDqKgg' + ]; + $data = http_build_query($data, '', '&'); + $url = $this->server . "helpdesk/internal-note?token=" . \Config::get('app.token'); + $_this = new self(); + $respose = $_this->CallPostApi($url, $data); + return $respose; + } + +} diff --git a/app/Http/Controllers/Api/v1/TicketController.php b/app/Http/Controllers/Api/v1/TicketController.php new file mode 100644 index 000000000..9448bc237 --- /dev/null +++ b/app/Http/Controllers/Api/v1/TicketController.php @@ -0,0 +1,519 @@ + + * @copyright (c) 2016, Ladybird Web Solution + * @name Faveo HELPDESK + * @version v1 + * + * + */ + +class TicketController extends Controller { + + /** + * Create Ticket + * @param type $user_id + * @param type $subject + * @param type $body + * @param type $helptopic + * @param type $sla + * @param type $priority + * @return type string + */ + public function create_ticket($user_id, $subject, $body, $helptopic, $sla, $priority, $source, $headers, $dept, $assignto, $form_data, $attach = '') { + try { + $max_number = Tickets::whereRaw('id = (select max(`id`) from tickets)')->first(); + if ($max_number == null) { + $ticket_number = "AAAA-9999-9999999"; + } else { + foreach ($max_number as $number) { + $ticket_number = $max_number->ticket_number; + } + } + $ticket = new Tickets; + $ticket->ticket_number = $this->ticket_number($ticket_number); + $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->save(); + $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; + $form_value->ticket_id = $id; + $form_value->title = $from->label; + $form_value->content = $form_details; + $form_value->save(); + } + } + } + } + + + + $this->store_collaborators($headers, $id); + + $thread = $this->ticket_thread($subject, $body, $id, $user_id); + if (!empty($attach)) { + $this->attach($thread, $attach); + } + return $thread; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * store_collaborators + * @param type $headers + * @return type + */ + public function store_collaborators($headers, $id) { + try { + $company = $this->company(); + if (isset($headers)) { + foreach ($headers as $email => $name) { + $name = $name; + $email = $email; + if ($this->check_email($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->save(); + $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'); + }); + } else { + $user = $this->check_email($email); + $user_id = $user->id; + } + $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->save(); + } + } + return true; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * Generate Ticket Thread + * @param type $subject + * @param type $body + * @param type $id + * @param type $user_id + * @return type + */ + public function ticket_thread($subject, $body, $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->save(); + return $thread->id; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * Generates Ticket Number + * @param type $ticket_number + * @return type integer + */ + public function ticket_number($ticket_number) { + try { + $number = $ticket_number; + $number = explode('-', $number); + $number1 = $number[0]; + if ($number1 == 'ZZZZ') { + $number1 = 'AAAA'; + } + $number2 = $number[1]; + if ($number2 == '9999') { + $number2 = '0000'; + } + $number3 = $number[2]; + if ($number3 == '9999999') { + $number3 = '0000000'; + } + $number1++; + $number2++; + $number3++; + $number2 = sprintf('%04s', $number2); + $number3 = sprintf('%07s', $number3); + $array = array($number1, $number2, $number3); + $number = implode('-', $array); + return $number; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * Generate a random string for password + * @param type $length + * @return type string + */ + public function generateRandomString($length = 10) { + try { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * Replying a ticket + * @param type Ticket_Thread $thread + * @param type TicketRequest $request + * @return type bool + */ + public function reply($thread, $request, $ta, $attach = '') { + try { + + $check_attachment = null; + $eventthread = $thread->where('ticket_id', $request->input('ticket_ID'))->first(); + $eventuserid = $eventthread->user_id; + $emailadd = User::where('id', $eventuserid)->first()->email; + $source = $eventthread->source; + $form_data = $request->except('ReplyContent', 'ticket_ID', 'attachment'); + \Event::fire(new \App\Events\ClientTicketFormPost($form_data, $emailadd, $source)); + $reply_content = $request->input('ReplyContent'); + $thread->ticket_id = $request->input('ticket_ID'); + $thread->poster = 'support'; + $thread->body = $request->input('ReplyContent'); + $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) { + $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->is_internal = 1; + $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'; + $tickets->isanswered = '1'; + $tickets->save(); + } + $thread->save(); + + + if (!empty($attach)) { + $check_attachment = $this->attach($thread->id, $attach); + } + + $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)) { + $agentsign = Auth::user()->agent_sign; + } else { + $agentsign = null; + } + \Event::fire(new \App\Events\FaveoAfterReply($reply_content, $user->phone_number, $request, $tickets)); + + Mail::send(array('html' => 'emails.ticket_re-reply'), ['content' => $reply_content, 'ticket_number' => $ticket_number, 'From' => $company, 'name' => $username, 'Agent_Signature' => $agentsign], function ($message) use ($email, $user_name, $ticket_number, $ticket_subject, $check_attachment) { + $message->to($email, $user_name)->subject($ticket_subject . '[#' . $ticket_number . ']'); + // if(isset($attachments)){ +// if ($check_attachment == 1) { +// $size = count($attach); +// for ($i = 0; $i < $size; $i++) { +// $message->attach($attachments[$i]->getRealPath(), ['as' => $attachments[$i]->getClientOriginalName(), 'mime' => $attachments[$i]->getClientOriginalExtension()]); +// } +// } + }, true); + + + $collaborators = Ticket_Collaborator::where('ticket_id', '=', $ticket_id)->get(); + 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_user_name = $user_id_collab->user_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 . ']'); +// if ($check_attachment == 1) { +// $size = sizeOf($attachments); +// for ($i = 0; $i < $size; $i++) { +// $message->attach($attachments[$i]->getRealPath(), ['as' => $attachments[$i]->getClientOriginalName(), 'mime' => $attachments[$i]->getClientOriginalExtension()]); +// } +// } + }, true); + } + return $thread; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * company + * @return type + */ + public function company() { + try { + $company = Company::Where('id', '=', '1')->first(); + if ($company->company_name == null) { + $company = "Support Center"; + } else { + $company = $company->company_name; + } + return $company; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * Ticket edit and save ticket data + * @param type $ticket_id + * @param type Ticket_Thread $thread + * @return type bool + */ + public function ticket_edit_post($ticket_id, $thread, $ticket) { + try { + + $ticket = $ticket->where('id', '=', $ticket_id)->first(); + + $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->save(); + + $threads = $thread->where('ticket_id', '=', $ticket_id)->first(); + $threads->title = Input::get("subject"); + $threads->save(); + return $threads; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * function to assign ticket + * @param type $id + * @return type bool + */ + public function assign($id) { + try { + $UserEmail = Input::get('user'); + //dd($id); + // $UserEmail = 'sujitprasad12@yahoo.in'; + $user = User::where('email', '=', $UserEmail)->first(); + $user_id = $user->id; + $ticket = Tickets::where('id', '=', $id)->first(); + $ticket_number = $ticket->ticket_number; + $ticket->assigned_to = $user_id; + $ticket->save(); + $ticket_thread = Ticket_Thread::where('ticket_id', '=', $id)->first(); + $ticket_subject = $ticket_thread->title; + $thread = New Ticket_Thread; + $thread->ticket_id = $ticket->id; + $thread->user_id = Auth::user()->id; + $thread->is_internal = 1; + $thread->body = "This Ticket has been assigned to " . $user->first_name . " " . $user->last_name; + $thread->save(); + + $company = $this->company(); + $system = $this->system(); + + $agent = $user->first_name; + $agent_email = $user->email; + + $master = Auth::user()->first_name . " " . Auth::user()->last_name; + if (Alert::first()->internal_status == 1 || Alert::first()->internal_assigned_agent == 1) { + // ticket assigned send mail + Mail::send('emails.Ticket_assign', ['agent' => $agent, 'ticket_number' => $ticket_number, 'from' => $company, 'master' => $master, 'system' => $system], function ($message) use ($agent_email, $agent, $ticket_number, $ticket_subject) { + $message->to($agent_email, $agent)->subject($ticket_subject . '[#' . $ticket_number . ']'); + }); + } + + return 1; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * Function to delete ticket + * @param type $id + * @param type Tickets $ticket + * @return type string + */ + public function delete($ids, $ticket) { + + try { + foreach ($ids as $id) { + $ticket_delete = $ticket->where('id', '=', $id)->first(); + 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) { + $ticket_thread->delete(); + } + } + } + $ticket_attachments = Ticket_attachments::where('thread_id', '=', $id)->get(); + if ($ticket_attachments) { + foreach ($ticket_attachments as $ticket_attachment) { + if ($ticket_attachment) { + $ticket_attachment->delete(); + } + } + } + } else { + $ticket_delete->is_deleted = 0; + $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; + $thread->save(); + } + } else { + return "ticket not found"; + } + } + return "your tickets has been deleted"; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * check email for dublicate entry + * @param type $email + * @return type bool + */ + public function check_email($email) { + try { + $check = User::where('email', '=', $email)->first(); + if ($check == true) { + return $check; + } else { + return false; + } + } catch (\Exception $e) { + return $e->getMessage(); + } + } + + /** + * system + * @return type + */ + public function system() { + try { + $system = System::Where('id', '=', '1')->first(); + if ($system->name == null) { + $system = "Support Center"; + } else { + $system = $system->name; + } + return $system; + } catch (\Exception $e) { + return $e->getMessage(); + } + } + /** + * Create Attachment + * @param type $thread + * @param type $attach + * @return int + */ + public function attach($thread, $attach) { + try { + $ta = new Ticket_attachments(); + 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) { + return $e->getMessage(); + } + } + +} diff --git a/app/Http/Controllers/Api/v1/TokenAuthController.php b/app/Http/Controllers/Api/v1/TokenAuthController.php new file mode 100644 index 000000000..4a59c81d5 --- /dev/null +++ b/app/Http/Controllers/Api/v1/TokenAuthController.php @@ -0,0 +1,136 @@ + + * @copyright (c) 2016, Ladybird Web Solution + * @name Faveo HELPDESK + * @version v1 + * + * + */ + +class TokenAuthController extends Controller { + + public function __construct() { + $this->middleware('api'); + } + /** + * Authenticating user with username and password and retuen token + * @param Request $request + * @return type json + */ + public function authenticate(Request $request) { + $usernameinput = $request->input('username'); + $password = $request->input('password'); + $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name'; + + //$credentials = $request->only('email', 'password'); + + try { + if (!$token = JWTAuth::attempt([$field => $usernameinput, 'password' => $password])) { + return response()->json(['error' => 'invalid_credentials', 'status_code'=>401]); + } + } catch (JWTException $e) { + return response()->json(['error' => 'could_not_create_token', 500]); + } catch (\Exception $e){ + $error = $e->getMessage(); + return response()->json(compact('error')); + } + + // if no errors are encountered we can return a JWT + return response()->json(compact('token')); + } + /** + * Get the user details from token + * @return type json + */ + public function getAuthenticatedUser() { + //dd(JWTAuth::parseToken()->authenticate()); + try { + + if (!$user = JWTAuth::parseToken()->authenticate()) { + return response()->json(['user_not_found', 404]); + } + } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) { + + return response()->json(['token_expired', $e->getStatusCode()]); + } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) { + + return response()->json(['token_invalid', $e->getStatusCode()]); + } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) { + + return response()->json(['token_absent', $e->getStatusCode()]); + } + catch (\Exception $e){ + $error = $e->getMessage(); + return response()->json(compact('error')); + } + //dd($user); + return response()->json(compact('user')); + } + + /** + * Register a user with username and password + * @param Request $request + * @return type json + */ + public function register(Request $request) { + try { + $v = \Validator::make($request->all(), [ + 'email' => 'required|email|unique:users', + 'password' => 'required', + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + $newuser = $request->all(); + $password = Hash::make($request->input('password')); + + $newuser['password'] = $password; + + return User::create($newuser); + } catch (\Exception $e) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + /** + * verify the url is existing or not + * @return type json + */ + public function CheckUrl() { + try { + $v = \Validator::make($request->all(), [ + 'url' => 'required|url' + ]); + if ($v->fails()) { + $error = $v->errors(); + return response()->json(compact('error')); + } + + $url = $this->request->input('url'); + $url = $url.'/api/v1/helpdesk/check-url'; + + } catch (Exception $ex) { + $error = $e->getMessage(); + return response()->json(compact('error')); + } + } + +}