Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions enhance.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function manageEditRow($module_row, array &$vars)
*/
public function addModuleRow(array &$vars)
{
$meta_fields = ['server_label', 'hostname', 'org_id', 'api_token'];
$meta_fields = ['server_label', 'hostname', 'org_id', 'api_token', 'name_servers'];
$encrypted_fields = [];

// Set unset checkboxes
Expand Down Expand Up @@ -206,7 +206,7 @@ public function addModuleRow(array &$vars)
*/
public function editModuleRow($module_row, array &$vars)
{
$meta_fields = ['server_label','hostname','org_id','api_token'];
$meta_fields = ['server_label','hostname','org_id','api_token','name_servers'];
$encrypted_fields = [];

$this->Input->setRules($this->getRowRules($vars));
Expand Down Expand Up @@ -272,6 +272,16 @@ private function getRowRules(&$vars)
],
'message' => Language::_('Enhance.!error.api_token.valid', true)
]
],
'name_servers' => [
'count' => [
'rule' => [[$this, 'validateNameServerCount']],
'message' => Language::_('Enhance.!error.name_servers.count', true)
],
'format' => [
'rule' => [[$this, 'validateNameServers']],
'message' => Language::_('Enhance.!error.name_servers.format', true)
]
]
];

Expand All @@ -290,6 +300,36 @@ public function validateHostName($host_name)
return $validator->isDomain($host_name) || $validator->isIp($host_name);
}

/**
* Validates that at least 2 name servers are set in the given array of name servers.
*
* @param array $name_servers An array of name servers
* @return bool True if the array count is >= 2, false otherwise
*/
public function validateNameServerCount($name_servers)
{
return is_array($name_servers) && count($name_servers) >= 2;
}

/**
* Validates that the given name servers are formatted correctly.
*
* @param array $name_servers An array of name servers
* @return bool True if every name server is formatted correctly, false otherwise
*/
public function validateNameServers($name_servers)
{
if (is_array($name_servers)) {
foreach ($name_servers as $name_server) {
if (!$this->validateHostName($name_server)) {
return false;
}
}
}

return true;
}

