2
2
3
3
namespace Vcian \LaravelDBAuditor \Controllers ;
4
4
5
+ use Illuminate \Http \JsonResponse ;
5
6
use Vcian \LaravelDBAuditor \Constants \Constant ;
7
+ use Vcian \LaravelDBAuditor \Traits \Audit ;
6
8
use Vcian \LaravelDBAuditor \Traits \Rules ;
7
- use Yajra \DataTables \Services \DataTable ;
8
9
9
10
class DisplayController
10
11
{
11
- use Rules;
12
+ use Rules, Audit ;
12
13
13
14
/**
14
15
* Return view for audit
15
16
* @return view
16
17
*/
17
- public function index ()
18
+ public function index ()
18
19
{
19
- return view ('DBAuditor::auditor.pages.audit ' );
20
+ $ tables = $ this ->getTableList ();
21
+ return view ('DBAuditor::auditor.pages.audit ' , compact ('tables ' ));
20
22
}
21
-
22
- public function getAudit ()
23
+
24
+ /**
25
+ * Get audit table list
26
+ * @return JsonResponse
27
+ */
28
+ public function getAudit () : JsonResponse
23
29
{
24
- return $ this ->tablesRule ();
25
-
30
+ $ columnName = 'status ' ;
31
+ $ noConstraint = '<img src= ' . asset ("auditor/icon/close.svg " ) . ' alt="key" class="m-auto" /> ' ;
32
+ $ constraint = '<img src= ' . asset ("auditor/icon/check.svg " ) . ' alt="key" class="m-auto" /> ' ;
33
+
34
+ $ tableRules = array_map (function ($ value ) use ($ columnName , $ noConstraint , $ constraint ) {
35
+ if ($ value [$ columnName ] === "" ) {
36
+ $ value [$ columnName ] = $ noConstraint ;
37
+ } else {
38
+ $ value [$ columnName ] = $ constraint ;
39
+ }
40
+ return $ value ;
41
+ }, $ this ->tablesRule ());
42
+
43
+ return response ()->json (
44
+ array (
45
+ "data " => $ tableRules
46
+ )
47
+ );
26
48
}
27
49
28
- }
50
+ /**
51
+ * Get table data
52
+ * @param string $tableName
53
+ * @return JsonResponse
54
+ */
55
+ public function getTableData (string $ tableName ) : JsonResponse
56
+ {
57
+ return response ()->json (array (
58
+ "data " => $ this ->tableRules ($ tableName )
59
+ ));
60
+ }
61
+
62
+ /**
63
+ * Get Constraint list
64
+ * @param string $tableName
65
+ * @return JsonResponse
66
+ */
67
+ public function getTableConstraint (string $ tableName ) : JsonResponse
68
+ {
69
+
70
+ $ data = [
71
+ "fields " => $ this ->getFieldsDetails ($ tableName ),
72
+ 'constrain ' => [
73
+ 'primary ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_PRIMARY_KEY ),
74
+ 'unique ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_UNIQUE_KEY ),
75
+ 'foreign ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_FOREIGN_KEY ),
76
+ 'index ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_INDEX_KEY )
77
+ ]
78
+ ];
79
+
80
+ $ response = [];
81
+ $ greenKey = '<img src= ' . asset ("auditor/icon/green-key.svg " ) . ' alt="key" class="m-auto" /> ' ;
82
+ $ grayKey = '<img src= ' . asset ("auditor/icon/gray-key.svg " ) . ' alt="key" class="m-auto" /> ' ;
83
+
84
+ foreach ($ data ['fields ' ] as $ table ) {
85
+
86
+ $ primaryKey = $ indexing = $ uniqueKey = $ foreignKey = "- " ;
87
+
88
+ if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['primary ' ])) {
89
+ $ primaryKey = $ greenKey ;
90
+ } else if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['unique ' ])) {
91
+ $ uniqueKey = $ grayKey ;
92
+ } else if (in_array ($ table ->COLUMN_NAME , $ data ['constrain ' ]['index ' ])) {
93
+ $ indexing = $ grayKey ;
94
+ }
95
+
96
+ foreach ($ data ['constrain ' ]['foreign ' ] as $ foreign ) {
97
+ if ($ table ->COLUMN_NAME === $ foreign ['column_name ' ]) {
98
+ $ foreignKeyToottip = '<div class="inline-flex">
99
+ <img src= ' . asset ("auditor/icon/gray-key.svg " ) . ' alt="key" class="mr-2">
100
+ <div class="relative flex flex-col items-center group">
101
+ <svg class="w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
102
+ <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd" />
103
+ </svg>
104
+ <div class="absolute bottom-0 flex flex-col items-center hidden group-hover:flex">
105
+ <span class="relative z-10 p-2 text-xs leading-none text-white whitespace-no-wrap bg-black shadow-lg">Foreign Table Name : ' . $ foreign ['foreign_table_name ' ] . ' and Foreign Column Name : ' . $ foreign ['foreign_column_name ' ] . '</span>
106
+ <div class="w-3 h-3 -mt-2 rotate-45 bg-black"></div>
107
+ </div>
108
+ </div>
109
+ </div> ' ;
110
+ $ foreignKey = $ foreignKeyToottip ;
111
+ }
112
+ }
113
+
114
+ $ response [] = ["column " => $ table ->COLUMN_NAME , "primaryKey " => $ primaryKey , "indexing " => $ indexing , "uniqueKey " => $ uniqueKey , "foreignKey " => $ foreignKey ];
115
+ }
116
+
117
+ return response ()->json (array (
118
+ "data " => $ response
119
+ ));
120
+ }
121
+ }
0 commit comments