| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- class BxDolAIQuery extends BxDolDb
- {
- public function __construct()
- {
- parent::__construct();
- }
- static public function getModelObject($iId)
- {
- $oDb = BxDolDb::getInstance();
- $aModel = $oDb->getRow("SELECT * FROM `sys_agents_models` WHERE `id` = :id", ['id' => $iId]);
- if(!$aModel || !is_array($aModel))
- return false;
- return $aModel;
- }
- static public function getProviderObject($iId)
- {
- $oDb = BxDolDb::getInstance();
- $aProvider = $oDb->getRow("SELECT * FROM `sys_agents_providers` WHERE `id` = :id", ['id' => $iId]);
- if(!$aProvider || !is_array($aProvider))
- return false;
- // get type
- $aProviderType = $oDb->getRow("SELECT * FROM `sys_agents_provider_types` WHERE `id` = :id", ['id' => $aProvider['type_id']]);
- if(!$aProviderType || !is_array($aProviderType))
- return false;
- $aProvider['type'] = $aProviderType;
- // get options
- $sQuery = "SELECT
- `tpo`.`name` AS `name`,
- `tpv`.`value` AS `value`
- FROM `sys_agents_provider_options` AS `tpo`
- LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tpo`.`id` = `tpv`.`option_id` AND `tpv`.`provider_id` = :provider_id
- WHERE 1 ORDER BY `tpo`.`order`";
- $aProvider['options'] = $oDb->getAllWithKey($sQuery, 'name', [
- 'provider_id' => $iId
- ]);
- return $aProvider;
- }
- public function getModelsBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `id`=:id";
- break;
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'title';
- break;
- }
- $aMethod['params'][0] = "SELECT *
- FROM `sys_agents_models`
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function getAutomatorsBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`taa`.*";
- $sJoinClause = $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `taa`.`id`=:id";
- break;
- case 'id_full':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
- $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
- $sWhereClause .= " AND `taa`.`id`=:id";
- break;
-
- case 'events':
- $aMethod['params'][1] = [
- 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
- 'alert_unit' => $aParams['alert_unit'],
- 'alert_action' => $aParams['alert_action']
- ];
- $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
- if(isset($aParams['active'])) {
- $aMethod['params'][1]['active'] = (int)$aParams['active'];
- $sWhereClause .= " AND `taa`.`active`=:active";
- }
- break;
-
- case 'schedulers':
- $aMethod['params'][1] = [
- 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
- ];
- $sWhereClause .= " AND `taa`.`type`=:type";
- if(isset($aParams['active'])) {
- $aMethod['params'][1]['active'] = (int)$aParams['active'];
- $sWhereClause .= " AND `taa`.`active`=:active";
- }
- break;
- case 'providers_by_id_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'provider_id';
- $aMethod['params'][3] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause = "`tap`.`id`, `tap`.`provider_id`";
- $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
- $sWhereClause = " AND `taa`.`id`=:id";
- break;
- }
- $aMethod['params'][0] = "SELECT " . $sSelectClause . "
- FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function updateAutomators($aSetClause, $aWhereClause)
- {
- if(empty($aSetClause) || empty($aWhereClause))
- return false;
- return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
- }
- public function insertAutomatorProvider($aParamsSet)
- {
- if(empty($aParamsSet))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
- }
- public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
- {
- if(empty($aParamsSet) || empty($aParamsWhere))
- return false;
- return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
- }
- public function deleteAutomatorProviders($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
- }
-
- public function deleteAutomatorProvidersById($mixedId)
- {
- if(!is_array($mixedId))
- $mixedId = [$mixedId];
- return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
- }
- public function getProviderTypesBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `id`=:id";
- break;
- case 'name':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'name' => $aParams['name']
- ];
- $sWhereClause .= " AND `name`=:name";
- break;
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'title';
- if(isset($aParams['active'])) {
- $aMethod['params'][3] = [
- 'active' => $aParams['active']
- ];
- $sWhereClause = " AND `active`=:active";
- }
- break;
- }
- $aMethod['params'][0] = "SELECT *
- FROM `sys_agents_provider_types`
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function getProviderOptionsBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sWhereClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause .= " AND `id`=:id";
- break;
- case 'provider_type_id':
- $aMethod['params'][1] = [
- 'provider_type_id' => $aParams['provider_type_id']
- ];
- $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
- break;
- }
- $aMethod['params'][0] = "SELECT *
- FROM `sys_agents_provider_options`
- WHERE 1" . $sWhereClause;
- return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
- }
- public function getProviderBy($aParams = [])
- {
- $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
- $sSelectClause = "`tp`.*";
- $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
- switch($aParams['sample']) {
- case 'id':
- $aMethod['name'] = 'getRow';
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sWhereClause = " AND `tp`.`id`=:id";
- break;
- case 'ids':
- $sSelectClause .= ", `tpt`.`name` AS `provider_name`";
- $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
- $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
-
- break;
-
- case 'options_by_id':
- $aMethod['params'][1] = [
- 'id' => $aParams['id']
- ];
- $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
- $sJoinClause = "LEFT JOIN `sys_agents_provider_options` AS `tpo` ON `tp`.`type_id`=`tpo`.`provider_type_id` LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tp`.`id`=`tpv`.`provider_id` AND `tpo`.`id`=`tpv`.`option_id`";
- $sWhereClause = " AND `tp`.`id`=:id";
- break;
-
- case 'all_pairs':
- $aMethod['name'] = 'getPairs';
- $aMethod['params'][1] = 'id';
- $aMethod['params'][2] = 'title';
- if(isset($aParams['active'])) {
- $aMethod['params'][3] = [
- 'active' => $aParams['active']
- ];
- $sWhereClause = " AND `tp`.`active`=:active";
- }
- break;
- }
- $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
- $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
- $aMethod['params'][0] = "SELECT
- " . $sSelectClause . "
- FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
- WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
- return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
- }
- public function insertProviderValue($aParamsSet)
- {
- if(empty($aParamsSet))
- return false;
- return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
- }
- public function updateProviderValue($aParamsSet, $aParamsWhere)
- {
- if(empty($aParamsSet))
- return false;
- $sWhereClause = "1";
- if(!empty($aParamsWhere) && is_array($aParamsWhere))
- $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
- return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
- }
- public function deleteProviderValues($aParamsWhere)
- {
- if(empty($aParamsWhere))
- return false;
- return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
- }
- }
- /** @} */
|