Newer
Older
smart-home-server / server / SHServ / Models / Areas.php
<?php

namespace SHServ\Models;

use \SHServ\Entities\Area;

class Areas extends \SHServ\Middleware\Model {
	public function create_new_area(String $type, String $alias, String $display_name): Area | bool {
		$area_id = app() -> thin_builder -> insert(Area::$table_name, [
			"alias" => $alias,
			"display_name" => $name,
			"type" => $device_info["data"]["device_type"],
			"schema" => "",
			"parent_id" => "0",
			"create_at" => date("Y-m-d H:i:s")
		]);

		$area = $area_id ? new Area($area_id) : null;

		if(!$area) {
			return false;
		}	
	}

	public function alias_is_uniq(String $alias): bool {
		$count = app() -> thin_builder -> count(
			Area::$table_name, 
			[ ["alias", "=", $alias] ]
		);

		return $count ? false : true;
	}

	public function get_exists_types(): Array {
		$areas = app() -> thin_builder -> select( Area::$table_name, [ "type" ], [] );
		if(!$areas) {
			return [];
		}

		$types = array_map(function($area) { 
			return $area["type"];
		}, $areas);

		$uniq_types = [];

		foreach($types as $type) {
			if(in_array($type, $uniq_types)) {
				continue;
			}

			$uniq_types[] = $type;
		}

		return $uniq_types;
	}

	public function get_areas_by_type(String $type): Array {
		$raw_areas = app() -> thin_builder -> select(
			Area::$table_name,
			Area::get_fields(),
			[["type", "=", $type]]
		);

		if(!$raw_areas) {
			return [];
		}

		$areas = [];
		foreach ($raw_areas as $raw_area) {
			$areas[] = new Area($raw_area["id"], $raw_area);
		}

		return $areas;
	}

	public function get_area_by_alias(String $alias): Area | null {
		$raw_area = app() -> thin_builder -> select(
			Area::$table_name,
			Area::get_fields(),
			[[ "type", "=", $type ]],
			[], "DESC", [ 1 ]
		);

		if(!$raw_area) {
			return null;
		}

		return new Area($raw_area["id"], $raw_area);
	}
}