BxDolAIQuery.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. static public function getProviderIdByName($sName)
  47. {
  48. return (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `sys_agents_providers` WHERE `name`=:name LIMIT 1", [
  49. 'name' => $sName
  50. ]);
  51. }
  52. public function getModelsBy($aParams = [])
  53. {
  54. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  55. $sWhereClause = "";
  56. switch($aParams['sample']) {
  57. case 'id':
  58. $aMethod['name'] = 'getRow';
  59. $aMethod['params'][1] = [
  60. 'id' => $aParams['id']
  61. ];
  62. $sWhereClause .= " AND `id`=:id";
  63. break;
  64. case 'all_pairs':
  65. $aMethod['name'] = 'getPairs';
  66. $aMethod['params'][1] = 'id';
  67. $aMethod['params'][2] = 'title';
  68. break;
  69. }
  70. $aMethod['params'][0] = "SELECT *
  71. FROM `sys_agents_models`
  72. WHERE 1" . $sWhereClause;
  73. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  74. }
  75. public function getAutomatorsBy($aParams = [])
  76. {
  77. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  78. $sSelectClause = "`taa`.*";
  79. $sJoinClause = $sWhereClause = "";
  80. switch($aParams['sample']) {
  81. case 'id':
  82. $aMethod['name'] = 'getRow';
  83. $aMethod['params'][1] = [
  84. 'id' => $aParams['id']
  85. ];
  86. $sWhereClause .= " AND `taa`.`id`=:id";
  87. break;
  88. case 'id_full':
  89. $aMethod['name'] = 'getRow';
  90. $aMethod['params'][1] = [
  91. 'id' => $aParams['id']
  92. ];
  93. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
  94. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  95. $sWhereClause .= " AND `taa`.`id`=:id";
  96. break;
  97. case 'events':
  98. $aMethod['params'][1] = [
  99. 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
  100. 'alert_unit' => $aParams['alert_unit'],
  101. 'alert_action' => $aParams['alert_action']
  102. ];
  103. $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
  104. if(isset($aParams['active'])) {
  105. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  106. $sWhereClause .= " AND `taa`.`active`=:active";
  107. }
  108. break;
  109. case 'schedulers':
  110. $aMethod['params'][1] = [
  111. 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
  112. ];
  113. $sWhereClause .= " AND `taa`.`type`=:type";
  114. if(isset($aParams['active'])) {
  115. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  116. $sWhereClause .= " AND `taa`.`active`=:active";
  117. }
  118. break;
  119. case 'webhooks':
  120. $aMethod['params'][1] = [
  121. 'type' => BX_DOL_AI_AUTOMATOR_WEBHOOK,
  122. ];
  123. $sWhereClause .= " AND `taa`.`type`=:type";
  124. if(isset($aParams['provider_id'])) {
  125. $aMethod['params'][1]['provider_id'] = (int)$aParams['provider_id'];
  126. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  127. $sWhereClause .= " AND `tap`.`provider_id`=:provider_id";
  128. }
  129. if(isset($aParams['active'])) {
  130. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  131. $sWhereClause .= " AND `taa`.`active`=:active";
  132. }
  133. break;
  134. case 'providers_by_id_pairs':
  135. $aMethod['name'] = 'getPairs';
  136. $aMethod['params'][1] = 'id';
  137. $aMethod['params'][2] = 'provider_id';
  138. $aMethod['params'][3] = [
  139. 'id' => $aParams['id']
  140. ];
  141. $sSelectClause = "`tap`.`id`, `tap`.`provider_id`";
  142. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  143. $sWhereClause = " AND `taa`.`id`=:id";
  144. break;
  145. case 'helpers_by_id_pairs':
  146. $aMethod['name'] = 'getPairs';
  147. $aMethod['params'][1] = 'id';
  148. $aMethod['params'][2] = 'helper_id';
  149. $aMethod['params'][3] = [
  150. 'id' => $aParams['id']
  151. ];
  152. $sSelectClause = "`tah`.`id`, `tah`.`helper_id`";
  153. $sJoinClause = "INNER JOIN `sys_agents_automators_helpers` AS `tah` ON `taa`.`id`=`tah`.`automator_id`";
  154. $sWhereClause = " AND `taa`.`id`=:id";
  155. break;
  156. }
  157. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  158. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  159. WHERE 1" . $sWhereClause;
  160. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  161. }
  162. public function getHelpersBy($aParams = [])
  163. {
  164. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  165. $sSelectClause = "`th`.*";
  166. $sJoinClause = $sWhereClause = "";
  167. switch($aParams['sample']) {
  168. case 'id':
  169. $aMethod['name'] = 'getRow';
  170. $aMethod['params'][1] = [
  171. 'id' => $aParams['id']
  172. ];
  173. $sWhereClause .= " AND `th`.`id`=:id";
  174. break;
  175. case 'name':
  176. $aMethod['name'] = 'getRow';
  177. $aMethod['params'][1] = [
  178. 'name' => $aParams['name']
  179. ];
  180. $sWhereClause .= " AND `th`.`name`=:name";
  181. break;
  182. case 'ids':
  183. $sWhereClause = " AND `th`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  184. break;
  185. case 'all_pairs':
  186. $aMethod['name'] = 'getPairs';
  187. $aMethod['params'][1] = 'id';
  188. $aMethod['params'][2] = 'name';
  189. if(isset($aParams['active'])) {
  190. $aMethod['params'][3] = [
  191. 'active' => $aParams['active']
  192. ];
  193. $sWhereClause = " AND `th`.`active`=:active";
  194. }
  195. break;
  196. }
  197. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  198. FROM `sys_agents_helpers` AS `th` " . $sJoinClause . "
  199. WHERE 1" . $sWhereClause;
  200. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  201. }
  202. public function updateAutomators($aSetClause, $aWhereClause)
  203. {
  204. if(empty($aSetClause) || empty($aWhereClause))
  205. return false;
  206. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  207. }
  208. public function insertAutomatorProvider($aParamsSet)
  209. {
  210. if(empty($aParamsSet))
  211. return false;
  212. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  213. }
  214. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  215. {
  216. if(empty($aParamsSet) || empty($aParamsWhere))
  217. return false;
  218. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  219. }
  220. public function deleteAutomatorProviders($aParamsWhere)
  221. {
  222. if(empty($aParamsWhere))
  223. return false;
  224. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  225. }
  226. public function deleteAutomatorProvidersById($mixedId)
  227. {
  228. if(!is_array($mixedId))
  229. $mixedId = [$mixedId];
  230. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  231. }
  232. public function insertAutomatorHelper($aParamsSet)
  233. {
  234. if(empty($aParamsSet))
  235. return false;
  236. return (int)$this->query("INSERT INTO `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  237. }
  238. public function updateAutomatorHelper($aParamsSet, $aParamsWhere)
  239. {
  240. if(empty($aParamsSet) || empty($aParamsWhere))
  241. return false;
  242. return $this->query("UPDATE `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  243. }
  244. public function deleteAutomatorHelpers($aParamsWhere)
  245. {
  246. if(empty($aParamsWhere))
  247. return false;
  248. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  249. }
  250. public function deleteAutomatorHelpersById($mixedId)
  251. {
  252. if(!is_array($mixedId))
  253. $mixedId = [$mixedId];
  254. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  255. }
  256. public function getProviderTypesBy($aParams = [])
  257. {
  258. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  259. $sWhereClause = "";
  260. switch($aParams['sample']) {
  261. case 'id':
  262. $aMethod['name'] = 'getRow';
  263. $aMethod['params'][1] = [
  264. 'id' => $aParams['id']
  265. ];
  266. $sWhereClause .= " AND `id`=:id";
  267. break;
  268. case 'name':
  269. $aMethod['name'] = 'getRow';
  270. $aMethod['params'][1] = [
  271. 'name' => $aParams['name']
  272. ];
  273. $sWhereClause .= " AND `name`=:name";
  274. break;
  275. case 'all_pairs':
  276. $aMethod['name'] = 'getPairs';
  277. $aMethod['params'][1] = 'id';
  278. $aMethod['params'][2] = 'title';
  279. if(isset($aParams['active'])) {
  280. $aMethod['params'][3] = [
  281. 'active' => $aParams['active']
  282. ];
  283. $sWhereClause = " AND `active`=:active";
  284. }
  285. break;
  286. }
  287. $aMethod['params'][0] = "SELECT *
  288. FROM `sys_agents_provider_types`
  289. WHERE 1" . $sWhereClause;
  290. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  291. }
  292. public function getProviderOptionsBy($aParams = [])
  293. {
  294. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  295. $sWhereClause = "";
  296. switch($aParams['sample']) {
  297. case 'id':
  298. $aMethod['name'] = 'getRow';
  299. $aMethod['params'][1] = [
  300. 'id' => $aParams['id']
  301. ];
  302. $sWhereClause .= " AND `id`=:id";
  303. break;
  304. case 'provider_type_id':
  305. $aMethod['params'][1] = [
  306. 'provider_type_id' => $aParams['provider_type_id']
  307. ];
  308. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  309. break;
  310. }
  311. $aMethod['params'][0] = "SELECT *
  312. FROM `sys_agents_provider_options`
  313. WHERE 1" . $sWhereClause;
  314. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  315. }
  316. public function getProvidersBy($aParams = [])
  317. {
  318. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  319. $sSelectClause = "`tp`.*";
  320. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  321. switch($aParams['sample']) {
  322. case 'id':
  323. $aMethod['name'] = 'getRow';
  324. $aMethod['params'][1] = [
  325. 'id' => $aParams['id']
  326. ];
  327. $sWhereClause = " AND `tp`.`id`=:id";
  328. break;
  329. case 'ids':
  330. $sSelectClause .= ", `tpt`.`name` AS `type_name`";
  331. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  332. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  333. break;
  334. case 'options_by_id':
  335. $aMethod['params'][1] = [
  336. 'id' => $aParams['id']
  337. ];
  338. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  339. $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`";
  340. $sWhereClause = " AND `tp`.`id`=:id";
  341. break;
  342. case 'all_pairs':
  343. $aMethod['name'] = 'getPairs';
  344. $aMethod['params'][1] = 'id';
  345. $aMethod['params'][2] = 'name';
  346. if(isset($aParams['active'])) {
  347. $aMethod['params'][3] = [
  348. 'active' => $aParams['active']
  349. ];
  350. $sWhereClause = " AND `tp`.`active`=:active";
  351. }
  352. break;
  353. }
  354. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  355. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  356. $aMethod['params'][0] = "SELECT
  357. " . $sSelectClause . "
  358. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  359. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  360. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  361. }
  362. public function insertProviderValue($aParamsSet)
  363. {
  364. if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
  365. return false;
  366. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
  367. 'value' => $aParamsSet['value']
  368. ]) > 0 ? (int)$this->lastId() : false;
  369. }
  370. public function updateProviderValue($aParamsSet, $aParamsWhere)
  371. {
  372. if(empty($aParamsSet))
  373. return false;
  374. $sWhereClause = "1";
  375. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  376. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  377. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  378. }
  379. public function deleteProviderValues($aParamsWhere)
  380. {
  381. if(empty($aParamsWhere))
  382. return false;
  383. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  384. }
  385. }
  386. /** @} */