lk.zachem.info/app/Modules/Plan7/Http/Livewire/Plan7SelectorLivewire.php

135 lines
4.4 KiB
PHP

<?php
namespace Modules\Plan7\Http\Livewire;
use Livewire\Component;
use Modules\Main\Models\Complex;
class Plan7SelectorLivewire extends Component
{
public $mode = 'new';
public $complexId;
public $containerId = 'plan7__selector';
public $filter = [];
public $allObjects;
public $types;
public $access;
public $summary;
public $room = false;
public $objects = [];
private $apiSummary;
private $apiAll;
public function mount()
{
$this->containerId = $this->containerId . '__' . $this->complexId;
if ($plan7ApiData = Complex::find($this->complexId)->getPlan7ApiData()) {
$this->apiSummary = 'https://plan7.ru/catalog/exp/json/summary/?token=' . $plan7ApiData->token . '&zk=' . $plan7ApiData->zk;
$this->apiAll = 'https://plan7.ru/catalog/exp/json/?token=' . $plan7ApiData->token . '&zk=' . $plan7ApiData->zk;
$this->getSummary();
$this->getData();
}
}
public function updated($property, $value)
{
if ($property === 'complexId') {
$this->mount();
}
}
private function getSummary() {
$URL = $this->apiSummary;
$opt = [];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$fields = http_build_query($opt);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$out = curl_exec($curl);
$this->summary = json_decode($out, true);
curl_close($curl);
}
private function getData()
{
$URL = $this->apiAll;
$opt = [];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$fields = http_build_query($opt);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$out = curl_exec($curl);
$this->allObjects = json_decode($out, true);
$this->types = $this->allObjects['values']['type'];
$this->access = $this->allObjects['values']['access'];
curl_close($curl);
}
public function setHouse($id)
{
$this->filter['bs'] = $id;
}
public function setRoom($id)
{
$this->room = $this->allObjects['data'][$id];
//$this->filter['id'] = $id;
}
public function unsetRoom() {
$this->room = false;
}
public function resetFilter() {
$this->filter = [];
}
public function start() {
$this->mode = 'select';
$this->filter = [];
}
public function done() {
$this->mode = 'selected';
$this->filter = [];
}
public function render()
{
$objs = [];
if (count($this->filter)) {
foreach ($this->allObjects['data'] as $key => $room) {
if ($room['access'] != 0) {
if (array_key_exists('id', $this->filter) && $this->filter['id'] != null && $room['id'] != $this->filter['id']) {
continue;
}
if (array_key_exists('bs', $this->filter) && $this->filter['bs'] != null && $room['bs'] != $this->filter['bs']) {
continue;
}
if (array_key_exists('type', $this->filter) && $this->filter['type'] != null && $room['type'] != $this->filter['type']) {
continue;
}
if (array_key_exists('min_area', $this->filter) && $this->filter['min_area'] != null && $room['area'] < $this->filter['min_area']) {
continue;
}
if (array_key_exists('max_area', $this->filter) && $this->filter['max_area'] != null && $room['area'] > $this->filter['max_area']) {
continue;
}
$room['mode'] = 'room';
$objs[] = $room;
}
}
} else {
foreach ($this->allObjects['values']['bs'] as $key => $house) {
$objs[] = [
'mode' => 'bs',
'id' => $house['id'],
'name' => $house['name'],
];
}
}
return view('plan7::livewire.selector', [
'objs' => $objs
]);
}
}