249 lines
8.1 KiB
PHP
249 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Backend;
|
|
|
|
use App\Models\Options;
|
|
use App\Models\OptionsGroups;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
|
|
helper('image');
|
|
class OptionController extends ResourceController
|
|
{
|
|
|
|
private $options;
|
|
private $option_groups;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->options = new Options();
|
|
$this->option_groups = new OptionsGroups();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
// echo(123);exit;
|
|
$groups = $this->option_groups->findAll();
|
|
$data = [];
|
|
|
|
foreach ($groups as $group) {
|
|
$group['options'] = $this->options
|
|
->where('option_group_id', $group['id'])
|
|
->findAll();
|
|
$data[] = $group;
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'data' => $data]);
|
|
}
|
|
|
|
|
|
public function show($id = null)
|
|
{
|
|
$group = $this->option_groups->find($id);
|
|
|
|
if (!$group) {
|
|
return $this->failNotFound("Option group not found.");
|
|
}
|
|
|
|
$group['options'] = $this->options
|
|
->where('option_group_id', $group['id'])
|
|
->findAll();
|
|
|
|
return $this->respond(['status' => 200, 'data' => $group]);
|
|
}
|
|
|
|
|
|
public function create()
|
|
{
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
'min_quantity' => 'permit_empty|integer',
|
|
'max_quantity' => 'permit_empty|integer',
|
|
'is_required' => 'required',
|
|
'status' => 'required',
|
|
'order_index' => 'permit_empty|integer',
|
|
'options' => 'permit_empty',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$groupData = [
|
|
'title' => $this->request->getVar('title'),
|
|
'min_quantity' => $this->request->getVar('min_quantity') ?? 0,
|
|
'max_quantity' => $this->request->getVar('max_quantity') ?? 0,
|
|
'is_required' => $this->request->getVar('is_required'),
|
|
'status' => $this->request->getVar('status'),
|
|
'order_index' => $this->request->getVar('order_index') ?? 0,
|
|
];
|
|
|
|
$groupId = $this->option_groups->insert($groupData);
|
|
|
|
if (!$groupId) {
|
|
return $this->fail("Failed to create group.", 400);
|
|
}
|
|
// echo '<pre>';
|
|
// print_r($this->request->getPost());
|
|
// exit;
|
|
$options = $this->request->getJSON(true)['options'] ?? $this->request->getVar('options') ?? [];
|
|
if (!is_array($options)) {
|
|
return $this->failValidationErrors('Options not formatted correctly.');
|
|
}
|
|
$savedOptions = [];
|
|
foreach ($options as $i => $opt) {
|
|
$imageUrl = null;
|
|
$compressedUrl = null;
|
|
|
|
$imageFile = $this->request->getFile("image$i");
|
|
|
|
if ($imageFile && $imageFile->isValid() && !$imageFile->hasMoved()) {
|
|
$result = save_image_with_compression(
|
|
$imageFile,
|
|
FCPATH . 'backend/uploads/options',
|
|
FCPATH . 'backend/uploads/options_compressed',
|
|
900,
|
|
80
|
|
);
|
|
|
|
$imageUrl = base_url('backend/uploads/options' . basename($result['original']));
|
|
$compressedUrl = base_url('backend/uploads/options_compressed' . basename($result['compressed']));
|
|
}
|
|
$optionData = [
|
|
'option_group_id' => $groupId,
|
|
'title' => isset($opt['title']) ? $opt['title'] : '',
|
|
'price_adjustment' => isset($opt['price_adjustment']) ? $opt['price_adjustment'] : 0,
|
|
'order_index' => isset($opt['order_index']) ? $opt['order_index'] : $i,
|
|
'images' => $imageUrl,
|
|
'images_compressed' => $compressedUrl,
|
|
];
|
|
|
|
$this->options->insert($optionData);
|
|
$savedOptions[] = $optionData;
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Group with options created.',
|
|
'data' => [
|
|
'group_id' => $groupId,
|
|
'group' => $groupData,
|
|
'options' => $savedOptions
|
|
]
|
|
]);
|
|
}
|
|
|
|
|
|
public function update($id = null)
|
|
{
|
|
$group = $this->option_groups->find($id);
|
|
|
|
if (!$group) {
|
|
return $this->fail("Group not found.", 400);
|
|
}
|
|
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
'min_quantity' => 'permit_empty|integer',
|
|
'max_quantity' => 'permit_empty|integer',
|
|
'is_required' => 'required|in_list[0,1,true,false]',
|
|
'status' => 'required',
|
|
'order_index' => 'permit_empty|integer',
|
|
'options' => 'permit_empty'
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$updateData = [
|
|
'title' => $this->request->getVar('title'),
|
|
'min_quantity' => $this->request->getVar('min_quantity') ?? 0,
|
|
'max_quantity' => $this->request->getVar('max_quantity') ?? 0,
|
|
'is_required' => $this->request->getVar('is_required'),
|
|
'status' => $this->request->getVar('status'),
|
|
'order_index' => $this->request->getVar('order_index') ?? 0,
|
|
];
|
|
|
|
$this->option_groups->update($id, $updateData);
|
|
|
|
$options = $this->request->getVar('options') ?? [];
|
|
|
|
$exist_option = $this->options->where('option_group_id', $id)->findAll();
|
|
$exist_option_group = array_column($exist_option, 'id');
|
|
|
|
$update_option_group = [];
|
|
|
|
foreach ($options as $i => $opt) {
|
|
|
|
$title = is_array($opt) ? $opt['title'] ?? '' : $opt->title ?? '';
|
|
$price = is_array($opt) ? $opt['price_adjustment'] ?? 0 : $opt->price_adjustment ?? 0;
|
|
$order = is_array($opt) ? $opt['order_index'] ?? 0 : $opt->order_index ?? 0;
|
|
$optId = is_array($opt) ? $opt['id'] ?? null : $opt->id ?? null;
|
|
|
|
$optionData = [
|
|
'option_group_id' => $id,
|
|
'title' => $title,
|
|
'price_adjustment' => $price,
|
|
'order_index' => $order,
|
|
'images' => $opt['images'] ?? null,
|
|
'images_compressed' => $opt['images_compressed'] ?? null,
|
|
];
|
|
|
|
$imageUrl = null;
|
|
$compressedUrl = null;
|
|
|
|
$imageFile = $this->request->getFile("image$i");
|
|
|
|
if ($imageFile && $imageFile->isValid() && !$imageFile->hasMoved()) {
|
|
$result = save_image_with_compression(
|
|
$imageFile,
|
|
FCPATH . 'backend/uploads/options/',
|
|
FCPATH . 'backend/uploads/options_compressed/',
|
|
900,
|
|
80
|
|
);
|
|
|
|
$imageUrl = base_url('backend/uploads/options/' . basename($result['original']));
|
|
$compressedUrl = base_url('backend/uploads/options_compressed/' . basename($result['compressed']));
|
|
}
|
|
|
|
|
|
|
|
if ($imageUrl) {
|
|
$optionData['images'] = $imageUrl;
|
|
$optionData['images_compressed'] = $compressedUrl;
|
|
}
|
|
|
|
if (!isset($opt->id)) {
|
|
$this->options->insert($optionData);
|
|
} else {
|
|
$this->options->update($opt->id, $optionData);
|
|
$update_option_group[] = $opt->id;
|
|
}
|
|
}
|
|
|
|
foreach ($exist_option_group as $option_id) {
|
|
if (!in_array($option_id, $update_option_group)) {
|
|
$this->options->delete($option_id);
|
|
}
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Group updated.']);
|
|
}
|
|
|
|
public function delete($id = null)
|
|
{
|
|
$group = $this->option_groups->find($id);
|
|
|
|
if (!$group) {
|
|
return $this->failNotFound("Group not found.");
|
|
}
|
|
|
|
$this->option_groups->delete($id);
|
|
$this->options->where('option_group_id', $id)->delete();
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Group and options deleted.']);
|
|
}
|
|
}
|