BxDolAIQuery.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 getHelpersBy($aParams = [])
  146. {
  147. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  148. $sSelectClause = "`taa`.*";
  149. $sJoinClause = $sWhereClause = "";
  150. switch($aParams['sample']) {
  151. case 'id':
  152. $aMethod['name'] = 'getRow';
  153. $aMethod['params'][1] = [
  154. 'id' => $aParams['id']
  155. ];
  156. $sWhereClause .= " AND `taa`.`id`=:id";
  157. break;
  158. case 'id_full':
  159. $aMethod['name'] = 'getRow';
  160. $aMethod['params'][1] = [
  161. 'id' => $aParams['id']
  162. ];
  163. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
  164. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  165. $sWhereClause .= " AND `taa`.`id`=:id";
  166. break;
  167. }
  168. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  169. FROM `sys_agents_helpers` AS `taa` " . $sJoinClause . "
  170. WHERE 1" . $sWhereClause;
  171. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  172. }
  173. public function updateAutomators($aSetClause, $aWhereClause)
  174. {
  175. if(empty($aSetClause) || empty($aWhereClause))
  176. return false;
  177. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  178. }
  179. public function insertAutomatorProvider($aParamsSet)
  180. {
  181. if(empty($aParamsSet))
  182. return false;
  183. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  184. }
  185. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  186. {
  187. if(empty($aParamsSet) || empty($aParamsWhere))
  188. return false;
  189. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  190. }
  191. public function deleteAutomatorProviders($aParamsWhere)
  192. {
  193. if(empty($aParamsWhere))
  194. return false;
  195. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  196. }
  197. public function deleteAutomatorProvidersById($mixedId)
  198. {
  199. if(!is_array($mixedId))
  200. $mixedId = [$mixedId];
  201. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  202. }
  203. public function getProviderTypesBy($aParams = [])
  204. {
  205. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  206. $sWhereClause = "";
  207. switch($aParams['sample']) {
  208. case 'id':
  209. $aMethod['name'] = 'getRow';
  210. $aMethod['params'][1] = [
  211. 'id' => $aParams['id']
  212. ];
  213. $sWhereClause .= " AND `id`=:id";
  214. break;
  215. case 'name':
  216. $aMethod['name'] = 'getRow';
  217. $aMethod['params'][1] = [
  218. 'name' => $aParams['name']
  219. ];
  220. $sWhereClause .= " AND `name`=:name";
  221. break;
  222. case 'all_pairs':
  223. $aMethod['name'] = 'getPairs';
  224. $aMethod['params'][1] = 'id';
  225. $aMethod['params'][2] = 'title';
  226. if(isset($aParams['active'])) {
  227. $aMethod['params'][3] = [
  228. 'active' => $aParams['active']
  229. ];
  230. $sWhereClause = " AND `active`=:active";
  231. }
  232. break;
  233. }
  234. $aMethod['params'][0] = "SELECT *
  235. FROM `sys_agents_provider_types`
  236. WHERE 1" . $sWhereClause;
  237. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  238. }
  239. public function getProviderOptionsBy($aParams = [])
  240. {
  241. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  242. $sWhereClause = "";
  243. switch($aParams['sample']) {
  244. case 'id':
  245. $aMethod['name'] = 'getRow';
  246. $aMethod['params'][1] = [
  247. 'id' => $aParams['id']
  248. ];
  249. $sWhereClause .= " AND `id`=:id";
  250. break;
  251. case 'provider_type_id':
  252. $aMethod['params'][1] = [
  253. 'provider_type_id' => $aParams['provider_type_id']
  254. ];
  255. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  256. break;
  257. }
  258. $aMethod['params'][0] = "SELECT *
  259. FROM `sys_agents_provider_options`
  260. WHERE 1" . $sWhereClause;
  261. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  262. }
  263. public function getProviderBy($aParams = [])
  264. {
  265. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  266. $sSelectClause = "`tp`.*";
  267. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  268. switch($aParams['sample']) {
  269. case 'id':
  270. $aMethod['name'] = 'getRow';
  271. $aMethod['params'][1] = [
  272. 'id' => $aParams['id']
  273. ];
  274. $sWhereClause = " AND `tp`.`id`=:id";
  275. break;
  276. case 'ids':
  277. $sSelectClause .= ", `tpt`.`name` AS `provider_name`";
  278. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  279. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  280. break;
  281. case 'options_by_id':
  282. $aMethod['params'][1] = [
  283. 'id' => $aParams['id']
  284. ];
  285. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  286. $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`";
  287. $sWhereClause = " AND `tp`.`id`=:id";
  288. break;
  289. case 'all_pairs':
  290. $aMethod['name'] = 'getPairs';
  291. $aMethod['params'][1] = 'id';
  292. $aMethod['params'][2] = 'title';
  293. if(isset($aParams['active'])) {
  294. $aMethod['params'][3] = [
  295. 'active' => $aParams['active']
  296. ];
  297. $sWhereClause = " AND `tp`.`active`=:active";
  298. }
  299. break;
  300. }
  301. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  302. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  303. $aMethod['params'][0] = "SELECT
  304. " . $sSelectClause . "
  305. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  306. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  307. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  308. }
  309. public function insertProviderValue($aParamsSet)
  310. {
  311. if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
  312. return false;
  313. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
  314. 'value' => $aParamsSet['value']
  315. ]) > 0 ? (int)$this->lastId() : false;
  316. }
  317. public function updateProviderValue($aParamsSet, $aParamsWhere)
  318. {
  319. if(empty($aParamsSet))
  320. return false;
  321. $sWhereClause = "1";
  322. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  323. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  324. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  325. }
  326. public function deleteProviderValues($aParamsWhere)
  327. {
  328. if(empty($aParamsWhere))
  329. return false;
  330. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  331. }
  332. }
  333. /** @} */