/**
* Validates the API connection
*
Expand Down
14 changes: 14 additions & 0 deletions language/en_us/enhance.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,23 @@
// Add row
$lang['Enhance.add_row.box_title'] = 'Enhance - Add Server';
$lang['Enhance.add_row.add_btn'] = 'Add Server';
$lang['Enhance.add_row.name_servers_title'] = 'Name Servers';
$lang['Enhance.add_row.name_server_btn'] = 'Add Additional Name Server';
$lang['Enhance.add_row.name_server_col'] = 'Name Server';
$lang['Enhance.add_row.name_server_host_col'] = 'Hostname';
$lang['Enhance.add_row.name_server'] = 'Name server %1$s'; // %1$s is the name server number (e.g. 3)
$lang['Enhance.add_row.remove_name_server'] = 'Remove';


// Edit row
$lang['Enhance.edit_row.box_title'] = 'Enhance - Edit Server';
$lang['Enhance.edit_row.edit_btn'] = 'Update Server';
$lang['Enhance.edit_row.name_servers_title'] = 'Name Servers';
$lang['Enhance.edit_row.name_server_btn'] = 'Add Additional Name Server';
$lang['Enhance.edit_row.name_server_col'] = 'Name Server';
$lang['Enhance.edit_row.name_server_host_col'] = 'Hostname';
$lang['Enhance.edit_row.name_server'] = 'Name server %1$s'; // %1$s is the name server number (e.g. 3)
$lang['Enhance.edit_row.remove_name_server'] = 'Remove';


// Row meta
Expand All @@ -67,6 +79,8 @@
$lang['Enhance.!error.org_id.empty'] = 'Please enter an organization ID.';
$lang['Enhance.!error.api_token.empty'] = 'Please enter an API token.';
$lang['Enhance.!error.api_token.valid'] = 'Unable to connect to the Enhance server. Please verify your API credentials.';
$lang['Enhance.!error.name_servers.count'] = 'You must define at least 2 name servers.';
$lang['Enhance.!error.name_servers.format'] = 'One or more of the name servers entered are invalid.';
$lang['Enhance.!error.module_row.missing'] = 'An internal error occurred. The module row is unavailable.';
$lang['Enhance.!error.domain.format'] = 'Please enter a valid domain name.';
$lang['Enhance.!error.username.format'] = 'The username may contain only letters, numbers, dashes, and underscores and must be between 3 and 16 characters in length.';
Expand Down
65 changes: 65 additions & 0 deletions views/default/add_row.pdt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,41 @@ $this->Widget->create($this->_('Enhance.add_row.box_title', true));
]); ?>
</div>

<h6 class="form-section-header mt-4"><?php $this->_('Enhance.add_row.name_servers_title'); ?></h6>

<div class="mb-3">
<table class="table">
<thead>
<tr>
<th><?php $this->_('Enhance.add_row.name_server_col'); ?></th>
<th><?php $this->_('Enhance.add_row.name_server_host_col'); ?></th>
<th style="width: 50px;"></th>
</tr>
</thead>
<tbody>
<?php
$num_servers = count($vars->name_servers ?? []);
for ($i = 0; $i < max(2, $num_servers); $i++) {
?>
<tr class="ns_row">
<td><?php $this->_('Enhance.add_row.name_server', false, '<span>' . ($i + 1) . '</span>'); ?></td>
<td><?php $this->Form->fieldText('name_servers[]', $vars->name_servers[$i] ?? null, ['class' => 'form-control']); ?></td>
<td class="text-center">
<button type="button" class="btn btn-sm btn-outline-danger ns_row_remove" title="<?php $this->_('Enhance.add_row.remove_name_server'); ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<a class="btn btn-outline-success btn-sm ns_row_add" href="#"><i class="bi bi-plus-lg"></i> <span><?php $this->_(
'Enhance.add_row.name_server_btn',
); ?></span></a>

<?php $this->Widget->footer(); ?>
<div class="d-flex justify-content-end">
<?php $this->Form->fieldSubmit('save', $this->_('Enhance.add_row.add_btn', true), [
Expand All @@ -63,3 +98,33 @@ $this->Widget->create($this->_('Enhance.add_row.box_title', true));
<?php $this->Widget->end(); ?>
<?php $this->Form->end();
?>

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
// Add a row
document.querySelector('.ns_row_add').addEventListener('click', function(e) {
e.preventDefault();
var fields = document.querySelector('tr.ns_row').cloneNode(true);
fields.querySelectorAll('input').forEach(function(el) {
el.value = '';
});
fields.querySelector('td:first-child span').textContent = document.querySelectorAll('tr.ns_row').length + 1;
var ns_rows = document.querySelectorAll('tr.ns_row');
ns_rows[ns_rows.length - 1].insertAdjacentElement('afterend', fields);
});

// Remove a row (event delegation)
document.querySelector('tbody').addEventListener('click', function(e) {
var removeLink = e.target.closest('.ns_row_remove');
if (removeLink && document.querySelectorAll('tr.ns_row').length > 1) {
removeLink.closest('tr').remove();
// Reorder the counts for these rows
var i = 1;
document.querySelectorAll('tr.ns_row').forEach(function(row) {
row.querySelector('td:first-child span').textContent = i++;
});
e.preventDefault();
}
});
});
</script>
66 changes: 66 additions & 0 deletions views/default/edit_row.pdt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,41 @@ $this->Widget->create($this->_('Enhance.edit_row.box_title', true));
]); ?>
</div>

<h6 class="form-section-header mt-4"><?php $this->_('Enhance.edit_row.name_servers_title'); ?></h6>

<div class="mb-3">
<table class="table">
<thead>
<tr>
<th><?php $this->_('Enhance.edit_row.name_server_col'); ?></th>
<th><?php $this->_('Enhance.edit_row.name_server_host_col'); ?></th>
<th style="width: 50px;"></th>
</tr>
</thead>
<tbody>
<?php
$num_servers = count($vars->name_servers ?? []);
for ($i = 0; $i < max(2, $num_servers); $i++) {
?>
<tr class="ns_row">
<td><?php $this->_('Enhance.edit_row.name_server', false, '<span>' . ($i + 1) . '</span>'); ?></td>
<td><?php $this->Form->fieldText('name_servers[]', $vars->name_servers[$i] ?? null, ['class' => 'form-control']); ?></td>
<td class="text-center">
<button type="button" class="btn btn-sm btn-outline-danger ns_row_remove" title="<?php $this->_('Enhance.edit_row.remove_name_server'); ?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<a class="btn btn-outline-success btn-sm ns_row_add" href="#"><i class="bi bi-plus-lg"></i> <span><?php $this->_(
'Enhance.edit_row.name_server_btn',
); ?></span></a>

<?php $this->Widget->footer(); ?>
<div class="d-flex justify-content-end">
<?php $this->Form->fieldSubmit('save', $this->_('Enhance.edit_row.edit_btn', true), [
Expand All @@ -63,3 +98,34 @@ $this->Widget->create($this->_('Enhance.edit_row.box_title', true));
<?php $this->Widget->end(); ?>
<?php $this->Form->end();
?>

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
// Add a row
document.querySelector('.ns_row_add').addEventListener('click', function(e) {
e.preventDefault();
var fields = document.querySelector('tr.ns_row').cloneNode(true);
fields.querySelectorAll('input').forEach(function(el) {
el.value = '';
});
fields.querySelector('td:first-child span').textContent = document.querySelectorAll('tr.ns_row').length + 1;
var ns_rows = document.querySelectorAll('tr.ns_row');
ns_rows[ns_rows.length - 1].insertAdjacentElement('afterend', fields);
});
// Remove a row - event delegation for dynamic rows
document.querySelector('tbody').addEventListener('click', function(e) {
var removeLink = e.target.closest('.ns_row_remove');
if (removeLink) {
e.preventDefault();
if (document.querySelectorAll('tr.ns_row').length > 1) {
removeLink.closest('tr').remove();
// Reorder the counts for these rows
var i = 1;
document.querySelectorAll('tr.ns_row').forEach(function(row) {
row.querySelector('td:first-child span').textContent = i++;
});
}
}
});
});
</script>