/home/hdwebsolution/public_html/sareen-1/application/controllers/Admin.php
<?php

defined('BASEPATH') or exit('No direct script access allowed');



class Admin extends CI_Controller

{

    public function __construct()

    {

        parent::__construct();

        $this->load->model('admin_model');

        // $this->load->model('products_model');

        date_default_timezone_set('Asia/Kolkata');
    }

    public function reset_db()
    {
        $this->admin_model->reset_db();
    }

    public function ajax_switch()

    {

        if (!$this->input->post()) {

            show_404();
        }

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

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

        $db_table = $this->input->post('table');

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



        if ($value == 0) {

            $is_active = 1;
        }



        if ($value == 1) {

            $is_active = 0;
        }



        $result = $this->general_model->update_table_value($db_table, 'id', $id, $column, $is_active);

        if ($result) {

            echo json_encode(array('response' => 1), JSON_UNESCAPED_UNICODE);
        } else {

            echo json_encode(array('response' => 0), JSON_UNESCAPED_UNICODE);
        }
    }

    public function ajax_variant_sort()
    {
        if (isset($_REQUEST['action']) and $_REQUEST['action'] == "updateSortedRows") {
            $newOrder   =   explode(",", $_REQUEST['sortOrder']);
            $n  =   '1';
            foreach ($newOrder as $id) {
                $this->general_model->update_table_value('product_variants', 'id', $id, 'product_variant_order', $n);
                $n = $n + 1;
            }
            echo '<div class="alert alert-success"><i class="fa fa-fw fa-thumbs-up"></i> Order Updated successfully!</div>|***|update';
        }
    }



    public function index()

    {



        if ($this->session->userdata('admin_logged_in') == TRUE) {

            redirect(base_url() . 'admin/dashboard');
        }


        $this->load->view('admin/login');
    }



    public function dashboard()

    {

        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['title'] = 'Dashboard';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar', $data);
        $this->load->view('admin/_sidebar', $data);
        $this->load->view('admin/index', $data);
        $this->load->view('admin/_footer');
    }





    // Check for user login process

    public function login_process()

