Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
DZywolf committed Nov 21, 2023
1 parent b5920d4 commit 097b991
Show file tree
Hide file tree
Showing 25 changed files with 573 additions and 325 deletions.
54 changes: 36 additions & 18 deletions controllers/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,11 @@ public function add_product()
$this->form_validation->set_rules('realm', lang('realm'), 'trim|required|is_natural_no_zero');
$this->form_validation->set_rules('description', lang('description'), 'trim');
$this->form_validation->set_rules('file', lang('file'), 'callback__file_required');
$this->form_validation->set_rules('currency', lang('currency'), 'trim|required|in_list[dp,vp,both]');
$this->form_validation->set_rules('currency', lang('currency'), 'trim|required|in_list[dp,vp,both,choice]');
$this->form_validation->set_rules('dp', lang('dp'), 'trim|is_natural');
$this->form_validation->set_rules('vp', lang('vp'), 'trim|is_natural');
$this->form_validation->set_rules('visible', lang('visible'), 'trim');
$this->form_validation->set_rules('highlight', lang('highlight'), 'trim');

if ($this->input->method() === 'post' && $this->form_validation->run()) {
$directory = current_date('Y') . '/' . current_date('m') . '/';
Expand Down Expand Up @@ -348,9 +349,10 @@ public function add_product()
'description' => $this->input->post('description', true),
'image' => $directory . $uploadData['file_name'],
'currency' => $currency,
'dp' => in_array($currency, [CURRENCY_DP, CURRENCY_BOTH], true) ? $this->input->post('dp') : 0,
'vp' => in_array($currency, [CURRENCY_VP, CURRENCY_BOTH], true) ? $this->input->post('vp') : 0,
'visible' => empty($this->input->post('visible', true)) ? 0 : 1
'dp' => in_array($currency, [Store_product_model::CURRENCY_DP, Store_product_model::CURRENCY_BOTH, Store_product_model::CURRENCY_CHOICE], true) ? $this->input->post('dp') : 0,
'vp' => in_array($currency, [Store_product_model::CURRENCY_VP, Store_product_model::CURRENCY_BOTH, Store_product_model::CURRENCY_CHOICE], true) ? $this->input->post('vp') : 0,
'visible' => empty($this->input->post('visible', true)) ? 0 : 1,
'highlight' => empty($this->input->post('highlight', true)) ? 0 : 1
]);

$productId = $this->db->insert_id();
Expand Down Expand Up @@ -396,10 +398,11 @@ public function edit_product($productId = null)
$this->form_validation->set_rules('category', lang('category'), 'trim|required|is_natural_no_zero');
$this->form_validation->set_rules('realm', lang('realm'), 'trim|required|is_natural_no_zero');
$this->form_validation->set_rules('description', lang('description'), 'trim');
$this->form_validation->set_rules('currency', lang('currency'), 'trim|required|in_list[dp,vp,both]');
$this->form_validation->set_rules('currency', lang('currency'), 'trim|required|in_list[dp,vp,both,choice]');
$this->form_validation->set_rules('dp', lang('dp'), 'trim|is_natural');
$this->form_validation->set_rules('vp', lang('vp'), 'trim|is_natural');
$this->form_validation->set_rules('visible', lang('visible'), 'trim');
$this->form_validation->set_rules('highlight', lang('highlight'), 'trim');

if ($this->input->method() === 'post' && $this->form_validation->run()) {
if (isset($_FILES['file']['name']) && $_FILES['file']['name'] !== '') {
Expand Down Expand Up @@ -439,9 +442,10 @@ public function edit_product($productId = null)
'name' => $this->input->post('name'),
'description' => $this->input->post('description', true),
'currency' => $currency,
'dp' => in_array($currency, [CURRENCY_DP, CURRENCY_BOTH], true) ? $this->input->post('dp') : 0,
'vp' => in_array($currency, [CURRENCY_VP, CURRENCY_BOTH], true) ? $this->input->post('vp') : 0,
'visible' => empty($this->input->post('visible', true)) ? 0 : 1
'dp' => in_array($currency, [Store_product_model::CURRENCY_DP, Store_product_model::CURRENCY_BOTH, Store_product_model::CURRENCY_CHOICE], true) ? $this->input->post('dp') : 0,
'vp' => in_array($currency, [Store_product_model::CURRENCY_VP, Store_product_model::CURRENCY_BOTH, Store_product_model::CURRENCY_CHOICE], true) ? $this->input->post('vp') : 0,
'visible' => empty($this->input->post('visible', true)) ? 0 : 1,
'highlight' => empty($this->input->post('highlight', true)) ? 0 : 1
], ['id' => $productId]);

