diff --git a/server/SHServ/App.php b/server/SHServ/App.php index ddaf4a3..b195f0f 100644 --- a/server/SHServ/App.php +++ b/server/SHServ/App.php @@ -31,6 +31,9 @@ global $argv; $this -> console_flag = isset($argv) ? true : false; $this -> app_init(); + + $scripts = new \SHServ\Models\Scripts(); + $scripts -> set_script_state("action", "spotlights_off", true); } public function app_init(): void { diff --git a/server/SHServ/Controllers/ScriptsRESTAPIController.php b/server/SHServ/Controllers/ScriptsRESTAPIController.php index f6a9612..cb8025f 100644 --- a/server/SHServ/Controllers/ScriptsRESTAPIController.php +++ b/server/SHServ/Controllers/ScriptsRESTAPIController.php @@ -100,46 +100,32 @@ } public function scope_remove($name) { - $scripts_model = new Scripts(); - - return $scopes = $scripts_model -> remove_scope($name) + return (new Scripts()) -> 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) + protected function set_script_state(String $type, String $uniq_name, String $state) { + if($state == "enable") { + $result = (new Scripts()) -> enable_script($type, $uniq_name); + } else { + $result = (new Scripts()) -> disable_script($type, $uniq_name); + } + + return $result === true ? $this -> utils() -> response_success() - : $this -> utils() -> response_error("undefined_error"); + : $this -> utils() -> response_error($result); } - 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 set_scope_state($uniq_name, $state) { + return $this -> set_script_state("scope", $uniq_name, $state); } - 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 set_regular_script_state($uniq_name, $state) { + return $this -> set_script_state("regular", $uniq_name, $state); } - 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"); + public function set_action_script_state($uniq_name, $state) { + return $this -> set_script_state("action", $uniq_name, $state); } } \ No newline at end of file diff --git a/server/SHServ/Models/Scripts.php b/server/SHServ/Models/Scripts.php index a78f26a..5e7c7ac 100644 --- a/server/SHServ/Models/Scripts.php +++ b/server/SHServ/Models/Scripts.php @@ -60,7 +60,37 @@ return !$result ? false : ($result[0]["state"] == "enabled"); } - public function set_script_state(String $type, String $uniq_name, Bool $state): Bool { + public function set_script_state(String $type, String $uniq_name, Bool $state): String | Bool { + // Тут нужно проверить существует ли скрипт с таким именем + + if($type == "scope") { + $scopes_names = array_map(function($scope) { + return $scope["name"]; + }, $this -> get_scopes_list()); + + if(!in_array($uniq_name, $scopes_names)) { + return "scope_not_found"; // Err. Scope not exists + } + } elseif($type == "regular") { + $regular_scripts_names = array_map(function($regular_script) { + return $regular_script["alias"]; + }, $this -> regular_scripts_list()); + + if(!in_array($uniq_name, $regular_scripts_names)) { + return "regular_script_not_found"; // Err. Regular script not exists + } + } elseif($type == "action") { + $action_scripts_names = array_map(function($action_script) { + return $action_script["alias"]; + }, $this -> actions_scripts_list()); + + if(!in_array($uniq_name, $action_scripts_names)) { + return "action_script_not_found"; // Err. Action script not exists + } + } else { + return "invalid_type"; // Err of type + } + $result = $this -> thin_builder() -> select( Script::$table_name, Script::get_fields(), @@ -76,7 +106,7 @@ "state" => $state ? "enabled" : "disabled", "create_at" => date("Y-m-d H:i:s") ] - ) ? true : false; + ) ? true : "undefined_error"; } // update @@ -85,11 +115,11 @@ return $script -> update() ? true : false; } - public function enable_script(String $type, String $uniq_name): Bool { + public function enable_script(String $type, String $uniq_name): String | Bool { return $this -> set_script_state($type, $uniq_name, true); } - public function disable_script(String $type, String $uniq_name): Bool { + public function disable_script(String $type, String $uniq_name): String | Bool { return $this -> set_script_state($type, $uniq_name, false); } diff --git a/server/SHServ/Routes/ScriptsRESTAPI_v1.php b/server/SHServ/Routes/ScriptsRESTAPI_v1.php index 54ad146..14189d6 100644 --- a/server/SHServ/Routes/ScriptsRESTAPI_v1.php +++ b/server/SHServ/Routes/ScriptsRESTAPI_v1.php @@ -9,12 +9,9 @@ $this -> router -> uri("/api/v1/scripts/regular/list", "{$this -> cn}\\ScriptsRESTAPIController@regular_scripts_list"); $this -> router -> uri('/api/v1/scripts/scopes/name/$name', "{$this -> cn}\\ScriptsRESTAPIController@scope_file"); - $this -> router -> uri('/api/v1/scripts/actions/alias/$uniq_name/enable', "{$this -> cn}\\ScriptsRESTAPIController@action_script_enable"); - $this -> router -> uri('/api/v1/scripts/actions/alias/$uniq_name/disable', "{$this -> cn}\\ScriptsRESTAPIController@action_script_disable"); - $this -> router -> uri('/api/v1/scripts/scopes/name/$uniq_name/enable', "{$this -> cn}\\ScriptsRESTAPIController@scope_enable"); - $this -> router -> uri('/api/v1/scripts/scopes/name/$uniq_name/disable', "{$this -> cn}\\ScriptsRESTAPIController@scope_disable"); - $this -> router -> uri('/api/v1/scripts/regular/alias/$uniq_name/enable', "{$this -> cn}\\ScriptsRESTAPIController@regular_script_enable"); - $this -> router -> uri('/api/v1/scripts/regular/alias/$uniq_name/disable', "{$this -> cn}\\ScriptsRESTAPIController@regular_script_disable"); + $this -> router -> uri('/api/v1/scripts/actions/alias/$uniq_name/$state', "{$this -> cn}\\ScriptsRESTAPIController@set_action_script_state"); + $this -> router -> uri('/api/v1/scripts/scopes/name/$uniq_name/$state', "{$this -> cn}\\ScriptsRESTAPIController@set_scope_state"); + $this -> router -> uri('/api/v1/scripts/regular/alias/$uniq_name/$state', "{$this -> cn}\\ScriptsRESTAPIController@set_regular_script_state"); } protected function scripts_restapi_post_routes() { diff --git a/server/SHServ/text-msgs.php b/server/SHServ/text-msgs.php index 0b14ad6..03e17cb 100644 --- a/server/SHServ/text-msgs.php +++ b/server/SHServ/text-msgs.php @@ -36,12 +36,15 @@ "device_error_of_auth" => "", "action_script_not_found" => "", "scope_not_found" => "", + "regular_script_not_found" => "", + "action_script_not_found" => "", "file_not_exists" => "", "parent_area_not_found" => "", "alias_already_exists" => "", "invalid_id" => "", "area_not_exists" => "", "wrong_state_name" => "", + "invalid_type" => "", // Other "accept_removing" => "Подтвердите удаление",