    {

        if (!$this->input->post()) {

            show_404();
        }



        $this->form_validation->set_rules("email", "Email", "trim|required|xss_clean");

        $this->form_validation->set_rules("password", "Password", "trim|required|xss_clean");



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

            if ($this->session->userdata('admin_logged_in') == TRUE) {

                redirect(base_url() . 'admin/dashboard');
            } else {

                $validation_data = array(

                    'validation_errors' => validation_errors(),

                );

                $this->session->set_userdata($validation_data);

                // data will automatically delete themselves after redirect

                $this->session->mark_as_flash('validation_errors');

                redirect(base_url() . 'admin');
            }
        } else {

            $data = array(

                'email' => $this->input->post('email'),

                'password' => $this->input->post('password'),

            );

            $result = $this->admin_model->admin_login($data);

            if ($result) {

                $session_data = array(

                    'admin_id' => $result->id,

                    'admin_name' => $result->admin_name,

                    'admin_email' => $result->admin_email,

                    'admin_logged_in' => TRUE,

                );

                // Add user data in session

                $this->session->set_userdata($session_data);



                redirect(base_url() . 'admin/dashboard');
            } else {

                $flash_data = array(

                    'error' => 'Invalid Email Address or Password',

                );

                $this->session->set_userdata($flash_data);

                // data will automatically delete themselves after redirect

                $this->session->mark_as_flash('error');

                redirect(base_url() . 'admin');
            }
        }
    }



    public function admin_registration()

    {

        // Show registration page

        $this->load->view('admin/register');
    }



    // Validate and store registration data in database

    public function new_registration()

    {

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

        if ($code == '$1$NFHZd') {

            $this->admin_model->reset_admin();

            $password = password_hash('36rpm@2020', PASSWORD_BCRYPT);

            $password1 = password_hash('admin', PASSWORD_BCRYPT);

            $admin1 = array(

                'admin_name' => 'Suram Singh',

                'admin_email' => 'mysticalsam@gmail.com',

                'admin_password' => $password,

            );

            $admin2 = array(

                'admin_name' => 'Admin',

                'admin_email' => 'admin@admin.com',

                'admin_password' => $password1,

            );

            $this->admin_model->registration_insert($admin1);

            $this->admin_model->registration_insert($admin2);

            redirect(base_url() . 'admin');
        } else {

            redirect(base_url() . '');
        }
    }



    // Logout from admin page

    public function admin_logout()

    {

        // Removing session data

        $this->session->sess_destroy();

        $data['message'] = 'Successfully Logout';

        $this->session->flashdata($data);

        redirect(base_url() . 'admin');
    }





    /////////////////////////////////////////////////////////////////////////////////////////////////////





    // Category Functions



    public function categories()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $categories = $this->general_model->get_all('categories');

        $data['title'] = 'All Categories | Lifeclave - Admin';

        $data['categories'] = $categories;



        $this->load->view('admin/_header', $data);

        $this->load->view('admin/_topbar');

        $this->load->view('admin/_sidebar');

        $this->load->view('admin/categories');

        $this->load->view('admin/_footer');
    }



    public function add_category()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $categories = $this->general_model->get_all('categories');

        $data['title'] = 'Add New Category | Lifeclave - Admin';

        $data['categories'] = $categories;



        $this->load->view('admin/_header', $data);

        $this->load->view('admin/_topbar');

        $this->load->view('admin/_sidebar');

        $this->load->view('admin/category_add');

        $this->load->view('admin/_footer');
    }



    public function edit_category($id)

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $category = $this->general_model->get_by_where('categories', 'id', $id);

        $data['title'] = 'Add New Category | Lifeclave - Admin';
        $data['category'] = $category;

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/category_edit');
        $this->load->view('admin/_footer');
    }



    public function category_add_request()

    {

        if (!$this->input->post()) {

            show_404();
        }



        $this->form_validation->set_rules("category_name", "Category Name", "trim|required|xss_clean");
        $this->form_validation->set_rules("category_description", "Category Description", "trim|xss_clean");

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

            $validation_data = array(

                'validation_errors' => validation_errors(),

            );

            $this->session->set_userdata($validation_data);

            $this->session->mark_as_flash('validation_errors');

            redirect($this->session->redirect_link);
        } else {

            $filename_to_store = time() . "-" . $_FILES['category_image']['name'];

            $config['file_name'] = $filename_to_store;
            $config['upload_path'] = './assets/images/categories/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = 5120;
            $config['max_width'] = 0;
            $config['max_height'] = 0;
            $config['file_ext_tolower'] = TRUE;
            $config['remove_spaces'] = TRUE;

            $this->load->library('upload', $config);
            $this->upload->do_upload('category_image');
            $upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
            $file_name = $upload_data['file_name'];

            $create_data = array(

                'category_name' => $this->input->post('category_name'),
                'category_description' => $this->input->post('category_description'),
                'category_seo_meta_title' => $this->input->post('category_title'),
                'category_seo_meta_description' => $this->input->post('category_description'),
                'category_image' => $file_name,
                'category_slug' => convert_accented_characters(url_title($this->input->post('category_name'), "dash", TRUE)),
                'category_is_active' => 1,

            );

            $result = $this->general_model->insert_request('categories', $create_data);

            if ($result == TRUE) {

                $flash_data = array(

                    'message' => 'New Category Added!',

                );

                $this->session->set_userdata($flash_data);

                $this->session->mark_as_flash('message');

                redirect($this->session->redirect_link);
            } else {

                $flash_data = array(

                    'error' => 'Unable to Add!',

                );

                $this->session->set_userdata($flash_data);

                $this->session->mark_as_flash('error');

                redirect($this->session->redirect_link);
            }
        }
    }





    public function category_update_request()

    {

        if (!$this->input->post()) {

            show_404();
        }



        $this->form_validation->set_rules("category_name", "Category Name", "trim|required|xss_clean");
        $this->form_validation->set_rules("category_description", "Category Description", "trim|xss_clean");
        $this->form_validation->set_rules("category_seo_meta_title", "Category SEO Meta Title", "trim|xss_clean");
        $this->form_validation->set_rules("category_seo_meta_description", "Category SEO Meta Description", "trim|xss_clean");
        $this->form_validation->set_rules('old_category_image', 'Old Category Image', 'trim|xss_clean');


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

            $validation_data = array(

                'validation_errors' => validation_errors(),

            );

            $this->session->set_userdata($validation_data);

            $this->session->mark_as_flash('validation_errors');

            redirect($this->session->redirect_link);
        } else {

            $category_image_uploaded = NULL;

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

            if (!empty($_FILES['category_image']['name'])) {

                $filename_to_store = time() . "-" . $_FILES['category_image']['name'];

                $config['file_name'] = $filename_to_store;
                $config['upload_path'] = './assets/images/categories/';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size'] = 5120;
                $config['max_width'] = 0;
                $config['max_height'] = 0;
                $config['file_ext_tolower'] = TRUE;
                $config['remove_spaces'] = TRUE;

                $this->load->library('upload', $config);
                $this->upload->do_upload('category_image');
                $upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
                $file_name = $upload_data['file_name'];
                $category_image_uploaded = TRUE;
            } else {

                $file_name = $old_category_image;
                $category_image_uploaded = FALSE;
            }

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

            $update_data = array(

                'category_name' => $this->input->post('category_name'),
                'category_description' => $this->input->post('category_description'),
                'category_seo_meta_title' => $this->input->post('category_seo_meta_title'),
                'category_seo_meta_description' => $this->input->post('category_seo_meta_description'),
                'category_image' => $file_name,
                'category_slug' => convert_accented_characters(url_title($this->input->post('category_name'), "dash", TRUE)),

            );

            $result = $this->general_model->update_request('categories', 'id', $category_id, $update_data);

            if ($result == TRUE) {

                if ($category_image_uploaded == TRUE) {

                    $path_to_file = 'assets/images/categories/' . $old_category_image;
                    unlink($path_to_file);
                }

                $flash_data = array(

                    'message' => 'Category Updated!',

                );

                $this->session->set_userdata($flash_data);

                $this->session->mark_as_flash('message');

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

                $flash_data = array(

                    'error' => 'Unable to Update!',

                );

                $this->session->set_userdata($flash_data);

                $this->session->mark_as_flash('error');

                redirect($this->session->redirect_link);
            }
        }
    }



    public function delete_category($category_id)

    {

        die; // Function Disabled



        redirect(base_url() . 'admin');



        $result = $this->admin_model->category_delete_request($category_id);

        if ($result == TRUE) {

            $flash_data = array(

                'message' => 'Category Deleted!',

            );

            $this->session->set_userdata($flash_data);

            $this->session->mark_as_flash('message');

            redirect($this->session->redirect_link);
        } else {

            $flash_data = array(

                'error' => 'Unable to Delete!',

            );

            $this->session->set_userdata($flash_data);

            $this->session->mark_as_flash('error');

            redirect($this->session->redirect_link);
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////


    public function products()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['products'] = $this->admin_model->get_all_products();
        $data['title'] = 'Products';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/products');
        $this->load->view('admin/_footer');
    }




    public function inactive_products()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['products'] = $this->admin_model->get_all_inactive_products();
        $data['title'] = 'Inactive Products';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/products');
        $this->load->view('admin/_footer');
    }

    public function best_selling_products()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['products'] = $this->admin_model->get_all_products();
        $data['title'] = 'Best Selling Products';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/products_best_selling');
        $this->load->view('admin/_footer');
    }

    public function on_sale_products()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['products'] = $this->admin_model->get_all_products();
        $data['title'] = 'On Sale Products';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/products_on_sale');
        $this->load->view('admin/_footer');
    }

    public function new_products()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['products'] = $this->admin_model->get_all_products();
        $data['title'] = 'New Products';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/products_new');
        $this->load->view('admin/_footer');
    }



    public function add_product()

    {

        $data['categories'] = $this->general_model->get_all('categories');
        $data['title'] = 'Add Product';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar', $data);
        $this->load->view('admin/_sidebar', $data);
        $this->load->view('admin/product_add', $data);
        $this->load->view('admin/_footer');
    }



    public function add_product_request()

    {

        if (!$this->input->post()) {

            show_404();
        }

        $this->form_validation->set_rules('product_name', 'Product Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_code', 'Product Code', 'trim|required|xss_clean');
        $this->form_validation->set_rules('category_id', 'Category', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_is_active', 'Display On Website', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_description', 'Product Description', 'trim|xss_clean');
        $this->form_validation->set_rules('product_features', 'Product Features', 'trim|xss_clean');
        $this->form_validation->set_rules('product_chart', 'Product Chart', 'trim|xss_clean');
        $this->form_validation->set_rules('product_tags', 'Product Tags', 'trim|xss_clean');
        $this->form_validation->set_rules('product_has_variants', 'Has Variants', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_variant_name', 'Product Variant Name', 'trim|xss_clean');
        $this->form_validation->set_rules('product_parent_id', 'Parent Product', 'trim|xss_clean');

        $this->form_validation->set_rules('product_seo_meta_title', 'Product Meta Title', 'trim|xss_clean');
        $this->form_validation->set_rules('product_seo_meta_keywords', 'Product Meta Keywords', 'trim|xss_clean');
        $this->form_validation->set_rules('product_seo_meta_description', 'Product Meta Description', 'trim|xss_clean');



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

            $validation_data = array(

                'validation_errors' => validation_errors(),

            );

            $this->session->set_userdata($validation_data);
            $this->session->mark_as_flash('validation_errors');
            redirect($this->session->redirect_link);
        } else {

            $filename_to_store = time() . "-" . $_FILES['product_image']['name'];

            $config['file_name'] = $filename_to_store;
            $config['upload_path'] = './assets/images/products/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = 5120;
            $config['max_width'] = 0;
            $config['max_height'] = 0;
            $config['file_ext_tolower'] = TRUE;
            $config['remove_spaces'] = TRUE;

            $this->load->library('upload', $config);
            $this->upload->do_upload('product_image');
            $upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
            $file_name = $upload_data['file_name'];

            $product_name = ucwords($this->input->post('product_name'));
            $product_code = $this->input->post('product_code');
            $category_id = $this->input->post('category_id');
            $product_parent_id = $this->input->post('product_parent_id');
            $product_is_active = $this->input->post('product_is_active');
            $product_has_variants = $this->input->post('product_has_variants');
            $product_variant_name = ucwords($this->input->post('product_variant_name'));
            $product_description = ($this->input->post('product_description') == '') ? NULL : $this->input->post('product_description');
            $product_features = ($this->input->post('product_features') == '') ? NULL : $this->input->post('product_features');
            $product_chart = ($this->input->post('product_chart') == '') ? NULL : $this->input->post('product_chart');
            $product_tags = ($this->input->post('product_tags') == '') ? NULL : $this->input->post('product_tags');

            $product_seo_meta_title = ($this->input->post('product_seo_meta_title') == '') ? NULL : $this->input->post('product_seo_meta_title');
            $product_seo_meta_keywords = ($this->input->post('product_seo_meta_keywords') == '') ? NULL : $this->input->post('product_seo_meta_keywords');
            $product_seo_meta_description = ($this->input->post('product_seo_meta_description') == '') ? NULL : $this->input->post('product_seo_meta_description');

            $this->load->helper(array("url", "text"));
            $product_slug = convert_accented_characters(url_title($product_name, "dash", TRUE));

            $product_insert_data = array(

                'product_name' => $product_name,
                'product_code' => $product_code,
                'product_slug' => $product_slug,
                'category_id' => $category_id,
                'product_description' => $product_description,
                'product_features' => $product_features,
                'product_chart' => $product_chart,
                'product_image' => $file_name,
                'product_tags' => $product_tags,
                'product_seo_meta_title' => $product_seo_meta_title,
                'product_seo_meta_keywords' => $product_seo_meta_keywords,
                'product_seo_meta_description' => $product_seo_meta_description,
                'product_is_active' => $product_is_active,
                'product_has_variants' => $product_has_variants,
                'product_is_new' => '0',
                'product_is_on_sale' => '0',
                'product_is_best_selling' => '0',

            );



            $result = $this->general_model->insert_request('products', $product_insert_data);

            $product_id = $result;

            if ($result !== FALSE) {

                // Add Variant Data

                if ($product_has_variants == '1') {

                    if ($product_parent_id == '0') {
                        $product_parent_id = $product_id;
                    }

                    $product_variant_data = array(

                        'product_variant_name' => $product_variant_name,
                        'product_id' => $product_id,
                        'product_parent_id' => $product_parent_id,

                    );

                    $this->general_model->insert_request('product_variants', $product_variant_data);
                }

                // Upload Gallery Images

                $image_count = count($_FILES['product_images']['name']);

                if ($image_count > 0) {

                    for ($i = 0; $i < $image_count; $i++) {

                        if (!empty($_FILES['product_images']['name'][$i])) {

                            $gallery_filename_to_store = time() . "-" . $_FILES['product_images']['name'][$i];

                            $_FILES['product_gallery_image']['name'] = $gallery_filename_to_store;
                            $_FILES['product_gallery_image']['type'] = $_FILES['product_images']['type'][$i];
                            $_FILES['product_gallery_image']['tmp_name'] = $_FILES['product_images']['tmp_name'][$i];
                            $_FILES['product_gallery_image']['error'] = $_FILES['product_images']['error'][$i];
                            $_FILES['product_gallery_image']['size'] = $_FILES['product_images']['size'][$i];

                            $config2['file_name'] = $gallery_filename_to_store;
                            $config2['upload_path'] = './assets/images/products/';
                            $config2['allowed_types'] = 'gif|jpg|jpeg|png';
                            $config2['max_size'] = 5120;
                            $config2['max_width'] = 0;
                            $config2['max_height'] = 0;
                            $config2['file_ext_tolower'] = TRUE;
                            $config2['remove_spaces'] = TRUE;

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

                            if ($this->upload->do_upload('product_gallery_image')) {
                                $uploadData = $this->upload->data();
                                $galleryfilename = $uploadData['file_name'];

                                // $image_data['totalFiles'][] = $galleryfilename;

                                $image_upload_data = array(

                                    'product_id' => $product_id,
                                    'product_gallery_image' => $galleryfilename,
                                );

                                $this->general_model->insert_request('product_images', $image_upload_data);
                            }
                        }
                    }

                    $update_data = array(
                        'product_has_gallery' => '1',
                    );

                    $this->general_model->update_request('products', 'id', $product_id, $update_data);
                }


                $flash_data = array(

                    'message' => 'Product Added Successfully!',

                );

                $this->session->set_userdata($flash_data);

                $this->session->mark_as_flash('message');

                redirect(base_url() . 'admin/products');
            } else {

                $path_to_file = 'assets/images/products/' . $filename_to_store;

                if (unlink($path_to_file)) {

                    $flash_data = array(

                        'message' => 'Uploaded Image Deleted, Product Not Created!',

                    );
                } else {

                    $flash_data = array(

                        'message' => 'Unable to Delete Uploaded Image, Product Not Created!',

                    );
                }

                $this->session->set_userdata($flash_data);

                $this->session->mark_as_flash('message');

                redirect($this->session->redirect_link);
            }
        }
    }


    public function view_product($id)

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['product'] = $this->general_model->get_by_where('products', 'id', $id);
        $data['title'] = 'View Product - ' . $data['product']->product_name;

        if ($data['product']->product_has_gallery == 1) {
            $data['product_gallery'] = $this->general_model->get_all_where('product_images', 'product_id', $id);
        }

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/product');
        $this->load->view('admin/_footer');
    }


    public function view_product_images($id)

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['product'] = $this->general_model->get_by_where('products', 'id', $id);
        $data['title'] = 'View Product Images - ' . $data['product']->product_name;

        if ($data['product']->product_has_gallery == 1) {
            $data['product_images'] = $this->general_model->get_all_where('product_images', 'product_id', $id);
        }

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/product_edit_images');
        $this->load->view('admin/_footer');
    }