$this->log_model->create('store product', 'edit', 'Edited a product', [
Expand Down Expand Up @@ -537,7 +541,7 @@ public function add_command($productId = null)

$this->form_validation->set_rules('type', lang('type'), 'trim|required|in_list[item,custom]');

if ($inputs['type'] === 'item') {
if ($inputs['type'] === Store_command_model::TYPE_ITEM) {
$this->form_validation->set_rules('item', lang('item'), 'trim|required|is_natural_no_zero');
$this->form_validation->set_rules('quantity', lang('quantity'), 'trim|required|is_natural_no_zero');
} else {
Expand All @@ -550,9 +554,9 @@ public function add_command($productId = null)
$this->store_command_model->insert([
'product_id' => $productId,
'type' => $inputs['type'],
'item' => $inputs['type'] === 'item' ? $inputs['item'] : 0,
'quantity' => $inputs['type'] === 'item' ? $inputs['quantity'] : 0,
'command' => $inputs['type'] === 'custom' ? $inputs['command'] : ''
'item' => $inputs['type'] === Store_command_model::TYPE_ITEM ? $inputs['item'] : 0,
'quantity' => $inputs['type'] === Store_command_model::TYPE_ITEM ? $inputs['quantity'] : 0,
'command' => $inputs['type'] === Store_command_model::TYPE_CUSTOM ? $inputs['command'] : ''
]);

$commandId = $this->db->insert_id();
Expand Down Expand Up @@ -593,7 +597,7 @@ public function edit_command($productId = null, $commandId = null)

$this->form_validation->set_rules('type', lang('type'), 'trim|required|in_list[item,custom]');

if ($inputs['type'] === 'item') {
if ($inputs['type'] === Store_command_model::TYPE_ITEM) {
$this->form_validation->set_rules('item', lang('item'), 'trim|required|is_natural_no_zero');
$this->form_validation->set_rules('quantity', lang('quantity'), 'trim|required|is_natural_no_zero');
} else {
Expand All @@ -605,9 +609,9 @@ public function edit_command($productId = null, $commandId = null)
if ($this->form_validation->run()) {
$this->store_command_model->update([
'type' => $inputs['type'],
'item' => $inputs['type'] === 'item' ? $inputs['item'] : 0,
'quantity' => $inputs['type'] === 'item' ? $inputs['quantity'] : 0,
'command' => $inputs['type'] === 'custom' ? $inputs['command'] : ''
'item' => $inputs['type'] === Store_command_model::TYPE_ITEM ? $inputs['item'] : 0,
'quantity' => $inputs['type'] === Store_command_model::TYPE_ITEM ? $inputs['quantity'] : 0,
'command' => $inputs['type'] === Store_command_model::TYPE_CUSTOM ? $inputs['command'] : ''
], ['id' => $commandId]);

$this->session->set_flashdata('success', lang('alert_product_updated'));
Expand Down Expand Up @@ -681,9 +685,23 @@ public function view_order($id = null)
show_404();
}

$inputPage = $this->input->get('page');
$page = ctype_digit((string) $inputPage) ? (int) $inputPage : 0;

$perPage = 50;
$offset = $page > 1 ? ($page - 1) * $perPage : $page; // Calculate offset for paginate
$filters = ['order' => $id];

$this->pagination->initialize([
'base_url' => site_url('store/admin/orders/view/' . $id),
'total_rows' => $this->store_order_product_model->total_paginate($filters),
'per_page' => $perPage
]);

$data = [
'order' => $order,
'products' => $this->store_order_product_model->find_all(['order_id' => $id])
'order' => $order,
'products' => $this->store_order_product_model->paginate($perPage, $offset, $filters),
'pagination' => $this->pagination->create_links()
];

$this->template->title(lang('admin_panel'), config_item('app_name'));
Expand Down
99 changes: 64 additions & 35 deletions controllers/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function __construct()
public function index()
{
$data = [
'categories' => $this->store_category_model->categories()
'categories' => $this->store_category_model->categories(),
'products' => $this->store_product_model->highlight_products()
];

$this->template->title(lang('store'), config_item('app_name'));
Expand Down Expand Up @@ -91,7 +92,7 @@ public function product($productId = null)
{
$product = $this->store_product_model->find([
'id' => $productId,
'visible' => 1
'visible' => true
]);

if (empty($product)) {
Expand All @@ -100,38 +101,66 @@ public function product($productId = null)

$userId = $this->session->userdata('id');
$data = [
'categories' => $this->store_category_model->categories(),
'characters' => $this->server_characters_model->all_characters($product->realm_id, $userId),
'product' => $product,
'category' => $this->store_category_model->find(['id' => $product->category_id])
'category' => $this->store_category_model->find(['id' => $product->category_id]),
'categories' => $this->store_category_model->categories(),
'characters' => $this->server_characters_model->all_characters($product->realm_id, $userId)
];

$this->template->title(lang('store'), config_item('app_name'));

if ($product->currency === Store_product_model::CURRENCY_CHOICE) {
$this->form_validation->set_rules('price', lang('price'), 'trim|required|in_list[dp,vp]');
}

$this->form_validation->set_rules('guid', lang('character'), 'trim|required|is_natural_no_zero');
$this->form_validation->set_rules('qty', lang('quantity'), 'trim|required|is_natural_no_zero');

if ($this->input->method() === 'post' && $this->form_validation->run()) {
$guid = $this->input->post('guid');
$qty = $this->input->post('qty');

if (! $this->server_characters_model->character_linked($product->realm_id, $guid, $userId)) {
$this->session->set_flashdata('error', lang('alert_character_not_related'));
redirect(site_url('store/product/' . $productId));
}

$this->cart->insert([
$content = [
'id' => $productId,
'name' => $product->name,
'qty' => $qty,
'dp' => in_array($product->currency, [CURRENCY_DP, CURRENCY_BOTH], true) ? (int) $product->dp : 0,
'vp' => in_array($product->currency, [CURRENCY_VP, CURRENCY_BOTH], true) ? (int) $product->vp : 0,
'qty' => $this->input->post('qty'),
'dp' => 0,
'vp' => 0,
'options' => [
'guid' => (int) $guid,
'realm' => (int) $product->realm_id,
'currency' => $product->currency
'currency' => ''
]
]);
];

if ($product->currency === Store_product_model::CURRENCY_DP) {
$content['dp'] = (int) $product->dp;
$content['options']['currency'] = Store_product_model::CURRENCY_DP;
}

if ($product->currency === Store_product_model::CURRENCY_VP) {
$content['vp'] = (int) $product->vp;
$content['options']['currency'] = Store_product_model::CURRENCY_VP;
}

if ($product->currency === Store_product_model::CURRENCY_BOTH) {
$content['dp'] = (int) $product->dp;
$content['vp'] = (int) $product->vp;
$content['options']['currency'] = Store_product_model::CURRENCY_BOTH;
}

if ($product->currency === Store_product_model::CURRENCY_CHOICE) {
$price = $this->input->post('price');

$content[$price] = (int) $product->$price;
$content['options']['currency'] = $price;
}

$this->cart->insert($content);

$this->session->set_flashdata('success', lang('alert_cart_product_added'));
redirect(site_url('store/product/' . $productId));
Expand Down Expand Up @@ -193,8 +222,8 @@ public function update_quantity()

public function checkout()
{
$totalDp = $this->cart->total_dp();
$totalVp = $this->cart->total_vp();
$totalDP = $this->cart->total_dp();
$totalVP = $this->cart->total_vp();
$totalItems = $this->cart->total_items();
$user = user();

Expand All @@ -203,27 +232,27 @@ public function checkout()
redirect(site_url('store/cart'));
}

if ((int) $user->dp < $totalDp) {
if ((int) $user->dp < $totalDP) {
$this->session->set_flashdata('error', lang('alert_user_not_dp'));
redirect(site_url('store/cart'));
}

if ((int) $user->vp < $totalVp) {
if ((int) $user->vp < $totalVP) {
$this->session->set_flashdata('error', lang('alert_user_not_vp'));
redirect(site_url('store/cart'));
}

$this->user_model->set([
'dp' => 'dp-' . $totalDp,
'vp' => 'vp-' . $totalVp
'dp' => 'dp-' . $totalDP,
'vp' => 'vp-' . $totalVP
], ['id' => $user->id], false);

$this->store_order_model->insert([
'user_id' => $user->id,
'products_sold' => $totalItems,
'total_dp' => $totalDp,
'total_vp' => $totalVp,
'ip' => $this->input->ip_address()
'user_id' => $user->id,
'total_products' => $totalItems,
'total_dp' => $totalDP,
'total_vp' => $totalVP,
'ip' => $this->input->ip_address()
]);

$orderId = $this->db->insert_id();
Expand Down Expand Up @@ -338,31 +367,31 @@ public function orders()
*/
public function view_order($id = null)
{
$order = $this->store_order_model->find([
'id' => $id,
'user_id' => $this->session->userdata('id')
]);

if (empty($order)) {
show_404();
}

$inputPage = $this->input->get('page');
$page = ctype_digit((string) $inputPage) ? (int) $inputPage : 0;

$perPage = 25;
$offset = $page > 1 ? ($page - 1) * $perPage : $page; // Calculate offset for paginate
$filters = ['order_id' => $id];
$filters = ['order' => $id];

$this->pagination->initialize([
'base_url' => site_url('store/orders/view/' . $id),
'total_rows' => $this->store_order_product_model->total_paginate($filters),
'per_page' => $perPage
]);

$order = $this->store_order_model->find([
'id' => $id,
'user_id' => $this->session->userdata('id')
]);

if (empty($order)) {
show_404();
}

$data = [
'order' => $order,
'products' => $this->store_order_product_model->paginate($perPage, $offset, $filters),
'order' => $order,
'products' => $this->store_order_product_model->paginate($perPage, $offset, $filters),
'pagination' => $this->pagination->create_links()
];

Expand Down
9 changes: 7 additions & 2 deletions language/english/store_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
$lang['order_details'] = 'Order details';
$lang['commands'] = 'Commands';
$lang['custom'] = 'Custom';
$lang['products_sold'] = 'Products sold';
$lang['highlight'] = 'Highlight';
$lang['purchased_products'] = 'Purchased products';
$lang['dp_and_vp'] = 'DP & VP';
$lang['dp_or_vp'] = 'DP or VP';

$lang['top_products'] = 'Top products';
$lang['featured_products'] = 'Featured products';
$lang['view_product'] = 'View product';
$lang['add_cart'] = 'Add to cart';
$lang['back_store'] = 'Back to store';
$lang['proceed_checkout'] = 'Proceed to checkout';

$lang['latest_purchases'] = 'Latest purchases';
$lang['categories_added'] = 'Categories added';
Expand Down
11 changes: 8 additions & 3 deletions language/spanish/store_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
$lang['order_details'] = 'Detalles de la orden';
$lang['commands'] = 'Comandos';
$lang['custom'] = 'Personalizado';
$lang['products_sold'] = 'Productos vendidos';
$lang['highlight'] = 'Destacar';
$lang['purchased_products'] = 'Productos comprados';
$lang['dp_and_vp'] = 'PD y PV';
$lang['dp_or_vp'] = 'PD o PV';

$lang['top_products'] = 'Productos destacados';
$lang['featured_products'] = 'Productos destacados';
$lang['view_product'] = 'Ver producto';
$lang['add_cart'] = 'Añadir al carro';
$lang['add_cart'] = 'Añadir al carrito';
$lang['back_store'] = 'Volver a la tienda';
$lang['proceed_checkout'] = 'Proceder al pago';

$lang['latest_purchases'] = 'Últimas compras';
$lang['categories_added'] = 'Categorías agregadas';
Expand Down
8 changes: 7 additions & 1 deletion migrations/20220920120100_create_store_products.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function up()
'null' => TRUE
],
'currency' => [
'type' => 'ENUM("vp","dp","both")'
'type' => 'ENUM("vp","dp","both","choice")'
],
'dp' => [
'type' => 'INT',
Expand All @@ -54,6 +54,12 @@ public function up()
'constraint' => 1,
'unsigned' => TRUE,
'default' => 1
],
'highlight' => [
'type' => 'TINYINT',
'constraint' => 1,
'unsigned' => TRUE,
'default' => 0
]
]);
$this->dbforge->add_key('id', true);
Expand Down
2 changes: 1 addition & 1 deletion migrations/20220920120300_create_store_orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up()
'constraint' => 20,
'unsigned' => TRUE
],
'products_sold' => [
'total_products' => [
'type' => 'INT',
'constraint' => 10,
'unsigned' => TRUE,
Expand Down
8 changes: 8 additions & 0 deletions models/Store_command_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ class Store_command_model extends BS_Model
{
protected $table = 'store_commands';

/**
* Command type
*
* @var string
*/
public const TYPE_ITEM = 'item';
public const TYPE_CUSTOM = 'custom';

/**
* Class constructor
*
Expand Down
Loading

0 comments on commit 097b991

Please sign in to comment.