208 lines
9.5 KiB
PHP
Executable File
208 lines
9.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\BusinessModel;
|
|
|
|
class Business extends BaseController
|
|
{
|
|
|
|
## For Business Listing
|
|
public function index()
|
|
{
|
|
helper('session');
|
|
if (is_session_active()) {
|
|
$session_role = get_user_role();
|
|
if (!empty($session_role) && $session_role !== "sadmin") {
|
|
$this->logger->info("Business: Listing In admin role .");
|
|
$where = ['business_id' => (int)get_business_id(), 'isactive' => 1];
|
|
} else {
|
|
$this->logger->info("Business: Listing In Super-admin role .");
|
|
$where = ['isactive !=' => NULL];
|
|
}
|
|
$BusinessModel = new BusinessModel();
|
|
$data['page_name'] = 'Business Details';
|
|
$data['businesses'] = $BusinessModel->where($where)->findAll();
|
|
// $data['lastQuery'] = $BusinessModel->getLastQuery();
|
|
// print_r($data);die;
|
|
$this->render_page('business_list', $data);
|
|
} else {
|
|
return redirect()->to('login');
|
|
}
|
|
}
|
|
|
|
## To Load Business Form (Add/Update)
|
|
public function new_bussiness($id)
|
|
{
|
|
|
|
if ($id === '0') {
|
|
$data['page_name'] = 'Add Business';
|
|
$data['businesses'] = [];
|
|
} else if ($id !== '0') {
|
|
$data['page_name'] = 'Edit Business';
|
|
$model = new BusinessModel();
|
|
$model->setTable('business');
|
|
$edit_user_details = $model->where(['business_id ' => $id])->first();
|
|
|
|
$data['businesses'] = $edit_user_details;
|
|
}
|
|
// print_r($data);//die();
|
|
$this->render_page('business_form', $data);
|
|
}
|
|
|
|
## For inserting/updating details of business
|
|
public function insert_business()
|
|
{
|
|
helper('session');
|
|
$session_uid = get_logged_user_id();
|
|
$session_role = get_user_role();
|
|
|
|
## CI validation rule for business_logo
|
|
$validationRule = [
|
|
'business_logo' => [
|
|
'label' => 'Image File',
|
|
'rules' => 'uploaded[business_logo]|is_image[business_logo]|mime_in[business_logo,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
|
|
]
|
|
];
|
|
|
|
|
|
$img = $this->request->getFile('bfile');
|
|
$business_id = $this->request->getPost('business_id');
|
|
|
|
$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/';
|
|
|
|
##Step 1 : image details available
|
|
if($img_details){
|
|
$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 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 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 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 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 logo: Image Name After Upload".$final_img_name);
|
|
}else{
|
|
$final_img_name = $existing_img_name;
|
|
$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 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 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");
|
|
}
|
|
|
|
$BusinessModel = new BusinessModel();
|
|
$data = [
|
|
'title' => $this->request->getPost('bname'),
|
|
'email' => $this->request->getPost('bmail'),
|
|
'mobile_no' => $this->request->getPost('bmobile'),
|
|
'address' => $this->request->getPost('baddress'),
|
|
'city' => $this->request->getPost('bcity'),
|
|
'state' => $this->request->getPost('bstate'),
|
|
'postal_code' => $this->request->getPost('bzip'),
|
|
'country' => $this->request->getPost('bcountry'),
|
|
'terms'=>$this->request->getPost('bterms'),
|
|
'business_logo' => $final_img_name
|
|
];
|
|
$business_id = $this->request->getPost('business_id'); // Get the business ID for update
|
|
|
|
if (empty($business_id)) {
|
|
// It's an insert operation
|
|
$data['isactive'] = 1;
|
|
$data['created_by'] = $session_uid;
|
|
if ($BusinessModel->insert($data)) {
|
|
$business_id = $BusinessModel->insertID();
|
|
session()->setFlashdata('success', 'business successfully created.');
|
|
$this->logger->info("businesss: has been added successfully. Inserted ID = " . $business_id);
|
|
} else {
|
|
session()->setFlashdata('error', 'business could not be created. Please try again..');
|
|
$this->logger->error("business: Err could not be added. Please try again.");
|
|
}
|
|
|
|
} else {
|
|
// It's an update operation
|
|
if($session_role === 'sadmin'){
|
|
$isactive = $this->request->getPost('bcheckbox');
|
|
$data['isactive'] = ($isactive == 'on') ? 1 : 0; }
|
|
$data['updated_by'] = $session_uid;
|
|
if ($BusinessModel->update($business_id, $data)) {
|
|
$business_affectedRows = $BusinessModel->db->affectedRows();
|
|
session()->setFlashdata('success', 'business successfully updated.');
|
|
$this->logger->info("Business: has been updated successfully. Updated Business ID = " . $business_id . " Aff ".$business_affectedRows);
|
|
} else {
|
|
session()->setFlashdata('error', 'business updation failed. Please try again.');
|
|
$this->logger->error("Business: Err Failed to update ID =" . $business_id);
|
|
}
|
|
|
|
}
|
|
|
|
return redirect()->route('business_list');
|
|
}
|
|
|
|
## For delete the business details (Which means inactive the details)
|
|
public function delete_business($id)
|
|
{
|
|
helper('session');
|
|
$session_uid = get_logged_user_id();
|
|
try {
|
|
|
|
$BusinessModel = new BusinessModel();
|
|
$existed = $BusinessModel->find($id);
|
|
$this->logger->Info("Business : Going to Inactive ID = " . $id);
|
|
|
|
if ($existed) {
|
|
$data['isactive'] = 0;
|
|
$data['updated_by'] = get_logged_user_id();
|
|
|
|
if ($BusinessModel->update($id, $data)) {
|
|
session()->setFlashdata('success', 'Deleted successfully.');
|
|
$this->logger->info("Business: has been Inactived successfully. Inactived ID = " . $id);
|
|
} else {
|
|
$this->logger->error("Business: Not able to Inactive ID =" . $id);
|
|
throw new \Exception("Data Not able to Deleted");
|
|
}
|
|
} else {
|
|
$this->logger->error("Business: Does Not Exist To Inactive, ID = " . $id);
|
|
throw new \Exception("Business Already Deleted");
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Business: Err Occur = " . $e->getMessage());
|
|
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
|
|
}
|
|
return redirect()->route('business_list');
|
|
}
|
|
}
|