179 lines
5.4 KiB
PHP
179 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\RemoteContact;
|
|
use App\RemoteContactApi;
|
|
use App\Contact;
|
|
use Validator;
|
|
|
|
class RemoteContactController extends Controller
|
|
{
|
|
/**
|
|
* private filter function
|
|
* @return RemoteContact model
|
|
*/
|
|
private function search($str, $direction=2)
|
|
{
|
|
$a = RemoteContact::where('archive', 0);
|
|
if(strlen(trim($str)) > 0)
|
|
{
|
|
$a = $a->where(function($query) use ($str){
|
|
$query->orWhere('login', 'LIKE', '%'. $str .'%')
|
|
->orWhere('description', 'LIKE', '%'. $str .'%');
|
|
});
|
|
|
|
$a = $a->orWhereHas('contact', function($query) use ($str){
|
|
$query->where('organization_name', 'LIKE', '%'. $str .'%')
|
|
->where('alternative_name', 'LIKE', '%'. $str .'%');
|
|
});
|
|
}
|
|
if($direction != 2)
|
|
return $a->where('direction', $direction);
|
|
|
|
return $a;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$search = $request->input('search');
|
|
$direction = $request->input('direction') ?? 2;
|
|
$row = $this->search($search, $direction);
|
|
$row = $row->orderBy('id', 'desc')->with('contact:id,organization_name')->paginate(100)->appends([
|
|
'search' => $search,
|
|
'direction' => $direction,
|
|
]);
|
|
|
|
return view('admin.remote_contacts.index')->with('remotecontacts', $row)->with('search', $search)->with('direction', $direction);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
// $remotecontact = new RemoteContact;
|
|
$contacts = Contact::where('status', 1)->where('is_approved', 1)->get(['id', 'organization_name']);
|
|
|
|
return view('admin.remote_contacts.create')->with('contacts', $contacts);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validate = $this->rules();
|
|
if($validate->fails())
|
|
return back()->with('prev-url', $request->input('prev-url'))->withErrors($validate)->withInput();
|
|
|
|
$remotecontact = new RemoteContact($validate->valid());
|
|
if($remotecontact->save())
|
|
{
|
|
\Session::flash('success_message', 'RemoteContact has been added successfully');
|
|
return redirect()->route('remote_contacts.index');
|
|
}
|
|
|
|
return back()->with('prev-url', $request->input('prev-url'))->with('error', 'action create error')->withInput();
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
return back();
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(RemoteContact $remotecontact)
|
|
{
|
|
if($remotecontact->archive == 1)
|
|
return back()->with('error', 'already archived');
|
|
|
|
return view('admin.remote_contacts.edit')->with('remotecontact', $remotecontact);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, RemoteContact $remotecontact)
|
|
{
|
|
$validate = $this->updateRules();
|
|
|
|
if($validate->fails())
|
|
return back()->with('prev-url', $request->input('prev-url'))->withErrors($validate)->withInput();
|
|
|
|
$remotecontact->update($validate->valid());
|
|
|
|
if($remotecontact->save())
|
|
{
|
|
\Session::flash('success_message', 'Priority has been updated successfully');
|
|
return redirect()->route('remote_contacts.index');
|
|
}
|
|
|
|
return back()->with('prev-url', $request->input('prev-url'))->with('error', 'action create error')->withInput();
|
|
}
|
|
|
|
public function destroy(Request $request, RemoteContact $remotecontact)
|
|
{
|
|
$remotecontact->archive = 1;
|
|
$remotecontact->login = $remotecontact->login . '__' . now();
|
|
if($remotecontact->save())
|
|
{
|
|
RemoteContactApi::where('remote_contact_id', $remotecontact->id)->update(['archive' => 1]);
|
|
\Session::flash('success_message', 'Remote Contact deleted successfully');
|
|
return redirect()->route('remote_contacts.index');
|
|
}
|
|
|
|
return back();
|
|
}
|
|
|
|
private function rules()
|
|
{
|
|
return Validator::make(request()->all(), [
|
|
'contact_id' => 'required',
|
|
'direction' => 'required',
|
|
'login' => 'required',
|
|
'password' => 'required',
|
|
'public_key' => 'nullable',
|
|
'description' => 'nullable | max:255',
|
|
]);
|
|
}
|
|
|
|
private function updateRules()
|
|
{
|
|
return Validator::make(request()->all(), [
|
|
'direction' => 'required',
|
|
'login' => 'required',
|
|
'password' => 'required',
|
|
'public_key' => 'nullable',
|
|
'description' => 'nullable | max:255',
|
|
]);
|
|
}
|
|
}
|