BxDolAIQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. if(isset($aParams['active'])) {
  184. $aMethod['params'][3] = [
  185. 'active' => $aParams['active']
  186. ];
  187. $sWhereClause = " AND `active`=:active";
  188. }
  189. break;
  190. }
  191. $aMethod['params'][0] = "SELECT *
  192. FROM `sys_agents_provider_types`
  193. WHERE 1" . $sWhereClause;
  194. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  195. }
  196. public function getProviderOptionsBy($aParams = [])
  197. {
  198. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  199. $sWhereClause = "";
  200. switch($aParams['sample']) {
  201. case 'id':
  202. $aMethod['name'] = 'getRow';
  203. $aMethod['params'][1] = [
  204. 'id' => $aParams['id']
  205. ];
  206. $sWhereClause .= " AND `id`=:id";
  207. break;
  208. case 'provider_type_id':
  209. $aMethod['params'][1] = [
  210. 'provider_type_id' => $aParams['provider_type_id']
  211. ];
  212. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  213. break;
  214. }
  215. $aMethod['params'][0] = "SELECT *
  216. FROM `sys_agents_provider_options`
  217. WHERE 1" . $sWhereClause;
  218. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  219. }
  220. public function getProviderBy($aParams = [])
  221. {
  222. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  223. $sSelectClause = "`tp`.*";
  224. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  225. switch($aParams['sample']) {
  226. case 'id':
  227. $aMethod['name'] = 'getRow';
  228. $aMethod['params'][1] = [
  229. 'id' => $aParams['id']
  230. ];
  231. $sWhereClause = " AND `tp`.`id`=:id";
  232. break;
  233. case 'ids':
  234. $sSelectClause .= ", `tpt`.`name` AS `provider_name`";
  235. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  236. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  237. break;
  238. case 'options_by_id':
  239. $aMethod['params'][1] = [
  240. 'id' => $aParams['id']
  241. ];
  242. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  243. $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`";
  244. $sWhereClause = " AND `tp`.`id`=:id";
  245. break;
  246. case 'all_pairs':
  247. $aMethod['name'] = 'getPairs';
  248. $aMethod['params'][1] = 'id';
  249. $aMethod['params'][2] = 'title';
  250. if(isset($aParams['active'])) {
  251. $aMethod['params'][3] = [
  252. 'active' => $aParams['active']
  253. ];
  254. $sWhereClause = " AND `tp`.`active`=:active";
  255. }
  256. break;
  257. }
  258. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  259. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  260. $aMethod['params'][0] = "SELECT
  261. " . $sSelectClause . "
  262. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  263. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  264. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  265. }
  266. public function insertProviderValue($aParamsSet)
  267. {
  268. if(empty($aParamsSet))
  269. return false;
  270. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  271. }
  272. public function updateProviderValue($aParamsSet, $aParamsWhere)
  273. {
  274. if(empty($aParamsSet))
  275. return false;
  276. $sWhereClause = "1";
  277. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  278. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  279. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  280. }
  281. public function deleteProviderValues($aParamsWhere)
  282. {
  283. if(empty($aParamsWhere))
  284. return false;
  285. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  286. }
  287. }
  288. /** @} */