HEX
Server: Apache/2.4.6 (CentOS) mpm-itk/2.4.7-04 mod_fcgid/2.3.9 PHP/5.4.16
System: Linux dvm.vladweb.ru 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: region-gk.ru (1016)
PHP: 7.3.33
Disabled: NONE
Upload Files
File: //home/temp/yarusvl.ru/api/Reviews.php
<?php

require_once('Mgc.php');

class Reviews extends Mgc {

    public function get_review($id) {
		$query = $this->db->placehold("SELECT r.* FROM __reviews r WHERE id=? LIMIT 1", intval($id));

        if($this->db->query($query)) {
	        return $this->db->result();
        }
        return false;
	}

    public function get_reviews($filter = array()) {
		$limit = 0;
		$page = 1;
		$keyword_filter = '';
		$approved_filter = '';
        $has_parent_filter = '';
        if (isset($filter['has_parent'])) {
            $has_parent_filter = 'and r.parent_id'.($filter['has_parent'] ? '>0' : '=0');
        }

        if(isset($filter['limit'])) {
	        $limit = max(1, intval($filter['limit']));
        }

        if(isset($filter['page'])) {
	        $page = max(1, intval($filter['page']));
        }

        if(isset($filter['ip'])) {
	        $ip = $this->db->placehold("OR r.ip=?", $filter['ip']);
        }

        if(isset($filter['approved'])) {
	        $approved_filter = $this->db->placehold("AND (r.approved=? $ip)", intval($filter['approved']));
        }

	    $sql_limit = '';
        if($limit) {
	        $sql_limit = $this->db->placehold(' LIMIT ?, ? ', ($page - 1) * $limit, $limit);
        }

        if(!empty($filter['keyword'])) {
			$keywords = explode(' ', $filter['keyword']);
            foreach($keywords as $keyword) {
	            $keyword_filter .= $this->db->placehold('AND r.name LIKE "%' . $this->db->escape(trim($keyword)) . '%" OR r.text LIKE "%' . $this->db->escape(trim($keyword)) . '%" ');
            }
		}

		$sort='DESC';

		$query = $this->db->placehold("SELECT r.*
            FROM __reviews r WHERE 1 $keyword_filter $approved_filter $has_parent_filter ORDER BY id $sort $sql_limit");

		$this->db->query($query);
		return $this->db->results();
	}

    public function count_reviews($filter = array()) {
		$approved_filter = '';
		$keyword_filter = '';
        $has_parent_filter = '';
        if (isset($filter['has_parent'])) {
            $has_parent_filter = 'and r.parent_id'.($filter['has_parent'] ? '>0' : '=0');
        }

        if(isset($filter['approved'])) {
	        $approved_filter = $this->db->placehold('AND r.approved=?', intval($filter['approved']));
        }

        if(!empty($filter['keyword'])) {
			$keywords = explode(' ', $filter['keyword']);
			foreach($keywords as $keyword) {
				$keyword_filter .= $this->db->placehold('AND r.name LIKE "%' . $this->db->escape(trim($keyword)) . '%" OR r.text LIKE "%' . $this->db->escape(trim($keyword)) . '%" ');
			}
		}

		$query = $this->db->placehold("SELECT count(distinct r.id) as count
            FROM __reviews r WHERE 1 $keyword_filter $approved_filter $has_parent_filter");

		$this->db->query($query);
		return $this->db->result('count');

	}

    public function add_review($review) {
		$query = $this->db->placehold('INSERT INTO __reviews SET ?%, date = NOW()', $review);
        if(!$this->db->query($query)) {
	        return false;
        }
		$id = $this->db->insert_id();
		return $id;
	}

    public function update_review($id, $review) {
		$date_query = '';
        if(isset($review->date)) {
			$date = $review->date;
			unset($review->date);
			$date_query = $this->db->placehold(', date=STR_TO_DATE(?, ?)', $date, $this->settings->date_format);
		}
		$query = $this->db->placehold("UPDATE __reviews SET ?% $date_query WHERE id in(?@) LIMIT 1", $review, (array)$id);
		$this->db->query($query);
		return $id;
	}

    public function delete_review($id) {
        if(!empty($id)) {
            $this->db->query('SELECT id from __reviews where parent_id=?', intval($id));
            $children = $this->db->results('id');
            foreach($children as $child_id) {
                $this->delete_review($child_id);
            }
            $this->image->delete_image($id, 'image', 'reviews', $this->config->original_reviews_dir, $this->config->resized_reviews_dir);
			$query = $this->db->placehold("DELETE FROM __reviews WHERE id=? LIMIT 1", intval($id));
			$this->db->query($query);
		}
	}
}