BxDolAIQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 'providers_by_id_pairs':
  114. $aMethod['name'] = 'getPairs';
  115. $aMethod['params'][1] = 'id';
  116. $aMethod['params'][2] = 'provider_id';
  117. $aMethod['params'][3] = [
  118. 'id' => $aParams['id']
  119. ];
  120. $sSelectClause = "`tap`.`id`, `tap`.`provider_id`";
  121. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  122. $sWhereClause = " AND `taa`.`id`=:id";
  123. break;
  124. }
  125. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  126. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  127. WHERE 1" . $sWhereClause;
  128. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  129. }
  130. public function updateAutomators($aSetClause, $aWhereClause)
  131. {
  132. if(empty($aSetClause) || empty($aWhereClause))
  133. return false;
  134. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  135. }
  136. public function insertAutomatorProvider($aParamsSet)
  137. {
  138. if(empty($aParamsSet))
  139. return false;
  140. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  141. }
  142. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  143. {
  144. if(empty($aParamsSet) || empty($aParamsWhere))
  145. return false;
  146. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  147. }
  148. public function deleteAutomatorProviders($aParamsWhere)
  149. {
  150. if(empty($aParamsWhere))
  151. return false;
  152. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  153. }
  154. public function deleteAutomatorProvidersById($mixedId)
  155. {
  156. if(!is_array($mixedId))
  157. $mixedId = [$mixedId];
  158. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  159. }
  160. public function getProviderTypesBy($aParams = [])
  161. {
  162. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  163. $sWhereClause = "";
  164. switch($aParams['sample']) {
  165. case 'id':
  166. $aMethod['name'] = 'getRow';
  167. $aMethod['params'][1] = [
  168. 'id' => $aParams['id']
  169. ];
  170. $sWhereClause .= " AND `id`=:id";
  171. break;
  172. case 'name':
  173. $aMethod['name'] = 'getRow';
  174. $aMethod['params'][1] = [
  175. 'name' => $aParams['name']
  176. ];
  177. $sWhereClause .= " AND `name`=:name";
  178. break;
  179. case 'all_pairs':
  180. $aMethod['name'] = 'getPairs';
  181. $aMethod['params'][1] = 'id';
  182. $aMethod['params'][2] = 'title';
  183. break;
  184. }
  185. $aMethod['params'][0] = "SELECT *
  186. FROM `sys_agents_provider_types`
  187. WHERE 1" . $sWhereClause;
  188. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  189. }
  190. public function getProviderOptionsBy($aParams = [])
  191. {
  192. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  193. $sWhereClause = "";
  194. switch($aParams['sample']) {
  195. case 'id':
  196. $aMethod['name'] = 'getRow';
  197. $aMethod['params'][1] = [
  198. 'id' => $aParams['id']
  199. ];
  200. $sWhereClause .= " AND `id`=:id";
  201. break;
  202. case 'provider_type_id':
  203. $aMethod['params'][1] = [
  204. 'provider_type_id' => $aParams['provider_type_id']
  205. ];
  206. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  207. break;
  208. }
  209. $aMethod['params'][0] = "SELECT *
  210. FROM `sys_agents_provider_options`
  211. WHERE 1" . $sWhereClause;
  212. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  213. }
  214. public function getProviderBy($aParams = [])
  215. {
  216. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  217. $sSelectClause = "`tp`.*";
  218. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  219. switch($aParams['sample']) {
  220. case 'id':
  221. $aMethod['name'] = 'getRow';
  222. $aMethod['params'][1] = [
  223. 'id' => $aParams['id']
  224. ];
  225. $sWhereClause = " AND `tp`.`id`=:id";
  226. break;
  227. case 'ids':
  228. $sSelectClause .= ", `tpt`.`name` AS `provider_name`";
  229. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  230. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  231. break;
  232. case 'options_by_id':
  233. $aMethod['params'][1] = [
  234. 'id' => $aParams['id']
  235. ];
  236. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  237. $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`";
  238. $sWhereClause = " AND `tp`.`id`=:id";
  239. break;
  240. case 'all_pairs':
  241. $aMethod['name'] = 'getPairs';
  242. $aMethod['params'][1] = 'id';
  243. $aMethod['params'][2] = 'title';
  244. if(isset($aParams['active'])) {
  245. $aMethod['params'][3] = [
  246. 'active' => $aParams['active']
  247. ];
  248. $sWhereClause = " AND `tp`.`active`=:active";
  249. }
  250. break;
  251. }
  252. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  253. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  254. $aMethod['params'][0] = "SELECT
  255. " . $sSelectClause . "
  256. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  257. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  258. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  259. }
  260. public function insertProviderValue($aParamsSet)
  261. {
  262. if(empty($aParamsSet))
  263. return false;
  264. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  265. }
  266. public function updateProviderValue($aParamsSet, $aParamsWhere)
  267. {
  268. if(empty($aParamsSet))
  269. return false;
  270. $sWhereClause = "1";
  271. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  272. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  273. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  274. }
  275. public function deleteProviderValues($aParamsWhere)
  276. {
  277. if(empty($aParamsWhere))
  278. return false;
  279. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  280. }
  281. }
  282. /** @} */