policybot/application/controllers/Category.php

443 lines
12 KiB
PHP
Raw Permalink Normal View History

2021-11-30 10:56:55 +00:00
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
date_default_timezone_set('Asia/Kolkata');
class Category extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Category_model');
}
function list_category()
{
if($this->session->userdata('id')==""){redirect('admin/login');}
$data['cat']=$this->Category_model->list_cat();
$this->load->view('admin/category/list',$data);
}
function add_category()
{
if($this->session->userdata('id')==""){redirect('admin/login');}
if($this->input->post('cat'))
{
$name=$this->input->post('catname');
$short_des=$this->input->post('short_des');
$comparefields=$this->input->post('comparefields');
$questions=$this->input->post('questions');
$options=$this->input->post('options');
$optionmethods=$this->input->post('optionmethods');
$Documents=$this->input->post('Documents');
//make directory
if (!is_dir('uploads/category')) {
mkdir('./uploads/category', 0777, TRUE);
}
$valid_extensions = array('jpeg', 'jpg', 'png'); // valid extensions
$path = 'uploads/category/'; // upload directory
if($_FILES['Image'])
{
$img = $_FILES['Image']['name'];
$tmp = $_FILES['Image']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
// can upload same image using rand function
$final_image = "Image".rand(1000,1000000).$img;
// check's valid format
if(in_array($ext, $valid_extensions))
{
$path = $path.strtolower($final_image);
if(move_uploaded_file($tmp,$path))
{
$table="categories";
$rvalue=array(
'name'=>$name,
'short_description'=>$short_des,
'icon'=>$path,
'cdate'=>date('Y-m-d H:i:s'),
'status'=>1);
$result=$this->commonsql_model->insert_table($table,$rvalue);
$catid=$this->db->insert_id();
} } }
// document details
foreach($Documents as $ke=>$val)
{
$tblname="documents_master";
if (is_numeric($Documents[$ke])){
$did = $Documents[$ke];
}
else{
$values=array('name'=>ucwords($Documents[$ke]),
'status'=>1);
if($Documents[$ke]!="") {
$result=$this->commonsql_model->insert_table($tblname,$values);
}
$did = $this->db->insert_id();
}
$tblnames="category_document_details";
$value=array(
'category'=>$catid,
'document'=>$did ,
'status'=>1);
if($catid!="" && $did!="") {
$result=$this->commonsql_model->insert_table($tblnames,$value);
}
}
// compare details
foreach($comparefields as $ke=>$val)
{
$tblname="category_compare_master";
if (is_numeric($comparefields[$ke])){
$cid = $comparefields[$ke];
}
else{
$values=array('compare_name'=>ucwords($comparefields[$ke]),
'status'=>1);
if($comparefields[$ke]!="") {
$result=$this->commonsql_model->insert_table($tblname,$values);
}
$cid = $this->db->insert_id();
}
$tblnames="category_compare_detail";
$value=array(
'category'=>$catid,
'compare'=>$cid ,
'status'=>1);
if($catid!="" && $cid!="") {
$result=$this->commonsql_model->insert_table($tblnames,$value);
}
}
foreach($questions as $que=>$que_va)
{
$optionsnew=$options[$que];$opti=array();
foreach($optionsnew as $opt=>$optval)
{
$opti[]=$options[$que][$opt];
}$opt_res= implode('|', $opti);
$tblname="category_questions_before_quote";
$value=array(
'category'=>$catid,
'question'=>$questions[$que],
'option_input_type'=>$optionmethods[$que],
'options'=>$opt_res,
'status'=>1);
if($questions[$que]!=""){
$result=$this->commonsql_model->insert_table($tblname,$value);
}
}
if($result)
{
$this->session->set_userdata('suc','successfully added');
redirect('admin/category');
}
else
{
$this->session->set_userdata('err','Please try again');
redirect('admin/category');
}
}
$data['catcom']=$this->Category_model->get_compare_master();
$data['doccom']=$this->Category_model->get_document_master();
$this->load->view('admin/category/add',$data);
}
function cataction()
{
if($this->session->userdata('id')==""){redirect('admin/login');}
$type=$_POST['type']; //types like view, edit etc.,
$id=$_POST['id'];
//selecting table
$tableName="categories";
$select=array('id','name','icon');
$where=array('id'=>$id);
$data['cat']=$this->commonsql_model->selectTable($tableName,$where,$select);
$data['type']=$type;
$this->load->view('admin/category/cataction',$data);
}
function view_category($id)
{
if($this->session->userdata('id')==""){redirect('admin/login');}
$data['cat']=$this->Category_model->list_cat($id);
$data['com']=$this->Category_model->get_compare_details($id);
$data['quest']=$this->Category_model->questions_before_quote($id);
$data['doc']=$this->Category_model->get_document_details($id);
$this->load->view('admin/category/view',$data);
}
function edit_category($id)
{
if($this->session->userdata('id')==""){redirect('admin/login');}
if($this->input->post('cat'))
{
$name=$this->input->post('cmpname');
$oldimage=$this->input->post("old_image");
$image=$this->input->post("Image");
$short_des=$this->input->post("short_des");
$comparefields=$this->input->post("comparefields");
$Documentsfields=$this->input->post("Documents");
$hiddenprimary=$this->input->post("hiddenprimary");
$questions=$this->input->post('questions');
$options=$this->input->post('options');
$optionmethods=$this->input->post('optionmethods');
if($Image=="") {
$simage=$oldimage;
}
if (!is_dir('uploads/category')) {
mkdir('./uploads/category', 0777, TRUE);
}
$valid_extensions = array('jpeg', 'jpg', 'png'); // valid extensions
$path = 'uploads/category/'; // upload directory
if($_FILES['Image']['tmp_name'])
{
$img = $_FILES['Image']['name'];
$tmp = $_FILES['Image']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
// can upload same image using rand function
$final_image = "Image".rand(1000,1000000).$img;
// check's valid format
if(in_array($ext, $valid_extensions))
{
$simage = $path.strtolower($final_image);
if(move_uploaded_file($tmp,$simage))
{}} }
$tblname="categories";
$wheredata=(array('id'=>$id));
$values=array(
'name'=>$name,
'short_description'=>$short_des,
'icon'=>$simage,
'udate'=>date('Y-m-d H:i:s'),
);
$result=$this->commonsql_model->updateTable($tblname,$wheredata,$values);
/* comparision edit */
$exist_tag=$this->Category_model->get_compare_details($id);
if($exist_tag->num_rows()>0)
{
$oldcompare=array();
foreach($exist_tag->result() as $list)
{
$oldcompare[]=$list->compare ;
}
}
//different1
$diff=array_diff($oldcompare,$comparefields);
if($diff>0){
foreach($diff as $value){
$where=array('compare'=>$value,'category'=>$id);
$tablename="category_compare_detail";
$values=array('status'=>0);
$product=$this->commonsql_model->updateTable($tablename,$where,$values);
}
}
//new one
$diffnew=array_diff($comparefields,$oldcompare);
if($diffnew>0)
{
foreach ($diffnew as $new_tag)
{
$tblname="category_compare_master";
if (is_numeric($new_tag)){
$cid = $new_tag;
}
else{
$values=array('compare_name'=>ucwords($new_tag),
'status'=>1);
if($new_tag!="") {
$result=$this->commonsql_model->insert_table($tblname,$values);
}
$cid = $this->db->insert_id();
}
$tblnames="category_compare_detail";
$value=array(
'category'=>$id,
'compare'=>$cid ,
'status'=>1);
if($id!="" && $cid!="") {
$result=$this->commonsql_model->insert_table($tblnames,$value);
}
}
}
/* end of comparision edit */
/* Documentsfields edit */
$exist_tags=$this->Category_model->get_document_details($id);
if($exist_tags->num_rows()>0)
{
$olddocument=array();
foreach($exist_tags->result() as $lists)
{
$olddocument[]=$lists->document ;
}
}
//different1
$diffs=array_diff($olddocument,$Documentsfields);
if($diffs>0){
foreach($diffs as $value){
$where=array('document'=>$value,'category'=>$id);
$tablename="category_document_details";
$values=array('status'=>0);
$product=$this->commonsql_model->updateTable($tablename,$where,$values);
}
}
//new one
$diffnews=array_diff($Documentsfields,$olddocument);
if($diffnews>0)
{
foreach ($diffnews as $new_tags)
{
$tblname="documents_master";
if (is_numeric($new_tags)){
$cdid = $new_tags;
}
else{
$values=array('name'=>ucwords($new_tags),
'status'=>1);
if($new_tags!="") {
$result=$this->commonsql_model->insert_table($tblname,$values);
}
$cdid = $this->db->insert_id();
}
$tblnames="category_document_details";
$value=array(
'category'=>$id,
'document'=>$cdid ,
'status'=>1);
if($id!="" && $cdid!="") {
$result=$this->commonsql_model->insert_table($tblnames,$value);
}
}
}
/* end of comparision edit */
/* delete entire row minus */
foreach($questions as $que=>$que_va)
{
$optionsnew=$options[$que];$opti=array();
foreach($optionsnew as $opt=>$optval)
{
$opti[]=$options[$que][$opt];
}$opt_res= implode('|', $opti);
$tblname="category_questions_before_quote";
$value=array(
'category'=>$id,
'question'=>$questions[$que],
'option_input_type'=>$optionmethods[$que],
'options'=>$opt_res,
'status'=>1);
if($hiddenprimary[$que]==""){
if($questions[$que]!=""){
$result=$this->commonsql_model->insert_table($tblname,$value);
}
}
if($hiddenprimary[$que]!=""){
$where=array("id"=>$hiddenprimary[$que]);
$result=$this->commonsql_model->updateTable($tblname,$where,$value);
}
}
/* end of delete entire row minus */
if($result)
{
$this->session->set_userdata('suc','successfully Updated');
redirect('admin/category');
}
else
{
$this->session->set_userdata('err','Please try again');
redirect('admin/category');
}
}
$data['catcom']=$this->Category_model->get_compare_master();
$data['doccom']=$this->Category_model->get_document_master();
$data['cat']=$this->Category_model->list_cat($id);
$data['com']=$this->Category_model->get_compare_details($id);
$data['quest']=$this->Category_model->questions_before_quote($id);
$data['doc']=$this->Category_model->get_document_details($id);
$this->load->view('admin/category/edit',$data);
}
function delete_category($id)
{
if($this->session->userdata('id')==""){redirect('admin/login');}
if($this->input->post('yes'))
{
$table="categories";
//echo $ledgername;
$values=array(
'status'=>0);
$where=array("id"=>$id);
$result=$this->commonsql_model->updatetable($table,$where,$values);
if($result)
{
$this->session->set_userdata('suc','successfully deleted');
redirect('admin/category');
}
else
{
$this->session->set_userdata('err','Please try again');
redirect('admin/category');
}
}
}
function delete_minus_questions()
{
$rid=$_POST['rid'];
$table="category_questions_before_quote";
//echo $ledgername;
$values=array(
'status'=>0);
$where=array("id"=>$rid);
$result=$this->commonsql_model->updatetable($table,$where,$values);
if($result)
{
echo "1";
}
else
{
echo "2";
}
}
}
?>