258 lines
12 KiB
PHP
Executable File
258 lines
12 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers;
|
|
use App\Models\BusinessModel;
|
|
use App\Models\SettingsModel;
|
|
|
|
class Settings extends BaseController
|
|
{
|
|
public function index()
|
|
{
|
|
helper('session');
|
|
$session_role = get_user_role();
|
|
$session_bid = get_business_id();
|
|
|
|
$model = new SettingsModel();
|
|
$model->setTable('settings');
|
|
if (!empty($session_role) && $session_role !== "sadmin") {
|
|
$where = ['settings.business_id !='=> '1', 'settings.isactive' => 1];
|
|
$sitesetting_details = $model->where($where)->orderBy('setting_id', 'DESC')->findAll();
|
|
}else{
|
|
$where = ['settings.business_id !='=> '1'];
|
|
$sitesetting_details = $model->where($where)->orderBy('setting_id', 'DESC')->findAll();
|
|
}
|
|
$data['page_name'] = 'Site Settings Details';
|
|
$data['details'] = $sitesetting_details;
|
|
$data['session_role'] = $session_role;
|
|
$this->render_page('setting_list', $data);
|
|
}
|
|
|
|
## To Load Settings Add/Update page
|
|
public function sitesetting_page($id)
|
|
{
|
|
if ($id === '0') {
|
|
$data['page_name'] = 'Add Site Settings';
|
|
$data['details'] = [];
|
|
} else if ($id !== '0') {
|
|
$data['page_name'] = 'Edit Site Settings';
|
|
$model = new SettingsModel();
|
|
$model->setTable('settings');
|
|
$details = $model->where(['setting_id' => $id, 'isactive' => 1])->first();
|
|
$data['details'] = $details;
|
|
|
|
}
|
|
$data['business_details']=$this->business_details();
|
|
$this->render_page('setting_form', $data);
|
|
}
|
|
public function business_details()
|
|
{
|
|
$model = new BusinessModel();
|
|
$model->setTable('business');
|
|
$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;
|
|
}
|
|
|
|
|
|
## To Save/Update Setting Data on DB.
|
|
public function sitesetting_synchronization()
|
|
{
|
|
helper('session');
|
|
$session_bid = get_business_id();
|
|
$session_uid = get_logged_user_id();
|
|
|
|
// $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;
|
|
// }
|
|
|
|
$setting_id = $this->request->getPost('setting_id');
|
|
$business_id = $this->request->getPost('business_id');
|
|
|
|
$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/ico,image/x-icon]'
|
|
]
|
|
];
|
|
|
|
$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/';
|
|
|
|
##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{
|
|
## 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");
|
|
}
|
|
|
|
$SettingsModel = new SettingsModel();
|
|
$data = [
|
|
'site_name' => $this->request->getPost('site_name'),
|
|
'site_short_name' => $this->request->getPost('site_short_name'),
|
|
'site_color' => $this->request->getPost('site_color'),
|
|
'logo' => $this->request->getPost('logo'),
|
|
'terms_service' => $this->request->getPost('terms_service'),
|
|
'footer_about' => $this->request->getPost('footer_about'),
|
|
'site_email' => $this->request->getPost('site_email'),
|
|
'mobile'=> $this->request->getPost('mobile'),
|
|
'copyright'=> $this->request->getPost('copyright'),
|
|
'site_info'=> $this->request->getPost('site_info'),
|
|
'about_info'=> $this->request->getPost('about_info'),
|
|
'country'=> $this->request->getPost('country'),
|
|
'business_id'=> $this->request->getPost('business_id'),
|
|
'favicon'=>$final_img_name
|
|
];
|
|
$setting_id = $this->request->getPost('setting_id'); // Get the business ID for update
|
|
|
|
|
|
if (empty($setting_id)) {
|
|
// It's an insert operation
|
|
$data['isactive'] = 1;
|
|
$data['created_by'] = $session_uid;
|
|
if ($SettingsModel->insert($data)) {
|
|
$setting_id = $SettingsModel->insertID();
|
|
session()->setFlashdata('success', 'Site Settings has been added successfully.');
|
|
$this->logger->info("Site Settings : has been added successfully. Inserted ID = " . $setting_id);
|
|
// If it's a subscription invoice, also store data in the 'subscription' table
|
|
} else {
|
|
session()->setFlashdata('error', 'Site Settings could not be added. Please try again.');
|
|
$this->logger->error("Site Settings : Err Occur could not be added. Please try again.");
|
|
}
|
|
// print_r($data);die;
|
|
} else {
|
|
// It's an update operation
|
|
$isactive = $this->request->getPost('isactive');
|
|
$data['isactive'] = ($isactive == 'on') ? 1 : 0;
|
|
$data['updated_by'] = $session_uid;
|
|
if ($SettingsModel->update($setting_id, $data)) {
|
|
$business_affectedRows = $SettingsModel->db->affectedRows();
|
|
session()->setFlashdata('success', 'Site Settings successfully updated.');
|
|
$this->logger->info("Site Settings: has been updated successfully. Updated Site Settings ID = " . $business_id . " Aff ".$business_affectedRows);
|
|
} else {
|
|
session()->setFlashdata('error', 'Site Settings updation failed. Please try again.');
|
|
$this->logger->error("Site Settings: Err Failed to update ID =" . $business_id);
|
|
}
|
|
}
|
|
return redirect()->route('sitesetting_list');
|
|
}
|
|
|
|
public function delete_sitesetting($id)
|
|
{
|
|
helper('session');
|
|
$session_uid = get_logged_user_id();
|
|
try {
|
|
$SettingsModel = new SettingsModel();
|
|
$existed = $SettingsModel->find($id);
|
|
$this->logger->Info("SiteSetting : Going to Inactive ID = " . $id);
|
|
|
|
if ($existed) {
|
|
$data['isactive'] = 0;
|
|
$data['updated_by'] = get_logged_user_id();
|
|
|
|
if ($SettingsModel->update($id, $data)) {
|
|
session()->setFlashdata('success', 'Deleted successfully.');
|
|
$this->logger->info("SiteSetting: has been Inactived successfully. Inactived ID = " . $id);
|
|
} else {
|
|
$this->logger->error("SiteSetting: Not able to Inactive ID =" . $id);
|
|
throw new \Exception("Data Not able to Deleted");
|
|
}
|
|
} else {
|
|
$this->logger->error("SiteSetting: Does Not Exist To Inactive, ID = " . $id);
|
|
throw new \Exception("SiteSetting Already Deleted");
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("SiteSetting: Err Occur = " . $e->getMessage());
|
|
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
|
|
}
|
|
return redirect()->route('sitesetting_list');
|
|
}
|
|
}
|
|
|
|
|
|
// $model = new SettingsModel();
|
|
// $model->setTable('settings');
|
|
// if ($this->request->is('post')) {
|
|
// $requestData = $this->request->getVar();
|
|
// }
|
|
// $requestData['business_id'] = $session_bid;
|
|
// if ($requestData['setting_id']) {
|
|
// #edit
|
|
// $requestData['updated_by'] = $session_uid;
|
|
// $requestData['isactive'] = isset($requestData['isactive']) && $requestData['isactive'] == 'on' ? 1 : 0;
|
|
// $model->saveData($requestData, $requestData['setting_id']);
|
|
// $alert_message = "Site Settings details successfully updated.";
|
|
// } else {
|
|
// #add
|
|
// $requestData['created_by'] = $session_uid;
|
|
// $requestData['isactive'] = 1;
|
|
// $model->saveData($requestData, null);
|
|
// $alert_message = "Site Settings details successfully created.";
|
|
// }
|
|
// return redirect()->route('sitesetting_list');
|
|
// }
|
|
// }
|