202 lines
5.2 KiB
PHP
202 lines
5.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Contact;
|
||
|
|
use App\Setting;
|
||
|
|
use Session;
|
||
|
|
use DB;
|
||
|
|
use Auth;
|
||
|
|
|
||
|
|
class ContactsController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Create a new controller instance.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->middleware('auth:web');
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* Display a listing of the resource.
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function search(Request $request)
|
||
|
|
{
|
||
|
|
$input = $request->all();
|
||
|
|
$request->session()->put('search_val', $input['search']);
|
||
|
|
return redirect()->route('contact-list');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index(Request $request)
|
||
|
|
{
|
||
|
|
$setting = Setting::first();
|
||
|
|
if(isset($setting->data_limit_per_page) && $setting->data_limit_per_page > 0)
|
||
|
|
{
|
||
|
|
$limit_val = $setting->data_limit_per_page;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
$limit_val = 20;
|
||
|
|
}
|
||
|
|
$search_val = null;
|
||
|
|
if($request->session()->has('search_val')){
|
||
|
|
$search_val = $request->session()->get('search_val');
|
||
|
|
$request->session()->forget('search_val');
|
||
|
|
}
|
||
|
|
|
||
|
|
if($request->ajax())
|
||
|
|
$search_val = $request->input('search');
|
||
|
|
|
||
|
|
if($search_val !== null)
|
||
|
|
{
|
||
|
|
$contacts = Contact::orderBy('id', 'desc')
|
||
|
|
->where('organization_name', 'like', '%'.$search_val.'%')
|
||
|
|
->orWhere('alternative_name', 'like', '%'.$search_val.'%')
|
||
|
|
->orWhere('address', 'like', '%'.$search_val.'%')
|
||
|
|
->orWhere('telephone_number', 'like', '%'.$search_val.'%')
|
||
|
|
->paginate($limit_val)->appends([
|
||
|
|
'search_val' => $search_val
|
||
|
|
]);
|
||
|
|
}else{
|
||
|
|
$contacts = Contact::orderBy('id', 'desc')->paginate($limit_val);
|
||
|
|
}
|
||
|
|
|
||
|
|
if($request->ajax())
|
||
|
|
return response()->json($contacts->map(function($item, $key){
|
||
|
|
return $item->only(['id', 'organization_name']);
|
||
|
|
}));
|
||
|
|
|
||
|
|
return view('contacts.index', compact('contacts', 'search_val'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the form for creating a new resource.
|
||
|
|
*
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function create()
|
||
|
|
{
|
||
|
|
return view('contacts.create');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Store a newly created resource in storage.
|
||
|
|
*
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function store(Request $request)
|
||
|
|
{
|
||
|
|
$validatedData = $this->validate($request, [
|
||
|
|
'organization_name' => 'required|unique:contacts,organization_name|max:150',
|
||
|
|
'alternative_name' => 'max:150',
|
||
|
|
'address' => 'required',
|
||
|
|
'telephone_number' => 'required|numeric',
|
||
|
|
]);
|
||
|
|
$input = $request->all();
|
||
|
|
if(Auth::user()->department_id != 7):
|
||
|
|
$input['is_approved']='0';
|
||
|
|
endif;
|
||
|
|
$saved_data=Contact::create($input);
|
||
|
|
if($saved_data)
|
||
|
|
{
|
||
|
|
Session::flash('success_message', __('Contact has been added successfully'));
|
||
|
|
return redirect()->route('contact-list');
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->route('add-contact-list');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display the specified resource.
|
||
|
|
*
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function show($id)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the form for editing the specified resource.
|
||
|
|
*
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function edit($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$contact = Contact::findOrFail($id);
|
||
|
|
if($contact):
|
||
|
|
return view('contacts.edit', compact('contact'));
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', __('Invalid contact id.'));
|
||
|
|
return redirect()->route('contact-list');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the specified resource in storage.
|
||
|
|
*
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function update(Request $request, $id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$contact = Contact::findOrFail($id);
|
||
|
|
$validatedData = $this->validate($request, [
|
||
|
|
'organization_name' => 'required|unique:contacts,organization_name,'.$id.'|max:150',
|
||
|
|
'alternative_name' => 'max:150',
|
||
|
|
'address' => 'required',
|
||
|
|
'telephone_number' => 'required|numeric',
|
||
|
|
]);
|
||
|
|
$input = $request->all();
|
||
|
|
$saved_data=$contact->fill($input)->save();
|
||
|
|
if($saved_data):
|
||
|
|
Session::flash('success_message', __('Contact has been updated successfully'));
|
||
|
|
return redirect()->route('contact-list');
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->route('edit-contact-list');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified resource from storage.
|
||
|
|
*
|
||
|
|
* @param int $id
|
||
|
|
* @return \Illuminate\Http\Response
|
||
|
|
*/
|
||
|
|
public function statusupdate($id)
|
||
|
|
{
|
||
|
|
$id=base64_decode($id);
|
||
|
|
$find_contact = Contact::findOrFail($id);
|
||
|
|
if($find_contact->status==1)
|
||
|
|
$status=0;
|
||
|
|
else
|
||
|
|
$status=1;
|
||
|
|
$input=[
|
||
|
|
'status'=>$status
|
||
|
|
];
|
||
|
|
$saved_data=$find_contact->fill($input)->save();
|
||
|
|
if($saved_data):
|
||
|
|
Session::flash('success_message', __('Contact has been updated successfully'));
|
||
|
|
return redirect()->route('contacts');
|
||
|
|
else:
|
||
|
|
Session::flash('error_message', __('We are having some problem. Please try later.'));
|
||
|
|
return redirect()->route('contacts');
|
||
|
|
endif;
|
||
|
|
}
|
||
|
|
}
|