Файл: Just Wallet 2.0.4/application/models/Vouchers_model.php
Строк: 73
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Vouchers_model extends CI_Model {
/**
* @vars
*/
private $_db;
/**
* Constructor
*/
function __construct()
{
parent::__construct();
// define primary table
$this->_db = 'vouchers';
}
function get_all($limit = 0, $offset = 0, $filters = array(), $sort = 'dir', $dir = 'ASC')
{
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
WHERE id > '0'
";
if ( ! empty($filters))
{
foreach ($filters as $key=>$value)
{
$value = $this->db->escape('%' . $value . '%');
$sql .= " AND {$key} LIKE {$value}";
}
}
$sql .= " ORDER BY {$sort} {$dir}";
if ($limit)
{
$sql .= " LIMIT {$offset}, {$limit}";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
$results['results'] = $query->result_array();
}
else
{
$results['results'] = NULL;
}
$sql = "SELECT FOUND_ROWS() AS total";
$query = $this->db->query($sql);
$results['total'] = $query->row()->total;
return $results;
}
function get_active($limit = 0, $offset = 0, $filters = array(), $sort = 'dir', $dir = 'ASC')
{
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
WHERE status = '2'
";
if ( ! empty($filters))
{
foreach ($filters as $key=>$value)
{
$value = $this->db->escape('%' . $value . '%');
$sql .= " AND {$key} LIKE {$value}";
}
}
$sql .= " ORDER BY {$sort} {$dir}";
if ($limit)
{
$sql .= " LIMIT {$offset}, {$limit}";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
$results['results'] = $query->result_array();
}
else
{
$results['results'] = NULL;
}
$sql = "SELECT FOUND_ROWS() AS total";
$query = $this->db->query($sql);
$results['total'] = $query->row()->total;
return $results;
}
function get_pending($limit = 0, $offset = 0, $filters = array(), $sort = 'dir', $dir = 'ASC')
{
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
WHERE status = '1'
";
if ( ! empty($filters))
{
foreach ($filters as $key=>$value)
{
$value = $this->db->escape('%' . $value . '%');
$sql .= " AND {$key} LIKE {$value}";
}
}
$sql .= " ORDER BY {$sort} {$dir}";
if ($limit)
{
$sql .= " LIMIT {$offset}, {$limit}";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
$results['results'] = $query->result_array();
}
else
{
$results['results'] = NULL;
}
$sql = "SELECT FOUND_ROWS() AS total";
$query = $this->db->query($sql);
$results['total'] = $query->row()->total;
return $results;
}
function get_user_vouchers($limit = 0, $offset = 0, $filters = array(), $sort = 'dir', $dir = 'ASC', $user = NULL)
{
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
WHERE (creator = '{$user}' OR activator = '{$user}')
";
if ( ! empty($filters))
{
foreach ($filters as $key=>$value)
{
$value = $this->db->escape('%' . $value . '%');
$sql .= " AND {$key} LIKE {$value}";
}
}
$sql .= " ORDER BY {$sort} {$dir}";
if ($limit)
{
$sql .= " LIMIT {$offset}, {$limit}";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
$results['results'] = $query->result_array();
}
else
{
$results['results'] = NULL;
}
$sql = "SELECT FOUND_ROWS() AS total";
$query = $this->db->query($sql);
$results['total'] = $query->row()->total;
return $results;
}
function get_disapproved($limit = 0, $offset = 0, $filters = array(), $sort = 'dir', $dir = 'ASC')
{
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
WHERE status = '3'
";
if ( ! empty($filters))
{
foreach ($filters as $key=>$value)
{
$value = $this->db->escape('%' . $value . '%');
$sql .= " AND {$key} LIKE {$value}";
}
}
$sql .= " ORDER BY {$sort} {$dir}";
if ($limit)
{
$sql .= " LIMIT {$offset}, {$limit}";
}
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
$results['results'] = $query->result_array();
}
else
{
$results['results'] = NULL;
}
$sql = "SELECT FOUND_ROWS() AS total";
$query = $this->db->query($sql);
$results['total'] = $query->row()->total;
return $results;
}
function add_voucher($data)
{
$this->db->insert("vouchers", $data);
return $this->db->insert_id();
}
function get_vouchers($id = NULL)
{
if ($id)
{
$sql = "
SELECT *
FROM {$this->_db}
WHERE id = " . $this->db->escape($id) . "
";
$query = $this->db->query($sql);
if ($query->num_rows())
{
return $query->row_array();
}
}
return FALSE;
}
/**
* Edit ticket
*
* @param array $data
* @return boolean
*/
function edit_voucher($data = array())
{
if ($data)
{
$sql = "
UPDATE {$this->_db}
SET
status = " . $this->db->escape($data['status']) . ",
code = " . $this->db->escape($data['code']) . ",
activator = " . $this->db->escape($data['activator']) . ",
date_creature = " . $this->db->escape($data['date_creature']) . ",
creator = " . $this->db->escape($data['creator']) . ",
amount = " . $this->db->escape($data['amount']) . ",
date_activation = " . $this->db->escape($data['date_activation']) . "
WHERE id = " . $this->db->escape($data['id']) . "
";
$this->db->query($sql);
if ($this->db->affected_rows())
{
return TRUE;
}
}
return FALSE;
}
function validate_code($code = NULL)
{
if ($code)
{
$sql = "
SELECT *
FROM {$this->_db}
WHERE code = " . $this->db->escape($code) . " AND status = 1
";
$query = $this->db->query($sql);
if ($query->num_rows())
{
return $query->row_array();
}
}
return FALSE;
}
function update_voucher($code, $data) {
$this->db->where("code", $code)->update("vouchers", $data);
}
// total vouchers ////////////////////////////////////////////
function total_dash_vouchers()
{
$s= $this->db->select("COUNT(*) as num")->get("vouchers");
$r = $s->row();
if(isset($r->num)) return $r->num;
return 0;
return $result[0]->Vouchers;
}
/**
* Update status
*
* @param array $data
* @return boolean
*/
function update_merchant($id, $data) {
$this->db->where("ID", $id)->update("merchants", $data);
}
}