changed the listing function on contacts frontend, still got issues

This commit is contained in:
Aqeeb Imtiaz Harun
2018-07-06 18:05:12 +08:00
parent 5412e1aa50
commit 1759b088a3
10 changed files with 269 additions and 150 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ class Contact extends Model
public function user() {
return $this->belongsTo(App/User);
return $this->belongsTo('App\User');
}
+19 -3
View File
@@ -16,14 +16,13 @@ class CompanyController extends Controller
return '';
}
$output= array();
$matchedTerms = Company::where('company_name', 'LIKE','%'. $request->search . '%')->get();
$matchedTerms = Company::where('company_name', 'LIKE','%'. $request->search . '%')->where('id', '<>', Auth::user()->company()->first()->id)->get();
if($matchedTerms)
{
foreach ($matchedTerms as $key => $matchedTerm)
{
array_push($output, ['com_id'=> $matchedTerm->id, 'company_name' => $matchedTerm->company_name, 'tel_no' => $matchedTerm->tel_no,
'contact_person'=>$matchedTerm->contact_person ]);
array_push($output, ['recipient_id'=> $matchedTerm->id, 'company_name' => $matchedTerm->company_name, 'tel_no' => $matchedTerm->tel_no, 'contact_person'=>$matchedTerm->contact_person ]);
}
return response($output);
@@ -34,6 +33,7 @@ class CompanyController extends Controller
public function update(Request $request)
{
$company = Company::where("user_id", Auth::user()->id)->first();
if (!$company)
{
return response()->json(['success'=> false, 'message'=>'Company not found'], 404);
@@ -59,6 +59,22 @@ class CompanyController extends Controller
return response()->json(['success'=> false, 'error'=> 'All the fields are required'], 400);
//return response()->json(['success'=> false, 'error'=> $validator->messages()], 400);
}
$company->company_name=$request->input('company_name');
$company->registration_no=$request->input('registration_no');
$company->tax_no=$request->input('tax_no');
$company->tel_no=$request->input('tel_no');
$company->fax=$request->input('fax');
$company->address=$request->input('address');
$company->city=$request->input('city');
$company->postcode=$request->input('postcode');
$company->state=$request->input('state');
$company->country=$request->input('country');
$company->contact_person=$request->input('contact_person');
$company->save();
return response()->json(['success'=> true, 'message'=>'Company created successfully'],200);
}
public function create()
+51 -35
View File
@@ -11,32 +11,51 @@ class ContactController extends Controller
{
public function index()
{
//return Auth::user()->company()->first()->id;
$output= array();
$contacts = Contact::where('user_id', Auth::user()->id)->get();
$companies= Contact::with('companies')->get();
//$companies =\DB::table('companies')->select('contacts.com_id', 'companies.id', 'companies.company_name')->join('contacts', 'contacts.com_id', '=', 'companies.id')->get();
//$companies = "SELECT contacts.com_id, companies.id, companies.company_name FROM `companies` INNER JOIN contacts ON contacts.com_id = companies.id";
// $companies = \DB::table('companies')
// ->select('id', 'company_name')
// ->where('com_id', Auth::user()->id)
// ->get();
// $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);
}
return response()->json($companies, 200);
$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;
}
}
// foreach ($contacts2 as $contact => $value) {
// print_r($value->sender_company_name);
// //if($value['sender_company_id'] == Auth::user()->company()->first()->id){
// // var_dump($contact);
// //print_r($value['sender_company_id']);
// //}
// //$contact->helo
// }
return response()->json($contacts2, 200);
}
public function sendRequest(Request $request)
{
$user = Auth::user();
$com_id = $request->input('com_id');
$recipient_id = $request->input('recipient_id');
// query to find company asisng to $company
$company = Company::find($com_id);
$company = Company::find($recipient_id);
// get user from session
$request->user();
@@ -44,51 +63,48 @@ class ContactController extends Controller
// prepare query
$contact = new Contact();
$contact->com_id = $company->id;
$contact->user_id = $user->id;
$contact->recipient_id = $company->id;
$contact->sender_id = $user->id;
$contact->save();
return response()->json($contact, 201);
}
public function acceptRequest(Request $request, $id)
public function acceptRequest(Request $request)
{
$contact = Contact::where('com_id', Auth::user()->company())->where('id', $id)->first();
$contact = Contact::where('id', $request->input('id'))->first();//->where('id', $id)->first();
//$contact = Contact::where('id', '=', e($id))->first();
if($contact)
{
$action=$request->input('action');
if ($action==1)
{
$contact->status=1;
}
if ($action==0)
{
$contact->status=0;
}
else{
return response()->json(["message" => "not allowed"], 400);
}
$contact->status=1;
$saved = $contact->save();
if ($saved){
return response()->json($contact, 201);
return response()->json(["success"=> true, "message" => "Successfully accepted"], 201);
}
return response()->json($contact, 400);
else{
return response()->json($contact, ["message" => "not saved"], 400);
}
}
else
{
return response()->json( ["message" => "not allowed"], 400);
}
}
public function destroy($id)
public function destroy(Request $request)
{
$contact = Contact::where('com_id', Auth::user()->company())->where('id', $id)->first();
$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);
}
$contact->delete($id);
else
{
$contact->delete();
}
return response()->json($contact, 204);
}
@@ -63,5 +63,16 @@ class LaratrustSetupTeams extends Migration
*/
public function down()
{
Schema::dropIfExists('teams');
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign('role_user_role_id_foreign');
$table->dropForeign('role_user_team_id_foreign');
});
Schema::table('permission_user', function (Blueprint $table) {
$table->dropForeign('permission_user_permission_id_foreign');
$table->dropForeign('permission_user_team_id_foreign');
});
}
}
@@ -15,18 +15,17 @@ class CreateContactsTable extends Migration
{
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')
$table->integer('user_id')->unsigned();
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
//$table->unsignedInteger('user_id');
// $table->foreign('user_id')->references('id')->on('users');
$table->integer('company_id')->unsigned();
$table->foreign('company_id')
->references('id')->on('companies')
->onDelete('cascade');
$table->Integer('status')->default('0');
$table->timestamps();
});
@@ -0,0 +1,47 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserIdAndComIdInContacts extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('contacts', function (Blueprint $table) {
//
$table->dropForeign('contacts_user_id_foreign');
$table->integer('sender_id')->unsigned();
$table->foreign('sender_id')
->references('id')->on('companies')
->onDelete('cascade');
$table->dropForeign('contacts_company_id_foreign');
$table->integer('recipient_id')->unsigned();
$table->foreign('recipient_id')
->references('id')->on('companies')
->onDelete('cascade');
$table->dropColumn('user_id');
$table->dropColumn('com_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('contacts', function (Blueprint $table) {
//
});
}
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

