Skip to content

Commit c6f8366

Browse files
authored
Merge pull request #301 from armanist/Toolkit-refactoring
Refactoring Toolkit module
2 parents dc4f611 + 761bc86 commit c6f8366

46 files changed

Lines changed: 1271 additions & 900 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Console/Commands/InstallToolkitCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<?php
22

3+
/**
4+
* Quantum PHP Framework
5+
*
6+
* An open source software development framework for PHP
7+
*
8+
* @package Quantum
9+
* @author Arman Ag. <arman.ag@softberg.org>
10+
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11+
* @link http://quantum.softberg.org/
12+
* @since 2.9.8
13+
*/
14+
315
namespace Quantum\Console\Commands;
416

517
use Quantum\Environment\Exceptions\EnvException;
@@ -9,6 +21,10 @@
921
use Quantum\Environment\Environment;
1022
use Quantum\Console\QtCommand;
1123

24+
/**
25+
* Class InstallToolkitCommand
26+
* @package Quantum\Console
27+
*/
1228
class InstallToolkitCommand extends QtCommand
1329
{
1430
/**

src/Libraries/Storage/Adapters/Local/LocalFileSystemAdapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ public function getLines(string $filename, int $offset = 0, ?int $length = null)
229229
return $lines;
230230
}
231231

232+
/**
233+
* Gets the filename with extension
234+
* @param string $path
235+
* @return string
236+
*/
237+
public function fileNameWithExtension(string $path): string
238+
{
239+
return (string)pathinfo($path, PATHINFO_BASENAME);
240+
}
241+
232242
/**
233243
* Gets the file name
234244
* @param string $path

src/Libraries/Storage/FileSystem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* @method bool isFile(string $filename)
3636
* @method bool isDirectory(string $dirname)
3737
* @method string fileName(string $path)
38+
* @method string fileNameWithExtension(string $path)
3839
* @method string extension(string $path)
3940
* @method bool isReadable(string $filename)
4041
* @method bool isWritable(string $filename)
Lines changed: 152 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,188 @@
1-
jQuery(document).ready(function ($) {
2-
$('.modal').modal();
3-
4-
$('.modal-trigger').on('click', function (event) {
5-
event.stopPropagation();
6-
let targetModalId = $(this).data('target');
7-
$('#' + targetModalId).modal('open');
8-
});
1+
class ToolkitLogs {
2+
constructor() {
3+
this.$logsIframe = $('.logs-iframe');
4+
}
95

10-
$('.visibility-icon').on('click', function () {
11-
if ($(this).hasClass('on')) {
12-
$('.off').removeClass('hide');
13-
$(this).parent('.input-field').find('input[type=text]').attr('type', 'password');
6+
handleLogFileClick(event) {
7+
const $target = $(event.currentTarget);
8+
const file = $target.data('file');
149

15-
} else {
16-
$('.on').removeClass('hide');
17-
$(this).parent('.input-field').find('input[type=password]').attr('type', 'text');
18-
}
10+
this.$logsIframe.attr('src', `${window.location.origin}/toolkit/logs/view?logFile=${file}`);
11+
$target.siblings().removeClass('active');
12+
$target.addClass('active');
13+
}
1914

20-
$(this).addClass('hide');
21-
});
15+
events() {
16+
$('.logFile-item').on('click', this.handleLogFileClick.bind(this));
17+
}
2218

23-
$('.collapsible').collapsible();
19+
init() {
20+
this.events();
21+
}
22+
}
2423

25-
$('.collapsible-header').hover(
26-
function () {
27-
const icon = $(this).find('.status-icon');
28-
icon.data('original', icon.text());
29-
$(icon).parent().addClass("chevron");
30-
icon.text('chevron_right');
31-
},
32-
function () {
33-
const icon = $(this).find('.status-icon');
34-
$(icon).parent().removeClass("chevron");
35-
icon.text(icon.data('original'));
36-
}
37-
);
24+
class ToolkitEmails {
25+
constructor() {
26+
this.$deleteModal = $('#deleteModal');
27+
this.$confirmDelete = $('#confirmDelete');
28+
}
3829

39-
$('.logFile-item').on('click', function (){
40-
$('.logs-iframe').attr('src', window.location.origin + "/toolkit/logs/view?logDate=" + $(this).data("file"));
41-
$(this).siblings().removeClass('active');
42-
$(this).addClass('active');
43-
});
30+
handleDeleteClick(event) {
31+
event.stopPropagation();
32+
const $target = $(event.currentTarget);
33+
const deleteUrl = $target.data('delete-url');
34+
this.$confirmDelete.attr('href', deleteUrl);
35+
this.$deleteModal.modal('open');
36+
}
4437

45-
$('.table-item').on('click', function (){
46-
$('.table-iframe').attr('src', window.location.origin + "/toolkit/database/view?table=" + $(this).data("name"));
47-
$(this).siblings().removeClass('active');
48-
$(this).addClass('active');
49-
});
38+
events() {
39+
$('.delete-trigger').on('click', this.handleDeleteClick.bind(this));
40+
}
5041

51-
let editor;
42+
init() {
43+
this.events();
44+
}
45+
}
46+
47+
class ToolkitDatabase {
48+
constructor() {
49+
this.$jsonEditorContainer = $('#jsoneditor');
50+
this.$createTableModal = $('#createTableModal');
51+
this.$rowActionModal = $('#rowActionModal');
52+
this.$rowDeleteModal = $('#rowDelete');
53+
this.$modalConfirm = $('#modal-confirm');
54+
}
5255

53-
function setupModal(modal, action, title, data = {}, options = { mode: 'form', search: false }) {
54-
modal.data('action', action);
56+
openModal(modal, actionUrl, title, data = {}, options = { mode: 'form', search: false }) {
57+
modal.data('action', actionUrl);
5558
modal.find('.modal-title').text(title);
56-
$("#jsoneditor").empty();
57-
editor = new JSONEditor(document.getElementById("jsoneditor"), options);
58-
editor.set(data);
59+
60+
this.$jsonEditorContainer.empty();
61+
window.editor = new JSONEditor(this.$jsonEditorContainer[0], options);
62+
window.editor.set(data);
63+
5964
modal.modal('open');
6065
}
6166

62-
function submitForm(modal, additionalFields = {}) {
67+
submitForm(modal, additionalFields = {}) {
6368
const $form = $('<form>', { method: 'POST', action: modal.data('action') });
6469

65-
$form.append($('<input>', { type: 'hidden', name: 'data', value: JSON.stringify(editor.get()) }));
70+
$form.append($('<input>', {
71+
type: 'hidden',
72+
name: 'data',
73+
value: JSON.stringify(window.editor.get())
74+
}));
6675

67-
Object.entries(additionalFields).forEach(([name, value]) => {
76+
for (const [name, value] of Object.entries(additionalFields)) {
6877
$form.append($('<input>', { type: 'hidden', name, value }));
69-
});
78+
}
7079

7180
$('body').append($form);
7281
$form.submit();
7382
}
7483

75-
$('.row-add').on('click', function(){
84+
handleTableItemClick(event) {
85+
const $target = $(event.currentTarget);
86+
const table = $target.data('name');
7687

77-
let rowData = $('.row-data').first().data('row');
88+
$('.table-iframe').attr('src', `${window.location.origin}/toolkit/database/view?table=${table}`);
89+
$target.siblings().removeClass('active');
90+
$target.addClass('active');
91+
}
92+
93+
handleRowAddOrCreate(event) {
94+
const $target = $(event.currentTarget);
95+
const isTableCreate = $target.hasClass('create-table');
96+
const modal = isTableCreate ? this.$createTableModal : this.$rowActionModal;
97+
const actionUrl = '/toolkit/database/create';
98+
const title = isTableCreate ? 'Creating Table' : $target.data('modal-title');
7899

79100
let json = {};
101+
let options = { mode: 'tree', search: false };
102+
103+
if (!isTableCreate) {
104+
const rowData = $('.row-data').first().data('row');
105+
options.mode = rowData ? 'form' : 'tree';
106+
107+
if (rowData) {
108+
for (const key of Object.keys(rowData)) {
109+
if (key !== 'id') {
110+
json[key] = '';
111+
}
112+
}
113+
}
114+
115+
const tableName = modal.data('table');
116+
modal.data('original-name', tableName);
117+
} else {
118+
modal.removeData('original-name');
119+
}
120+
121+
this.openModal(modal, actionUrl, title, json, options);
122+
}
123+
124+
handleRowEdit(event) {
125+
const data = { ...$(event.currentTarget).parent().data('row') };
126+
const title = $(event.currentTarget).data('modal-title');
127+
128+
this.$rowActionModal.data('id', data.id);
129+
delete data.id;
80130

81-
let options = { mode: 'form', search: false };
131+
this.openModal(this.$rowActionModal, '/toolkit/database/update', title, data);
132+
}
133+
134+
handleRowDelete(event) {
135+
const deleteUrl = $(event.currentTarget).data('url');
136+
this.$modalConfirm.attr('href', deleteUrl);
137+
this.$rowDeleteModal.modal('open');
138+
}
82139

83-
if(rowData){
84-
json = Object.fromEntries(
85-
Object.keys(rowData)
86-
.filter(key => key !== 'id')
87-
.map(key => [key, ''])
88-
);
89-
}else{
90-
options.mode = 'tree';
140+
handleRowAction() {
141+
const originalName = this.$rowActionModal.data('original-name');
142+
143+
const additionalFields = {
144+
table: this.$rowActionModal.data('table'),
145+
rowId: this.$rowActionModal.data('id'),
146+
'csrf-token': this.$rowActionModal.data('csrf')
147+
};
148+
149+
if (originalName) {
150+
additionalFields.originalName = originalName;
91151
}
92152

93-
setupModal($('#rowActionModal'), 'create', $(this).data('modal-title'), json, options);
94-
});
153+
this.submitForm(this.$rowActionModal, additionalFields);
154+
}
95155

96-
$('.row-edit').on('click', function(){
97-
let data = $(this).parent().data("row");
98-
let editModal = $('#rowActionModal');
99-
editModal.data('id', data.id);
100-
delete data.id;
101-
setupModal(editModal, 'update', $(this).data('modal-title'), data);
102-
});
103-
104-
$('.row-action').on('click', function () {
105-
let $rowActionModal = $('#rowActionModal');
106-
submitForm($rowActionModal, {
107-
table: $rowActionModal.data('table'),
108-
rowId: $rowActionModal.data('id'),
109-
'csrf-token': $rowActionModal.data('csrf'),
156+
handleSubmitTable() {
157+
this.submitForm(this.$createTableModal, {
158+
table: this.$createTableModal.find('#tableName').val(),
159+
'csrf-token': this.$createTableModal.data('csrf')
110160
});
111-
});
161+
}
112162

113-
$('.create-table').on('click', function () {
114-
setupModal($('#createTableModal'), 'database/create', 'Creating Table', {}, { mode: 'tree', search: false });
115-
});
163+
events() {
164+
$('.table-item').on('click', this.handleTableItemClick.bind(this));
165+
$('.row-add').on('click', this.handleRowAddOrCreate.bind(this));
166+
$('.row-edit').on('click', this.handleRowEdit.bind(this));
167+
$('.row-delete').on('click', this.handleRowDelete.bind(this));
168+
$('.row-action').on('click', this.handleRowAction.bind(this));
169+
$('.create-table').on('click', this.handleRowAddOrCreate.bind(this));
170+
$('.submit-table').on('click', this.handleSubmitTable.bind(this));
171+
}
116172

117-
$('.submit-table').on('click', function () {
118-
let $createTableModal = $('#createTableModal');
119-
submitForm($createTableModal, {
120-
table: $createTableModal.find('#tableName').val(),
121-
'csrf-token': $createTableModal.data('csrf'),
122-
});
123-
});
173+
init() {
174+
this.events();
175+
}
176+
}
124177

125-
$('.sidenav').sidenav();
126-
});
178+
jQuery(document).ready(function ($) {
179+
window.editor = undefined;
127180

181+
$('.modal').modal();
182+
$('.collapsible').collapsible();
183+
$('.sidenav').sidenav();
128184

185+
new ToolkitLogs().init();
186+
new ToolkitEmails().init();
187+
new ToolkitDatabase().init();
188+
});

src/Module/Templates/Toolkit/src/Controllers/MainController.tpl renamed to src/Module/Templates/Toolkit/src/Controllers/BaseController.php.tpl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
<?php
22

3+
/**
4+
* Quantum PHP Framework
5+
*
6+
* An open source software development framework for PHP
7+
*
8+
* @package Quantum
9+
* @author Arman Ag. <arman.ag@softberg.org>
10+
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11+
* @link http://quantum.softberg.org/
12+
* @since 2.9.8
13+
*/
14+
315
namespace Modules\Toolkit\Controllers;
416

517
use Quantum\View\Factories\ViewFactory;
618
use Quantum\Router\RouteController;
719
use Quantum\Libraries\Asset\Asset;
20+
use Quantum\View\QtView;
821

9-
class MainController extends RouteController
22+
/**
23+
* Class BaseController
24+
* @package Modules\Toolkit
25+
*/
26+
class BaseController extends RouteController
1027
{
28+
1129
/**
1230
* Main layout
1331
*/
1432
const LAYOUT = 'layouts/main';
1533
1634
/**
17-
* Emails per page
18-
*/
19-
const EMAILS_PER_PAGE = 10;
20-
21-
/**
22-
* Logs per page
35+
* Items per page
2336
*/
24-
const LOGS_PER_PAGE = 10;
37+
const ITEMS_PER_PAGE = 20;
2538
2639
/**
27-
* Rows per page
40+
* Current page
2841
*/
29-
const ROWS_PER_PAGE = 20;
42+
const CURRENT_PAGE = 1;
3043
3144
/**
32-
* Current page
45+
* @var QtView
3346
*/
34-
const CURRENT_PAGE = 1;
47+
protected $view;
3548
3649
/**
3750
* Works before an action

0 commit comments

Comments
 (0)