the_mechanic/app/Controllers/Model.php

170 lines
5.8 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Models\BikemodelsModel;
use App\Models\BikemakeModel;
use App\Models\ComplaintModel;
use CodeIgniter\API\ResponseTrait;
class Model extends BaseController
{
public $session;
use ResponseTrait;
public $BikemodelsModel;
public $BikemakeModel;
public $ComplaintModel;
public function __construct()
{
$this->session = session();
$this->BikemodelsModel = new BikemodelsModel();
$this->BikemakeModel = new BikemakeModel();
$this->ComplaintModel = new ComplaintModel();
}
public function index($make_id)
{
$make_value = $this->BikemakeModel->where('make_id',$make_id)->select('make_id,make')->find();
$make_name= $make_value[0]['make'];
$make_id= $make_value[0]['make_id'];
$model = $this->BikemodelsModel->select('model.*, cubic_capacity.value')
->join('cubic_capacity', 'cubic_capacity.id = model.cubic_capacity_id', 'left')
->where('make_id',$make_id)->orderBy('model_name')
->findAll();
$data['model']=$model;
$data['make_name'] =$make_name;
$data['make_id'] =$make_id;
$cubiccapacity = $this->ComplaintModel->getCubicCapacityName();
$data['cubiccapacity']=$cubiccapacity;
// dd($model);
return view('bike_model_list',$data);
}
public function delete_model($model_id){
$BikemodelsModel = new BikemodelsModel();
$model = $BikemodelsModel->where('model_id',$model_id)->first();
if ($model) {
$delete_done = $BikemodelsModel->where('model_id',$model_id)->delete();
if($delete_done){
return json_encode("delete_done");
}
}else{
}
}
public function create_model($model_id = 0)
{
try {
$make_id = $this->request->getPost('make_id');
$model_name = $this->request->getPost('modelname');
$cubic_capacity = $this->request->getPost('cubiccapacity');
$BikemodelsModel = new BikemodelsModel();
//they check model name already exist or not here.
if ($make_id>0) {
$make = $BikemodelsModel->where(['model_name' => $model_name, 'isactive' => 1])
->where('make_id !=', $make_id)
->findAll();
} else {
$make = $BikemodelsModel->where(['model_name' => $model_name, 'isactive' => 1])->findAll();
}
if (!empty($make)) {
$result = "Name Already Exist";
return $this->respond(['status' => 'failed','code' => 404,'data' => $result], 200);
}
$data = [
'make_id' => $make_id,
'model_name' => $model_name,
'cubic_capacity_id'=>$cubic_capacity
];
// print_r($model_id);die;
//here model id they get from Param.
if (!$model_id) {
try {
$data['business_id'] = $this->session->get('logged_user_business_id');
$data['created_by'] = (int)$this->session->get('logged_user');
$inserted = $BikemodelsModel->insert($data);
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
return $this->respond(['status' => 'failed','code' => 404,'data' => $e->getMessage()],200);
}
if ($inserted) {
$result = $data;
$model_list = $BikemodelsModel->where('make_id',$make_id)->orderBy('make_id','asc')->findAll();
return $this->respond(['status' => 'success','code' => 200,'data' => $result,'model_list'=>$model_list],200);
} else {
$result = "No Match's";
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
}
}
else{
$data['updated_by'] = (int)$this->session->get('logged_user');
$updated = $BikemodelsModel->update($model_id ,$data);
if ($updated) {
$result = $data;
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
} else {
$result = "No Match's";
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
}
}
} catch (\Exception $exception) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
public function status_model()
{
try {
$model_id = $this->request->getPost('model_id');
$isactive = $this->request->getPost('isactive');
$BikemodelsModel = new BikemodelsModel();
$data = [
'isactive' => $isactive
];
$data['updated_by'] = (int)$this->session->get('logged_user');
$updated = $BikemodelsModel->update($model_id, $data);
$model = $BikemodelsModel->where('model_id',$model_id)->first();
$models = $BikemodelsModel->where('make_id', $model['make_id'])->orderBy('model_name', 'asc')->findAll();
if ($updated) {
// $result = $data;
return $this->respond(['status' => 'success','code' => 200,'data' => $models],200);
} else {
$result = "No Match's";
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],404);
}
} catch (\Exception $exception) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $exception],500);
}
}
}