Skip to content

Commit 1f78b8b

Browse files
author
vc-urvin
committed
Implement the foreign constraint action
1 parent f8d3249 commit 1f78b8b

File tree

5 files changed

+200
-49
lines changed

5 files changed

+200
-49
lines changed

routes/api.php

+5
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010

1111

1212
Route::post('change-constraint', [DisplayController::class, 'changeConstraint']);
13+
14+
Route::get('foreign-key-table', [DisplayController::class, 'getForeignKeyTableList']);
15+
Route::get('foreign-key-field/{table}', [DisplayController::class, 'getForeignKeyFieldList']);
16+
Route::post('add-foreign-constraint', [DisplayController::class, 'addForeignKeyConstraint']);
17+
1318
});
1419

src/Commands/DBConstraintCommand.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
135135
$fields = Constant::ARRAY_DECLARATION;
136136

137137
do {
138-
$referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $this->getTablesList());
138+
$referenceTable = $this->anticipate(__('Lang::messages.constraint.question.foreign_table'), $this->getTableList());
139139

140-
if ($referenceTable && $this->checkTableExistOrNot($referenceTable)) {
140+
if ($referenceTable && $this->checkTableExist($referenceTable)) {
141141

142-
foreach ($this->getTableFields($referenceTable) as $field) {
142+
foreach ($this->getFieldsDetails($referenceTable) as $field) {
143143
$fields[] = $field->COLUMN_NAME;
144144
}
145145
do {

src/Controllers/DisplayController.php

+57-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function getTableConstraint(string $tableName): JsonResponse
102102
}
103103

104104
foreach ($data['constrain']['foreign'] as $foreign) {
105-
if ($table->COLUMN_NAME === $foreign['column_name']) {
105+
if ($table->COLUMN_NAME === $foreign) {
106106
$foreignKeyTooltip = '<div class="inline-flex">
107107
<img src=' . asset("auditor/icon/gray-key.svg") . ' alt="key" class="mr-2">
108108
<div class="relative flex flex-col items-center group">
@@ -139,6 +139,13 @@ public function getTableConstraint(string $tableName): JsonResponse
139139
}
140140
}
141141
break;
142+
case Constant::CONSTRAINT_FOREIGN_KEY:
143+
if(in_array($table->COLUMN_NAME, $noConstraintFields['integer'])) {
144+
if(!$this->tableHasValue($tableName)) {
145+
$foreignKey = '<img src=' . asset("auditor/icon/add.svg") . ' alt="key" class="m-auto add-constraint-'.$table->COLUMN_NAME.'-'.Constant::CONSTRAINT_FOREIGN_KEY.'" style="height:30px;cursor: pointer;" onclick="add(`'.$table->COLUMN_NAME.'`, `'.Constant::CONSTRAINT_FOREIGN_KEY.'`,`'.$tableName.'`)"/>';
146+
}
147+
}
148+
break;
142149
default:
143150
break;
144151
}
@@ -152,10 +159,57 @@ public function getTableConstraint(string $tableName): JsonResponse
152159
));
153160
}
154161

155-
public function changeConstraint(Request $request)
162+
/**
163+
* Update the field Constraint
164+
* @param Request
165+
* @return
166+
*/
167+
public function changeConstraint(Request $request): bool
156168
{
157169
$data = $request->all();
158170
$this->addConstraint($data['table_name'], $data['colum_name'], $data['constraint']);
159-
return $data['colum_name'];
171+
return Constant::STATUS_TRUE;
172+
}
173+
174+
/**
175+
* Get Foreign Key Details
176+
* @return array
177+
*/
178+
public function getForeignKeyTableList(): array
179+
{
180+
return $this->getTableList();
181+
}
182+
183+
/**
184+
* Get Foreign Key Field List
185+
* @param string
186+
* @return array
187+
*/
188+
public function getForeignKeyFieldList(string $tableName): array
189+
{
190+
return $this->getFieldsDetails($tableName);
191+
}
192+
193+
/**
194+
* Add Foreign Key Constraint
195+
* @param Request
196+
* @return mixed
197+
*/
198+
public function addForeignKeyConstraint(Request $request): mixed
199+
{
200+
$data= $request->all();
201+
202+
if($data['reference_table'] === $data['table_name']) {
203+
return __('Lang::messages.constraint.error_message.foreign_selected_table_match', ['foreign' => $data['reference_table'], 'selected' => $data['table_name']]);
204+
}
205+
206+
$referenceFieldType = $this->getFieldDataType($data['reference_table'], $data['reference_field']);
207+
$selectedFieldType = $this->getFieldDataType($data['table_name'], $data['select_field']);
208+
if ($referenceFieldType['data_type'] !== $selectedFieldType['data_type']) {
209+
return __('Lang::messages.constraint.error_message.foreign_not_apply');
210+
}
211+
212+
$this->addConstraint($data['table_name'], $data['select_field'], Constant::CONSTRAINT_FOREIGN_KEY, $data['reference_table'], $data['reference_field']);
213+
return Constant::STATUS_TRUE;
160214
}
161215
}

