338 lines
20 KiB
PHP
338 lines
20 KiB
PHP
<div class="row">
|
|
<div class="col-lg-12">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h4 class="header-title"><?= $page_name; ?></h4>
|
|
<?php if (session()->getFlashdata('success')) : ?>
|
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
|
<?= session('success') ?>
|
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<?php endif; // print_r($details); ?>
|
|
<form class="parsley-examples" action="<?= base_url() . "insert_users"; ?>" method="post" enctype="multipart/form-data" id="myForm">
|
|
<div class="form-group">
|
|
<div class="form-row">
|
|
<div class="form-group col-md-4">
|
|
<label for="first_name" class="col-form-label">Name <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="Name" value="<?= isset($details['first_name']) ? $details['first_name'] : '' ?>" required />
|
|
</div>
|
|
<div class="form-group col-md-4">
|
|
<label for="mobile_no" class="col-form-label">Phone Number <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="mobile_no" name="mobile_no" placeholder="Phone Number (Enter only numbers)" data-toggle="input-mask" data-mask-format="0000000000" value="<?= isset($details['mobile_no']) ? $details['mobile_no'] : '' ?>" autofocus required onchange="checkExisting(this,'mobile_no','mobile_no')">
|
|
<span id="mobileValidationMessage"></span>
|
|
</div>
|
|
<?php $role = isset($details['role']) ? $details['role'] : "" ;
|
|
if($role !== 'sadmin'){ ?>
|
|
<div class="form-group col-md-4">
|
|
<label for="role" class="col-form-label" id="role_label">Role <span class="text-danger">*</span></label>
|
|
<select id="role" name="role" class="form-control" required>
|
|
<option value="">--Select--</option>
|
|
<?php $roleArr = array('admin', 'accounts', 'auditor','volunteer');
|
|
foreach ($roleArr as $value) {
|
|
$selected = ($role === $value) ? "selected" : "";
|
|
?>
|
|
<option value="<?php echo $value; ?>" <?php echo $selected; ?>><?php echo ucfirst($value); ?></option>
|
|
<?php } ?>
|
|
</select>
|
|
</div>
|
|
<?php }else{ ?>
|
|
<input type="hidden" class="form-control" id="role" name="role" placeholder="Role" value="sadmin">
|
|
<?php } ?>
|
|
</div>
|
|
<?php if ($loggedin_person_role === 'sadmin') { ?>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-6">
|
|
<label for="business_id" class="col-form-label">Organization <span class="text-danger">*</span></label>
|
|
<select id="business_id" name="business_id" class="form-control" required>
|
|
<?php foreach ($organzation_details as $business) { ?>
|
|
<option value="<?= $business['business_id']; ?>" <?= isset($details['business_id']) && ($details['business_id'] == $business['business_id']) ? 'selected' : '' ?>>
|
|
<?= $business['title']; ?>
|
|
</option>
|
|
<?php } ?>
|
|
</select>
|
|
</div>
|
|
<div class="form-group col-md-6">
|
|
<label for="branch_id" class="col-form-label" id="branch_id_label">Organization Branches</label>
|
|
<select id="branch_id" name="branch_id" class="form-control" required>
|
|
<?php if (!empty($branches)) { ?>
|
|
<?php foreach ($branches as $branch) { ?>
|
|
<option value="<?= $branch['id']; ?>" <?= isset($details['branch_id']) && ($details['branch_id'] == $branch['id']) ? 'selected' : '' ?>>
|
|
<?= $branch['branch_name']; ?>
|
|
</option>
|
|
<?php } ?>
|
|
<?php } else { ?>
|
|
<option value="">No branches available</option>
|
|
<?php } ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<?php } ?>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-4">
|
|
<label for="email" class="col-form-label">Email <span class="text-danger">*</span></label>
|
|
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?= isset($details['email']) ? $details['email'] : '' ?>" required onchange="checkExisting(this,'email','email')"/>
|
|
<span id="emailValidationMessage"></span>
|
|
</div>
|
|
<?php if (empty($details)) { ?>
|
|
<div class="form-group col-md-4">
|
|
<label for="password" class="col-form-label">Password <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="password" name="password" placeholder="Password" value="<?= isset($details['password']) ? $details['password'] : '' ?>" required />
|
|
</div>
|
|
<?php } ?>
|
|
<?php if ($loggedin_person_role !== 'sadmin') : ?>
|
|
<div class="form-group col-md-4">
|
|
<label for="branch" class="col-form-label" id="branch_label">Branch</label>
|
|
<select id="branch" name="branch" class="form-control">
|
|
<option value="">--Select--</option>
|
|
<?php if (!empty($branches)) {
|
|
foreach ($branches as $branch) { ?>
|
|
<option data-business-id="<?= $branch['business_id']; ?>" value="<?= $branch['id']; ?>" <?= isset($details['branch_id']) && ($details['branch_id'] == $branch['id']) ? 'selected' : '' ?>>
|
|
<?= $branch['branch_name']; ?>
|
|
</option>
|
|
<?php }
|
|
} ?>
|
|
</select>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group col-md-4">
|
|
<label for="address" class="col-form-label">Address </label>
|
|
<input type="text" class="form-control" id="address" name="address" placeholder="Address (eg : 1234 Main St)" value="<?= isset($details['address']) ? $details['address'] : '' ?>" />
|
|
</div>
|
|
<div class="form-group col-md-3">
|
|
<label for="city" class="col-form-label">City </label>
|
|
<input type="text" class="form-control" id="city" name="city" placeholder="City" value="<?= isset($details['city']) ? $details['city'] : '' ?>" />
|
|
</div>
|
|
<div class="form-group col-md-3">
|
|
<label for="state" class="col-form-label">State </label>
|
|
<input type="text" class="form-control" id="state" name="state" placeholder="State" value="<?= isset($details['state']) ? $details['state'] : '' ?>" />
|
|
</div>
|
|
<div class="form-group col-md-2">
|
|
<label for="postal_code" class="col-form-label">Postal Code </label>
|
|
<input data-parsley-type="number" type="text" class="form-control" id="postal_code" name="postal_code" placeholder="Postal Code (PIN)" value="<?= isset($details['postal_code']) ? $details['postal_code'] : '' ?>" pattern="[0-9]{6}" maxlength="6" />
|
|
</div>
|
|
</div>
|
|
<input type="hidden" id="user_id" name="user_id" placeholder="hidden for user id" value="<?= isset($details['user_id']) ? $details['user_id'] : '' ?>" />
|
|
<input type="hidden" id="dummy_flag" name="dummy_flag" placeholder="hidden for dummy flag redirect purpose" value="<?= isset($dummy_flag) ? $dummy_flag : '' ?>" />
|
|
<?php if (!empty($details) && $loggedin_person_role !== 'volunteer') { ?>
|
|
<div class="form-group text-right m-b-0 checkbox checkbox-purple" style="display: none;">
|
|
<input type="checkbox" id="isactive" name="isactive" class="form-control" <?= isset($details) && $details['isactive'] == 1 ? 'checked' : '' ?>>
|
|
<label for="isactive"> Is Active</label>
|
|
</div>
|
|
<br>
|
|
<?php } ?>
|
|
|
|
<div class="form-group text-right m-b-0">
|
|
<button class="btn btn-success waves-effect waves-light mr-1" type="submit" id="submitBtn">
|
|
<?= isset($details['user_id']) ? 'Update' : 'Save' ?>
|
|
</button>
|
|
<?php if (isset($success_message)) : ?>
|
|
<div class="alert alert-success"><?= esc($success_message) ?></div>
|
|
<?php endif; ?>
|
|
|
|
|
|
<?php if ($loggedin_person_role !== 'volunteer') :
|
|
$user_id = isset($details['user_id']) ? $details['user_id'] : null;
|
|
$redirect_url = ($session_uid == $user_id) ? base_url() . "dashboard" : $redirect_url = base_url() . "user_list";
|
|
?>
|
|
<a href="<?= $redirect_url; ?>" class="btn btn-secondary waves-effect">Cancel</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<!-- end card-body-->
|
|
</div>
|
|
<!-- end card-->
|
|
</div>
|
|
<!-- end col-->
|
|
</div>
|
|
<!-- end row -->
|
|
<script>
|
|
$(function() {
|
|
role = "<?php echo get_user_role() ?>";
|
|
console.log(role);
|
|
if (role == "auditor") {
|
|
// for input
|
|
var formElements = document.querySelectorAll('#myForm input, #myForm textarea');
|
|
formElements.forEach(function(element) {
|
|
element.setAttribute('readonly', true);
|
|
});
|
|
// for select
|
|
var formElements = document.querySelectorAll('#myForm select, #myForm button');
|
|
formElements.forEach(function(element) {
|
|
element.setAttribute('disabled', true);
|
|
});
|
|
}
|
|
var flag_for_validations = "<?= isset($dummy_flag) ? $dummy_flag : '' ?>";
|
|
|
|
if (flag_for_validations == '1') {
|
|
$('#branch_label').html('Branch');
|
|
$('#role_label').html('Role');
|
|
$('#branch').removeAttr("required").prop('disabled', true);
|
|
$('#role').removeAttr("required").prop('disabled', true);
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<script>
|
|
document.getElementById('myForm').addEventListener('submit', function(event) {
|
|
var form = event.target;
|
|
if (form.checkValidity() === false) {
|
|
form.reportValidity(); // Display validation error messages
|
|
event.preventDefault(); // Prevent form submission if it's not valid
|
|
$('#submitBtn').prop('disabled', false);
|
|
} else {
|
|
$('#submitBtn').prop('disabled', true);
|
|
}
|
|
});
|
|
</script>
|
|
<script>
|
|
$(document).ready(function() {
|
|
|
|
var logged_person_role = "<?php echo get_user_role() ?>";
|
|
var dropdown_choosen_role = "<?= isset($details['role']) ? $details['role'] : ""; ?>";
|
|
if (dropdown_choosen_role && logged_person_role === 'sadmin') {
|
|
if (dropdown_choosen_role === 'volunteer' || dropdown_choosen_role === 'donor') {
|
|
$('#branch_id').prop('required', true);
|
|
$('#branch_id_label').html('Organization Branches<span class="text-danger">*</span>');
|
|
} else {
|
|
$('#branch_id').prop('required', false);
|
|
$('#branch_id_label').html('Organization Branches');
|
|
}
|
|
}else if(dropdown_choosen_role && logged_person_role === 'admin'){
|
|
if (dropdown_choosen_role === 'volunteer' || dropdown_choosen_role === 'donor') {
|
|
$('#branch').prop('required', true);
|
|
$('#branch_label').html('Branch <span class="text-danger">*</span>');
|
|
} else {
|
|
$('#branch').prop('required', false);
|
|
$('#branch_label').html('Branch');
|
|
}
|
|
}
|
|
|
|
// Function to fetch branches based on selected organization
|
|
function fetchBranches(selectedBusinessId, preSelectedBranchId) {
|
|
var businessBranchDropdown = $('#branch_id');
|
|
|
|
// Clear existing options
|
|
businessBranchDropdown.empty().append('<option value="">--Select--</option>');
|
|
|
|
// Fetch branches via AJAX
|
|
$.ajax({
|
|
url: '<?= base_url('users/get_branches') ?>/' + selectedBusinessId,
|
|
method: 'GET',
|
|
dataType: 'json',
|
|
success: function(response) {
|
|
console.log('Response from server:', response); // Log the response for debugging
|
|
|
|
if (response && response.length > 0) {
|
|
console.log('Number of branches:', response.length); // Log the number of branches
|
|
|
|
$.each(response, function(index, branch) {
|
|
console.log('Appending option:', branch); // Log each branch before appending
|
|
|
|
var option = $('<option></option>').attr('value', branch.id).text(branch.branch_name);
|
|
|
|
// Set selected attribute if the branch ID matches the pre-selected branch ID
|
|
if (branch.id == preSelectedBranchId) {
|
|
option.attr('selected', 'selected');
|
|
}
|
|
|
|
businessBranchDropdown.append(option);
|
|
});
|
|
} else {
|
|
console.log('No branches available.'); // Log if no branches are available
|
|
businessBranchDropdown.append('<option value="">No branches available</option>');
|
|
}
|
|
},
|
|
error: function(error) {
|
|
console.error('Error fetching branches:', error);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Change event handler for the organization dropdown
|
|
$('#business_id').change(function() {
|
|
var selectedBusinessId = $(this).val();
|
|
console.log('Selected Business ID:', selectedBusinessId); // Log the selected business ID
|
|
|
|
// Call the function to fetch branches
|
|
if (selectedBusinessId) {
|
|
fetchBranches(selectedBusinessId, <?= json_encode($details['branch_id'] ?? null) ?>);
|
|
}
|
|
});
|
|
|
|
$('#role').change(function() {
|
|
|
|
var selectedOption = $(this).val();
|
|
if(logged_person_role === 'sadmin'){
|
|
if (selectedOption === 'volunteer' || selectedOption === 'donor') {
|
|
$('#branch_id').prop('required', true);
|
|
$('#branch_id_label').html('Organization Branches<span class="text-danger">*</span>');
|
|
} else {
|
|
$('#branch_id').prop('required', false);
|
|
$('#branch_id_label').html('Organization Branches');
|
|
}
|
|
}else if(logged_person_role === 'admin'){
|
|
if (selectedOption === 'volunteer' || selectedOption === 'donor') {
|
|
$('#branch').prop('required', true);
|
|
$('#branch_label').html('Branch <span class="text-danger">*</span>');
|
|
} else {
|
|
$('#branch').prop('required', false);
|
|
$('#branch_label').html('Branch');
|
|
}
|
|
}
|
|
});
|
|
|
|
// Trigger the change event if the organization is pre-selected (e.g., during edit)
|
|
var preSelectedBusinessId = $('#business_id').val();
|
|
if (preSelectedBusinessId) {
|
|
fetchBranches(preSelectedBusinessId, <?= json_encode($details['branch_id'] ?? null) ?>);
|
|
}
|
|
});
|
|
</script>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Get the form group element
|
|
var formGroup = document.querySelector('.form-group.text-right.checkbox.checkbox-purple');
|
|
|
|
// Check if the form group exists and the logged-in person role is not 'volunteer'
|
|
if (formGroup && <?= $loggedin_person_role !== 'volunteer' ?>) {
|
|
// Hide the form group
|
|
formGroup.style.display = 'none';
|
|
}
|
|
});
|
|
function checkExisting(input,databasecolumnname,elementid){
|
|
spanid = elementid == 'email' ? "emailValidationMessage" : "mobileValidationMessage";
|
|
var validationMessage = document.getElementById(spanid);
|
|
var value = input.value.trim();
|
|
if (value === "") {
|
|
validationMessage.style.display = 'none';
|
|
return;
|
|
}
|
|
$.ajax({
|
|
type: "POST",
|
|
url: "<?= base_url() . 'check_existing' ?>",
|
|
data: {
|
|
value: value,
|
|
module: 'users',
|
|
field: databasecolumnname
|
|
},
|
|
success: function(response) {
|
|
console.log("check existing " + elementId + "--" + response['data']['message']);
|
|
validationMessage.style.display = response['data']['status'] ? 'block' : 'none';
|
|
validationMessage.innerText = response['data']['message'];
|
|
validationMessage.style.color = "red";
|
|
input.setCustomValidity(response['data']['status'] ? response['data']['message'] : '');
|
|
if (response['data']['status']) input.value = '';
|
|
},
|
|
error: function(xhr, status, error) {
|
|
console.error("check existing " + elementId + " -- " + status + ": " + error);
|
|
}
|
|
});
|
|
}
|
|
|
|
</script>
|