admin can add more fields when editing customers
This commit is contained in:
Focuslinkstech 2024-02-18 04:56:30 +01:00
parent 941c723193
commit aa8fa3f436
2 changed files with 77 additions and 8 deletions

View file

@ -422,18 +422,42 @@ switch ($action) {
$customField->set('field_value', $customFieldValue);
$customField->save();
}
}
if (isset($_POST['delete_custom_fields'])) {
$fieldsToDelete = $_POST['delete_custom_fields'];
foreach ($fieldsToDelete as $fieldName) {
// Delete the custom field with the given field name
ORM::for_table('tbl_customers_custom_fields')
->where('field_name', $fieldName)
->delete_many();
// Add new custom fields
if (isset($_POST['custom_field_name']) && isset($_POST['custom_field_value'])) {
$newCustomFieldNames = $_POST['custom_field_name'];
$newCustomFieldValues = $_POST['custom_field_value'];
// Check if the number of field names and values match
if (count($newCustomFieldNames) == count($newCustomFieldValues)) {
$numNewFields = count($newCustomFieldNames);
for ($i = 0; $i < $numNewFields; $i++) {
$fieldName = $newCustomFieldNames[$i];
$fieldValue = $newCustomFieldValues[$i];
// Insert the new custom field
$newCustomField = ORM::for_table('tbl_customers_custom_fields')->create();
$newCustomField->set('customer_id', $id);
$newCustomField->set('field_name', $fieldName);
$newCustomField->set('field_value', $fieldValue);
$newCustomField->save();
}
}
}
// Delete custom fields
if (isset($_POST['delete_custom_fields'])) {
$fieldsToDelete = $_POST['delete_custom_fields'];
foreach ($fieldsToDelete as $fieldName) {
// Delete the custom field with the given field name
ORM::for_table('tbl_customers_custom_fields')
->where('field_name', $fieldName)
->delete_many();
}
}
if ($userDiff || $pppoeDiff || $passDiff) {
$c = ORM::for_table('tbl_user_recharges')->where('username', ($userDiff) ? $oldusername : $username)->find_one();
if ($c) {

View file

@ -102,7 +102,16 @@
</div>
{/foreach}
{/if}
<!--custom field edit end-->
<!--custom field edit end -->
<!--custom field add start -->
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Custom Field')}</label>
<div id="custom-fields-container" class="col-md-6">
<button class="btn btn-success btn-sm waves-effect waves-light" type="button"
id="add-custom-field">+</button>
</div>
</div>
<!--custom field add end -->
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-primary waves-effect waves-light" type="submit">{Lang::T('Save
@ -117,4 +126,40 @@
</div>
</div>
{literal}
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function () {
var customFieldsContainer = document.getElementById('custom-fields-container');
var addCustomFieldButton = document.getElementById('add-custom-field');
addCustomFieldButton.addEventListener('click', function () {
var fieldIndex = customFieldsContainer.children.length;
var newField = document.createElement('div');
newField.className = 'form-group';
newField.innerHTML = `
<label class="col-md-2 control-label">Name:</label>
<div class="col-md-3">
<input type="text" class="form-control" name="custom_field_name[]" placeholder="Name">
</div>
<label class="col-md-2 control-label">Value:</label>
<div class="col-md-3">
<input type="text" class="form-control" name="custom_field_value[]" placeholder="Value">
</div>
<div class="col-md-2">
<button type="button" class="remove-custom-field btn btn-danger btn-sm waves-effect waves-light">-</button>
</div>
`;
customFieldsContainer.appendChild(newField);
});
customFieldsContainer.addEventListener('click', function (event) {
if (event.target.classList.contains('remove-custom-field')) {
var fieldContainer = event.target.parentNode.parentNode;
fieldContainer.parentNode.removeChild(fieldContainer);
}
});
});
</script>
{/literal}
{include file="sections/footer.tpl"}