/+modules/AdminQuickEditPro-5.6.0/upload/admin/model/sale/aqe/voucher_theme.php
https://bitbucket.org/DidenkoDima/opta · PHP · 152 lines · 127 code · 25 blank · 0 comment · 78 complexity · fe409873cdb9cf5ab375331496ddaa2c MD5 · raw file
- <?php
- class ModelSaleAqeVoucherTheme extends Model {
- protected static $count = 0;
- public function getVoucherThemes($data = array()) {
- if (isset($data['columns'])) {
- $columns = $data['columns'];
- } else {
- $columns = array('name');
- }
- $sql = "SELECT SQL_CALC_FOUND_ROWS vt.*";
- if (in_array("name", $columns)) {
- $sql .= ", vtd.name AS name";
- }
- $sql .= " FROM `" . DB_PREFIX . "voucher_theme` vt";
- if (in_array("name", $columns)) {
- $sql .= " LEFT JOIN " . DB_PREFIX . "voucher_theme_description vtd ON (vt.voucher_theme_id = vtd.voucher_theme_id AND vtd.language_id = '" . (int)$this->config->get('config_language_id') . "')";
- }
- $where = array();
- $int_filters = array(
- 'id' => 'vt.voucher_theme_id',
- );
- foreach ($int_filters as $key => $value) {
- if (isset($data["filter_$key"]) && !is_null($data["filter_$key"])) {
- $where[] = "$value = '" . (int)$data["filter_$key"] . "'";
- }
- }
- $anywhere_filters = array(
- 'name' => 'vtd.name',
- );
- foreach ($anywhere_filters as $key => $value) {
- if (!empty($data["filter_$key"])) {
- if ($this->config->get('aqe_match_anywhere')) {
- $where[] = "$value LIKE '%" . $this->db->escape($data["filter_$key"]) . "%'";
- } else {
- $where[] = "$value LIKE '" . $this->db->escape($data["filter_$key"]) . "%'";
- }
- }
- }
- if ($where) {
- $sql .= " WHERE " . implode($where, " AND ");
- }
- $sql .= " GROUP BY vt.voucher_theme_id";
- $sort_data = array(
- 'vt.voucher_theme_id',
- 'vtd.name',
- );
- if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
- $sql .= " ORDER BY " . $data['sort'];
- } else {
- $sql .= " ORDER BY vtd.name";
- }
- if (isset($data['order']) && ($data['order'] == 'DESC')) {
- $sql .= " DESC";
- } else {
- $sql .= " ASC";
- }
- if (isset($data['start']) || isset($data['limit'])) {
- if ($data['start'] < 0) {
- $data['start'] = 0;
- }
- if ($data['limit'] < 1) {
- $data['limit'] = 20;
- }
- $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
- }
- $query = $this->db->query($sql);
- $count = $this->db->query("SELECT FOUND_ROWS() AS count");
- $this->count = ($count->num_rows) ? (int)$count->row['count'] : 0;
- return $query->rows;
- }
- public function getTotalVoucherThemes() {
- return $this->count;
- }
- public function quickEditVoucherTheme($voucher_theme_id, $column, $value, $lang_id=null, $data=null) {
- $editable = array('image');
- $result = false;
- if (in_array($column, $editable)) {
- if ($column == 'image') {
- $result = $this->db->query("UPDATE `" . DB_PREFIX . "voucher_theme` SET " . $column . " = '" . $this->db->escape($value) . "' WHERE voucher_theme_id = '" . (int)$voucher_theme_id . "'");
- }
- } else if ($column == 'name') {
- if (isset($data['value']) && is_array($data['value'])) {
- foreach ((array)$data['value'] as $language_id => $value) {
- $this->db->query("UPDATE " . DB_PREFIX . "voucher_theme_description SET " . $column . " = '" . $this->db->escape($value) . "' WHERE voucher_theme_id = '" . (int)$voucher_theme_id . "' AND language_id = '" . (int)$language_id . "'");
- }
- $result = 1;
- } else if ($value) {
- $result = $this->db->query("UPDATE " . DB_PREFIX . "voucher_theme_description SET " . $column . " = '" . $this->db->escape($value) . "' WHERE voucher_theme_id = '" . (int)$voucher_theme_id . "' AND language_id = '" . (int)$lang_id . "'");
- $result = 1;
- } else {
- $result = 0;
- }
- }
- return $result;
- }
- public function filterInterval($filter, $field, $date=false) {
- if ($date) {
- if (preg_match('/^(!=|<>)\s*(\d{2,4}-\d{1,2}-\d{1,2})$/', html_entity_decode(trim($filter)), $matches) && count($matches) == 3) {
- return "DATE($field) <> DATE('" . $matches[2] . "')";
- } else if (preg_match('/^(\d{2,4}-\d{1,2}-\d{1,2})\s*(<|<=)\s*(\d{2,4}-\d{1,2}-\d{1,2})$/', html_entity_decode(trim($filter)), $matches) && count($matches) == 4 && strtotime($matches[1]) <= strtotime($matches[3])) {
- return "DATE('" . $matches[1] . "') ${matches[2]} DATE($field) AND DATE($field) ${matches[2]} DATE('" . $matches[3] . "')";
- } else if (preg_match('/^(\d{2,4}-\d{1,2}-\d{1,2})\s*(>|>=)\s*(\d{2,4}-\d{1,2}-\d{1,2})$/', html_entity_decode(trim($filter)), $matches) && count($matches) == 4 && strtotime($matches[1]) >= strtotime($matches[3])) {
- return "DATE('" . $matches[1] . "') ${matches[2]} DATE($field) AND DATE($field) ${matches[2]} DATE('" . $matches[3] . "')";
- } else if (preg_match('/^(<|<=|>|>=)\s*(\d{2,4}-\d{1,2}-\d{1,2})$/', html_entity_decode(trim($filter)), $matches) && count($matches) == 3) {
- return "DATE($field) ${matches[1]} DATE('" . $matches[2] . "')";
- } else if (preg_match('/^(\d{2,4}-\d{1,2}-\d{1,2})\s*(>|>=|<|<=)$/', html_entity_decode(trim($filter)), $matches) && count($matches) == 3) {
- return "DATE('" . $matches[1] . "') ${matches[2]} DATE($field)";
- } else {
- return "DATE(${field}) = DATE('${filter}')";
- }
- } else {
- if (preg_match('/^(!=|<>)\s*(-?\d+\.?\d*)$/', html_entity_decode(trim(str_replace(",", ".", $filter))), $matches) && count($matches) == 3) {
- return "$field <> '" . (float)$matches[2] . "'";
- } else if (preg_match('/^(-?\d+\.?\d*)\s*(<|<=)\s*(-?\d+\.?\d*)$/', html_entity_decode(trim(str_replace(",", ".", $filter))), $matches) && count($matches) == 4 && (float)$matches[1] <= (float)$matches[3]) {
- return "'" . (float)$matches[1] . "' ${matches[2]} $field AND $field ${matches[2]} '" . (float)$matches[3] . "'";
- } else if (preg_match('/^(-?\d+\.?\d*)\s*(>|>=)\s*(-?\d+\.?\d*)$/', html_entity_decode(trim(str_replace(",", ".", $filter))), $matches) && count($matches) == 4 && (float)$matches[1] >= (float)$matches[3]) {
- return "'" . (float)$matches[1] . "' ${matches[2]} $field AND $field ${matches[2]} '" . (float)$matches[3] . "'";
- } else if (preg_match('/^(<|<=|>|>=)\s*(-?\d+\.?\d*)$/', html_entity_decode(trim(str_replace(",", ".", $filter))), $matches) && count($matches) == 3) {
- return "$field ${matches[1]} '" . (float)$matches[2] . "'";
- } else if (preg_match('/^(-?\d+\.?\d*)\s*(>|>=|<|<=)$/', html_entity_decode(trim(str_replace(",", ".", $filter))), $matches) && count($matches) == 3) {
- return "'" . (float)$matches[1] . "' ${matches[2]} $field";
- } else {
- return $field . " = '" . $this->db->escape($filter) . "'";
- }
- }
- }
- }