src/Datatable/AuditDatatable.php

-27
This file was deleted.

src/views/auditor/pages/audit.blade.php

+135-16
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class="btn dropdown-toggle bg-light-black border-0 rounded-2xl text-white w-64 t
172172
</dialog>
173173

174174
<p class="colum-value"></p>
175+
<p class="table-value"></p>
175176
<p class="constraint-value"></p>
176177
@endsection
177178

@@ -201,6 +202,7 @@ class="btn dropdown-toggle bg-light-black border-0 rounded-2xl text-white w-64 t
201202
});
202203
203204
$(document).ready(function() {
205+
204206
// Standards
205207
var table = $('#standards').DataTable({
206208
scrollX: true,
@@ -288,8 +290,6 @@ function format(d) {
288290
tableComment += '</tbody>' + '</table>';
289291
}
290292
291-
292-
293293
var table =
294294
'<table class="table table-stripped table-bordered display nowrap w-100 border-gray" cellpadding="5" cellspacing="0" border="0">' +
295295
'<thead class="bg-black">' +
@@ -411,13 +411,28 @@ function confirmDialog() {
411411
addConstraint();
412412
}
413413
414-
function add(columnName, constraint) {
415-
$('#confDialog h2').replaceWith("<h2>ADD " + constraint.toUpperCase() + " KEY</h2>");
416-
$('#confDialog p').replaceWith('<p>Do you want to add ' + constraint.toLowerCase() +
417-
' in <span style="color:red;">' + columnName + '</span> field?</p>');
418-
$('.colum-value').replaceWith("<p class='colum-value'>" + columnName + "</p>");
419-
$('.constraint-value').replaceWith("<p class='constraint-value'>" + constraint + "</p>");
420-
openDialog();
414+
function add(columnName, constraint, tableName) {
415+
416+
if(constraint.toLowerCase() === "foreign") {
417+
$('.colum-value').replaceWith("<p class='colum-value'>" + columnName + "</p>");
418+
$('.table-value').replaceWith("<p class='table-value'>" + tableName + "</p>");
419+
$('.constraint-value').replaceWith("<p class='constraint-value'>" + constraint + "</p>");
420+
getForeignTables();
421+
} else {
422+
console.log($(".main-dialog")[0]);
423+
if ( $(".main-dialog")[0] ) {
424+
$('#confDialog h2').replaceWith("<h2>ADD " + constraint.toUpperCase() + " KEY</h2>");
425+
$('.main-dialog').replaceWith('<p>Do you want to add ' + constraint.toLowerCase() +' in <span style="color:red;">' + columnName + '</span> field?</p>');
426+
$('.confirm-dialog-btn').replaceWith('<button onclick="confirmDialog()" class="btn btn-success confirm-dialog-btn">Yes</button>');
427+
428+
} else {
429+
$('#confDialog h2').replaceWith("<h2>ADD " + constraint.toUpperCase() + " KEY</h2>");
430+
$('#confDialog p').replaceWith('<p>Do you want to add ' + constraint.toLowerCase() +' in <span style="color:red;">' + columnName + '</span> field?</p>');
431+
}
432+
$('.colum-value').replaceWith("<p class='colum-value'>" + columnName + "</p>");
433+
$('.constraint-value').replaceWith("<p class='constraint-value'>" + constraint + "</p>");
434+
openDialog();
435+
}
421436
}
422437
423438
function addConstraint() {
@@ -437,18 +452,17 @@ function addConstraint() {
437452
}),
438453
success: function(response) {
439454
if (response) {
440-
var key;
455+
var key = "";
441456
442457
if(constraint.toLowerCase() === "primary") {
443-
key = $('<img src="auditor/icon/green-key.svg" alt="key" class="m-auto" />');
458+
key = '<img src="auditor/icon/green-key.svg" alt="key" class="m-auto" />';
444459
} else {
445-
key = $('<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />');
460+
key = '<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />';
446461
}
447-
448-
$(".add-constraint-" + response + '-' + constraint).replaceWith(key);
462+
463+
$(".add-constraint-" + columnName + "-" + constraint).replaceWith(key);
449464
$(".toast-container").css("display", "block");
450-
$(".toastCustom").replaceWith("<p class='toastCustom'>" + constraint.toLowerCase() +
451-
" key added successfully</p>");
465+
$(".toastCustom").replaceWith("<p class='toastCustom'>" + constraint.toLowerCase() + " key added successfully</p>");
452466
453467
setTimeout(function() {
454468
$(".toast-container").css("display", "none");;
@@ -461,6 +475,111 @@ function addConstraint() {
461475
});
462476
}
463477
478+
function getForeignTables()
479+
{
480+
$.ajax({
481+
url: 'api/foreign-key-table',
482+
type: 'get',
483+
headers: {
484+
'Content-Type': 'application/json',
485+
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
486+
},
487+
success: function(response) {
488+
if (response) {
489+
$('#confDialog h2').replaceWith("<h2>Add Foreign Key</h2>");
490+
491+
var html = "<div class='row main-dialog' style='margin-top: 30px;margin-bottom: 20px;'>";
492+
493+
// foreign table
494+
html += "<div class='col-md-6 sub-dialog'><select class='form-control select-foreign-table' id='select-foreign-tbl'>";
495+
html += "<option disabled selected>Select Foreign Table</option>";
496+
$.each(response, function(key, value) {
497+
html += "<option value="+value+">"+value+"</option>";
498+
});
499+
html += "</select></div>";
500+
501+
html += "<div class='field-list'></div>"
502+
503+
html += "</div>";
504+
505+
$('#confDialog p').replaceWith(html);
506+
$('#confDialog .confirm-dialog-btn').replaceWith('<button onclick="addforeignKey()" class="btn btn-success confirm-dialog-btn">Add</button>');
507+
openDialog();
508+
}
509+
},
510+
error: function(xhr, status, error) {
511+
console.error(error);
512+
}
513+
});
514+
}
515+
516+
$(document).on('change','#select-foreign-tbl', function() {
517+
518+
$.ajax({
519+
url: 'api/foreign-key-field/'+this.value,
520+
type: 'get',
521+
headers: {
522+
'Content-Type': 'application/json',
523+
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
524+
},
525+
success: function(response) {
526+
if (response) {
527+
// foreign column
528+
var html = "<div class='col-md-6 field-list'><select class='form-control' id='select-foreign-field'>";
529+
html += "<option disabled selected>Select Foreign Column</option>";
530+
$.each(response, function(key, value) {
531+
html += "<option value="+value.COLUMN_NAME+">"+value.COLUMN_NAME+"</option>";
532+
});
533+
html += "</select></div>";
534+
535+
$('.field-list').replaceWith(html);
536+
}
537+
},
538+
error: function(xhr, status, error) {
539+
console.error(error);
540+
}
541+
});
542+
543+
544+
});
545+
546+
function addforeignKey()
547+
{
548+
var foreignTable = document.getElementById('select-foreign-tbl');
549+
var foreignField = document.getElementById('select-foreign-field');
550+
var columnName = $('.colum-value').text();
551+
var tableName = $('.table-value').text();
552+
$.ajax({
553+
url: 'api/add-foreign-constraint',
554+
type: 'POST',
555+
headers: {
556+
'Content-Type': 'application/json',
557+
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
558+
},
559+
data: JSON.stringify({
560+
"table_name": tableName,
561+
"select_field": columnName,
562+
"reference_table": foreignTable.value,
563+
"reference_field": foreignField.value
564+
}),
565+
success: function(response) {
566+
if (response) {
567+
$(".add-constraint-" + columnName + '-FOREIGN').replaceWith('<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />');
568+
closeDialog();
569+
$(".toast-container").css("display", "block");
570+
$(".toastCustom").replaceWith("<p class='toastCustom'>foreign key added successfully</p>");
571+
572+
setTimeout(function() {
573+
$(".toast-container").css("display", "none");;
574+
}, 1000);
575+
}
576+
},
577+
error: function(xhr, status, error) {
578+
console.error(error);
579+
}
580+
});
581+
}
582+
464583
$(document).on("click", ".custom-action", function() {
465584
$("#constraints").DataTable().draw();
466585
$("#standards").DataTable().draw();

0 commit comments

Comments
 (0)