<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Class Category_model
*/
class Category_model extends MY_Model
{
/**
* @var string - Categories table name.
*/
private $tableName = 'categories';
/**
* Category_model constructor.
* -------------------------------------------------------------------
*
*/
public function __construct()
{
}
// -------------------------------------------------------------------
/**
* getCategory ()
* -------------------------------------------------------------------
*
* @param $id
* @return array
*/
public function getCategory($id)
{
$data = [];
$options = ['id' => $id];
$query = $this->db->getwhere($this->tableName, $options, 1);
if ($query->num_rows() > 0)
{
$data[] = $query->row_array();
}
$query->free_result();
return $data;
}
// -------------------------------------------------------------------
/**
* getAllCategories ()
* -------------------------------------------------------------------
*
* @return array
*/
public function getAllCategories()
{
$data = [];
$query = $this->db->get($this->tableName);
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
// -------------------------------------------------------------------
/**
* getCategoriesDropDown ()
* -------------------------------------------------------------------
*
* @return array
*/
public function getCategoriesDropDown()
{
$data = [];
$query = $this->db->select('id, title')
->get($this->tableName);
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['id']] = $row['title'];
}
}
$query->free_result();
return $data;
}
// -------------------------------------------------------------------
/**
* getTopCategories ()
* -------------------------------------------------------------------
*
* @return mixed
*/
function getTopCategories()
{
$data = array();
$query = $this->db->where('parent_id', 0)
->get($this->tableName);
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['id']] = $row['title'];
}
}
$query->free_result();
return $data;
}
// -------------------------------------------------------------------
/**
* addCategory ()
* -------------------------------------------------------------------
*
*/
function addCategory()
{
$data = [
'title' => str_replace(" ", "_", $this->input->post('title', true)),
'short_desc' => $this->input->post('short_desc', true),
'long_desc' => $this->input->post('long_desc', true),
'status' => $this->input->post('status', true),
'sort_order' => $this->input->post('sort_order', true)
];
$this->db->insert($this->tableName, $data);
}
// -------------------------------------------------------------------
/**
* updateCategory ()
* -------------------------------------------------------------------
*
*/
function updateCategory()
{
$data = [
'title' => str_replace(" ", "_", $this->input->post('title', true)),
'short_desc' => $this->input->post('short_desc', true),
'long_desc' => $this->input->post('long_desc', true),
'status' => $this->input->post('status', true),
'sort_order' => $this->input->post('sort_order', true)
];
$this->db->where('id', $this->input->post('id', true));
$this->db->update($this->tableName, $data);
}
// -------------------------------------------------------------------
/**
* deleteCategory ()
* -------------------------------------------------------------------
*
* @param $id
*/
function deleteCategory($id)
{
$data = ['status' => 'inactive'];
$this->db->where('id', $id)
->update($this->tableName, $data);
}
}
/**
* -----------------------------------------------------------------------
* Filename: Category_model.php
* Location: ./application/models/Category_model.php
* -----------------------------------------------------------------------
*/