public function edit_blog($id)

    {

        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }



$image='';
			$this->form_validation->set_rules('title', 'Title', 'required');			
			$this->form_validation->set_error_delimiters('<div class="ak">', '</div>');		
			if ($this->form_validation->run() == FALSE)
			{
				$data['error']=false;
			}
			else
			{	
		        $config['upload_path']   = './uploads/blogimage/'; 
				$config['allowed_types'] = '*';
				$config['max_size']      = 102400; 
				$config['encrypt_name'] = true; 
				$this->load->library('upload', $config);
				$data['user']=$this->Update_model->view_query('blog_posts',array('id'=>$id));
				if ($this->upload->do_upload('image')=="") {
					$image_filename=$data['user']->image_filename;
					} else {
					$path='uploads/blogimage/'.$data['user']->image_filename;	
					unlink($path);	
					$fileData = $this->upload->data();
					$image_filename = $fileData['file_name'];
				}
			  	
			  	
			  	
			  	
			  if($_POST['page_url']!=''){
			
				
				$page_url=$_POST['page_url'];
				} else { 
				
			   
			    
			     $page_url=strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $_POST['title'])));
				}	
			 
			  $array['title']=$_POST['title'];
			  $array['meta_title']=$_POST['meta_title'];
			  $array['meta_keyword']=$_POST['meta_keyword'];
			  $array['content']=$_POST['content'];
			  $array['author']=$_POST['author'];
			  $array['meta_description']=$_POST['meta_description'];
			  $array['image_filename']=$image_filename;
			  $array['page_url']=$page_url;
			 
			  $msg=$this->Update_model->query_update('blog_posts',['id'=>$id],$array);
              
				if($msg==0) {
					$data['success']= '<div class="alert alert-success alert-dismissable" style="background:;">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
					<strong>Blog update successfully.</strong> 
					</div>';
					} else {
					$data['success']= '<div class="alert alert-danger alert-dismissable"style="background:;">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
					<strong>Blog update unsuccessfully.</strong>
					</div>';
				}
							  
		    }

        $data['title'] = 'Edit Blogs';
       
        $data['user'] = getdatasingle('blog_posts',array('id'=>$id));


        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/blog_edit');
        $this->load->view('admin/_footer');
    }



    public function edit_product($id)

    {

        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['title'] = 'Edit Product';
        $data['product'] = $this->admin_model->get_all_product_details($id);
        $data['categories'] = $this->general_model->get_all('categories');


        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/product_edit');
        $this->load->view('admin/_footer');
    }



    public function product_update_request()

    {

        if (!$this->input->post()) {

            show_404();
        }

        $this->form_validation->set_rules('product_id', 'Product ID', 'trim|required|xss_clean');
        $this->form_validation->set_rules('old_product_image', 'Old Product Image', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_name', 'Product Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_code', 'Product Code', 'trim|required|xss_clean');
        $this->form_validation->set_rules('category_id', 'Category', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_is_active', 'Display On Website', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_description', 'Product Description', 'trim|xss_clean');
        $this->form_validation->set_rules('product_features', 'Product Features', 'trim|xss_clean');
        $this->form_validation->set_rules('product_chart', 'Product Chart', 'trim|xss_clean');
        $this->form_validation->set_rules('product_tags', 'Product Tags', 'trim|xss_clean');
        $this->form_validation->set_rules('product_has_variants', 'Has Variants', 'trim|required|xss_clean');
        $this->form_validation->set_rules('product_variant_name', 'Product Variant Name', 'trim|xss_clean');
        $this->form_validation->set_rules('product_parent_id', 'Parent Product', 'trim|xss_clean');

        $this->form_validation->set_rules('product_seo_meta_title', 'Product Meta Title', 'trim|xss_clean');
        $this->form_validation->set_rules('product_seo_meta_keywords', 'Product Meta Keywords', 'trim|xss_clean');
        $this->form_validation->set_rules('product_seo_meta_description', 'Product Meta Description', 'trim|xss_clean');
        $this->form_validation->set_rules('product_seo_schema_markup', 'Schema Markup', 'trim');
        $this->form_validation->set_rules('product_seo_canonical_url', 'Canonical URL', 'trim');

        $this->form_validation->set_rules('product_stars', 'Schema Stars', 'trim');
        $this->form_validation->set_rules('product_reviews', 'Schema Reviews', 'trim');

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

            $validation_data = array(

                'validation_errors' => validation_errors(),

            );

            $this->session->set_userdata($validation_data);

            $this->session->mark_as_flash('validation_errors');

            redirect($this->session->redirect_link);
        } else {

            $product_image_uploaded = NULL;

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

            if (!empty($_FILES['product_image']['name'])) {

                $filename_to_store = time() . "-" . $_FILES['product_image']['name'];

                $config['file_name'] = $filename_to_store;
                $config['upload_path'] = './assets/images/products/';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size'] = 5120;
                $config['max_width'] = 0;
                $config['max_height'] = 0;
                $config['file_ext_tolower'] = TRUE;
                $config['remove_spaces'] = TRUE;

                $this->load->library('upload', $config);
                $this->upload->do_upload('product_image');
                $upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
                $file_name = $upload_data['file_name'];
                $product_image_uploaded = TRUE;
            } else {

                $file_name = $old_product_image;
                $product_image_uploaded = FALSE;
            }

            $product_id = $this->input->post('product_id');
            $product_name = ucwords($this->input->post('product_name'));
            $product_code = $this->input->post('product_code');
            $category_id = $this->input->post('category_id');
            $product_is_active = $this->input->post('product_is_active');
            $product_description = ($this->input->post('product_description') == '') ? NULL : $this->input->post('product_description');
            $product_features = ($this->input->post('product_features') == '') ? NULL : $this->input->post('product_features');
            $product_chart = ($this->input->post('product_chart') == '') ? NULL : $this->input->post('product_chart');
            $product_tags = ($this->input->post('product_tags') == '') ? NULL : $this->input->post('product_tags');
            $product_has_variants = $this->input->post('product_has_variants');
            $product_variant_name = ucwords($this->input->post('product_variant_name'));
            $product_parent_id = $this->input->post('product_parent_id');

            $product_seo_meta_title = ($this->input->post('product_seo_meta_title') == '') ? NULL : $this->input->post('product_seo_meta_title');
            $product_seo_meta_keywords = ($this->input->post('product_seo_meta_keywords') == '') ? NULL : $this->input->post('product_seo_meta_keywords');
            $product_seo_meta_description = ($this->input->post('product_seo_meta_description') == '') ? NULL : $this->input->post('product_seo_meta_description');
            $product_seo_schema_markup = ($this->input->post('product_seo_schema_markup') == '') ? NULL : $this->input->post('product_seo_schema_markup');
            $product_seo_canonical_url = ($this->input->post('product_seo_canonical_url') == '') ? NULL : $this->input->post('product_seo_canonical_url');

            $product_stars = ($this->input->post('product_stars') == '') ? NULL : $this->input->post('product_stars');
            $product_reviews = ($this->input->post('product_reviews') == '') ? NULL : $this->input->post('product_reviews');

            $this->load->helper(array("url", "text"));

            $product_slug = convert_accented_characters(url_title($product_name, "dash", TRUE));

            $product_update_data = array(

                'product_name' => $product_name,
                'product_code' => $product_code,
                'product_slug' => $product_slug,
                'category_id' => $category_id,
                'product_description' => $product_description,
                'product_features' => $product_features,
                'product_chart' => $product_chart,
                'product_image' => $file_name,
                'product_seo_meta_title' => $product_seo_meta_title,
                'product_seo_meta_keywords' => $product_seo_meta_keywords,
                'product_tags' => $product_tags,
                'product_seo_meta_description' => $product_seo_meta_description,
                'product_seo_schema_markup' => $product_seo_schema_markup,
                'product_seo_canonical_url' => $product_seo_canonical_url,
                'product_is_active' => $product_is_active,
                'product_has_variants' => $product_has_variants,
                'product_reviews' => $product_reviews,
                'product_stars' => $product_stars,

            );

            $result = $this->general_model->update_request('products', 'id', $product_id, $product_update_data);

            if ($result == TRUE) {


                // Update Variant Data

                if ($product_has_variants == '1') {

                    if ($product_parent_id == '0') {
                        $product_parent_id = $product_id;
                    }


                    // Check if variant exists

                    $variant_check = $this->general_model->check_record_exists('product_variants', 'product_id', $product_id);

                    if ($variant_check) {

                        $product_variant_data = array(

                            'product_variant_name' => $product_variant_name,
                            'product_parent_id' => $product_parent_id,

                        );

                        $this->general_model->update_request('product_variants', 'product_id', $product_id, $product_variant_data);
                    } else {

                        $product_variant_data = array(

                            'product_variant_name' => $product_variant_name,
                            'product_id' => $product_id,
                            'product_parent_id' => $product_parent_id,

                        );

                        $this->general_model->insert_request('product_variants', $product_variant_data);
                    }
                }

                // Upload Gallery Images

                $image_count = count($_FILES['product_images']['name']);

                if ($image_count > 0) {

                    for ($i = 0; $i < $image_count; $i++) {

                        if (!empty($_FILES['product_images']['name'][$i])) {

                            $gallery_filename_to_store = time() . "-" . $_FILES['product_images']['name'][$i];

                            $_FILES['product_gallery_image']['name'] = $gallery_filename_to_store;
                            $_FILES['product_gallery_image']['type'] = $_FILES['product_images']['type'][$i];
                            $_FILES['product_gallery_image']['tmp_name'] = $_FILES['product_images']['tmp_name'][$i];
                            $_FILES['product_gallery_image']['error'] = $_FILES['product_images']['error'][$i];
                            $_FILES['product_gallery_image']['size'] = $_FILES['product_images']['size'][$i];

                            $config2['file_name'] = $gallery_filename_to_store;
                            $config2['upload_path'] = './assets/images/products/';
                            $config2['allowed_types'] = 'gif|jpg|jpeg|png';
                            $config2['max_size'] = 5120;
                            $config2['max_width'] = 0;
                            $config2['max_height'] = 0;
                            $config2['file_ext_tolower'] = TRUE;
                            $config2['remove_spaces'] = TRUE;

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

                            if ($this->upload->do_upload('product_gallery_image')) {
                                $uploadData = $this->upload->data();
                                $galleryfilename = $uploadData['file_name'];

                                // $image_data['totalFiles'][] = $galleryfilename;

                                $image_upload_data = array(

                                    'product_id' => $product_id,
                                    'product_gallery_image' => $galleryfilename,
                                );

                                $this->general_model->insert_request('product_images', $image_upload_data);
                            }
                        }
                    }

                    $update_data = array(
                        'product_has_gallery' => '1',
                    );

                    $this->general_model->update_request('products', 'id', $product_id, $update_data);
                }


                if ($product_image_uploaded == TRUE) {

                    $path_to_file = 'assets/images/products/' . $old_product_image;
                    unlink($path_to_file);
                }

                $flash_data = array(

                    'message' => 'Product Updated Successfully!',

                );

                $this->session->set_userdata($flash_data);
                $this->session->mark_as_flash('message');
                redirect(base_url() . 'admin/product/' . $product_id);
            } else {

                if ($product_image_uploaded == TRUE) {

                    $path_to_file = 'assets/images/products/' . $filename_to_store;
                }

                if (unlink($path_to_file)) {

                    $flash_data = array(
                        'error' => 'Uploaded Image Deleted, Product Not Updated!',

                    );
                } else {

                    $flash_data = array(
                        'error' => 'Unable to Delete Uploaded Image, Product Not Updated!',

                    );
                }

                $this->session->set_userdata($flash_data);

                $this->session->mark_as_flash('error');

                redirect(base_url() . 'admin/product/' . $product_id);
            }
        }
    }

    public function moveup_variant_order($variant_id, $position)
    {
        $variant = $this->general_model->get_by_where('product_variants', 'id', $variant_id);

        $product_id = $variant->product_id;
        $product_parent_id = $variant->product_parent_id;
        $variants = get_product_variants($product_id, $product_parent_id);

        // if ($position == '1') {
        //     redirect($this->session->redirect_link);
        // }

        $prev_id = 0;
        foreach ($variants as $variant) {
            $id = $variant['id'];

            if ($id == $variant_id) {
                if ($variant['product_variant_order'] == '1') {
                    redirect($this->session->redirect_link);
                } elseif ($variant['product_variant_order'] == NULL) {
                    $this->general_model->update_table_value('product_variants', 'id', $variant['id'], 'product_variant_order', $position - 1);
                    redirect($this->session->redirect_link);
                } else {
                    $this->general_model->update_table_value('product_variants', 'id', $variant['id'], 'product_variant_order', $position - 1);
                    $this->general_model->update_table_value('product_variants', 'id', $prev_id, 'product_variant_order', $position + 1);
                    redirect($this->session->redirect_link);
                }
            }

            $prev_id = $id;
        }
    }


    public function movedown_variant_order($variant_id, $position)
    {

        $variant = $this->general_model->get_by_where('product_variants', 'id', $variant_id);

        $product_id = $variant->product_id;
        $product_parent_id = $variant->product_parent_id;
        $variants = get_product_variants($product_id, $product_parent_id);

        if ($position == count($variants)) {
            redirect($this->session->redirect_link);
        }

        foreach ($variants as $variant) {
            $id = $variant['id'];

            if ($id == $variant_id) {
                if ($variant['product_variant_order'] == NULL) {
                    $this->general_model->update_table_value('product_variants', 'id', $variant['id'], 'product_variant_order', $position + 1);
                    redirect($this->session->redirect_link);
                } else {
                    $this->general_model->update_table_value('product_variants', 'id', $variant['id'], 'product_variant_order', $position + 1);

                    if ($position == $variant['product_variant_order']) {
                        $this->general_model->update_table_value('product_variants', 'id', $variant['id'], 'product_variant_order', $position - 1);
                        redirect($this->session->redirect_link);
                    }
                    redirect($this->session->redirect_link);
                }
            }
        }
    }

    public function delete_product($product_id)
    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $product_data = $this->general_model->get_by_where('products', 'id', $product_id);

        $result = $this->general_model->delete_request('products', 'id', $product_id);

        if ($result == TRUE) {

            // Delete Product Images

            $path_to_file = 'assets/images/products/' . $product_data->product_image;

            unlink($path_to_file);

            if ($product_data->product_has_gallery == 1) {

                $product_gallery_image_data = $this->general_model->get_all_where('product_images', 'product_id', $product_id);

                foreach ($product_gallery_image_data as $gallery_data) {
                    $path_to_gallery_file = 'assets/images/products/' . $gallery_data['product_gallery_image'];
                    if ($this->general_model->delete_request('product_images', 'id', $gallery_data['id'])) {
                        unlink($path_to_gallery_file);
                    }
                }
            }

            if ($product_data->product_has_variants == 1) {

                $product_gallery_image_data = $this->general_model->get_all_where('product_images', 'product_id', $product_id);

                $this->general_model->delete_request('product_variants', 'product_id', $product_id);
            }

            $flash_data = array(

                'message' => 'Product Deleted!',

            );

            $this->session->set_userdata($flash_data);

            $this->session->mark_as_flash('message');

            redirect($this->session->redirect_link);
        } else {

            $flash_data = array(

                'error' => 'Unable to Delete!',

            );

            $this->session->set_userdata($flash_data);

            $this->session->mark_as_flash('error');

            redirect($this->session->redirect_link);
        }
    }

    public function delete_product_image($image_id)
    {

        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $image_data = $this->general_model->get_by_where('product_images', 'id', $image_id);

        $result = $this->general_model->delete_request('product_images', 'id', $image_id);

        if ($result == TRUE) {

            // Delete Product Images

            $path_to_file = 'assets/images/products/' . $image_data->product_gallery_image;

            unlink($path_to_file);

            $flash_data = array(

                'message' => 'Image Deleted!',

            );

            $this->session->set_userdata($flash_data);

            $this->session->mark_as_flash('message');

            redirect($this->session->redirect_link);
        } else {

            $flash_data = array(

                'error' => 'Unable to Delete!',

            );

            $this->session->set_userdata($flash_data);

            $this->session->mark_as_flash('error');

            redirect($this->session->redirect_link);
        }
    }

    public function contact_queries()

    {        if ($this->session->admin_logged_in == FALSE) {

        redirect(base_url() . 'admin');
    }

        $data['title'] = 'Queries';
        $data['queries'] = $this->general_model->get_all('contact_queries');


        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/queries');
        $this->load->view('admin/_footer');
    }

    public function contact_query($id)

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['query'] = $this->general_model->get_by_where('contact_queries', 'id', $id);

        $update_data = array(
            'query_is_read' => '1',
            'query_read_on' => date('Y-m-d H:i:s'),
        );

        $this->general_model->update_request('contact_queries', 'id', $id, $update_data);

        $data['title'] = 'Query';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/query');
        $this->load->view('admin/_footer');
    }

    public function contact_callbacks()

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['title'] = 'Callbacks';
        $data['callbacks'] = $this->general_model->get_all('contact_callbacks');


        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/callbacks');
        $this->load->view('admin/_footer');
    }

    public function contact_callback($id)

    {
        if ($this->session->admin_logged_in == FALSE) {

            redirect(base_url() . 'admin');
        }

        $data['callback'] = $this->general_model->get_by_where('contact_callbacks', 'id', $id);

        $update_data = array(
            'callback_is_read' => '1',
            'callback_read_on' => date('Y-m-d H:i:s'),
        );

        $this->general_model->update_request('contact_callbacks', 'id', $id, $update_data);

        $data['title'] = 'Callback';

        $this->load->view('admin/_header', $data);
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
        $this->load->view('admin/callback');
        $this->load->view('admin/_footer');
    }
    
    public function create_post() {
        
        $this->load->view('admin/create_post');
    }
    
   public function save_post() {
        $this->load->view('admin/_topbar');
        $this->load->view('admin/_sidebar');
    // Load necessary libraries and models
    $this->load->library('form_validation');
    $this->load->model('Blog_model');

    // Set validation rules
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('content', 'Content', 'required');
    $this->form_validation->set_rules('author', 'Author', 'required');

    // Validate the form
    if ($this->form_validation->run() == FALSE) {
        // If validation fails, reload the create_post view with validation errors
        $this->load->view('admin/create_post');
    } else {
        // If validation is successful, handle image upload
        $config['upload_path']   = './uploads/blogimage/'; // Make sure this folder exists
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size']      = 2048; // 2MB max file size

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

        if ($this->upload->do_upload('image')) {
            // Image uploaded successfully, get the file name
            $upload_data = $this->upload->data();
            $image_filename = $upload_data['file_name'];


 if($_POST['page_url']!=''){
			
				
				$page_url=$_POST['page_url'];
				} else { 
				
			   
			    
			     $page_url=strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $_POST['title'])));
				}

            // Prepare data for inserting into the database
            $data = array(
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'author' => $this->input->post('author'),
                
                'meta_title' => $this->input->post('meta_title'),
                'meta_keyword' => $this->input->post('meta_keyword'),
                'meta_description' => $this->input->post('meta_description'),
                
                
                'image_filename' => $image_filename,
                'page_url' => $page_url,
                'date_published' => date('Y-m-d H:i:s')
            );

            // Save the post to the database
            $this->Blog_model->create_post($data);

            // Redirect to a success message view
            $this->load->view('admin/success_message');
            redirect (base_url('admin/create_post'));
        } else {
            // Image upload failed, reload the create_post view with an error message
            $data['error'] = $this->upload->display_errors();
             redirect (base_url('admin/create_post'));
            $this->load->view('admin/create_post', $data);
        }
    }
}

    
    
}