<?php
namespace SHServ\Controllers;
use \SHServ\Models\Areas;
use \SHServ\Entities\Area;
class AreasRESTAPIController extends \SHServ\Middleware\Controller {
public function areas_list($area_id = 0) {
$areas_model = new Areas();
if(!$area_id) {
$areas = $areas_model -> get_all();
} else {
$parent_area = new Area(intval($area_id));
if(!$parent_area) {
return $this -> utils() -> response_error("parent_area_not_found");
}
$areas = $parent_area -> get_inner_areas();
}
$response = [];
foreach($areas as $i => $area) {
$response[] = $area -> to_array();
}
return $this -> utils() -> response_success([
"areas" => $response,
"total" => count($response)
]);
}
public function new_area($type, $alias, $display_name) {
$areas_model = new Areas();
if(!$areas_model -> alias_is_uniq($alias)) {
return $this -> utils() -> response_error("alias_already_exists", ["alias"]);
}
if(!strlen($type)) {
return $this -> utils() -> response_error("empty_field", ["type"]);
}
if(!strlen($display_name)) {
return $this -> utils() -> response_error("empty_field", ["display_name"]);
}
$area = $areas_model -> create_new_area($type, $alias, $display_name);
if(!$area) {
return $this -> utils() -> response_error("area_not_exists");
}
return $this -> utils() -> response_success([
"alias" => $area -> alias,
"area" => $area -> to_array()
]);
}
public function remove_area($area_id) {
if($area_id != intval($area_id) or intval($area_id) <= 1) {
return $this -> utils() -> response_error("invalid_id", ["area_id"]);
}
$area = new Area(intval($area_id));
if(!$area) {
return $this -> utils() -> response_error("area_not_exists");
}
if(!$area -> remove()) {
return $this -> utils() -> response_error("undefined_error");
}
return $this -> utils() -> response_success();
}
public function place_in_area($target_area_id, $place_in_area_id) {
if($target_area_id != intval($target_area_id) or intval($target_area_id) <= 1) {
return $this -> utils() -> response_error("invalid_id", ["target_area_id"]);
}
if($place_in_area_id != intval($place_in_area_id) or intval($place_in_area_id) <= 1) {
return $this -> utils() -> response_error("invalid_id", ["place_in_area_id"]);
}
$target_area = new Area(intval($target_area_id));
$place_area = new Area(intval($place_in_area_id));
if(!$target_area or !$place_area) {
return $this -> utils() -> response_error("area_not_exists");
}
if(!$target_area -> place_in_area($place_area)) {
return $this -> utils() -> response_error("undefined_error");
}
return $this -> utils() -> response_success();
}
public function update_display_name($area_id, $display_name) {
if($area_id != intval($area_id) or intval($area_id) <= 1) {
return $this -> utils() -> response_error("invalid_id");
}
if(!strlen($display_name)) {
return $this -> utils() -> response_error("empty_field", ["display_name"]);
}
$area = new Area(intval($area_id));
if(!$area) {
return $this -> utils() -> response_error("area_not_exists");
}
$area -> display_name = $display_name;
return $area -> update()
? $this -> utils() -> response_success([
"alias" => $area -> alias,
"area" => $area -> to_array()
])
: $this -> utils() -> response_error("undefined_error");
}
public function update_alias($area_id, $new_alias) {
if($area_id != intval($area_id) or intval($area_id) <= 1) {
return $this -> utils() -> response_error("invalid_id", ["area_id"]);
}
if(!strlen($new_alias)) {
return $this -> utils() -> response_error("empty_field", ["new_alias"]);
}
$areas_model = new Areas();
if(!$areas_model -> alias_is_uniq($new_alias)) {
return $this -> utils() -> response_error("alias_already_exists", ["alias"]);
}
$area = new Area(intval($area_id));
if(!$area) {
return $this -> utils() -> response_error("area_not_exists");
}
$area -> alias = $new_alias;
return $area -> update()
? $this -> utils() -> response_success([
"alias" => $area -> alias,
"area" => $area -> to_array()
])
: $this -> utils() -> response_error("undefined_error");
}
public function reboot_devices($area_id = 0) {
$area = new Area(intval($area_id));
if(!$area) {
return $this -> utils() -> response_error("area_not_exists");
}
$devices = $area -> get_inner_devices(true);
$results = [];
foreach($devices as $device) {
$results["{$area -> alias}:{$device -> alias}"] = $device -> device_api() -> reboot();
}
return $this -> utils() -> response_success([
"results" => $results,
"total" => count($results)
]);
}
public function devices_list($area_id) {
if($area_id != intval($area_id) or intval($area_id) <= 1) {
return $this -> utils() -> response_error("invalid_id", ["area_id"]);
}
$area = new Area(intval($area_id));
if(!$area) {
return $this -> utils() -> response_error("area_not_exists");
}
$devices = $area -> get_inner_devices(true);
return $this -> utils() -> response_success([
"devices" => array_map(function($device) {
return $device -> to_array();
}, $devices),
"total" => count($devices)
]);
}
public function unassign_from_area($area_id) {
if($area_id != intval($area_id) or intval($area_id) <= 1) {
return $this -> utils() -> response_error("invalid_id", ["area_id"]);
}
$area = new Area(intval($area_id));
if(!$area) {
return $this -> utils() -> response_error("area_not_exists");
}
return $area -> place_in_area_by_id(0)
? $this -> utils() -> response_success()
: $this -> utils() -> response_error("undefined_error");
}
public function exists_types() {
return $this -> utils() -> response_success([
"types" => (new Areas()) -> get_exists_types()
]);
}
}