BxDolAIQuery.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAIQuery extends BxDolDb
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. static public function getModelObject($iId)
  16. {
  17. $oDb = BxDolDb::getInstance();
  18. $aModel = $oDb->getRow("SELECT * FROM `sys_agents_models` WHERE `id` = :id", ['id' => $iId]);
  19. if(!$aModel || !is_array($aModel))
  20. return false;
  21. return $aModel;
  22. }
  23. static public function getProviderObject($iId)
  24. {
  25. $oDb = BxDolDb::getInstance();
  26. $aProvider = $oDb->getRow("SELECT * FROM `sys_agents_providers` WHERE `id` = :id", ['id' => $iId]);
  27. if(!$aProvider || !is_array($aProvider))
  28. return false;
  29. // get type
  30. $aProviderType = $oDb->getRow("SELECT * FROM `sys_agents_provider_types` WHERE `id` = :id", ['id' => $aProvider['type_id']]);
  31. if(!$aProviderType || !is_array($aProviderType))
  32. return false;
  33. $aProvider['type'] = $aProviderType;
  34. // get options
  35. $sQuery = "SELECT
  36. `tpo`.`name` AS `name`,
  37. `tpv`.`value` AS `value`
  38. FROM `sys_agents_provider_options` AS `tpo`
  39. LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tpo`.`id` = `tpv`.`option_id` AND `tpv`.`provider_id` = :provider_id
  40. WHERE 1 ORDER BY `tpo`.`order`";
  41. $aProvider['options'] = $oDb->getAllWithKey($sQuery, 'name', [
  42. 'provider_id' => $iId
  43. ]);
  44. return $aProvider;
  45. }
  46. public function getModelsBy($aParams = [])
  47. {
  48. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  49. $sWhereClause = "";
  50. switch($aParams['sample']) {
  51. case 'id':
  52. $aMethod['name'] = 'getRow';
  53. $aMethod['params'][1] = [
  54. 'id' => $aParams['id']
  55. ];
  56. $sWhereClause .= " AND `id`=:id";
  57. break;
  58. case 'all_pairs':
  59. $aMethod['name'] = 'getPairs';
  60. $aMethod['params'][1] = 'id';
  61. $aMethod['params'][2] = 'title';
  62. break;
  63. }
  64. $aMethod['params'][0] = "SELECT *
  65. FROM `sys_agents_models`
  66. WHERE 1" . $sWhereClause;
  67. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  68. }
  69. public function getAutomatorsBy($aParams = [])
  70. {
  71. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  72. $sSelectClause = "`taa`.*";
  73. $sJoinClause = $sWhereClause = "";
  74. switch($aParams['sample']) {
  75. case 'id':
  76. $aMethod['name'] = 'getRow';
  77. $aMethod['params'][1] = [
  78. 'id' => $aParams['id']
  79. ];
  80. $sWhereClause .= " AND `taa`.`id`=:id";
  81. break;
  82. case 'id_full':
  83. $aMethod['name'] = 'getRow';
  84. $aMethod['params'][1] = [
  85. 'id' => $aParams['id']
  86. ];
  87. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
  88. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  89. $sWhereClause .= " AND `taa`.`id`=:id";
  90. break;
  91. case 'events':
  92. $aMethod['params'][1] = [
  93. 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
  94. 'alert_unit' => $aParams['alert_unit'],
  95. 'alert_action' => $aParams['alert_action']
  96. ];
  97. $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
  98. if(isset($aParams['active'])) {
  99. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  100. $sWhereClause .= " AND `taa`.`active`=:active";
  101. }
  102. break;
  103. case 'schedulers':
  104. $aMethod['params'][1] = [
  105. 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
  106. ];
  107. $sWhereClause .= " AND `taa`.`type`=:type";
  108. if(isset($aParams['active'])) {
  109. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  110. $sWhereClause .= " AND `taa`.`active`=:active";
  111. }
  112. break;
  113. case 'webhooks':
  114. $aMethod['params'][1] = [
  115. 'type' => BX_DOL_AI_AUTOMATOR_WEBHOOK,
  116. ];
  117. $sWhereClause .= " AND `taa`.`type`=:type";
  118. if(isset($aParams['provider_id'])) {
  119. $aMethod['params'][1]['provider_id'] = (int)$aParams['provider_id'];
  120. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  121. $sWhereClause .= " AND `tap`.`provider_id`=:provider_id";
  122. }
  123. if(isset($aParams['active'])) {
  124. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  125. $sWhereClause .= " AND `taa`.`active`=:active";
  126. }
  127. break;
  128. case 'providers_by_id_pairs':
  129. $aMethod['name'] = 'getPairs';
  130. $aMethod['params'][1] = 'id';
  131. $aMethod['params'][2] = 'provider_id';
  132. $aMethod['params'][3] = [
  133. 'id' => $aParams['id']
  134. ];
  135. $sSelectClause = "`tap`.`id`, `tap`.`provider_id`";
  136. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  137. $sWhereClause = " AND `taa`.`id`=:id";
  138. break;
  139. }
  140. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  141. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  142. WHERE 1" . $sWhereClause;
  143. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  144. }
  145. public function updateAutomators($aSetClause, $aWhereClause)
  146. {
  147. if(empty($aSetClause) || empty($aWhereClause))
  148. return false;
  149. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  150. }
  151. public function insertAutomatorProvider($aParamsSet)
  152. {
  153. if(empty($aParamsSet))
  154. return false;
  155. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  156. }
  157. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  158. {
  159. if(empty($aParamsSet) || empty($aParamsWhere))
  160. return false;
  161. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  162. }
  163. public function deleteAutomatorProviders($aParamsWhere)
  164. {
  165. if(empty($aParamsWhere))
  166. return false;
  167. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  168. }
  169. public function deleteAutomatorProvidersById($mixedId)
  170. {
  171. if(!is_array($mixedId))
  172. $mixedId = [$mixedId];
  173. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  174. }
  175. public function getProviderTypesBy($aParams = [])
  176. {
  177. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  178. $sWhereClause = "";
  179. switch($aParams['sample']) {
  180. case 'id':
  181. $aMethod['name'] = 'getRow';
  182. $aMethod['params'][1] = [
  183. 'id' => $aParams['id']
  184. ];
  185. $sWhereClause .= " AND `id`=:id";
  186. break;
  187. case 'name':
  188. $aMethod['name'] = 'getRow';
  189. $aMethod['params'][1] = [
  190. 'name' => $aParams['name']
  191. ];
  192. $sWhereClause .= " AND `name`=:name";
  193. break;
  194. case 'all_pairs':
  195. $aMethod['name'] = 'getPairs';
  196. $aMethod['params'][1] = 'id';
  197. $aMethod['params'][2] = 'title';
  198. if(isset($aParams['active'])) {
  199. $aMethod['params'][3] = [
  200. 'active' => $aParams['active']
  201. ];
  202. $sWhereClause = " AND `active`=:active";
  203. }
  204. break;
  205. }
  206. $aMethod['params'][0] = "SELECT *
  207. FROM `sys_agents_provider_types`
  208. WHERE 1" . $sWhereClause;
  209. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  210. }
  211. public function getProviderOptionsBy($aParams = [])
  212. {
  213. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  214. $sWhereClause = "";
  215. switch($aParams['sample']) {
  216. case 'id':
  217. $aMethod['name'] = 'getRow';
  218. $aMethod['params'][1] = [
  219. 'id' => $aParams['id']
  220. ];
  221. $sWhereClause .= " AND `id`=:id";
  222. break;
  223. case 'provider_type_id':
  224. $aMethod['params'][1] = [
  225. 'provider_type_id' => $aParams['provider_type_id']
  226. ];
  227. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  228. break;
  229. }
  230. $aMethod['params'][0] = "SELECT *
  231. FROM `sys_agents_provider_options`
  232. WHERE 1" . $sWhereClause;
  233. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  234. }
  235. public function getProviderBy($aParams = [])
  236. {
  237. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  238. $sSelectClause = "`tp`.*";
  239. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  240. switch($aParams['sample']) {
  241. case 'id':
  242. $aMethod['name'] = 'getRow';
  243. $aMethod['params'][1] = [
  244. 'id' => $aParams['id']
  245. ];
  246. $sWhereClause = " AND `tp`.`id`=:id";
  247. break;
  248. case 'ids':
  249. $sSelectClause .= ", `tpt`.`name` AS `provider_name`";
  250. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  251. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  252. break;
  253. case 'options_by_id':
  254. $aMethod['params'][1] = [
  255. 'id' => $aParams['id']
  256. ];
  257. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  258. $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`";
  259. $sWhereClause = " AND `tp`.`id`=:id";
  260. break;
  261. case 'all_pairs':
  262. $aMethod['name'] = 'getPairs';
  263. $aMethod['params'][1] = 'id';
  264. $aMethod['params'][2] = 'title';
  265. if(isset($aParams['active'])) {
  266. $aMethod['params'][3] = [
  267. 'active' => $aParams['active']
  268. ];
  269. $sWhereClause = " AND `tp`.`active`=:active";
  270. }
  271. break;
  272. }
  273. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  274. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  275. $aMethod['params'][0] = "SELECT
  276. " . $sSelectClause . "
  277. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  278. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  279. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  280. }
  281. public function insertProviderValue($aParamsSet)
  282. {
  283. if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
  284. return false;
  285. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
  286. 'value' => $aParamsSet['value']
  287. ]) > 0 ? (int)$this->lastId() : false;
  288. }
  289. public function updateProviderValue($aParamsSet, $aParamsWhere)
  290. {
  291. if(empty($aParamsSet))
  292. return false;
  293. $sWhereClause = "1";
  294. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  295. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  296. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  297. }
  298. public function deleteProviderValues($aParamsWhere)
  299. {
  300. if(empty($aParamsWhere))
  301. return false;
  302. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  303. }
  304. }
  305. /** @} */