mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/izyim-api.git
synced 2026-08-19 04:24:01 +00:00
103 lines
3.6 KiB
PHP
103 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Contact;
|
|
use App\Company;
|
|
use Illuminate\Http\Request;
|
|
use Auth;
|
|
|
|
class ContactController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
//return Auth::user()->company()->first()->id;
|
|
$output= array();
|
|
// $contacts = Contact::where('sender_id', Auth::user()->id)->get();
|
|
//$companies= Contact::with('companies')->get();
|
|
$contacts =\DB::table('contacts')->select('contacts.id as contact_id', 'sender_company.company_name as sender_company_name', 'sender_company.id as sender_company_id', 'recipient_company.id as recipient_company_id', 'recipient_company.company_name as recipient_company_name', 'contacts.status', 'sender_company.tel_no as sender_tel_no', 'recipient_company.tel_no as recipient_tel_no', 'sender_company.contact_person as sender_contact_pers', 'recipient_company.contact_person as recipient_contact_pers')->leftJoin('companies as sender_company', 'sender_company.id', '=', 'contacts.sender_id')->leftJoin('companies as recipient_company', 'recipient_company.id', '=', 'contacts.recipient_id')->where('contacts.sender_id', Auth::user()->company()->first()->id)->orWhere('contacts.recipient_id', Auth::user()->company()->first()->id)->get();
|
|
|
|
if (!$contacts)
|
|
{
|
|
return response()->json(['message'=>'Access Denied!'], 404);
|
|
}
|
|
|
|
$contacts2 = $contacts->toArray();
|
|
// $toprint = $contacts2[0]->sender_company_name;
|
|
// echo $toprint;
|
|
$length = count($contacts2);
|
|
|
|
for ($i=0; $i < $length; $i++) {
|
|
# code...
|
|
if($contacts2[$i]->sender_company_id == Auth::user()->company()->first()->id){
|
|
$contacts2[$i]->is_me = 1;
|
|
}
|
|
else{
|
|
$contacts2[$i]->is_me = 0;
|
|
}
|
|
}
|
|
|
|
return response()->json($contacts2, 200);
|
|
}
|
|
|
|
public function sendRequest(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
$recipient_id = $request->input('recipient_id');
|
|
|
|
// query to find company asisng to $company
|
|
$company = Company::find($recipient_id);
|
|
|
|
// get user from session
|
|
$request->user();
|
|
$user = $request->user();
|
|
|
|
// prepare query
|
|
$contact = new Contact();
|
|
$contact->recipient_id = $company->id;
|
|
$contact->sender_id = $user->id;
|
|
$contact->save();
|
|
|
|
return response()->json($contact, 201);
|
|
}
|
|
|
|
public function acceptRequest(Request $request)
|
|
{
|
|
$contact = Contact::where('id', $request->input('id'))->first();//->where('id', $id)->first();
|
|
//$contact = Contact::where('id', '=', e($id))->first();
|
|
if($contact)
|
|
{
|
|
$contact->status=1;
|
|
|
|
$saved = $contact->save();
|
|
if ($saved){
|
|
return response()->json(["success"=> true, "message" => "Successfully accepted"], 201);
|
|
}
|
|
else{
|
|
return response()->json($contact, ["message" => "not saved"], 400);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return response()->json( ["message" => "not allowed"], 400);
|
|
}
|
|
}
|
|
|
|
public function destroy(Request $request)
|
|
{
|
|
$contact = Contact::where('id', $request->input('id'))->first();
|
|
//$contact = Contact::where('com_id', Auth::user()->company())->where('id', $id)->first();
|
|
|
|
if (!$contact)
|
|
{
|
|
return response()->json(['message'=>'Access Denied!'], 404);
|
|
}
|
|
else
|
|
{
|
|
$contact->delete();
|
|
}
|
|
|
|
return response()->json($contact, 204);
|
|
}
|
|
}
|