+130 -102
View File
@@ -36,41 +36,13 @@
<em class="seperate"></em>
<span class="list-item-title">Add Contact</span>
</a>
<a>Favourite/Company</a>
<a>Contacts: </a>
<div class="form-divider-2"></div>
<a href="#" class="list-item">
<i class="fa fa-users"></i>
<em class="seperate"></em>
<span class="list-item-title">CIEF</span>
<span class="list-item-title-role">Importer</span>
</a>
<a href="#" class="list-item">
<i class="fa fa-users"></i>
<em class="seperate"></em>
<span class="list-item-title">FedEx</span>
<span class="list-item-title-role">Forwarder</span>
</a>
<a>Request send</a>
<div class="form-divider-2"></div>
<a href="#" class="list-item">
<i class="fa fa-user"></i>
<em class="seperate-contact"></em>
<span class="list-item-title-contact">Casey</span>
<br />
<span class="list-item-title-contact" style="margin-top:-13px; color:#069">Resquest sent</span>
<span class="list-item-title-role" style="margin-top:-13px">Importer</span>
</a>
<a>Waiting for approval</a>
<div class="form-divider-2"></div>
<a href="#" class="list-item" data-popup="formPopup6">
<i class="fa fa-user" style="color: red"></i>
<em class="seperate-contact"></em>
<span class="list-item-title-contact">Alexander</span>
<br />
<span class="list-item-title-contact" style="margin-top:-13px; color: #069">Waiting for appproval</span>
<span class="list-item-title-role" style="margin-top:-13px">Forwarder</span>
</a>
</div>
<div id="sent-req"></div>
</div>
<div class="form-divider"></div>
<div class="form-divider"></div>
<!-- Content List Spawner End Here-->
@@ -87,44 +59,97 @@
</div>
<!-- Wrapper Content End Here -->
<div class="popup-overlay" id="formPopup5" style="display: none">
<div class="popup-overlay" id="contactPopup" style="display: none">
<!-- if you dont want overlay add class .no-overlay -->
<div class="popup-container">
<div class="popup-header">
<h3 class="popup-title">Contact Details</h3>
<h3 class="popup-title">Request Pending</h3>
<span class="popup-close" data-dismiss="true"><i class="fa fa-times"></i></span>
</div>
<div class="popup-content">
<div class="popup-content">
<div class="form-row-group with-icons-no-padding">
<div class="form-row no-padding">
<div class="form-element-title">
<label id="companyname" style="color: #000">Company Name</label>
<span style="color: #667f97;">Company Name: </span>
<label id="companyname" style="color: #667f97"></label>
</div>
</div>
<div class="form-divider"></div>
<div class="form-row no-padding">
<div class="form-element-title">
<label id="telno" style="color: #000">Telephone number</label>
<span style="color: #667f97;">Telephone number: </span>
<label id="telno" style="color: #667f97"></label>
</div>
</div>
<div class="form-divider"></div>
<div class="form-row no-padding">
<div class="form-element-title">
<label id="contactpers" style="color: #000">Contact Person</label>
<span style="color: #667f97;">Contact Person: </span>
<label id="contactpers" style="color: #667f97"></label>
</div>
</div>
<div class="form-divider"></div>
</div>
</div>
<div class="popup-footer">
<button class="button blue" id="addContact" data-dismiss="true">Add Contact</button>
<button class="button blue" id="accept-req" data-dismiss="true">Approve</button>
<button class="button red" id="rej-req" data-dismiss="true">Reject</button>
</div>
</div>
</div>
<!--POPUP HTML CONTENT START -->
<div class="popup-overlay" id="friendDetails">
<!-- if you dont want overlay add class .no-overlay -->
<div class="popup-container">
<div class="popup-header">
<h1 style="color: black">Contact Details</h1>
<span class="popup-close" data-dismiss="true"><i class="fa fa-times"></i></span>
</div>
<div class="popup-content">
<div class="form-row-group with-icons-no-padding">
<div class="form-row no-padding">
<div class="form-element-title">
<span style="color: #667f97;">Company Name: </span>
<label id="approved-companyname" style="color: #667f97"></label>
</div>
</div>
<div class="form-divider"></div>
<div class="form-row no-padding">
<div class="form-element-title">
<span style="color: #667f97;">Telephone number: </span>
<label id="approved-telno" style="color: #667f97"></label>
</div>
</div>
<div class="form-divider"></div>
<div class="form-row no-padding">
<div class="form-element-title">
<span style="color: #667f97;">Contact Person: </span>
<label id="approved-contactpers" style="color: #667f97"></label>
</div>
</div>
<div class="form-divider"></div>
</div>
</div>
<div class="popup-footer">
<div class="txt-center">
<button id="del-contact" class="button red" data-dismiss="true">Delete Contact</button>
</div>
</div>
</div>
</div>
</div>
<!--POPUP HTML CONTENT END -->
<!-- Required For All Pages, Otherwise Ugly Others Function Won't Work -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/global.script.js"></script>
@@ -139,14 +164,24 @@
headers: {"Authorization": 'Bearer' + localStorage.getItem('token')},
success:function(data){
data.foreach($contacts=>{
console.log(data.com_id);
console.log(data.status);
});
// {{$contact->name}}
// foreach($contact->companies as $company
// {{ $company->name }};
console.log(data);
data.forEach(function(entry) {
console.log(entry.status);
//console.log(entry.company_name);
if(entry.status === 0)
{
if(entry.is_me === 0){
f
}
$('#sent-req').append('<a href="#" class="list-item" onclick="displaydetails(' + "'" + entry.sender_company_name + "'" + ',' + "'" + entry.sender_tel_no + "'" + ',' + "'" + entry.sender_contact_pers + "'" + ',' + "'" + entry.contact_id + "'" + ')"><i class="fa fa-user"></i> <em class="seperate-contact"></em> <span class="list-item-title-contact" style="color:#667f97">' + entry.sender_company_name + '</span> <br /> <span class="list-item-title-contact" style="margin-top:-23px; color:#069"> Request sent </span> <span class="list-item-title-role" style="margin-top:-23px; color:#667f97">'+ entry.sender_contact_pers + '<br /> ' + entry.sender_tel_no+ '</span></a>')
}
else
{
$('#sent-req').append('<a href="#" class="list-item" onclick="displayApproved(' + "'" + entry.sender_company_name + "'" + ',' + "'" + entry.sender_tel_no + "'" + ',' + "'" + entry.sender_contact_pers + "'" + ',' + "'" + entry.contact_id + "'" + ')"><i class="fa fa-user"></i> <em class="seperate-contact"></em> <span class="list-item-title-contact" style="color:#667f97">' + entry.sender_company_name + '</span> <br /> <span class="list-item-title-contact" style="margin-top:-23px; color:#069"> Friend </span> <span class="list-item-title-role" style="margin-top:-23px; color:#667f97">'+ entry.sender_contact_pers + '<br /> ' + entry.sender_tel_no+ '</span></a>')
}
});
},
error:function(data){
console.log(data);
@@ -159,75 +194,68 @@
{
console.log('success');
}
//var company_name, tel_no, contact_person;
$('#search').on('keyup', function(){
$('#search_results').empty();
var ul, li, data;
$.ajax({
type: "GET",
url: "api/contacts",
headers: {"Authorization": 'Bearer' + localStorage.getItem('token')},
success:function(data){
console.log(data);
// for(i=0; i<data.length; i++){
// $('#all-contacts').append('<li style="display: show;"> <a href="#" onclick="displayCompany(' + "'" + data[i].company_name + "'" + ',' + "'" + data[i].tel_no + "'" + ',' + "'" + data[i].contact_person + "'" + ',' + "'" + data[i].com_id + "'" + ')"> <i class="fa fa-user"></i>' + data[i].company_name + '<span class="list-item-title-role" style="margin-top:-10px">' + data[i].tel_no + '</span> <br> <span class="list-item-title-role" style="margin-top:-10px">'+ data[i].contact_person);
// }
//$('#searchresults').show();
},
error:function(data){
console.log(data);
}
});
function displaydetails(sender_company_name, sender_tel_no, sender_contact_pers, contact_id){
$('#companyname').text(sender_company_name);
$('#telno').text(sender_tel_no);
$('#contactpers').text(sender_contact_pers);
})
//displayCompany.apply(this, data);
function displayCompany(company_name, tel_no, contact_person, com_id){
$('#companyname').text(company_name);
$('#telno').text(tel_no);
$('#contactpers').text(contact_person);
$('#contactPopup').show();
$('#accept-req').click(function() {
$('#formPopup5').show();
$('#addContact').click(function() {
$.ajax({
type: "POST",
url: "api/contacts",
url: "api/contacts/accept",
headers: {"Authorization": 'Bearer' + localStorage.getItem('token')},
data: {
com_id: com_id,
id: contact_id,
},
success: function(data) {
console.log(data);
location.reload();
}
})
});
$('#rej-req').click(function() {
$.ajax({
type: "DELETE",
url: "api/contacts/reject",
headers: {"Authorization": 'Bearer' + localStorage.getItem('token')},
data: {
id: contact_id,
},
success: function(data) {
console.log(data);
location.reload();
}
})
})
}
// function CSearch() {
// // Declare variables
// var input, filter, ul, li, a, i;
// input = document.getElementById('searchInput');
// filter = input.value.toUpperCase();
// ul = document.getElementById("searchresults");
// li = ul.getElementsByTagName('li');
// if (input.value.length == 0) {
// ul.style.display = "none";
// } else {
// ul.style.display = "block";
// }
// // Loop through all list items, and hide those who don't match the search query
// for (i = 0; i < li.length; i++) {
// a = li[i].getElementsByTagName("a")[0];
// if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
// li[i].style.display = "block";
// } else {
// li[i].style.display = "none";
// }
// }
// }
// document.getElementById("btnback").onclick = function () {
// javascript: history.back();
// };
function displayApproved(sender_company_name, sender_tel_no, sender_contact_pers, contact_id){
$('#approved-companyname').text(sender_company_name);
$('#approved-telno').text(sender_tel_no);
$('#approved-contactpers').text(sender_contact_pers);
$('#friendDetails').show();
$('#del-contact').click(function() {
$.ajax({
type: "DELETE",
url: "api/contacts/reject",
headers: {"Authorization": 'Bearer' + localStorage.getItem('token')},
data: {
id: contact_id,
},
success: function(data) {
console.log(data);
location.reload();
}
})
})
}
</script>
</body>
</html>
+5 -3
View File
@@ -140,7 +140,7 @@
success:function(data){
console.log(data);
for(i=0; i<data.length; i++){
$('#search_results').append('<li style="display: show;"> <a href="#" onclick="displayCompany(' + "'" + data[i].company_name + "'" + ',' + "'" + data[i].tel_no + "'" + ',' + "'" + data[i].contact_person + "'" + ',' + "'" + data[i].com_id + "'" + ')"> <i class="fa fa-user"></i>' + data[i].company_name + '<span class="list-item-title-role" style="margin-top:-10px">' + data[i].tel_no + '</span> <br> <span class="list-item-title-role" style="margin-top:-10px">'+ data[i].contact_person);
$('#search_results').append('<li style="display: show;"> <a href="#" onclick="displayCompany(' + "'" + data[i].company_name + "'" + ',' + "'" + data[i].tel_no + "'" + ',' + "'" + data[i].contact_person + "'" + ',' + "'" + data[i].recipient_id + "'" + ')"> <i class="fa fa-user"></i>' + data[i].company_name + '<span class="list-item-title-role" style="margin-top:-10px">' + data[i].tel_no + '</span> <br> <span class="list-item-title-role" style="margin-top:-10px">'+ data[i].contact_person);
}
//$('#searchresults').show();
},
@@ -151,22 +151,24 @@
})
//displayCompany.apply(this, data);
function displayCompany(company_name, tel_no, contact_person, com_id){
function displayCompany(company_name, tel_no, contact_person, recipient_id){
$('#companyname').text(company_name);
$('#telno').text(tel_no);
$('#contactpers').text(contact_person);
$('#formPopup5').show();
$('#addContact').click(function() {
$.ajax({
type: "POST",
url: "api/contacts",
headers: {"Authorization": 'Bearer' + localStorage.getItem('token')},
data: {
com_id: com_id,
recipient_id: recipient_id,
},
success: function(data) {
console.log(data);
location.reload();
}
})
})
+2 -2
View File
@@ -70,8 +70,8 @@ Route::group(['middleware' => ['jwt.auth']], function() {
Route::get('/company/search', 'CompanyController@search');
Route::post('/contacts','ContactController@sendRequest');
Route::get('/contacts/display','ContactController@index');
Route::post('/contacts/{company_id}/request', 'ContactController@postApprove');
Route::delete('/contacts/{contacts_id}','ContactController@destroy');
Route::post('/contacts/accept', 'ContactController@acceptRequest');
Route::delete('/contacts/reject','ContactController@destroy');
});
//Route::post('/company', 'CompanyController@store'); //store company information