https://t.me/RX1948
Server : Apache/2.4.18 (Ubuntu)
System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64
User : oppastar ( 1041)
PHP Version : 7.0.33-0ubuntu0.16.04.15
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
Directory :  /var/www/laciasmara.com/public_html/shop/application/controllers/admin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/laciasmara.com/public_html/shop/application/controllers/admin/Categories.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Categories extends Admin_Controller
{

	//this property is used for validating existing category title on call back edit category
	protected $category_current_id;

	function __construct()
	{

		parent::__construct();
		$this->load->model('category_m');
		$this->load->model('product_m');
	}
	public function review_aspects($category_id = null)
	{
		// Validasi category_id
		if (empty($category_id) || !is_numeric($category_id)) {
			show_404();
			return;
		}

		// Load model jika belum di-load
		$this->load->model('category_m');
		$this->load->model('review_m');

		// Ambil data kategori untuk validasi dan info tampilan
		$category = $this->category_m->get_category_by_id($category_id);
		if (empty($category)) {
			show_404();
			return;
		}

		// Ambil data review aspects beserta rating options
		$review_aspects = $this->review_m->get_aspects_with_ratings($category_id);


		$data['userdata'] = $this->session->userdata();
		$data['title'] = 'Aspek Penilaian Kategori: ' . $category->category . ' | Laciasmara';
		$data['category'] = $category;
		$data['category_id'] = $category_id;
		$data['review_aspects'] = $review_aspects;

		$this->load->view('admin_new/layouts/header', $data);
		$this->load->view('admin_new/categories/review_aspects', $data);
		$this->load->view('admin_new/layouts/footer');
	}

	public function get_aspects_by_category()
	{
		$category_id = $this->input->post('category_id');

		if (empty($category_id) || !is_numeric($category_id)) {
			$response = array('status' => 'error', 'message' => 'Invalid category ID');
			echo json_encode($response);
			return;
		}

		$this->load->model('review_m');
		$aspects = $this->review_m->get_aspects_with_ratings($category_id);

		$response = array(
			'status' => 'success',
			'data' => $aspects
		);
		echo json_encode($response);
	}


	function manage()
	{
		$data['userdata'] = $this->session->userdata();
		$data['title'] = 'Pengaturan Kategori | Laciasmara';

		$data['total_categories'] = $this->category_m->count_categories();
		$data['active_categories'] = $this->category_m->count_categories(1);
		$data['inactive_categories'] = $this->category_m->count_categories(0);

		$this->load->view('admin_new/layouts/header', $data);
		$this->load->view('admin_new/categories/manage_category');
		$this->load->view('admin_new/layouts/footer');
	}

	function add_category()
	{
		$data['userdata'] = $this->session->userdata();
		$data['title'] = 'Tambah Kategori | Laciasmara';
		$data['parent_categories'] = $this->category_m->fetch_parent_categories();

		$this->load->view('admin_new/layouts/header', $data);
		$this->load->view('admin_new/categories/add_category');
		$this->load->view('admin_new/layouts/footer');
	}

	function edit_category($id_category = NULL)
	{
		if (!$id_category) {
			redirect('admin/products/manage-category');
		}
		$data['userdata'] = $this->session->userdata();
		$data['title'] = 'Ubah Kategori | Laciasmara';
		$data['category'] = $this->category_m->fetch_category_by_id($id_category);
		$data['parent_categories'] = $this->category_m->fetch_parent_categories();

		$this->load->view('admin_new/layouts/header', $data);
		$this->load->view('admin_new/categories/edit_category');
		$this->load->view('admin_new/layouts/footer');
	}

	function category_product($id_category = NULL)
	{

		if (!$id_category) {
			redirect('admin/products/manage-category');
		}
		$data['userdata'] = $this->session->userdata();
		$data['title'] = 'Produk Kategori | Laciasmara';
		$data['category'] = $this->category_m->fetch_category_by_id($id_category);

		$this->load->view('admin_new/layouts/header', $data);
		$this->load->view('admin_new/categories/product_category');
		$this->load->view('admin_new/layouts/footer');
	}

	public function get_category_products()
	{
		$categoryId = $this->input->get('id', true);
		$isEmptyStockOnly = filter_var($this->input->get('isEmptyStockOnly', true), FILTER_VALIDATE_BOOLEAN);
		$isLowStock = filter_var($this->input->get('isLowStock', true), FILTER_VALIDATE_BOOLEAN);
		$isNewProduct = filter_var($this->input->get('isNewProduct', true), FILTER_VALIDATE_BOOLEAN);
		$isDiscounted = filter_var($this->input->get('isDiscounted', true), FILTER_VALIDATE_BOOLEAN);
		$isBestSelling = filter_var($this->input->get('isBestSelling', true), FILTER_VALIDATE_BOOLEAN);
		$sort = $this->input->get('sort', true);

		// Tambahkan parameter filter tanggal
		$dateFilter = $this->input->get('date_filter', true);
		$startDate = $this->input->get('start_date', true);
		$endDate = $this->input->get('end_date', true);

		log_message('debug', 'Date Filter: ' . $dateFilter);


		// Query utama untuk mendapatkan produk
		$this->db->select('
			p.title,
			p.alias,
			p.product_status,
			p.id_products,
			p.created_at,
			p.updated_at,
			pd.id as product_detail_id,
			MIN(pd.price) as min_price,
			MAX(pd.price) as max_price,
			CASE 
				WHEN COUNT(pd.id) > 1 THEN "-"
				ELSE MAX(pd.sku)
			END as sku,
			pi.image as image,
			s.stock as total_stock,
			b.brand as brand_title,
			COALESCE(od_count.total_sold, 0) as total_sold
		');
		$this->db->from('products p');
		$this->db->join('product_details pd', 'pd.product_id = p.id_products', 'left');
		$this->db->join('stock s', 'pd.id = s.id_product_detail AND s.warehouse_id = 1', 'left');
		$this->db->join('brands b', 'p.brand_id = b.id_brands', 'left');
		$this->db->join('product_images pi', 'pi.product_details_id = pd.id AND pi.priority = 1 AND pi.status = 1', 'left');
		$this->db->join('category_product cp', 'cp.id_product = p.id_products', 'left');

		// Subquery untuk mendapatkan total_sold dengan filter payment_status = 5
		// dan filter tanggal sesuai parameter
		$od_subquery = "SELECT od.product_id, COUNT(*) as total_sold 
                   FROM orders_detail od
                   JOIN orders o ON od.orders_id = o.id_orders
                   WHERE o.payment_status = 5";

		// Tambahkan filter tanggal ke subquery
		if ($dateFilter) {
			switch ($dateFilter) {
				case 'today':
					$od_subquery .= " AND DATE(o.order_date) = CURDATE()";
					break;
				case 'yesterday':
					$od_subquery .= " AND DATE(o.order_date) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)";
					break;
				case 'last7days':
					$od_subquery .= " AND o.order_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)";
					break;
				case 'last30days':
					$od_subquery .= " AND o.order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)";
					break;
				case 'thisMonth':
					$od_subquery .= " AND YEAR(o.order_date) = YEAR(CURDATE()) AND MONTH(o.order_date) = MONTH(CURDATE())";
					break;
				case 'thisYear':
					$od_subquery .= " AND YEAR(o.order_date) = YEAR(CURDATE())";
					break;
				case 'custom':
					if ($startDate && $endDate) {
						$od_subquery .= " AND DATE(o.order_date) BETWEEN '$startDate' AND '$endDate'";
					}
					break;
			}
		}

		$od_subquery .= " GROUP BY od.product_id";

		$this->db->join("($od_subquery) as od_count", 'p.id_products = od_count.product_id', 'left');

		$this->db->where('cp.id_category', $categoryId);

		// Filter sebelumnya tetap dipertahankan
		if ($isEmptyStockOnly) {
			$this->db->having('total_stock = 0');
		}
		if ($isLowStock) {
			$this->db->having('total_stock > 0 AND total_stock < 5');
		}
		if ($isNewProduct) {
			$this->db->where("p.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)");
		}
		if ($isDiscounted) {
			$this->db->where("p.id_products IN (
            SELECT pd.product_id FROM product_details pd
            WHERE pd.discounted_price > 0
        )");
		}
		if ($isBestSelling) {
			// Perlu memodifikasi filter best selling juga untuk menyesuaikan dengan tanggal
			$bestselling_subquery = "SELECT od.product_id 
                               FROM orders_detail od
                               JOIN orders o ON od.orders_id = o.id_orders
                               WHERE o.payment_status = 5";

			// Tambahkan filter tanggal ke subquery best selling jika diperlukan
			if ($dateFilter) {
				switch ($dateFilter) {
					case 'today':
						$bestselling_subquery .= " AND DATE(o.order_date) = CURDATE()";
						break;
					case 'yesterday':
						$bestselling_subquery .= " AND DATE(o.order_date) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)";
						break;
					case 'last7days':
						$bestselling_subquery .= " AND o.order_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)";
						break;
					case 'last30days':
						$bestselling_subquery .= " AND o.order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)";
						break;
					case 'thisMonth':
						$bestselling_subquery .= " AND YEAR(o.order_date) = YEAR(CURDATE()) AND MONTH(o.order_date) = MONTH(CURDATE())";
						break;
					case 'thisYear':
						$bestselling_subquery .= " AND YEAR(o.order_date) = YEAR(CURDATE())";
						break;
					case 'custom':
						if ($startDate && $endDate) {
							$bestselling_subquery .= " AND DATE(o.order_date) BETWEEN '$startDate' AND '$endDate'";
						}
						break;
				}
			}

			$bestselling_subquery .= " GROUP BY od.product_id ORDER BY COUNT(*) DESC LIMIT 10";

			$this->db->join("($bestselling_subquery) AS best_selling", "p.id_products = best_selling.product_id");
		}

		// Sort
		switch ($sort) {
			case 'terlaris':
				$this->db->order_by('total_sold', 'DESC');
				break;
			case 'kurang-diminati':
				$this->db->order_by('total_sold', 'ASC');
				break;
			case 'harga-tertinggi':
				$this->db->order_by('max_price', 'DESC');
				break;
			case 'harga-terendah':
				$this->db->order_by('min_price', 'ASC');
				break;
			case 'nama-az':
				$this->db->order_by('p.title', 'ASC');
				break;
			case 'nama-za':
				$this->db->order_by('p.title', 'DESC');
				break;
			case 'stok-terbanyak':
				$this->db->order_by('total_stock', 'DESC');
				break;
			case 'stok-tersedikit':
				$this->db->order_by('total_stock', 'ASC');
				break;
		}

		$this->db->where('p.deleted_at', null);
		$this->db->group_by('p.id_products');
		$query = $this->db->get();
		$all_products = $query->result();

		// Post-processing (tidak ada perubahan)
		foreach ($all_products as $product) {
			// Process price range
			if (!empty($product->min_price) && !empty($product->max_price)) {
				$product->price = ($product->min_price == $product->max_price) ? $product->min_price : $product->min_price . ' - ' . $product->max_price;
			} else {
				$product->price = 'N/A';
			}
			unset($product->min_price, $product->max_price);

			// Ambil detail produk dengan variannya
			$this->db->select('
				pd.id as product_detail_id,
				pd.sku,
				pd.price,
				COALESCE(s.stock, 0) as stock,
				pd.discounted_price,
				GROUP_CONCAT(
					CONCAT_WS(": ", pa.product_attribute, pad.attribute_detail)
					SEPARATOR ", "
				) as variants,
				pa.product_attribute as attribute,
				pad.attribute_detail as attribute_detail
			');
			$this->db->from('product_details pd');
			$this->db->join('product_combination pc', 'pc.product_details_id = pd.id', 'left');
			$this->db->join('product_attributes pa', 'pc.attribute_id = pa.id', 'left');
			$this->db->join('product_attributes_detail pad', 'pc.attribute_detail_id = pad.id', 'left');
			$this->db->join('stock s', 'pd.id = s.id_product_detail AND s.warehouse_id = 1', 'left');
			$this->db->where('pd.product_id', $product->id_products);
			$this->db->group_by('pd.id');
			$this->db->order_by('pd.id', 'ASC');
			$variants_query = $this->db->get();
			$product->variants = $variants_query->result();
		}

		echo json_encode($all_products);
	}

	// Fetch category data
	public function get_categories()
	{
		$tab = $this->input->get('tab', true);
		$sort = $this->input->get('sort', true);

		$this->db->select('
			c.*,
			(SELECT COALESCE(SUM(s.stock), 0) 
			FROM stock s 
			JOIN product_details pd ON s.id_product_detail = pd.id
			JOIN products p ON pd.product_id = p.id_products
			JOIN category_product cp ON p.id_products = cp.id_product
			WHERE cp.id_category = c.id_categories) AS total_stock,
			
			(SELECT COALESCE(MIN(pd.price), 0) 
			FROM product_details pd
			JOIN products p ON pd.product_id = p.id_products
			JOIN category_product cp ON p.id_products = cp.id_product
			WHERE cp.id_category = c.id_categories) AS min_price,
			
			(SELECT COALESCE(MAX(pd.price), 0) 
			FROM product_details pd
			JOIN products p ON pd.product_id = p.id_products
			JOIN category_product cp ON p.id_products = cp.id_product
			WHERE cp.id_category = c.id_categories) AS max_price,
			
			(SELECT COALESCE(SUM(od.quantity), 0) 
			FROM orders_detail od
			JOIN product_details pd ON od.item_id = pd.id
			JOIN products p ON pd.product_id = p.id_products
			JOIN category_product cp ON p.id_products = cp.id_product
			WHERE cp.id_category = c.id_categories) AS total_sold,

			(SELECT COALESCE(COUNT(cp.id_product), 0)
			FROM category_product cp
			WHERE cp.id_category = c.id_categories) as total_products
		');
		$this->db->from('categories c');

		if ($tab === 'active') {
			$this->db->where('c.status', '1');
		} elseif ($tab === 'inactive') {
			$this->db->where('c.status', '0');
		}

		// Sorting berdasarkan parameter yang dipilih
		switch ($sort) {
			case 'baru':
				$this->db->order_by('created_at', 'DESC');
				break;
			case 'baru-diubah':
				$this->db->order_by('updated_at', 'DESC');
				break;
			case 'terlaris':
				$this->db->order_by('total_sold', 'DESC');
				break;
			case 'kurang-diminati':
				$this->db->order_by('total_sold', 'ASC');
				break;
			case 'harga-tertinggi':
				$this->db->order_by('max_price', 'DESC');
				break;
			case 'harga-terendah':
				$this->db->order_by('min_price', 'ASC');
				break;
			case 'nama-az':
				$this->db->order_by('c.category', 'ASC');
				break;
			case 'nama-za':
				$this->db->order_by('c.category', 'DESC');
				break;
			case 'stok-terbanyak':
				$this->db->order_by('total_stock', 'DESC');
				break;
			case 'stok-tersedikit':
				$this->db->order_by('total_stock', 'ASC');
				break;
			default:
				$this->db->order_by('c.priority', 'ASC');
				break;
		}

		$this->db->cache_on();
		$query = $this->db->get();
		$this->db->cache_off();

		$all_categories = $query->result();

		echo json_encode($all_categories);
	}

	// Insert new category
	public function store()
	{
		// Set upload configuration
		$config['upload_path'] = './uploads/category/';
		$config['allowed_types'] = 'jpg|jpeg|png|gif';
		$config['max_size'] = 500; // 500KB
		$config['encrypt_name'] = TRUE;

		// Create directory if not exists
		if (!is_dir($config['upload_path'])) {
			mkdir($config['upload_path'], 0777, TRUE);
		}

		// Load upload library
		$this->load->library('upload', $config);

		// Ambil data dari POST
		$data = $this->input->post();

		// Buat alias untuk kategori
		$alias = strtolower(str_replace(' ', '-', preg_replace('/[^a-zA-Z0-9\s]/', '', trim($data['categoryName']))));
		$alias_en = strtolower(str_replace(' ', '-', preg_replace('/[^a-zA-Z0-9\s]/', '', trim($data['enCategoryName']))));

		// Tentukan apakah kategori ini adalah parent atau subkategori
		$parentCategory = isset($data['parentCategory']) && !empty($data['parentCategory']) ? (int)$data['parentCategory'] : 0;

		// Last Priority
		$this->db->select_max('priority');
		$last_priority = $this->db->get('categories')->row()->priority ?? 0;
		$priority = $last_priority + 1;
		// Data yang akan disimpan ke dalam tabel categories
		$category_data = [
			'category' => htmlspecialchars($data['categoryName']),
			'category_en' => htmlspecialchars($data['enCategoryName']),
			'alias' => $alias,
			'alias_en' => $alias_en,
			'status' => isset($data['status']) ? (string)$data['status'] : 'inactive',
			'parent' => $parentCategory,
			'meta_title' => htmlspecialchars($data['seoTitle']),
			'meta_description' => htmlspecialchars($data['seoMetaDescription']),
			'description' => $data['short_desc_id'],
			'description_en' => $data['short_desc_en'],
			'priority' => $priority,
			'created_at' => date('Y-m-d H:i:s'),
			'updated_at' => date('Y-m-d H:i:s'),
			'updated_by' => $this->session->userdata('name') ?? 'System',
		];

		// Begin transaction
		$this->db->trans_begin();
		try {
			// Upload banner image if exists
			if (!empty($_FILES['bannerImage']['name'])) {
				if ($this->upload->do_upload('bannerImage')) {
					$upload_data = $this->upload->data();

					// Validasi dimensi gambar
					list($width, $height) = getimagesize($upload_data['full_path']);
					if ($width > 1500 || $height > 500) {
						// Hapus file yang sudah diupload
						@unlink($upload_data['full_path']);
						throw new Exception('Dimensi gambar terlalu besar. Maksimal 1500 x 500 pixel.');
					}

					$category_data['image'] = $upload_data['file_name'];
				} else {
					throw new Exception($this->upload->display_errors('', ''));
				}
			}

			// Insert kategori
			$this->db->insert('categories', $category_data);

			// Commit transaksi jika sukses
			if ($this->db->trans_status() === FALSE) {
				throw new Exception('Transaction failed');
			}

			$this->db->trans_commit();

			// Set pesan sukses
			$this->session->set_flashdata('message', 'Kategori berhasil ditambahkan');
			$this->session->set_flashdata('message_type', 'success');

			// Redirect ke halaman kategori
			redirect('admin/products/manage-category');
		} catch (Exception $e) {
			// Rollback transaksi jika terjadi kesalahan
			$this->db->trans_rollback();

			// Set pesan error
			$this->session->set_flashdata('message', 'Gagal menambahkan kategori: ' . $e->getMessage());
			$this->session->set_flashdata('message_type', 'error');

			// Redirect ke halaman tambah kategori
			redirect('admin/categories/add-category');
		}
	}

	// Update from edit_category
	public function update($id_category)
	{
		// Set upload configuration
		$config['upload_path'] = './uploads/category/';
		$config['allowed_types'] = 'jpg|jpeg|png|gif';
		$config['max_size'] = 500; // 500KB
		$config['encrypt_name'] = TRUE;

		// Create directory if not exists
		if (!is_dir($config['upload_path'])) {
			mkdir($config['upload_path'], 0777, TRUE);
		}

		// Load upload library
		$this->load->library('upload', $config);

		// Ambil data dari POST
		$data = $this->input->post();

		// Ambil data kategori saat ini dari database
		$current_category = $this->db->get_where('categories', ['id_categories' => $id_category])->row();

		// Buat alias untuk kategori
		$alias = strtolower(str_replace(' ', '-', preg_replace('/[^a-zA-Z0-9\s]/', '', trim($data['categoryName']))));
		$alias_en = strtolower(str_replace(' ', '-', preg_replace('/[^a-zA-Z0-9\s]/', '', trim($data['enCategoryName']))));

		// Tentukan apakah kategori ini adalah parent atau subkategori
		$parentCategory = isset($data['parentCategory']) && !empty($data['parentCategory']) ? (int)$data['parentCategory'] : 0;

		// Data yang akan diperbarui dalam tabel categories
		$category_data = [
			'category'          => htmlspecialchars($data['categoryName']),
			'category_en'       => htmlspecialchars($data['enCategoryName']),
			'alias'            => $alias,
			'alias_en'         => $alias_en,
			'status'           => isset($data['status']) ? (string)$data['status'] : 'inactive',
			'parent'           => $parentCategory,
			'meta_title'       => htmlspecialchars($data['seoTitle']),
			'meta_description' => htmlspecialchars($data['seoMetaDescription']),
			'description'      => $data['short_desc_id'],
			'description_en'   => $data['short_desc_en'],
			'updated_at'       => date('Y-m-d H:i:s'),
			'updated_by'       => $this->session->userdata('name') ?? 'system',
		];

		// Begin transaction
		$this->db->trans_begin();
		try {
			// Cek apakah ada gambar yang diupload
			if (!empty($_FILES['bannerImage']['name'])) {
				if ($this->upload->do_upload('bannerImage')) {
					$upload_data = $this->upload->data();

					// Validasi dimensi gambar
					list($width, $height) = getimagesize($upload_data['full_path']);
					if ($width > 1500 || $height > 500) {
						// Hapus file yang sudah diupload
						@unlink($upload_data['full_path']);
						throw new Exception('Dimensi gambar terlalu besar. Maksimal 1500 x 500 pixel.');
					}

					// Hapus gambar lama jika ada
					if (!empty($current_category->image) && file_exists('./uploads/category/' . $current_category->image)) {
						@unlink('./uploads/category/' . $current_category->image);
					}

					// Simpan gambar baru
					$category_data['image'] = $upload_data['file_name'];
				} else {
					throw new Exception($this->upload->display_errors('', ''));
				}
			} elseif (isset($data['removeImage']) && $data['removeImage'] == '1') {
				// Jika user memilih untuk menghapus gambar tanpa upload baru
				if (!empty($current_category->image) && file_exists('./uploads/category/' . $current_category->image)) {
					@unlink('./uploads/category/' . $current_category->image);
				}
				$category_data['image'] = ''; // Kosongkan field image
			}

			// Update kategori
			$this->db->where('id_categories', $id_category);
			$this->db->update('categories', $category_data);

			// Commit transaksi jika sukses
			if ($this->db->trans_status() === FALSE) {
				throw new Exception('Transaction failed');
			}

			$this->db->trans_commit();

			// Set pesan sukses
			$this->session->set_flashdata('message', 'Kategori berhasil diperbarui');
			$this->session->set_flashdata('message_type', 'success');

			// Redirect ke halaman kategori
			redirect('admin/products/manage-category');
		} catch (Exception $e) {
			// Rollback transaksi jika terjadi kesalahan
			$this->db->trans_rollback();

			// Set pesan error
			$this->session->set_flashdata('message', 'Gagal memperbarui kategori: ' . $e->getMessage());
			$this->session->set_flashdata('message_type', 'error');

			// Redirect ke halaman edit kategori
			redirect('admin/categories/edit-category/' . $id_category);
		}
	}

	public function updateCategory()
	{
		if (!$this->input->is_ajax_request()) {
			show_404();
		}

		$id_categories = $this->input->post('id_categories');
		$field = $this->input->post('field'); // Bisa 'category' atau 'category_en'
		$value = trim($this->input->post('value'));

		// Validasi input
		if (empty($id_categories) || empty($field) || empty($value)) {
			echo json_encode(['success' => false, 'message' => 'Data tidak valid']);
			return;
		}

		// Pastikan hanya field yang diperbolehkan yang bisa diupdate
		if (!in_array($field, ['category', 'category_en'])) {
			echo json_encode(['success' => false, 'message' => 'Field tidak valid']);
			return;
		}

		// Buat alias dari value yang dikirim
		$alias = preg_replace('/[^a-zA-Z0-9\s]/', '', $value); // Hapus karakter spesial
		$alias = preg_replace('/\s+/', ' ', $alias); // Ganti banyak spasi dengan satu spasi
		$alias = str_replace(' ', '-', $alias); // Ganti spasi dengan tanda "-"
		$alias = strtolower($alias); // Ubah ke huruf kecil

		// Tentukan field alias yang akan diperbarui
		$aliasField = ($field === 'category') ? 'alias' : 'alias_en';

		// Update data di database
		$this->db->where('id_categories', $id_categories);
		$update = $this->db->update('categories', [
			$field      => $value,
			$aliasField => $alias // Perbarui alias sesuai dengan category/category_en yang diedit
		]);

		if ($update) {
			echo json_encode([
				'success'   => true,
				'message'   => 'Kategori berhasil diperbarui',
				'csrf_hash' => $this->security->get_csrf_hash() // Perbarui CSRF jika diperlukan
			]);
		} else {
			echo json_encode(['success' => false, 'message' => 'Gagal memperbarui kategori']);
		}
	}

	// Delete Category
	public function delete_category($id)
	{
		// Validasi ID kategori
		if (!$id || !is_numeric($id)) {
			echo json_encode(["success" => false, "message" => "ID kategori tidak valid."]);
			return;
		}

		// Memastikan kategori yang akan dihapus ada
		$this->db->where('id_categories', $id);
		$product = $this->db->get('categories')->row();

		if (!$product) {
			echo json_encode(["success" => false, "message" => "Kategori tidak ditemukan."]);
			return;
		}

		// Menghapus produk
		$this->db->where('id_categories', $id);
		$deleteSuccess = $this->db->delete('categories');

		if ($deleteSuccess) {
			echo json_encode(["success" => true, "message" => "Kategori berhasil dihapus."]);
		} else {
			echo json_encode(["success" => false, "message" => "Gagal menghapus kategori, coba lagi nanti."]);
		}
	}

	// Delete category product
	public function delete_category_product()
	{
		// Ambil data dari request body
		if (!$this->input->is_ajax_request()) {
			show_error("Akses tidak diizinkan", 403, "Forbidden");
		}
		$id_product = $this->input->post("id_product");
		$id_category = $this->input->post("id_category");

		// Validasi input
		if (!$id_product || !$id_category || !is_numeric($id_product) || !is_numeric($id_category)) {
			echo json_encode(["success" => false, "message" => "ID produk atau kategori tidak valid."]);
			return;
		}

		// Hapus berdasarkan id_product & id_category
		$this->db->where('id_product', $id_product);
		$this->db->where('id_category', $id_category);
		$deleteSuccess = $this->db->delete('category_product');

		if ($deleteSuccess) {
			echo json_encode(["success" => true, "message" => "Produk berhasil dihapus dari kategori."]);
		} else {
			echo json_encode(["success" => false, "message" => "Gagal menghapus produk dari kategori, coba lagi nanti."]);
		}
	}


	// Update status from switch button
	public function updateStatus()
	{
		if (!$this->input->is_ajax_request()) {
			show_error('No direct script access allowed', 403);
			return;
		}
		// Ambil data dari POST
		$id_categories = $this->input->post('id_categories');
		$new_status = $this->input->post('new_status');

		// Update harga produk
		$this->db->where('id_categories', $id_categories);
		$update = $this->db->update('categories', ['status' => $new_status]);

		echo json_encode(["success" => $update]);
	}

	//this is to list all categories 
	public function index()
	{

		//Add pagination
		$this->load->helper('pagination_helper');
		add_pagination(base_url() . 'admin/categories/index', $this->category_m->record_count(), 6, 4);

		//get parent categories only
		$this->data['parent_categories'] = $this->category_m->get_all_parent_categories(6, $this->uri->segment(4));

		//load view
		$this->data['subview'] = 'admin/categories/index';
		$this->load->view('admin/templates/header', $this->data_header);
		$this->load->view('admin/_layout_main', $this->data);
		$this->load->view('admin/templates/footer');
	}

	//to add & edit category in admin
	public function edit($id = NULL)
	{

		$this_case = $this->input->post('this_case');

		if (isset($this_case)) {



			if ($this_case == 'addcategory_in_product') {

				$config = $this->category_m->rules;
				$this->load->library('form_validation');
				$this->form_validation->set_rules($config);
				$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

				if ($this->form_validation->run($this) == FALSE) {
					echo json_encode(array(
						'result' => validation_errors(),
					));
				}

				if ($this->form_validation->run($this) == TRUE) {

					$image_filename1 = $this->image_processing($_FILES['banner_image1']);
					$image_filename2 = $this->image_processing($_FILES['banner_image2']);

					$data = $this->table_data_processing($image_filename1, $image_filename2, $this_case);

					$this->category_m->add_category($data);

					$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil dibuat</p>');

					$categories = $this->category_m->get_new();
					$parent_categories = $this->category_m->get_parent_categories();

					//get ordering number and display at add form
					$this->db->select_max('priority')
						->from('categories')->where('parent', NULL);

					$current_priority = $this->db->get()->row()->priority;

					if ($current_priority == NULL) {
						$categories->priority = 1;
					} else {
						$categories->priority = $current_priority + 1;
					}

					$new_category = '';
					foreach ($parent_categories as $category) :
						$new_category .= '<input style="position:relative; bottom:3px; margin-right: 10px;" type="checkbox" name="category_id[]" value="' . $category->id_categories . '"' . set_checkbox('category_id[]', $category->id_categories);

						if (isset($chosen_categories)) :
							foreach ($chosen_categories as $chosen_category) :
								if ($chosen_category->id_category == $category->id_categories) :
									$new_category .= ' checked ';
								endif;
							endforeach;
						endif;
						$new_category .= '>';
						$new_category .= ucfirst($category->category) . '<br>';


						$this->db->select('id_categories')->from('categories')->where('parent', $category->id_categories);
						$count_child = $this->db->get()->num_rows();

						if ($count_child > 0) :
							$this->db->select('*')->from('categories')->where('parent', $category->id_categories)->order_by('priority', 'ASC');
							$child_categories = $this->db->get()->result();

							foreach ($child_categories as $child) :
								$new_category .= '-- <input style="position:relative; bottom:3px; margin-right: 10px; margin-left:10px;" type="checkbox" name="category_id[]" value="' . $child->id_categories . '"' . set_checkbox('category_id[]', $child->id_categories);

								if (isset($chosen_categories)) :

									foreach ($chosen_categories as $chosen_category) :
										if ($chosen_category->id_category == $child->id_categories) :
											$new_category .= ' checked ';
										endif;
									endforeach;
								endif;
								$new_category .= '>';
								$new_category .= ucfirst($child->category) . '<br>';

								$this->db->select('id_categories')->from('categories')->where('parent', $child->id_categories);
								$count_grandchild = $this->db->get()->num_rows();

								if ($count_grandchild > 0) :
									$this->db->select('*')->from('categories')->where('parent', $child->id_categories)->order_by('priority', 'ASC');
									$grandchild_categories = $this->db->get()->result();

									foreach ($grandchild_categories as $grandchild) :
										$new_category .= '------- <input style="position:relative; bottom:3px; margin-right: 10px; margin-left:10px;" type="checkbox" name="category_id[]" value="' . $grandchild->id_categories . '"' . set_checkbox('category_id[]', $grandchild->id_categories);

										if (isset($chosen_categories)) :

											foreach ($chosen_categories as $chosen_category) :
												if ($chosen_category->id_category == $grandchild->id_categories) :
													$new_category .= ' checked ';
												endif;
											endforeach;
										endif;
										$new_category .= '>';
										$new_category .= ucfirst($grandchild->category) . '<br>';
									endforeach;
								endif;
							endforeach;
						endif;
					endforeach;


					$new_category_in_modal = '';

					foreach ($parent_categories as $parent) {
						$new_category_in_modal .= '<option value="' . $parent->id_categories . '"';

						if ($categories->parent == $parent->id_categories) {
							$new_category_in_modal .= 'selected="selected" ';
						}

						$new_category_in_modal .= ">" . ucfirst($parent->category) . "</option>";


						$this->db->select('*')->from('categories')->where('parent', $parent->id_categories)->order_by('priority', 'ASC');
						$child = $this->db->get()->result();

						foreach ($child as $child_category) {
							$new_category_in_modal .= '<option value="' . $child_category->id_categories . '"';

							if ($categories->parent == $child_category->id_categories) {
								$new_category_in_modal .= 'selected="selected" ';
							}

							$new_category_in_modal .= ">-" . ucfirst($child_category->category) . "</option>";
						}
					}

					echo json_encode(array(
						'new_category' => $new_category,
						'new_category_in_modal' => $new_category_in_modal,
						'result' => 'sukses',
					));
				}
			}
		} else {


			if ($id == NULL) {

				$this->data['categories'] = $this->category_m->get_new();
				$this->data['parent_categories'] = $this->category_m->get_parent_categories();

				//get ordering number and display at add form
				$this->db->select_max('priority')->from('categories')->where('parent', NULL);
				$current_priority = $this->db->get()->row()->priority;
				if ($current_priority == NULL) {
					$this->data['categories']->priority = 1;
				} else {
					$this->data['categories']->priority = $current_priority + 1;
				}
			} else {

				//check if id exist. If not exist, redirect
				$count = $this->category_m->count_exist($id);
				if ($count == 0) {
					redirect(base_url('admin/categories/edit'));
				}

				$this->data['categories'] = $this->category_m->get($id);
				$this->data['parent_categories'] = $this->category_m->get_parent_categories();
				$this->category_current_id = (int) $this->data['categories']->id_categories;
			}

			//validation check
			$config = $this->category_m->rules;
			$this->load->library('form_validation');
			$this->form_validation->set_rules($config);
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

			if ($this->form_validation->run($this) == TRUE) {


				//check & processing image banner upload files	

				if ($_FILES['banner_image1']['size'] !== 0) {

					//get max image width and height from configuration table
					$this->db->select('category_image_width, category_image_height')->from('configuration')->where('id_configuration', 1);
					$image_dimension = $this->db->get()->row();

					$config['upload_path'] = './uploads/category/';
					$config['allowed_types'] = 'png|jpg|jpeg|gif|webp';
					$config['max_size']	= '500';
					$config['max_width']  = $image_dimension->category_image_width;
					$config['max_height']  = $image_dimension->category_image_height;

					$this->load->library('upload', $config);
					$this->upload->initialize($config);

					if (!$this->upload->do_upload('banner_image1')) {

						$error = array('error' => $this->upload->display_errors());
						$error_message = $error['error'];

						$this->session->set_flashdata('success', "<div style='background:red; color:white; padding:5px; font-weight:bold;'>$error_message</div>");

						if ($this->category_current_id != NULL) {
							redirect('admin/categories/edit/' . $this->category_current_id);
						} else {
							redirect('admin/categories/edit');
						}
					} else {

						$image1 = $this->upload->data();
						$image_filename1 =  $image1['file_name'];
					}
				} else {
					$image_filename1 = NULL;
				}


				if ($_FILES['banner_image2']['size'] !== 0) {

					$config['upload_path'] = './uploads/category/';
					$config['allowed_types'] = 'png|jpg|jpeg|gif|webp';
					$config['max_size']	= '500';
					$config['max_width']  = '800';
					$config['max_height']  = '500';

					$this->load->library('upload', $config);
					$this->upload->initialize($config);

					if (!$this->upload->do_upload('banner_image2')) {

						$error = array('error' => $this->upload->display_errors());
						$error_message = $error['error'];

						$this->session->set_flashdata('success', "<div style='background:red; color:white; padding:5px; font-weight:bold;'>$error_message</div>");

						if ($this->category_current_id != NULL) {
							redirect('admin/categories/edit/' . $this->category_current_id);
						} else {
							redirect('admin/categories/edit');
						}
					} else {

						$image2 = $this->upload->data();
						$image_filename2 =  $image2['file_name'];
					}
				} else {
					$image_filename2 = NULL;
				}

				$data = $this->table_data_processing($image_filename1, $image_filename2);

				if ($this->category_current_id == NULL) {

					$this->category_m->add_category($data);

					$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil dibuat</p>');

					redirect('admin/categories');
				} else {

					$this->category_m->edit_category($id, $data);

					//change the menu if available
					//check if category menu exist..
					$this->db->select('id_menus')->from('menus')->where('category_id', $id);
					$count_menu = $this->db->get()->num_rows();

					if ($count_menu > 0) {

						//menu exist..so need to change the url path for the menu
						$this->db->select('parent')->from('categories')->where('id_categories', $id);
						$parent_id = $this->db->get()->row()->parent;

						//check parent_id
						if ($parent_id === NULL) {

							//this is level1 category
							$menu_link = 'category/' . url_title($this->input->post('category_name'));
						} elseif ($parent_id !== NULL) {

							$this->db->select('parent')->from('categories')->where('id_categories', $parent_id);
							$parent2_id = $this->db->get()->row()->parent;

							if ($parent2_id === NULL) {

								//this is level 2 category..
								//get level 1 alias
								$this->db->select('alias')->from('categories')->where('id_categories', $parent_id);
								$alias_level1 = $this->db->get()->row()->alias;

								//get level 2 alias
								$this->db->select('alias')->from('categories')->where('id_categories', $id);
								$alias_level2 = $this->db->get()->row()->alias;

								$menu_link = 'category/' . $alias_level1 . '/' . url_title($this->input->post('category_name'));
							} else {

								//this is level 3 category..
								//get level 2 alias
								$this->db->select('alias')->from('categories')->where('id_categories', $parent_id);
								$alias_level2 = $this->db->get()->row()->alias;

								//get level 1 alias
								$this->db->select('parent')->from('categories')->where('id_categories', $parent_id);
								$level1_id = $this->db->get()->row()->parent;

								$this->db->select('alias')->from('categories')->where('id_categories', $level1_id);
								$alias_level1 = $this->db->get()->row()->alias;

								$menu_link = 'category/' . $alias_level1 . '/' . $alias_level2 . '/' . url_title($this->input->post('category_name'));
							}
						}

						//update menu link
						$data = array(
							'menu_link' => $menu_link
						);
						$this->db->where('category_id', $id);
						$this->db->update('menus', $data);
					}

					$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil diubah</p>');

					redirect('admin/categories/edit/' .  $id);
				}
			}

			$this->data['subview'] = 'admin/categories/edit';
			$this->load->view('admin/templates/header', $this->data_header);
			$this->load->view('admin/_layout_main', $this->data);
			$this->load->view('admin/templates/footer');
		}
	}

	//to delete a category
	public function delete($id = NULL)
	{

		if ($id == NULL) redirect(base_url('admin/categories'));
		//check if id exist. 
		$count = $this->category_m->count_exist($id);
		if ($count == 0) {
			redirect(base_url('admin/categories'));
		}

		//delete image from server
		//check if there is an existing image
		$this->db->select('image')->from('categories')->where('id_categories', (int) $id);
		$image = $this->db->get()->row()->image;

		if ($image != '' && $image != NULL) {
			if (file_exists(FCPATH . '/uploads/category/' . $image)) {
				//Delete the actual image file from server. FCPATH is codeigniter base path
				unlink(FCPATH . '/uploads/category/' . $image);
			}
		}

		//check if there are child categories belong to this parent category. if yes, set the parent category to NULL
		$this->db->select('id_categories')->from('categories')->where('parent', $id);
		$child_categories = $this->db->get()->result();

		if (count($child_categories) > 0) {

			//child categories exist, then set their parent to NULL
			foreach ($child_categories as $child_category) {

				$data = array(
					'parent' => NULL,
				);
				$this->db->where('id_categories', $child_category->id_categories);
				$this->db->update('categories', $data);
			}
		}
		//logging
		$user_id = $this->session->userdata('admin')['id'];

		$this->db->select('category');
		$this->db->from('categories');
		$this->db->where('id_categories', $id);
		$query = $this->db->get();
		$data = $query->row();

		if ($data) {
			$activity = 'User menghapus kategori (' . $data->category . ')';
		} else {
			$activity = 'User menghapus kategori (' . $id . ')';
		}

		log_activity($user_id, $activity);

		//delete parent category
		$this->category_m->delete($id);

		$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Kategori Produk berhasil dihapus</p>');
		redirect('admin/categories');
	}

	private function table_data_processing($image_filename1, $image_filename2, $this_case = false)
	{

		$data = array(
			'category' 		=> $this->security->xss_clean($this->input->post('category_name')),
			'category_en' 	=> $this->security->xss_clean($this->input->post('category_name_en')),
			'alias' 		=> url_title($this->security->xss_clean($this->input->post('category_name'))),
			'alias_en' 		=> url_title($this->security->xss_clean($this->input->post('category_name_en'))),
			'status' 		=> $this->input->post('status'),
			'priority' 		=> $this->input->post('priority'),
			'meta_title'	=> $this->security->xss_clean($this->input->post('meta_title')),
			'meta_description' => $this->security->xss_clean($this->input->post('meta_description')),
			'banner_link'	=> $this->security->xss_clean($this->input->post('banner_link')),
			'updated_by'	=> $this->session->userdata('admin')['name'],

		);

		if ($this_case != false && $this_case == 'addcategory_in_product') {
			$data['description'] = $this->security->xss_clean($this->input->post('description_category'));
			$data['description_en'] = $this->security->xss_clean($this->input->post('description_en_category'));
		} else {
			$data['description'] = $this->security->xss_clean($this->input->post('description'));
			$data['description_en'] = $this->security->xss_clean($this->input->post('description_en'));
		}

		if ($this->input->post('parent_id') == 'no-parent') {
			$data['parent'] = NULL;
		} else {
			$data['parent'] = (int) $this->input->post('parent_id');
		}

		//image upload
		if (isset($image_filename1)) {
			$data['image'] = $image_filename1;
		}

		if (isset($image_filename2)) {
			$data['image_mobile'] = $image_filename2;
		}

		return $data;
	}

	//To delete category image file from server, and from database
	public function delete_image($id = NULL)
	{

		$count = $this->category_m->count_exist($id);

		if ($id == NULL || $count == 0) {
			redirect('admin/categories');
		}

		//get image file name for deletion
		$this->db->select('image')->from('categories')->where('id_categories', (int) $id);
		$image = $this->db->get()->row()->image;

		if (file_exists(FCPATH . '/uploads/category/' . $image)) {
			//Delete the actual image file from server. FCPATH is codeigniter base path
			unlink(FCPATH . '/uploads/category/' . $image);
		}

		//Delete image field from database
		$data = array(
			'image' => ''
		);
		$this->db->where('id_categories', (int) $id);
		$this->db->update('categories', $data);

		$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Gambar berhasil dihapus</p>');

		redirect('admin/categories/edit/' . $id);
	}

	//callback function validation add new category
	//make it private by adding _
	public function _cek_existing_category_title($str)
	{

		$num_rows = $this->category_m->cek_existing_category_title($str, $this->category_current_id);
		if ($num_rows != 0) {
			$this->form_validation->set_message('_cek_existing_category_title', 'category name already exist !');
			return FALSE;
		} else {
			return TRUE;
		}
	}

	public function ajax_get_ordering()
	{

		//test if ajax call to prevent direct access
		if (!$this->input->is_ajax_request()) {
			exit('No direct script access allowed');
		}

		if ($this->input->post('id_parentcategory') == 'no-parent') {

			//choose select option where value is no-parent at edit view
			//get ordering number and display at add form
			$this->db->select_max('priority')->from('categories')->where('parent', NULL);
			$current_priority = $this->db->get()->row()->priority;
			if ($current_priority == NULL) {
				$next_priority = 1;
			} else {
				$next_priority = $current_priority + 1;
			}
		} else {

			$id_parentcategory = (int) $this->input->post('id_parentcategory');

			//get ordering number and display at add form
			$this->db->select_max('priority')->from('categories')->where('parent', $id_parentcategory);
			$current_priority = $this->db->get()->row()->priority;
			$next_priority = $current_priority + 1;
		}

		echo $next_priority;
	}

	public function delete_image_mobile($id = NULL)
	{

		$count = $this->category_m->count_exist($id);

		if ($id == NULL || $count == 0) {
			redirect('admin/categories');
		}

		//get image file name for deletion
		$this->db->select('image_mobile')->from('categories')->where('id_categories', (int) $id);
		$image = $this->db->get()->row()->image;

		if (file_exists(FCPATH . '/uploads/category/' . $image)) {
			//Delete the actual image file from server. FCPATH is codeigniter base path
			unlink(FCPATH . '/uploads/category/' . $image);
		}

		//Delete image field from database
		$data = array(
			'image_mobile' => ''
		);
		$this->db->where('id_categories', (int) $id);
		$this->db->update('categories', $data);

		$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Gambar berhasil dihapus</p>');

		redirect('admin/categories/edit/' . $id);
	}
}

https://t.me/RX1948 - 2025