edms2023/app/Http/Controllers/Admin/RemoteContactApiController.php

147 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\RemoteContact;
use App\RemoteContactApi;
use Validator;
class RemoteContactApiController extends Controller
{
/**
* private filter function
* @return RemoteContactApi model
*/
private function search($str)
{
$a = RemoteContactApi::where('archive', 0);
if(strlen(trim($str)) > 0)
{
$a = $a->where(function($query) use ($str){
$query->orWhere('method', 'LIKE', '%'. $str .'%')
->orWhere('description', 'LIKE', '%'. $str .'%');
});
}
return $a;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request, RemoteContact $remotecontact)
{
if($remotecontact->direction == 0)
return back();
$search = $request->input('search');
$row = $this->search($search)->where('remote_contact_id', $remotecontact->id);
$row = $row->orderBy('id', 'desc')->with('remotecontact.contact:id,organization_name')->paginate(100)->appends([
'search' => $search,
]);
return view('admin.remote_contacts_api.index')->with('remotecontactsapi', $row)->with('remotecontact', $remotecontact)->with('search', $search);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(RemoteContact $remotecontact)
{
if($remotecontact->direction == 0)
return back();
return view('admin.remote_contacts_api.create')->with('remotecontact', $remotecontact);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, RemoteContact $remotecontact)
{
if($remotecontact->direction == 0)
return back();
$validate = $this->rules();
if($validate->fails())
return back()->with('prev-url', $request->input('prev-url'))->withErrors($validate)->withInput();
$remotecontactapi = new RemoteContactApi($validate->valid());
$remotecontactapi->remote_contact_id = $remotecontact->id;
if($remotecontactapi->save())
{
\Session::flash('success_message', 'API added successfully');
return redirect()->route('remote_contacts_api.index', $remotecontact);
}
return back()->with('prev-url', $request->input('prev-url'))->with('error', 'action create error')->withInput();
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(RemoteContactApi $remotecontactapi)
{
return view('admin.remote_contacts_api.edit')->with('remotecontactapi', $remotecontactapi);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, RemoteContactApi $remotecontactapi)
{
$validate = $this->rules();
if($validate->fails())
return back()->with('prev-url', $request->input('prev-url'))->withErrors($validate)->withInput();
$remotecontactapi->update($validate->valid());
if($remotecontactapi->save())
{
\Session::flash('success_message', 'API updated successfully');
return redirect()->route('remote_contacts_api.index', $remotecontactapi->remotecontact);
}
return back()->with('prev-url', $request->input('prev-url'))->with('error', 'action create error')->withInput();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(RemoteContactApi $remotecontactapi)
{
$remotecontactapi->archive = 1;
$remotecontactapi->connection_string= $remotecontactapi->connection_string . '__' . now();
if($remotecontactapi->save())
{
\Session::flash('success_message', 'RemoteContactApi deleted successfully');
return redirect()->route('remote_contacts_api.index', $remotecontactapi->remotecontact);
}
return back();
}
private function rules()
{
return Validator::make(request()->all(), [
'connection_string' => 'required',
'connection_method' => 'required',
'mandatory_fields' => 'required',
]);
}
}