BxDolAIQuery.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. public function getModelsBy($aParams = [])
  16. {
  17. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  18. $sWhereClause = "";
  19. switch($aParams['sample']) {
  20. case 'id':
  21. $aMethod['name'] = 'getRow';
  22. $aMethod['params'][1] = [
  23. 'id' => $aParams['id']
  24. ];
  25. $sWhereClause .= " AND `id`=:id";
  26. break;
  27. case 'name':
  28. $aMethod['name'] = 'getRow';
  29. $aMethod['params'][1] = [
  30. 'name' => $aParams['name']
  31. ];
  32. $sWhereClause .= " AND `name`=:name";
  33. break;
  34. case 'all_pairs':
  35. $aMethod['name'] = 'getPairs';
  36. $aMethod['params'][1] = 'id';
  37. $aMethod['params'][2] = 'name';
  38. break;
  39. }
  40. $aMethod['params'][0] = "SELECT *
  41. FROM `sys_agents_models`
  42. WHERE 1" . $sWhereClause;
  43. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  44. }
  45. public function getAutomatorsBy($aParams = [])
  46. {
  47. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  48. $sSelectClause = "`taa`.*";
  49. $sJoinClause = $sWhereClause = "";
  50. switch($aParams['sample']) {
  51. case 'id':
  52. $aMethod['name'] = 'getRow';
  53. $aMethod['params'][1] = [
  54. 'id' => $aParams['id']
  55. ];
  56. $sWhereClause .= " AND `taa`.`id`=:id";
  57. break;
  58. case 'id_full':
  59. $aMethod['name'] = 'getRow';
  60. $aMethod['params'][1] = [
  61. 'id' => $aParams['id']
  62. ];
  63. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`url` AS `model_url`, `tam`.`key` AS `model_key`, `tam`.`model` AS `model_model`, `tam`.`params` AS `model_params`";
  64. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  65. $sWhereClause .= " AND `taa`.`id`=:id";
  66. break;
  67. case 'events':
  68. $aMethod['params'][1] = [
  69. 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
  70. 'alert_unit' => $aParams['alert_unit'],
  71. 'alert_action' => $aParams['alert_action']
  72. ];
  73. $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
  74. if(isset($aParams['active'])) {
  75. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  76. $sWhereClause .= " AND `taa`.`active`=:active";
  77. }
  78. break;
  79. case 'schedulers':
  80. $aMethod['params'][1] = [
  81. 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
  82. ];
  83. $sWhereClause .= " AND `taa`.`type`=:type";
  84. if(isset($aParams['active'])) {
  85. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  86. $sWhereClause .= " AND `taa`.`active`=:active";
  87. }
  88. break;
  89. }
  90. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  91. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  92. WHERE 1" . $sWhereClause;
  93. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  94. }
  95. public function updateAutomators($aSetClause, $aWhereClause)
  96. {
  97. if(empty($aSetClause) || empty($aWhereClause))
  98. return false;
  99. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  100. }
  101. }
  102. /** @} */