| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- /**
- * Report Data Model Class - Handles data structure and validation
- *
- * @package StudiouWCCustomReports
- */
- if (!defined('ABSPATH')) {
- exit;
- }
- class StudiouWC_Report_Data {
-
- private $view;
- private $date_from;
- private $date_to;
- private $statuses;
- private $per_page;
- private $paged;
- private $orderby;
- private $order;
-
- public function __construct($params = array()) {
- $this->view = $params['view'] ?? 'product_categories_summary';
- $this->date_from = $params['date_from'] ?? '';
- $this->date_to = $params['date_to'] ?? date('Y-m-d');
- $this->statuses = $params['statuses'] ?? array();
- $this->per_page = intval($params['per_page'] ?? 20);
- $this->paged = intval($params['paged'] ?? 1);
- $this->orderby = $params['orderby'] ?? 'category';
- $this->order = $params['order'] ?? 'ASC';
-
- $this->validate_params();
- }
-
- private function validate_params() {
- // Validate date format
- if (!empty($this->date_from) && !$this->is_valid_date($this->date_from)) {
- $this->date_from = date('Y-m-d', strtotime('-1 month'));
- }
-
- if (!empty($this->date_to) && !$this->is_valid_date($this->date_to)) {
- $this->date_to = date('Y-m-d');
- }
-
- // Validate order direction
- if (!in_array(strtoupper($this->order), array('ASC', 'DESC'))) {
- $this->order = 'ASC';
- }
-
- // Validate orderby column
- $valid_orderby = array('category', 'orders_count', 'total_price');
- if (!in_array($this->orderby, $valid_orderby)) {
- $this->orderby = 'category';
- }
-
- // Validate per_page limits
- if ($this->per_page < 1 || $this->per_page > 1000) {
- $this->per_page = 20;
- }
-
- // Validate paged
- if ($this->paged < 1) {
- $this->paged = 1;
- }
- }
-
- private function is_valid_date($date) {
- $d = DateTime::createFromFormat('Y-m-d', $date);
- return $d && $d->format('Y-m-d') === $date;
- }
-
- // Getters
- public function get_view() {
- return $this->view;
- }
-
- public function get_date_from() {
- return $this->date_from;
- }
-
- public function get_date_to() {
- return $this->date_to;
- }
-
- public function get_statuses() {
- return $this->statuses;
- }
-
- public function get_per_page() {
- return $this->per_page;
- }
-
- public function get_paged() {
- return $this->paged;
- }
-
- public function get_orderby() {
- return $this->orderby;
- }
-
- public function get_order() {
- return $this->order;
- }
-
- public function to_array() {
- return array(
- 'view' => $this->view,
- 'date_from' => $this->date_from,
- 'date_to' => $this->date_to,
- 'statuses' => $this->statuses,
- 'per_page' => $this->per_page,
- 'paged' => $this->paged,
- 'orderby' => $this->orderby,
- 'order' => $this->order
- );
- }
- }
|