<?php
namespace SHServ\Models;
use \SHServ\Entities\Script;
class Scripts extends \SHServ\Middleware\Model {
public function get_scopes_list(): Array {
$instances = app() -> control_scripts_instances;
$scopes = [];
foreach($instances as $name => $scope) {
$ref = new \ReflectionClass($scope);
$path_info = pathinfo($ref -> getFileName());
$scopes[] = [
"name" => $name,
"filename" => $path_info["basename"],
"path" => $path_info["dirname"]
];
}
return $scopes;
}
public function script_state(String $type, String $uniq_name): Bool {
$result = $this -> thin_builder() -> select(
Script::$table_name,
[ "state" ],
[ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ]
);
return !$result ? false : ($result[0]["state"] == "enabled");
}
public function set_script_state(String $type, String $uniq_name, Bool $state): Bool {
$result = $this -> thin_builder() -> select([
Script::$table_name,
Script::get_fields(),
[ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ]
]);
if(!$result) {
return $this -> thin_builder() -> insert(
Script::$table_name,
[
"uniq_name" => $uniq_name,
"type" => $type,
"state" => $state ? "enabled" : "disabled",
"create_at" => date("Y-m-d H:i:s")
]
);
}
// update
$script = new Script($result[0]["id"], $result[0]);
$script -> state = $state ? "enabled" : "disabled";
return $script -> update();
}
public function enable_script(String $type, String $uniq_name): Bool {
return $this -> set_script_state($type, $uniq_name, true);
}
public function disable_script(String $type, String $uniq_name): Bool {
return $this -> set_script_state($type, $uniq_name, false);
}
}