109 lines
3.2 KiB
PHP
109 lines
3.2 KiB
PHP
<?php
|
|
namespace Sodino\Repositories;
|
|
|
|
/**
|
|
* Event Repository
|
|
*/
|
|
class EventRepository {
|
|
private $table_name;
|
|
|
|
public function __construct() {
|
|
global $wpdb;
|
|
$this->table_name = $wpdb->prefix . 'sodino_events';
|
|
}
|
|
|
|
public function insert(array $data) {
|
|
global $wpdb;
|
|
return $wpdb->insert($this->table_name, $data);
|
|
}
|
|
|
|
public function getEvents(array $filters = []) {
|
|
global $wpdb;
|
|
$params = [];
|
|
$where = $this->buildWhereClauses($filters, $params);
|
|
|
|
$sql = "SELECT * FROM {$this->table_name} WHERE " . implode(' AND ', $where) . " ORDER BY created_at ASC";
|
|
return $wpdb->get_results($this->prepareSql($sql, $params), ARRAY_A);
|
|
}
|
|
|
|
public function getCount(array $filters = []) {
|
|
global $wpdb;
|
|
$params = [];
|
|
$where = $this->buildWhereClauses($filters, $params);
|
|
|
|
$sql = "SELECT COUNT(*) FROM {$this->table_name} WHERE " . implode(' AND ', $where);
|
|
return (int) $wpdb->get_var($this->prepareSql($sql, $params));
|
|
}
|
|
|
|
public function getSum($field, array $filters = []) {
|
|
global $wpdb;
|
|
if (!in_array($field, ['value', 'discount_value'], true)) {
|
|
return 0;
|
|
}
|
|
|
|
$params = [];
|
|
$where = $this->buildWhereClauses($filters, $params);
|
|
|
|
$sql = "SELECT SUM({$field}) FROM {$this->table_name} WHERE " . implode(' AND ', $where);
|
|
return floatval($wpdb->get_var($this->prepareSql($sql, $params)));
|
|
}
|
|
|
|
public function getRuleUsageCount($rule_id) {
|
|
global $wpdb;
|
|
return (int) $wpdb->get_var($wpdb->prepare(
|
|
"SELECT COUNT(*) FROM {$this->table_name} WHERE event_type = %s AND rule_id = %d",
|
|
'discount_applied',
|
|
$rule_id
|
|
));
|
|
}
|
|
|
|
private function buildWhereClauses(array $filters, array &$params) {
|
|
$where = ['1=1'];
|
|
|
|
if (!empty($filters['event_type'])) {
|
|
if (is_array($filters['event_type'])) {
|
|
$placeholders = implode(', ', array_fill(0, count($filters['event_type']), '%s'));
|
|
$where[] = "event_type IN ($placeholders)";
|
|
$params = array_merge($params, $filters['event_type']);
|
|
} else {
|
|
$where[] = 'event_type = %s';
|
|
$params[] = $filters['event_type'];
|
|
}
|
|
}
|
|
|
|
if (!empty($filters['product_ids'])) {
|
|
$ids = array_map('intval', (array) $filters['product_ids']);
|
|
$placeholders = implode(', ', array_fill(0, count($ids), '%d'));
|
|
$where[] = "product_id IN ($placeholders)";
|
|
$params = array_merge($params, $ids);
|
|
}
|
|
|
|
if (!empty($filters['rule_id'])) {
|
|
$where[] = 'rule_id = %d';
|
|
$params[] = intval($filters['rule_id']);
|
|
}
|
|
|
|
if (!empty($filters['from'])) {
|
|
$where[] = 'created_at >= %s';
|
|
$params[] = $filters['from'];
|
|
}
|
|
|
|
if (!empty($filters['to'])) {
|
|
$where[] = 'created_at <= %s';
|
|
$params[] = $filters['to'];
|
|
}
|
|
|
|
return $where;
|
|
}
|
|
|
|
private function prepareSql($sql, array $params) {
|
|
global $wpdb;
|
|
|
|
if (empty($params)) {
|
|
return $sql;
|
|
}
|
|
|
|
return $wpdb->prepare($sql, $params);
|
|
}
|
|
}
|