Newer
Older
smart-home-server / server / SHServ / Controllers / ScriptsRESTAPIController.php
<?php

namespace SHServ\Controllers;

use \SHServ\Middleware\ControlScripts;
use \SHServ\Models\Scripts;

class ScriptsRESTAPIController extends \SHServ\Middleware\Controller {
	public function run_action_script($alias, $params) {
		$result = ControlScripts::run_action_script($alias, $params);

		if(!$result) {
			return $this -> utils() -> response_error("action_script_not_found");
		}

		return $this -> utils() -> response_success([
			"return" => $result
		]);
	}

	public function actions_scripts_list() {
		$scripts = ControlScripts::get_actions_scripts();

		$data = [];

		foreach($scripts as $alias => $script_data) {
			$data[] = [
				"alias" => $alias,
				"type" => "action",
				"name" => $script_data["attributes"]["name"],
				"description" => $script_data["attributes"]["description"],
				"filename" => $script_data["attributes"]["filename"],
				"path" => $script_data["attributes"]["path"],
				"created_by" => $script_data["attributes"]["author"],
			];
		}

		return $this -> utils() -> response_success([
			"scripts" => $data,
			"total" => count($data)
		]);
	}

	public function regular_scripts_list() {
		$scripts = ControlScripts::get_regular_scripts();

		$data = [];

		foreach($scripts as $alias => $script_data) {
			$data[] = [
				"alias" => $alias,
				"type" => "regular",
				"name" => $script_data["attributes"]["name"],
				"description" => $script_data["attributes"]["description"],
				"filename" => $script_data["attributes"]["filename"],
				"path" => $script_data["attributes"]["path"],
				"created_by" => $script_data["attributes"]["author"],
			];
		}

		return $this -> utils() -> response_success([
			"scripts" => $data,
			"total" => count($data)
		]);
	}

	public function scope_list() {
		$scripts_model = new Scripts();
		$scopes = $scripts_model -> get_scopes_list();

		return $this -> utils() -> response_success([
			"scopes" => $scopes,
			"total" => count($scopes)
		]);
	}

	public function scope_file($name) {
		$scripts_model = new Scripts();
		$scopes = $scripts_model -> get_scopes_list();

		$file = "";
		foreach($scopes as $scope) {
			if($name != $scope["name"]) {
				continue;
			}

			$file = file_get_contents($scope["path"] . "/" . $scope["filename"]);

			break;
		}

		if(!$file) {
			return $this -> utils() -> response_error("scope_not_found");
		}

		return $file;
	}

	public function scope_update($name, $file) {

	}

	public function scope_remove($name) {
		$scripts_model = new Scripts();

		return $scopes = $scripts_model -> remove_scope($name)
			? $this -> utils() -> response_success()
			: $this -> utils() -> response_error("undefined_error");
	}

	public function scope_enable($uniq_name) {
		return (new Scripts()) -> enable_script("scope", $uniq_name) 
			? $this -> utils() -> response_success()
			: $this -> utils() -> response_error("undefined_error");
	}

	public function scope_disable($uniq_name) {
		return (new Scripts()) -> disable_script("scope", $uniq_name) 
			? $this -> utils() -> response_success()
			: $this -> utils() -> response_error("undefined_error");
	}

	public function regular_script_enable($uniq_name) {
		return (new Scripts()) -> enable_script("regular", $uniq_name) 
			? $this -> utils() -> response_success()
			: $this -> utils() -> response_error("undefined_error");
	}

	public function regular_script_disable($uniq_name) {
		return (new Scripts()) -> disable_script("regular", $uniq_name) 
			? $this -> utils() -> response_success()
			: $this -> utils() -> response_error("undefined_error");
	}

	public function action_script_enable($uniq_name) {
		return (new Scripts()) -> enable_script("action", $uniq_name) 
			? $this -> utils() -> response_success()
			: $this -> utils() -> response_error("undefined_error");
	}

	public function action_script_disable($uniq_name) {
		return (new Scripts()) -> disable_script("action", $uniq_name) 
			? $this -> utils() -> response_success()
			: $this -> utils() -> response_error("undefined_error");
	}
}