File Related Changes : ps

This commit is contained in:
VE10-Sanjeev 2023-12-21 13:07:51 +05:30
parent 68cf53b248
commit a8c173990b
4 changed files with 181 additions and 138 deletions

View File

@ -57,11 +57,11 @@ class Business extends BaseController
$session_uid = get_logged_user_id();
$session_role = get_user_role();
## CI validation rule for bfile
## CI validation rule for business_logo
$validationRule = [
'bfile' => [
'business_logo' => [
'label' => 'Image File',
'rules' => 'uploaded[bfile]|is_image[bfile]|mime_in[bfile,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
'rules' => 'uploaded[business_logo]|is_image[business_logo]|mime_in[business_logo,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
]
];
@ -69,58 +69,57 @@ class Business extends BaseController
$img = $this->request->getFile('bfile');
$business_id = $this->request->getPost('business_id');
$img_details = $this->request->getFile('bfile'); // Here I Have Image details ;
$img_details = $this->request->getFile('business_logo'); // Here I Have Image details ;
$final_img_name = NULL;//Just flag
$existing_img_name = $this->request->getPost('existing_business_logo_name'); // HiddenField for if have any pic name means;
$img_path = 'public/uploads/';
## File Already Existing or not
##Step 1 : image details available
if($img_details){
$this->logger->info("Business: image details avaiable");
$this->logger->info("business logo: image details avaiable");
##Step 2 : i have image details .<br/>
if ($img_details->isValid()) {
##Step 3 : image Name.$new_img_name."<br/>";
$new_img_name = $img_details->getName(); // before movement name
$this->logger->info("Business: Image Name B4 Upload".$new_img_name);
$this->logger->info("business logo: Image Name B4 Upload".$new_img_name);
##Step 4 : Validation Rule Apply here.if not throw the error"<br/>";
if (!$this->validate($validationRule)) {
if($new_img_name !== ''){
$error = $this->validator->getErrors();
// echo "Error : ".(string)$error."<br/>";
$this->logger->error("Business: Err on image upload".$error['bfile']);
throw new \Exception((string)$error['bfile']);
$this->logger->error("business logo: Err on image upload".$error['business_logo']);
throw new \Exception((string)$error['business_logo']);
}
}
##Step 5 : Check Existing and New Image Name Same Or not same no use to move on target folder
if($new_img_name !== $existing_img_name){
$this->logger->info("Business: Image Name are Differ".$new_img_name." & ".$existing_img_name);
$this->logger->info("business logo: Image Name are Differ".$new_img_name." & ".$existing_img_name);
## Step 6 : Different Image Name means removed on target folder using Unlink;
if ($existing_img_name && is_file($img_path.$existing_img_name)) {
$this->logger->info("Business: already Image Available on target Folder Path : ".$img_path.$existing_img_name." So, Deleted.");
$this->logger->info("business logo: already Image Available on target Folder Path : ".$img_path.$existing_img_name." So, Deleted.");
// echo "Deleted_file : ".(string)$img_path.$existing_img_name."<br/>";
unlink($img_path.$existing_img_name);
}
## Step 7 : Moved to target;
$img_details->move($img_path, $new_img_name);
$final_img_name = $img_details->getName(); // after movement name
$this->logger->info("Business: Image Name After Upload".$final_img_name);
$this->logger->info("business logo: Image Name After Upload".$final_img_name);
}else{
$final_img_name = $existing_img_name;
$this->logger->info("Business: Image Name are same".$new_img_name." & ".$existing_img_name." Can't Upload");
$this->logger->info("business logo: Image Name are same".$new_img_name." & ".$existing_img_name." Can't Upload");
}
}
else{
## Step 8 : Not a Vaild Image File so replace Existing file name;
$final_img_name = $existing_img_name;
$this->logger->info("Business: Not a Vaild Image File Nothing To Update/Upload");
$this->logger->info("business logo: Not a Vaild Image File Nothing To Update/Upload");
// throw new \Exception("Not a Vaild Image File");
}
}
else{
## Step 9 : Testing Purpose File Details Not Available. so i can't move it.;
$this->logger->info("Business: Testing Purpose Image Details Not Available. so i can't move it.");
$this->logger->info("business logo: Testing Purpose Image Details Not Available. so i can't move it.");
$final_img_name = $existing_img_name;
//throw new \Exception("Testing Purpose File Details Not Available so i can't move it if you have existing filee means save it or your choice");
}

View File

@ -47,7 +47,11 @@ class Settings extends BaseController
{
$model = new BusinessModel();
$model->setTable('business');
$business_details = $model->orderBy('business_id', 'DESC')->findAll();
$business_details = $model->orderBy('business.business_id', 'DESC')
->select('business.*,settings.setting_id')
->join('settings', 'settings.business_id = business.business_id', 'left')
->groupBy('business.business_id')
->findAll();
return $business_details;
}
@ -59,80 +63,95 @@ class Settings extends BaseController
$session_bid = get_business_id();
$session_uid = get_logged_user_id();
$validationRule = [
'usersetiing' => [
'label' => 'Image File',
'rules' => [
'uploaded[slogo]',
'is_image[slogo]',
'mime_in[slogo,image/jpg,image/jpeg,image/gif,image/png,image/webp]',
// $validationRule = [
// 'favicon' => [
// 'label' => 'Favicon',
// 'rules' => [
// 'uploaded[favicon]',
// 'is_image[favicon]',
// 'mime_in[favicon,image/ico,image/x-icon]',
// ],
// ],
// ];
],
],
// if (!$this->validate($validationRule)) {
// // Validation failed, return to the form with errors
// $data['validation_errors'] = $this->validator->getErrors();
// $this->render_page('setting_form', $data);
// return;
// }
'favicon' => [
'label' => 'Favicon',
'rules' => [
'uploaded[favicon]',
'is_image[favicon]',
'mime_in[favicon,image/ico,image/x-icon]',
],
],
];
if (!$this->validate($validationRule)) {
// Validation failed, return to the form with errors
$data['validation_errors'] = $this->validator->getErrors();
$this->render_page('setting_form', $data);
return;
}
$img = $this->request->getFile('slogo');
$setting_id = $this->request->getPost('setting_id');
$business_id = $this->request->getPost('business_id');
$filePath ='public/uploads/' . $this->request->getPost('slogo');
## File Already Existing or not
$img_details = $this->request->getFile('favicon'); // Here I Have Image details ;
## CI validation rule for favicon
$validationRule = [
'favicon' => [
'label' => 'Image File',
'rules' => 'uploaded[favicon]|is_image[favicon]|mime_in[favicon,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
]
];
$final_img_name = NULL;//Just flag
$existing_img_name = $this->request->getPost('existing_favicon_name'); // HiddenField for if have any pic name means;
$img_path = 'public/uploads/';
if ($img->isValid() && !$img->hasMoved()) {
$fileName = $img->getName();
$img->move('public/uploads/', $fileName);
// Delete the previous image file if it exists
if ($setting_id) {
$previousFileName = $this->request->getPost('previous_sfile');
if ($previousFileName && is_file('public/uploads/' . $previousFileName)) {
unlink('public/uploads/' . $previousFileName);
##Step 1 : image details available
if($img_details){
$this->logger->info("Sitesetting Favicon: image details avaiable");
##Step 2 : i have image details .<br/>
if ($img_details->isValid()) {
##Step 3 : image Name.$new_img_name."<br/>";
$new_img_name = $img_details->getName(); // before movement name
$this->logger->info("Sitesetting Favicon: Image Name B4 Upload".$new_img_name);
##Step 4 : Validation Rule Apply here.if not throw the error"<br/>";
if (!$this->validate($validationRule)) {
if($new_img_name !== ''){
$error = $this->validator->getErrors();
// echo "Error : ".(string)$error."<br/>";
$this->logger->error("Sitesetting Favicon: Err on image upload".$error['favicon']);
throw new \Exception((string)$error['favicon']);
}
}
##Step 5 : Check Existing and New Image Name Same Or not same no use to move on target folder
if($new_img_name !== $existing_img_name){
$this->logger->info("Sitesetting Favicon: Image Name are Differ".$new_img_name." & ".$existing_img_name);
## Step 6 : Different Image Name means removed on target folder using Unlink;
if ($existing_img_name && is_file($img_path.$existing_img_name)) {
$this->logger->info("Sitesetting Favicon: already Image Available on target Folder Path : ".$img_path.$existing_img_name." So, Deleted.");
// echo "Deleted_file : ".(string)$img_path.$existing_img_name."<br/>";
unlink($img_path.$existing_img_name);
}
## Step 7 : Moved to target;
$img_details->move($img_path, $new_img_name);
$final_img_name = $img_details->getName(); // after movement name
$this->logger->info("Sitesetting Favicon: Image Name After Upload".$final_img_name);
}else{
$final_img_name = $existing_img_name;
$this->logger->info("Sitesetting Favicon: Image Name are same".$new_img_name." & ".$existing_img_name." Can't Upload");
}
}
else{
## Step 8 : Not a Vaild Image File so replace Existing file name;
$final_img_name = $existing_img_name;
$this->logger->info("Sitesetting Favicon: Not a Vaild Image File Nothing To Update/Upload");
// throw new \Exception("Not a Vaild Image File");
}
} else {
$fileName = $this->request->getPost('previous_sfile', ''); // Use the previous filename if no new image is provided
}
$favicon = $this->request->getFile('favicon');
if ($favicon->isValid() && !$favicon->hasMoved()) {
$faviconName = $favicon->getName();
$favicon->move('public/uploads/', $faviconName);
// Delete the previous favicon file if it exists
if ($setting_id) {
$previousFaviconName = $this->request->getPost('previous_favicon');
if ($previousFaviconName && is_file('public/uploads/' . $previousFaviconName)) {
unlink('public/uploads/' . $previousFaviconName);
else{
## Step 9 : Testing Purpose File Details Not Available. so i can't move it.;
$this->logger->info("Sitesetting Favicon: Testing Purpose Image Details Not Available. so i can't move it.");
$final_img_name = $existing_img_name;
//throw new \Exception("Testing Purpose File Details Not Available so i can't move it if you have existing filee means save it or your choice");
}
}
} else {
$faviconName = $this->request->getPost('previous_favicon', ''); // Use the previous favicon filename if no new favicon is provided
}
$SettingsModel = new SettingsModel();
$data = [
'site_name' => $this->request->getPost('site_name'),
'site_title' => $this->request->getPost('site_title'),
'favicon' => $this->request->getPost('favicon'),
'logo' => $this->request->getPost('logo'),
'terms_service' => $this->request->getPost('terms_service'),
'footer_about' => $this->request->getPost('footer_about'),
@ -152,8 +171,7 @@ class Settings extends BaseController
'currency'=> $this->request->getPost('currency'),
'country'=> $this->request->getPost('country'),
'business_id'=> $this->request->getPost('business_id'),
'logo'=>$fileName,
'favicon'=>$faviconName
'favicon'=>$final_img_name
];
$setting_id = $this->request->getPost('setting_id'); // Get the business ID for update

View File

@ -87,23 +87,25 @@
</div>
</div>
<div class="form-row">
<?php if (!empty($businesses['business_logo'])) { ?>
<div class="form-group col-md-4 mt-3">
<div class="media">
<img src="<?= base_url('public/uploads/' . $businesses['business_logo']) ?>" alt="Business Logo" height="50" class="preview-image d-flex align-self-start rounded mr-2">
<div class="media-body">
<h5 class="mt-0">Existing Logo</h5>
<h5 class="mt-0">Existing Business Logo</h5>
<p class="mb-1"><?= $businesses['business_logo']; ?></p>
<input type="hidden" id="existing_business_logo_name" name="existing_business_logo_name" placeholder="hidden Field for business_logo name" readonly value="<?= $businesses['business_logo']; ?>" />
<input type="hidden" id="existing_business_logo_name" name="existing_business_logo_name" placeholder="hidden Field for Business Logo name" readonly value="<?= $businesses['business_logo']; ?>" />
</div>
</div>
</div>
<?php } ?>
<div class="form-group col-md-4">
<label for="bfile" class="col-form-label">Logo</label>
<input type="file" name="profile_picture" value="<?= isset($details['business_logo']) ? $details['business_logo'] : '' ?>" accept="image/*" onchange="validateFile(this)" />
<label for="business_logo" class="col-form-label">Business Logo</label>
<input type="file" name="business_logo" value="<?= isset($businesses['business_logo']) ? $details['business_logo'] : '' ?>" accept="image/*" onchange="validateFile(this)" />
<div id="error_message" style="color: red;"></div>
</div>
</div>
<input type="hidden" id="business_id" name="business_id" placeholder="hidden for business id" value="<?= isset($businesses['business_id']) ? $businesses['business_id'] : '' ?>" />
<?php if (!empty($businesses) && ($loggedin_person_role === 'sadmin')) { ?>

View File

@ -27,16 +27,32 @@
<form method="post" enctype="multipart/form-data"
action="<?= base_url()."sitesetting_synchronization"; ?>">
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-12">
<label for="business_id" class="col-form-label">Business<span
class="text-danger">*</span></label>
<select id="business_id" name="business_id" class="form-control" data-toggle="select2" onchange="fetch_details(this.value);">
<option value=null>Choose your Business</option>
<?php foreach ($business_details as $value) {
$disabled = ($value['setting_id'] != "") ? 'disabled' : '' ;
?>
<option value="<?php echo $value['business_id']; ?>"
<?php if (isset($details['business_id']) && ($details['business_id'] === $value['business_id'])) echo "selected"; ?> <?= $disabled ?> >
<?php echo $value['title'].($value['setting_id'] != "" ? " (Disabled) " : ""); ?>
</option>
<?php } ?>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="site_name" class="col-form-label">Site Name<span
class="text-danger">*</span></label>
<label for="site_name" class="col-form-label">Site Name</label>
<input type="text" class="form-control" id="site_name" name="site_name"
value="<?= isset($details['site_name']) ? $details['site_name'] : '' ?>"
placeholder="Site Name" required />
placeholder="Site Name" readonly />
</div>
<div class="form-group col-md-6">
<label for="site_title" class="col-form-label">Site Title<span
<label for="site_title" class="col-form-label">Site Short Name<span
class="text-danger">*</span></label>
<input type="text" class="form-control" id="site_title" name="site_title"
placeholder="Site Title"
@ -68,11 +84,10 @@
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="terms_service" class="col-form-label">Terms Service<span
class="text-danger">*</span></label>
<label for="terms_service" class="col-form-label">Terms Service</label>
<textarea class="form-control" id="terms_service" name="terms_service"
placeholder="Terms Service" required
rows="5"><?= isset($details['terms_service']) ? $details['terms_service'] : '' ?></textarea>
placeholder="Terms Service" readonly
rows="5"><?= isset($details['terms_service']) ? $details['terms_service'] : ''?></textarea>
</div>
<div class="form-group col-md-6">
<label for="footer_about" class="col-form-label">Footer about<span
@ -92,11 +107,10 @@
required />
</div>
<div class="form-group col-md-6">
<label for="mobile" class="col-form-label">Mobile Number<span
class="text-danger">*</span></label>
<label for="mobile" class="col-form-label">Mobile Number</label>
<input type="number" class="form-control" id="mobile" name="mobile"
placeholder="Mobile Number (Enter only numbers)" required data-parsley-type="number"
value="<?= isset($details['mobile']) ? $details['mobile'] : '' ?>" />
placeholder="Mobile Number (Enter only numbers)" data-parsley-type="number"
value="<?= isset($details['mobile']) ? $details['mobile'] : '' ?>" readonly />
</div>
</div>
<div class="form-row">
@ -115,33 +129,24 @@
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="logo" class="col-form-label">logo name</label>
<input type="file" class="form-control" style="border: 0px !important; " id="logo"
name="slogo" placeholder="Logo Name" required
value="<?= isset($details['logo']) ? $details['logo'] : '' ?>" />
</div>
<?php if (!empty($details['logo'])) { ?>
<div class="form-group col-md-6">
<label for="slogo" class="col-form-label"> Logo</label>
<img src="<?= base_url('public/uploads/'. $details['logo']) ?>" alt="Logo"
class="preview-image" />
</div>
<?php } ?>
<div class="form-group col-md-6">
<label for="favicon" class="col-form-label">Favicon</label>
<input type="file" class="form-control" style="border: 0px !important; " id="favic"
name="favicon" placeholder="Fav-icon Name" required
value="<?= isset($details['favicon']) ? $details['favicon'] : '' ?>" />
</div>
<?php if (!empty($details['favicon'])) { ?>
<div class="form-group col-md-6">
<label for="favicon" class="col-form-label">Icon</label>
<img src="<?= base_url('public/uploads/'. $details['favicon']) ?>" alt="Icon"
class="preview-image" />
</div>
<div class="form-group col-md-4 mt-3">
<div class="media">
<img src="<?= base_url('public/uploads/' . $details['favicon']) ?>" alt="Icon" height="50" class="preview-image d-flex align-self-start rounded mr-2">
<div class="media-body">
<h5 class="mt-0">Existing FavIcon</h5>
<p class="mb-1"><?= $details['favicon']; ?></p>
<input type="hidden" id="existing_favicon_name" name="existing_favicon_name" placeholder="hidden Field for Icon name" readonly value="<?= $details['favicon']; ?>" />
</div>
</div>
</div>
<?php } ?>
</div>
<div class="form-group col-md-4">
<label for="favicon" class="col-form-label">FavIcon</label>
<input type="file" name="favicon" value="<?= isset($details['favicon']) ? $details['favicon'] : '' ?>" accept="image/*" onchange="validateFile(this)" />
<div id="error_message" style="color: red;"></div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="currency" class="col-form-label">Currency<span
@ -151,11 +156,10 @@
value="<?= isset($details['currency']) ? $details['currency'] : '' ?>" />
</div>
<div class="form-group col-md-6">
<label for="country" class="col-form-label">Country<span
class="text-danger">*</span></label>
<label for="country" class="col-form-label">Country</label>
<input type="text" class="form-control" id="country" name="country"
placeholder="Country" required
value="<?= isset($details['country']) ? $details['country'] : '' ?>" />
placeholder="Country"
value="<?= isset($details['country']) ? $details['country'] : '' ?>" readonly />
</div>
</div>
<div class="form-row">
@ -163,7 +167,7 @@
<label for="mail_protocol" class="col-form-label">Mail Protocol<span
class="text-danger">*</span></label>
<input type="text" class="form-control" id="mail_protocol" name="mail_protocol"
placeholder="Mail Protocol (eg : zoho,gmail)" required
placeholder="Mail Protocol (eg : zoho,gmail,yahoo,outlook)" required
value="<?= isset($details['mail_protocol']) ? $details['mail_protocol'] : '' ?>" />
</div>
<div class="form-group col-md-4">
@ -210,23 +214,11 @@
placeholder="Mail Password" required
value="<?= isset($details['mail_password']) ? $details['mail_password'] : '' ?>" />
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="postal_code" class="col-form-label">Business<span
class="text-danger">*</span></label>
<select id="business_id" name="business_id" class="form-control">
<option value=null>Choose your Business</option>
<?php foreach ($business_details as $value) { ?>
<option value="<?php echo $value['business_id']; ?>"
<?php if (isset($details['business_id']) && ($details['business_id'] === $value['business_id'])) echo "selected"; ?>>
<?php echo $value['title']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<input type="hidden" id="setting_id" name="setting_id" placeholder="hidden for setting id"
value="<?= isset($details['setting_id']) ? $details['setting_id'] : '' ?>" />
<input type="hidden" id="logo" name="logo" placeholder="hidden for logo"
value="<?= isset($details['logo']) ? $details['logo'] : '' ?>" />
<?php if(!empty($details)){ ?>
<div class="form-group text-right m-b-0 checkbox checkbox-purple">
@ -252,6 +244,38 @@
</div> <!-- end col-->
</div>
<!-- end row -->
<script>
function fetch_details(buss_id){
var buss_arr = <?php echo json_encode($business_details); ?>;
var filtered_buss_arr = buss_arr.filter(item => item['business_id'] == buss_id);
if (filtered_buss_arr.length > 0) {
$.each(filtered_buss_arr, function(k, v) {
$("#site_name").val(v.title).prop("readonly", true);
$("#country").val(v.country).prop("readonly", true);
$("#mobile").val(v.mobile_no).prop("readonly", true);
$("#terms_service").val(v.terms).prop("readonly", true);
$("#logo").val(v.favicon);
});
}else{
alert("Business Details Not Founded");
}
}
function validateFile(input) {
const file = input.files[0];
const errorMessage = document.getElementById("error_message");
if (file) {
if (file.type.startsWith("image/")) {
// It's an image file, no error
errorMessage.textContent = "";
} else {
// Invalid file type
errorMessage.textContent = "Invalid file type. Please select an image.";
input.value = ""; // Clear the input field
}
}
}
</script>
<script>
document.getElementById('myForm').addEventListener('submit', function(event) {
var form = event.target;