File: //home/bk/efi/eficenter.ru/consultant/class/connection_operator.php
<?php
header("Content-type: application/json"); class ConnectionOperator{ private $mysql = null; public function __construct(){ require_once 'mysql.php'; $this->mysql = Mysql::getInstance(); $this->addOffline(); } public function getOperator($id_operator){ $id_operator = intval($id_operator); $sql = "SELECT operator_id, operator_name, operator_surname, operator_photo, operator_otdel, operator_messages FROM ok_operators WHERE operator_id = {$id_operator}"; $result = $this->mysql->query($sql); if($result->columnCount() == 0){ $this->connOperator(); }else{ $operator = $result->fetch(PDO::FETCH_ASSOC); $operator['online'] = true; $operator['operator_otdel'] = $this->getOtgel($operator['operator_otdel']); $_SESSION['ok_conn_operator'] = $operator['operator_id']; echo json_encode($operator); } } public function getFreeOperator(){ $sql = "SELECT operator_id, operator_name, operator_surname, operator_photo, operator_otdel, operator_messages FROM ok_operators WHERE operator_online = '1' AND operator_limit > operator_connected ORDER BY operator_connected LIMIT 1"; $result = $this->mysql->query($sql); if($result->columnCount() == 0){ return false; }else{ return $result->fetch(PDO::FETCH_ASSOC); } } public function connOperator(){ if($operator = $this->getFreeOperator()){ $_SESSION['ok_conn_operator'] = $operator['operator_id']; $operator['online'] = true; $operator['operator_otdel'] = $this->getOtgel($operator['operator_otdel']); echo json_encode($operator); exit; }else{ $operator['online'] = false; echo json_encode($operator); exit; } } public function connGroupOperator($id_group) { $id_group = intval($id_group); $sql = "SELECT operator_id, operator_name, operator_surname, operator_photo, operator_otdel, operator_messages FROM ok_operators WHERE operator_online = '1' AND operator_limit > operator_connected AND operator_otdel = {$id_group} ORDER BY operator_connected LIMIT 1"; $result = $this->mysql->query($sql); if($result->columnCount() == 0){ $this->connOperator(); }else{ $operator = $result->fetch(PDO::FETCH_ASSOC); $operator['online'] = true; $operator['operator_otdel'] = $this->getOtgel($operator['operator_otdel']); $_SESSION['ok_conn_operator'] = $operator['operator_id']; echo json_encode($operator); } } public function getOtgel($id) { $sql = "SELECT * FROM ok_group WHERE group_id = {$id}"; $result = $this->mysql->query($sql); if($result->columnCount() == 0){ return 'Консультант'; }else{ $group = $result->fetch(PDO::FETCH_ASSOC); return $group['group_name']; } } public function addOffline(){ $deltime = time() - 120; $sql = "UPDATE ok_operators SET operator_online = '0', operator_connected = 0 WHERE operator_ltime < '{$deltime}'"; $this->mysql->exec($sql); } } if(!isset($_SESSION)) session_start(); $conn = new ConnectionOperator(); if(!empty($_POST['id_operator'])){ $conn->getOperator($_POST['id_operator']); }elseif($_POST['id_group']){ $conn->connGroupOperator($_POST['id_group']); }else{ $conn->connOperator(); }