BxDolAIQuery.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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 protected function _getObject(int $iId, string $sTable): array|bool
  16. {
  17. $oDb = BxDolDb::getInstance();
  18. $a = $oDb->getRow("SELECT * FROM `$sTable` WHERE `id` = :id", ['id' => $iId]);
  19. if(!$a || !is_array($a))
  20. return false;
  21. return $a;
  22. }
  23. static public function getModelObject(int $iId): array|bool
  24. {
  25. return self::_getObject($iId, 'sys_agents_models');
  26. }
  27. static public function getVectorStoreObject(int $iId): array|bool
  28. {
  29. return self::_getObject($iId, 'sys_agents_vector_store');
  30. }
  31. static public function getAgentObject(int $iId): array|bool
  32. {
  33. return self::_getObject($iId, 'sys_agents_agents');
  34. }
  35. static public function getToolObject(int $iId): array|bool
  36. {
  37. return self::_getObject($iId, 'sys_agents_tools');
  38. }
  39. static public function getProviderObject(int $iId)
  40. {
  41. $oDb = BxDolDb::getInstance();
  42. $aProvider = $oDb->getRow("SELECT * FROM `sys_agents_providers` WHERE `id` = :id", ['id' => $iId]);
  43. if(!$aProvider || !is_array($aProvider))
  44. return false;
  45. // get type
  46. $aProviderType = $oDb->getRow("SELECT * FROM `sys_agents_provider_types` WHERE `id` = :id", ['id' => $aProvider['type_id']]);
  47. if(!$aProviderType || !is_array($aProviderType))
  48. return false;
  49. $aProvider['type'] = $aProviderType;
  50. // get options
  51. $sQuery = "SELECT
  52. `tpo`.`name` AS `name`,
  53. `tpv`.`value` AS `value`
  54. FROM `sys_agents_provider_options` AS `tpo`
  55. LEFT JOIN `sys_agents_providers_values` AS `tpv` ON `tpo`.`id` = `tpv`.`option_id` AND `tpv`.`provider_id` = :provider_id
  56. WHERE 1 ORDER BY `tpo`.`order`";
  57. $aProvider['options'] = $oDb->getAllWithKey($sQuery, 'name', [
  58. 'provider_id' => $iId
  59. ]);
  60. return $aProvider;
  61. }
  62. static public function getProviderIdByName($sName)
  63. {
  64. return (int)BxDolDb::getInstance()->getOne("SELECT `id` FROM `sys_agents_providers` WHERE `name`=:name LIMIT 1", [
  65. 'name' => $sName
  66. ]);
  67. }
  68. static public function getAssistantObject($iId)
  69. {
  70. $oDb = BxDolDb::getInstance();
  71. $aAssistant = $oDb->getRow("SELECT * FROM `sys_agents_assistants` WHERE `id` = :id", ['id' => $iId]);
  72. if(!$aAssistant || !is_array($aAssistant))
  73. return false;
  74. return $aAssistant;
  75. }
  76. public function insertModel($aModel)
  77. {
  78. if(empty($aModel))
  79. return false;
  80. return (int)$this->query("INSERT INTO `sys_agents_models` SET " . $this->arrayToSQL($aModel));
  81. }
  82. public function getModelsBy($aParams = [])
  83. {
  84. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  85. $sWhereClause = "";
  86. switch($aParams['sample']) {
  87. case 'id':
  88. $aMethod['name'] = 'getRow';
  89. $aMethod['params'][1] = [
  90. 'id' => $aParams['id']
  91. ];
  92. $sWhereClause .= " AND `id`=:id";
  93. break;
  94. case 'all_pairs':
  95. $aMethod['name'] = 'getPairs';
  96. $aMethod['params'][1] = 'id';
  97. $aMethod['params'][2] = 'title';
  98. $aMethod['params'][3] = [];
  99. if(isset($aParams['active'])) {
  100. $aMethod['params'][3]['active'] = $aParams['active'];
  101. $sWhereClause .= " AND `active`=:active";
  102. }
  103. if(isset($aParams['capabilities'])) {
  104. if (is_array($aParams['capabilities']))
  105. {
  106. $sWhereClause .= " AND `capabilities` IN (" . $this->implode_escape($aParams['capabilities']) . ")";
  107. }
  108. else {
  109. $aMethod['params'][3]['capabilities'] = $aParams['capabilities'];
  110. $sWhereClause .= " AND `capabilities` = :capabilities";
  111. }
  112. }
  113. break;
  114. }
  115. $aMethod['params'][0] = "SELECT *
  116. FROM `sys_agents_models`
  117. WHERE 1" . $sWhereClause;
  118. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  119. }
  120. public function getAutomatorsBy($aParams = [], $bFromCache = true)
  121. {
  122. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  123. $sSelectClause = "`taa`.*";
  124. $sPostfix = $sJoinClause = $sWhereClause = "";
  125. switch($aParams['sample']) {
  126. case 'id':
  127. $aMethod['name'] = 'getRow';
  128. $aMethod['params'][1] = [
  129. 'id' => $aParams['id']
  130. ];
  131. $sWhereClause .= " AND `taa`.`id`=:id";
  132. break;
  133. case 'id_full':
  134. $aMethod['name'] = 'getRow';
  135. $aMethod['params'][1] = [
  136. 'id' => $aParams['id']
  137. ];
  138. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
  139. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  140. $sWhereClause .= " AND `taa`.`id`=:id";
  141. break;
  142. case 'type':
  143. $sPostfix .= '_' . $aParams['type'];
  144. $aMethod['params'][1] = [
  145. 'type' => $aParams['type']
  146. ];
  147. $sWhereClause .= " AND `taa`.`type`=:type";
  148. if(isset($aParams['active'])) {
  149. $iActive = (int)$aParams['active'];
  150. $sPostfix .= '_' . (!$iActive ? 'in' : '') . 'active';
  151. $aMethod['params'][1]['active'] = $iActive;
  152. $sWhereClause .= " AND `taa`.`active`=:active";
  153. }
  154. break;
  155. case 'events':
  156. $aBindings = [
  157. 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
  158. 'alert_unit' => $aParams['alert_unit'],
  159. 'alert_action' => $aParams['alert_action']
  160. ];
  161. $sPostfix .= '_' . implode('_', $aBindings);
  162. $aMethod['params'][1] = $aBindings;
  163. $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
  164. if(isset($aParams['active'])) {
  165. $iActive = (int)$aParams['active'];
  166. $sPostfix .= '_' . (!$iActive ? 'in' : '') . 'active';
  167. $aMethod['params'][1]['active'] = $iActive;
  168. $sWhereClause .= " AND `taa`.`active`=:active";
  169. }
  170. break;
  171. case 'schedulers':
  172. $aMethod['params'][1] = [
  173. 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
  174. ];
  175. $sWhereClause .= " AND `taa`.`type`=:type";
  176. if(isset($aParams['active'])) {
  177. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  178. $sWhereClause .= " AND `taa`.`active`=:active";
  179. }
  180. break;
  181. case 'webhooks':
  182. $aMethod['params'][1] = [
  183. 'type' => BX_DOL_AI_AUTOMATOR_WEBHOOK,
  184. ];
  185. $sWhereClause .= " AND `taa`.`type`=:type";
  186. if(isset($aParams['provider_id'])) {
  187. $aMethod['params'][1]['provider_id'] = (int)$aParams['provider_id'];
  188. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  189. $sWhereClause .= " AND `tap`.`provider_id`=:provider_id";
  190. }
  191. if(isset($aParams['active'])) {
  192. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  193. $sWhereClause .= " AND `taa`.`active`=:active";
  194. }
  195. break;
  196. case 'providers_by_id_pairs':
  197. $aMethod['name'] = 'getPairs';
  198. $aMethod['params'][1] = 'id';
  199. $aMethod['params'][2] = 'provider_id';
  200. $aMethod['params'][3] = [
  201. 'id' => $aParams['id']
  202. ];
  203. $sSelectClause = "`taap`.`id`, `taap`.`provider_id`";
  204. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `taap` ON `taa`.`id`=`taap`.`automator_id`";
  205. $sWhereClause = " AND `taa`.`id`=:id";
  206. break;
  207. case 'helpers_by_id_pairs':
  208. $aMethod['name'] = 'getPairs';
  209. $aMethod['params'][1] = 'id';
  210. $aMethod['params'][2] = 'helper_id';
  211. $aMethod['params'][3] = [
  212. 'id' => $aParams['id']
  213. ];
  214. $sSelectClause = "`taah`.`id`, `taah`.`helper_id`";
  215. $sJoinClause = "INNER JOIN `sys_agents_automators_helpers` AS `taah` ON `taa`.`id`=`taah`.`automator_id`";
  216. $sWhereClause = " AND `taa`.`id`=:id";
  217. break;
  218. case 'assistants_by_id_pairs':
  219. $aMethod['name'] = 'getPairs';
  220. $aMethod['params'][1] = 'id';
  221. $aMethod['params'][2] = 'assistant_id';
  222. $aMethod['params'][3] = [
  223. 'id' => $aParams['id']
  224. ];
  225. $sSelectClause = "`taaa`.`id`, `taaa`.`assistant_id`";
  226. $sJoinClause = "INNER JOIN `sys_agents_automators_assistants` AS `taaa` ON `taa`.`id`=`taaa`.`automator_id`";
  227. $sWhereClause = " AND `taa`.`id`=:id";
  228. break;
  229. }
  230. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  231. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  232. WHERE 1" . $sWhereClause;
  233. if(!$bFromCache || empty($sPostfix))
  234. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  235. return call_user_func_array([$this, 'fromMemory'], array_merge(['sys_automators' . $sPostfix, $aMethod['name']], $aMethod['params']));
  236. }
  237. public function updateAutomators($aSetClause, $aWhereClause)
  238. {
  239. if(empty($aSetClause) || empty($aWhereClause))
  240. return false;
  241. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  242. }
  243. public function insertAutomatorProvider($aParamsSet)
  244. {
  245. if(empty($aParamsSet))
  246. return false;
  247. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  248. }
  249. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  250. {
  251. if(empty($aParamsSet) || empty($aParamsWhere))
  252. return false;
  253. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  254. }
  255. public function deleteAutomatorProviders($aParamsWhere)
  256. {
  257. if(empty($aParamsWhere))
  258. return false;
  259. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  260. }
  261. public function deleteAutomatorProvidersById($mixedId)
  262. {
  263. if(!is_array($mixedId))
  264. $mixedId = [$mixedId];
  265. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  266. }
  267. public function insertAutomatorHelper($aParamsSet)
  268. {
  269. if(empty($aParamsSet))
  270. return false;
  271. return (int)$this->query("INSERT INTO `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  272. }
  273. public function updateAutomatorHelper($aParamsSet, $aParamsWhere)
  274. {
  275. if(empty($aParamsSet) || empty($aParamsWhere))
  276. return false;
  277. return $this->query("UPDATE `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  278. }
  279. public function deleteAutomatorHelpers($aParamsWhere)
  280. {
  281. if(empty($aParamsWhere))
  282. return false;
  283. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  284. }
  285. public function deleteAutomatorHelpersById($mixedId)
  286. {
  287. if(!is_array($mixedId))
  288. $mixedId = [$mixedId];
  289. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  290. }
  291. public function insertAutomatorAssistant($aParamsSet)
  292. {
  293. if(empty($aParamsSet))
  294. return false;
  295. return (int)$this->query("INSERT INTO `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  296. }
  297. public function updateAutomatorAssistant($aParamsSet, $aParamsWhere)
  298. {
  299. if(empty($aParamsSet) || empty($aParamsWhere))
  300. return false;
  301. return $this->query("UPDATE `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  302. }
  303. public function deleteAutomatorAssistants($aParamsWhere)
  304. {
  305. if(empty($aParamsWhere))
  306. return false;
  307. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  308. }
  309. public function deleteAutomatorAssistantsById($mixedId)
  310. {
  311. if(!is_array($mixedId))
  312. $mixedId = [$mixedId];
  313. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  314. }
  315. public function getProviderTypesBy($aParams = [])
  316. {
  317. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  318. $sWhereClause = "";
  319. switch($aParams['sample']) {
  320. case 'id':
  321. $aMethod['name'] = 'getRow';
  322. $aMethod['params'][1] = [
  323. 'id' => $aParams['id']
  324. ];
  325. $sWhereClause .= " AND `id`=:id";
  326. break;
  327. case 'name':
  328. $aMethod['name'] = 'getRow';
  329. $aMethod['params'][1] = [
  330. 'name' => $aParams['name']
  331. ];
  332. $sWhereClause .= " AND `name`=:name";
  333. break;
  334. case 'all_pairs':
  335. $aMethod['name'] = 'getPairs';
  336. $aMethod['params'][1] = 'id';
  337. $aMethod['params'][2] = 'title';
  338. if(isset($aParams['active'])) {
  339. $aMethod['params'][3] = [
  340. 'active' => $aParams['active']
  341. ];
  342. $sWhereClause = " AND `active`=:active";
  343. }
  344. break;
  345. }
  346. $aMethod['params'][0] = "SELECT *
  347. FROM `sys_agents_provider_types`
  348. WHERE 1" . $sWhereClause;
  349. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  350. }
  351. public function getProviderOptionsBy($aParams = [])
  352. {
  353. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  354. $sWhereClause = "";
  355. switch($aParams['sample']) {
  356. case 'id':
  357. $aMethod['name'] = 'getRow';
  358. $aMethod['params'][1] = [
  359. 'id' => $aParams['id']
  360. ];
  361. $sWhereClause .= " AND `id`=:id";
  362. break;
  363. case 'provider_type_id':
  364. $aMethod['params'][1] = [
  365. 'provider_type_id' => $aParams['provider_type_id']
  366. ];
  367. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  368. break;
  369. }
  370. $aMethod['params'][0] = "SELECT *
  371. FROM `sys_agents_provider_options`
  372. WHERE 1" . $sWhereClause;
  373. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  374. }
  375. public function getProvidersBy($aParams = [])
  376. {
  377. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  378. $sSelectClause = "`tp`.*";
  379. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  380. switch($aParams['sample']) {
  381. case 'id':
  382. $aMethod['name'] = 'getRow';
  383. $aMethod['params'][1] = [
  384. 'id' => $aParams['id']
  385. ];
  386. $sWhereClause = " AND `tp`.`id`=:id";
  387. break;
  388. case 'ids':
  389. $sSelectClause .= ", `tpt`.`name` AS `type_name`";
  390. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  391. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  392. break;
  393. case 'options_by_id':
  394. $aMethod['params'][1] = [
  395. 'id' => $aParams['id']
  396. ];
  397. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  398. $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`";
  399. $sWhereClause = " AND `tp`.`id`=:id";
  400. break;
  401. case 'all_pairs':
  402. $aMethod['name'] = 'getPairs';
  403. $aMethod['params'][1] = 'id';
  404. $aMethod['params'][2] = 'name';
  405. if(isset($aParams['active'])) {
  406. $aMethod['params'][3] = [
  407. 'active' => $aParams['active']
  408. ];
  409. $sWhereClause = " AND `tp`.`active`=:active";
  410. }
  411. break;
  412. }
  413. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  414. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  415. $aMethod['params'][0] = "SELECT
  416. " . $sSelectClause . "
  417. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  418. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  419. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  420. }
  421. public function insertProviderValue($aParamsSet)
  422. {
  423. if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
  424. return false;
  425. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
  426. 'value' => $aParamsSet['value']
  427. ]) > 0 ? (int)$this->lastId() : false;
  428. }
  429. public function updateProviderValue($aParamsSet, $aParamsWhere)
  430. {
  431. if(empty($aParamsSet))
  432. return false;
  433. $sWhereClause = "1";
  434. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  435. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  436. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  437. }
  438. public function deleteProviderValues($aParamsWhere)
  439. {
  440. if(empty($aParamsWhere))
  441. return false;
  442. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  443. }
  444. public function getHelpersBy($aParams = [])
  445. {
  446. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  447. $sSelectClause = "`th`.*";
  448. $sJoinClause = $sWhereClause = "";
  449. switch($aParams['sample']) {
  450. case 'id':
  451. $aMethod['name'] = 'getRow';
  452. $aMethod['params'][1] = [
  453. 'id' => $aParams['id']
  454. ];
  455. $sWhereClause .= " AND `th`.`id`=:id";
  456. break;
  457. case 'name':
  458. $aMethod['name'] = 'getRow';
  459. $aMethod['params'][1] = [
  460. 'name' => $aParams['name']
  461. ];
  462. $sWhereClause .= " AND `th`.`name`=:name";
  463. break;
  464. case 'ids':
  465. $sWhereClause = " AND `th`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  466. break;
  467. case 'all_pairs':
  468. $aMethod['name'] = 'getPairs';
  469. $aMethod['params'][1] = 'id';
  470. $aMethod['params'][2] = 'name';
  471. if(isset($aParams['active'])) {
  472. $aMethod['params'][3] = [
  473. 'active' => $aParams['active']
  474. ];
  475. $sWhereClause = " AND `th`.`active`=:active";
  476. }
  477. break;
  478. }
  479. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  480. FROM `sys_agents_helpers` AS `th` " . $sJoinClause . "
  481. WHERE 1" . $sWhereClause;
  482. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  483. }
  484. public function getAssistantsBy($aParams = [])
  485. {
  486. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  487. $sSelectClause = "`ta`.*";
  488. $sJoinClause = $sWhereClause = "";
  489. switch($aParams['sample']) {
  490. case 'id':
  491. $aMethod['name'] = 'getRow';
  492. $aMethod['params'][1] = [
  493. 'id' => $aParams['id']
  494. ];
  495. $sWhereClause .= " AND `ta`.`id`=:id";
  496. break;
  497. case 'name':
  498. $aMethod['name'] = 'getRow';
  499. $aMethod['params'][1] = [
  500. 'name' => $aParams['name']
  501. ];
  502. $sWhereClause .= " AND `ta`.`name`=:name";
  503. break;
  504. case 'ids':
  505. $sWhereClause = " AND `ta`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  506. break;
  507. case 'all_pairs':
  508. $aMethod['name'] = 'getPairs';
  509. $aMethod['params'][1] = 'id';
  510. $aMethod['params'][2] = 'name';
  511. $aMethod['params'][3] = [];
  512. if(isset($aParams['active'])) {
  513. $aMethod['params'][3]['active'] = $aParams['active'];
  514. $sWhereClause .= " AND `ta`.`active`=:active";
  515. }
  516. if(isset($aParams['hidden'])) {
  517. $aMethod['params'][3]['hidden'] = $aParams['hidden'];
  518. $sWhereClause .= " AND `ta`.`hidden`=:hidden";
  519. }
  520. break;
  521. }
  522. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  523. FROM `sys_agents_assistants` AS `ta` " . $sJoinClause . "
  524. WHERE 1" . $sWhereClause;
  525. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  526. }
  527. public function updateAssistants($aSetClause, $aWhereClause)
  528. {
  529. if(empty($aSetClause) || empty($aWhereClause))
  530. return false;
  531. return (int)$this->query("UPDATE `sys_agents_assistants` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  532. }
  533. public function getChatsBy($aParams = [])
  534. {
  535. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  536. $sSelectClause = "`tac`.*";
  537. $sJoinClause = $sWhereClause = "";
  538. switch($aParams['sample']) {
  539. case 'id':
  540. $aMethod['name'] = 'getRow';
  541. $aMethod['params'][1] = [
  542. 'id' => $aParams['id']
  543. ];
  544. $sWhereClause .= " AND `tac`.`id`=:id";
  545. break;
  546. case 'name':
  547. $aMethod['name'] = 'getRow';
  548. $aMethod['params'][1] = [
  549. 'name' => $aParams['name']
  550. ];
  551. $sWhereClause .= " AND `tac`.`name`=:name";
  552. break;
  553. case 'assistant_id':
  554. $aMethod['params'][1] = [
  555. 'assistant_id' => $aParams['assistant_id']
  556. ];
  557. $sWhereClause .= " AND `tac`.`assistant_id`=:assistant_id";
  558. break;
  559. case 'type':
  560. $aMethod['params'][1] = [
  561. 'type' => $aParams['type']
  562. ];
  563. $sWhereClause .= " AND `tac`.`type`=:type";
  564. if(isset($aParams['lifetime']) && (int)$aParams['lifetime'] > 0) {
  565. $aMethod['params'][1]['lifetime'] = (int)$aParams['lifetime'];
  566. $sWhereClause .= " AND (UNIX_TIMESTAMP() - `tac`.`added`) >= :lifetime";
  567. }
  568. break;
  569. }
  570. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  571. FROM `sys_agents_assistants_chats` AS `tac` " . $sJoinClause . "
  572. WHERE 1" . $sWhereClause;
  573. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  574. }
  575. public function insertChat($aParamsSet)
  576. {
  577. if(empty($aParamsSet) || !is_array($aParamsSet))
  578. return false;
  579. return (int)$this->query("INSERT INTO `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  580. }
  581. public function updateChats($aSetClause, $aWhereClause)
  582. {
  583. if(empty($aSetClause) || empty($aWhereClause))
  584. return false;
  585. return (int)$this->query("UPDATE `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  586. }
  587. public function deleteChats($aParamsWhere)
  588. {
  589. if(empty($aParamsWhere))
  590. return false;
  591. return (int)$this->query("DELETE FROM `sys_agents_assistants_chats` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  592. }
  593. public function getFilesBy($aParams = [])
  594. {
  595. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  596. $sSelectClause = "`taf`.*";
  597. $sJoinClause = $sWhereClause = "";
  598. switch($aParams['sample']) {
  599. case 'id':
  600. $aMethod['name'] = 'getRow';
  601. $aMethod['params'][1] = [
  602. 'id' => $aParams['id']
  603. ];
  604. $sWhereClause .= " AND `taf`.`id`=:id";
  605. break;
  606. case 'assistant_id':
  607. $aMethod['params'][1] = [
  608. 'assistant_id' => $aParams['assistant_id']
  609. ];
  610. $sWhereClause .= " AND `taf`.`assistant_id`=:assistant_id";
  611. if(!empty($aParams['name'])) {
  612. $aMethod['name'] = 'getRow';
  613. $aMethod['params'][1]['name'] = $aParams['name'];
  614. $sWhereClause .= " AND `taf`.`name`=:name";
  615. }
  616. break;
  617. }
  618. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  619. FROM `sys_agents_assistants_files` AS `taf` " . $sJoinClause . "
  620. WHERE 1" . $sWhereClause;
  621. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  622. }
  623. public function insertFile($aParamsSet)
  624. {
  625. if(empty($aParamsSet) || !is_array($aParamsSet))
  626. return false;
  627. return (int)$this->query("INSERT INTO `sys_agents_assistants_files` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  628. }
  629. public function updateFiles($aSetClause, $aWhereClause)
  630. {
  631. if(empty($aSetClause) || empty($aWhereClause))
  632. return false;
  633. return (int)$this->query("UPDATE `sys_agents_assistants_files` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  634. }
  635. public function deleteFiles($aParamsWhere)
  636. {
  637. if(empty($aParamsWhere))
  638. return false;
  639. return (int)$this->query("DELETE FROM `sys_agents_assistants_files` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  640. }
  641. public function getVectorStores (): mixed
  642. {
  643. $sQuery = "SELECT `id`, `title` FROM `sys_agents_vector_store` WHERE `active` = 1 ORDER BY `title` ASC";
  644. return $this->getPairs($sQuery, 'id', 'title');
  645. }
  646. public function getVectorStoreById (int $iId): mixed
  647. {
  648. $sQuery = "SELECT * FROM `sys_agents_vector_store` WHERE `id` = :id";
  649. return $this->getRow($sQuery, ['id' => $iId]);
  650. }
  651. public function insertVectorStore($aVectorStore)
  652. {
  653. if(empty($aVectorStore) || !is_array($aVectorStore))
  654. return false;
  655. return (int)$this->query("INSERT INTO `sys_agents_vector_store` SET " . $this->arrayToSQL($aVectorStore));
  656. }
  657. public function getVectorStoreDataNum (int $iVectorStoreId): int
  658. {
  659. $sQuery = "SELECT COUNT(*) FROM `sys_agents_vector_store_data` WHERE `vector_store_id` = :vector_store_id";
  660. return (int)$this->getOne($sQuery, ['vector_store_id' => $iVectorStoreId]);
  661. }
  662. public function addVectorStoreData (int $iVectorStoreId, string $sType, string $sName, int $iFileSize, string $sMetadata, string $sSettings, string $sContent): bool
  663. {
  664. $sQuery = "INSERT INTO `sys_agents_vector_store_data` SET
  665. `vector_store_id` = :vector_store_id,
  666. `type` = :type,
  667. `name` = :name,
  668. `size` = :size,
  669. `metadata` = :metadata,
  670. `settings` = :settings,
  671. `content` = :content,
  672. `added` = :ts
  673. ";
  674. return $this->query($sQuery, [
  675. 'vector_store_id' => $iVectorStoreId,
  676. 'type' => $sType,
  677. 'name' => $sName,
  678. 'size' => $iFileSize,
  679. 'metadata' => $sMetadata,
  680. 'settings' => $sSettings,
  681. 'content' => $sContent,
  682. 'ts' => time()]) > 0;
  683. }
  684. public function getVectorStoreDataById (int $iId): mixed
  685. {
  686. $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `id` = :id";
  687. return $this->getRow($sQuery, ['id' => $iId]);
  688. }
  689. static public function getVectorStorePendingData (int $iLimit = 1): mixed
  690. {
  691. $oDb = BxDolDb::getInstance();
  692. $sQuery = "SELECT * FROM `sys_agents_vector_store_data` WHERE `status` = 'pending' ORDER BY `added` ASC LIMIT :limit";
  693. return $oDb->getAll($sQuery, ['limit' => $iLimit]);
  694. }
  695. static public function updateVectorStoreDataStatus (int $iId, string $sStatus): mixed
  696. {
  697. $oDb = BxDolDb::getInstance();
  698. $sQuery = "UPDATE `sys_agents_vector_store_data` SET `status` = :status WHERE `id` = :id";
  699. return $oDb->query($sQuery, ['status' => $sStatus, 'id' => $iId]);
  700. }
  701. public function getAgentsWithAlert($bActiveOnly = true)
  702. {
  703. return $this->fromCache('sys_agents_with_alert', 'getAll', "SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'alert' AND `active` = :active", ['active' => $bActiveOnly ? 1 : 0]);
  704. }
  705. public function getAgentsByProfileId($iProfileId, $bActiveOnly = true)
  706. {
  707. return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'message' AND `profile_id` = :profile AND `active` = :active", ['profile' => $iProfileId, 'active' => $bActiveOnly ? 1 : 0]);
  708. }
  709. public function getAgentsByFormObject($sFormObject, $bActiveOnly = true)
  710. {
  711. return $this->fromMemory('sys_agents_with_form_' . $sFormObject, 'getAll', "SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'form-input' AND `form_object` = :form_object AND `active` = :active", ['form_object' => $sFormObject, 'active' => $bActiveOnly ? 1 : 0]);
  712. }
  713. public function getAgentsBy($aParams = [])
  714. {
  715. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  716. $sWhereClause = "";
  717. switch($aParams['sample']) {
  718. case 'id':
  719. $aMethod['name'] = 'getRow';
  720. $aMethod['params'][1] = [
  721. 'id' => $aParams['id']
  722. ];
  723. $sWhereClause .= " AND `id`=:id";
  724. break;
  725. case 'all':
  726. if(isset($aParams['active'])) {
  727. $aMethod['params'][3]['active'] = $aParams['active'];
  728. $sWhereClause .= " AND `active`=:active";
  729. }
  730. break;
  731. }
  732. $aMethod['params'][0] = "SELECT *
  733. FROM `sys_agents_agents`
  734. WHERE 1" . $sWhereClause;
  735. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  736. }
  737. public function getAgentById(int $iId): mixed
  738. {
  739. return $this->getAgentsBy(['sample' => 'id', 'id' => $iId]);
  740. }
  741. public function getAgentByTriggerWebhookKey($sKey, $bActiveOnly = true)
  742. {
  743. return $this->getRow("SELECT * FROM `sys_agents_agents` WHERE `trigger` = 'scheduler' AND `webhook_key` = :key AND `active` = :active", ['key' => $sKey, 'active' => $bActiveOnly ? 1 : 0]);
  744. }
  745. public function getAgentsByTriggerType($sTrigger, $bActiveOnly = true)
  746. {
  747. return $this->getAll("SELECT * FROM `sys_agents_agents` WHERE `trigger` = :trigger AND `active` = :active", ['trigger' => $sTrigger, 'active' => $bActiveOnly ? 1 : 0]);
  748. }
  749. public function updateAgentField($iId, $sField, $sValue)
  750. {
  751. return $this->query("UPDATE `sys_agents_agents` SET `$sField` = :value WHERE `id` = :id", ['value' => $sValue, 'id' => $iId]);
  752. }
  753. public function getToolById (int $iId): mixed
  754. {
  755. $sQuery = "SELECT * FROM `sys_agents_tools` WHERE `id` = :id";
  756. return $this->getRow($sQuery, ['id' => $iId]);
  757. }
  758. public function insertTool(array $aFields): int
  759. {
  760. if(empty($aFields) || !is_array($aFields))
  761. return false;
  762. return (int)$this->query("INSERT INTO `sys_agents_tools` SET " . $this->arrayToSQL($aFields));
  763. }
  764. public function getTools()
  765. {
  766. $sQuery = "SELECT `id`, `title` FROM `sys_agents_tools` WHERE `active` = 1 ORDER BY `title` ASC";
  767. return $this->getPairs($sQuery, 'id', 'title');
  768. }
  769. public function wipeAgentChatHistory($aAgent)
  770. {
  771. $sQuery = "DELETE FROM `sys_agents_chat_history` WHERE `thread_id` LIKE :val";
  772. $iAffected = 0;
  773. $iAffected += $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id'] . ':%']);
  774. $iAffected += $this->query($sQuery, ['val' => $aAgent['trigger'] . ':' . $aAgent['id']]);
  775. return $iAffected;
  776. }
  777. public function getAgentChatHistoryRows($aAgent)
  778. {
  779. $sExact = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  780. $sFields = "`id`, `thread_id`, `messages`, `created_at`, `updated_at`";
  781. if ($this->isFieldExists('sys_agents_chat_history', 'closed_reason'))
  782. $sFields .= ", `closed_reason`";
  783. return $this->getAll("
  784. SELECT $sFields
  785. FROM `sys_agents_chat_history`
  786. WHERE `thread_id` = :exact OR `thread_id` LIKE :prefix
  787. ORDER BY `updated_at` DESC
  788. LIMIT 200
  789. ", [
  790. 'exact' => $sExact,
  791. 'prefix' => $sExact . ':%',
  792. ]);
  793. }
  794. public function getChatArtifactsByHistoryIds($aIds)
  795. {
  796. $aIds = array_values(array_unique(array_filter(array_map('intval', (array)$aIds))));
  797. if (!$aIds)
  798. return [];
  799. $sIn = implode(',', $aIds);
  800. $aRows = $this->getAll("
  801. SELECT `history_id`, `field_name`, `field_value`
  802. FROM `sys_agents_chat_artifacts`
  803. WHERE `history_id` IN ($sIn)
  804. ORDER BY `id` ASC
  805. ");
  806. return $this->mapChatArtifactRows($aRows);
  807. }
  808. public function getChatArtifactsByThreadId($sThreadId)
  809. {
  810. $sThreadId = (string)$sThreadId;
  811. if ($sThreadId === '')
  812. return [];
  813. $aRows = $this->getAll("
  814. SELECT `a`.`history_id`, `a`.`field_name`, `a`.`field_value`
  815. FROM `sys_agents_chat_artifacts` AS `a`
  816. INNER JOIN `sys_agents_chat_history` AS `h` ON `h`.`id` = `a`.`history_id`
  817. WHERE `h`.`thread_id` = :t
  818. ORDER BY `a`.`id` ASC
  819. ", [
  820. 't' => $sThreadId,
  821. ]);
  822. $aMap = $this->mapChatArtifactRows($aRows);
  823. if (!$aMap)
  824. return [];
  825. return (array)reset($aMap);
  826. }
  827. public function getChatArtifactsForAgent($aAgent)
  828. {
  829. $sExact = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  830. if ($sExact === ':')
  831. return [];
  832. $aRows = $this->getAll("
  833. SELECT `a`.`history_id`, `a`.`field_name`, `a`.`field_value`
  834. FROM `sys_agents_chat_artifacts` AS `a`
  835. INNER JOIN `sys_agents_chat_history` AS `h` ON `h`.`id` = `a`.`history_id`
  836. WHERE `h`.`thread_id` = :exact OR `h`.`thread_id` LIKE :prefix
  837. ORDER BY `a`.`id` ASC
  838. ", [
  839. 'exact' => $sExact,
  840. 'prefix' => $sExact . ':%',
  841. ]);
  842. return $this->mapChatArtifactRows($aRows);
  843. }
  844. protected function mapChatArtifactRows($aRows)
  845. {
  846. if (!is_array($aRows) || !$aRows)
  847. return [];
  848. $aOut = [];
  849. foreach ($aRows as $aRow) {
  850. if (!is_array($aRow))
  851. continue;
  852. $iHistoryId = (int)($aRow['history_id'] ?? 0);
  853. $sName = trim((string)($aRow['field_name'] ?? ''));
  854. if ($iHistoryId <= 0 || $sName === '')
  855. continue;
  856. $aOut[$iHistoryId][] = [
  857. 'field_name' => $sName,
  858. 'field_value' => (string)($aRow['field_value'] ?? ''),
  859. ];
  860. }
  861. return $aOut;
  862. }
  863. public function getExpiredAgentChatHistoryIds($aAgent, $iTtlMin)
  864. {
  865. if (!$this->isFieldExists('sys_agents_chat_history', 'closed_reason'))
  866. return [];
  867. $sExact = ($aAgent['trigger'] ?? '') . ':' . ($aAgent['id'] ?? '');
  868. $iTtlMin = (int)$iTtlMin;
  869. if ($sExact === ':' || $iTtlMin <= 0)
  870. return [];
  871. return $this->getColumn("
  872. SELECT `id` FROM `sys_agents_chat_history`
  873. WHERE (`thread_id` = :exact OR `thread_id` LIKE :prefix)
  874. AND `messages` IS NOT NULL AND `messages` != '' AND `messages` != '[]'
  875. AND (`closed_reason` = '' OR `closed_reason` IS NULL)
  876. AND `updated_at` <= DATE_SUB(NOW(), INTERVAL :ttl MINUTE)
  877. ", [
  878. 'exact' => $sExact,
  879. 'prefix' => $sExact . ':%',
  880. 'ttl' => $iTtlMin,
  881. ]);
  882. }
  883. /**
  884. * Rename this agent's guest threads (`…:{sessionId}`) onto a profile id.
  885. * If the member thread already has messages, drop the guest copy.
  886. */
  887. public function adoptGuestChatHistory($aAgent, $sSessionId, $iProfileId)
  888. {
  889. $sSessionId = (string)$sSessionId;
  890. $iProfileId = (int)$iProfileId;
  891. if ($sSessionId === '' || !$iProfileId || empty($aAgent['id']))
  892. return 0;
  893. $sPrefix = $aAgent['trigger'] . ':' . $aAgent['id'] . ':';
  894. $sOldSuffix = ':' . $sSessionId;
  895. $sNewSuffix = ':' . $iProfileId;
  896. $iLen = strlen($sOldSuffix);
  897. $aBindings = [
  898. 'p' => $sPrefix . '%',
  899. 'o' => $sOldSuffix,
  900. 'n' => $sNewSuffix,
  901. ];
  902. $this->query("
  903. DELETE `g` FROM `sys_agents_chat_history` AS `g`
  904. INNER JOIN `sys_agents_chat_history` AS `m`
  905. ON `m`.`thread_id` = CONCAT(LEFT(`g`.`thread_id`, CHAR_LENGTH(`g`.`thread_id`) - $iLen), :n)
  906. WHERE `g`.`thread_id` LIKE :p AND RIGHT(`g`.`thread_id`, $iLen) = :o
  907. AND `m`.`messages` IS NOT NULL AND `m`.`messages` != '' AND `m`.`messages` != '[]'
  908. ", $aBindings);
  909. $this->query("
  910. DELETE `m` FROM `sys_agents_chat_history` AS `m`
  911. INNER JOIN `sys_agents_chat_history` AS `g`
  912. ON `g`.`thread_id` LIKE :p AND RIGHT(`g`.`thread_id`, $iLen) = :o
  913. AND `m`.`thread_id` = CONCAT(LEFT(`g`.`thread_id`, CHAR_LENGTH(`g`.`thread_id`) - $iLen), :n)
  914. WHERE `m`.`messages` = '' OR `m`.`messages` = '[]' OR `m`.`messages` IS NULL
  915. ", $aBindings);
  916. $iAffected = (int)$this->query("
  917. UPDATE `sys_agents_chat_history`
  918. SET `thread_id` = CONCAT(LEFT(`thread_id`, CHAR_LENGTH(`thread_id`) - $iLen), :n)
  919. WHERE `thread_id` LIKE :p AND RIGHT(`thread_id`, $iLen) = :o
  920. ", $aBindings);
  921. return $iAffected;
  922. }
  923. static public function getAlert($s) {
  924. if (!$s)
  925. return false;
  926. $a = explode(':', $s);
  927. $oDb = BxDolDb::getInstance();
  928. return $oDb->getRow("SELECT * FROM `sys_alerts_log` WHERE `unit` = :unit AND `action` = :action", [
  929. 'unit' => $a[0] ?? '',
  930. 'action' => $a[1] ?? '',
  931. ]);
  932. }
  933. public function getAlerts()
  934. {
  935. $aValues = [];
  936. $aAlerts = $this->getAll("SELECT `unit`, `action`, `counter_24h`, `counter_per_request` FROM `sys_alerts_log` ORDER BY `unit`, `action`");
  937. foreach ($aAlerts as $a) {
  938. $sKey = $a['unit'] . ':' . $a['action'];
  939. $aValues[$sKey] = [
  940. 'key' => $sKey,
  941. 'unit' => $a['unit'],
  942. 'action' => $a['action'],
  943. 'name' => $a['unit'] . ' - ' . $a['action'],
  944. 'desc' => $this->getAlertDesc($a['unit'] . ':' . $a['action']),
  945. 'counter_24h' => $a['counter_24h'],
  946. 'counter_per_request' => $a['counter_per_request'],
  947. ];
  948. }
  949. return $aValues;
  950. }
  951. public function getFormDisplay($sObject)
  952. {
  953. $aDisplays = $this->getColumn("SELECT `display_name` FROM `sys_form_displays` WHERE `object` = :object", ['object' => $sObject]);
  954. return $aDisplays ? current($aDisplays) : null;
  955. }
  956. public function getFormObjects()
  957. {
  958. $aValues = [];
  959. $aForms = $this->getAll("SELECT `f`.`object`, `f`.`title`, `f`.`module`, `m`.`title` AS `module_title` FROM `sys_objects_form` AS f INNER JOIN `sys_modules` as `m` ON `m`.`name` = `f`.`module` ORDER BY `f`.`module`, `f`.`object` ASC");
  960. foreach ($aForms as $r) {
  961. if ('system' != $r['module']) {
  962. $oModule = BxDolModule::getInstance($r['module']);
  963. if (!$oModule || !$oModule->isEnabled())
  964. continue;
  965. }
  966. $sDisplay = $this->getFormDisplay($r['object']);
  967. if (!$sDisplay)
  968. continue;
  969. $oForm = BxDolForm::getObjectInstance($r['object'], $sDisplay);
  970. $sFormId = $oForm->getId();
  971. $aValues[$r['object']] = [
  972. 'form_id' => $sFormId,
  973. 'form_object' => $r['object'],
  974. 'module' => $r['module'],
  975. 'module_title' => $r['module_title'],
  976. 'form_title' => _t($r['title']),
  977. ];
  978. }
  979. return $aValues;
  980. }
  981. static public function getAlertDesc($sAlert)
  982. {
  983. if (!$sAlert)
  984. return '';
  985. $oDb = BxDolDb::getInstance();
  986. [$sUnit, $sAction] = explode(':', $sAlert);
  987. $sDesc = $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` = :unit AND `action` = :action LIMIT 1", [
  988. 'unit' => $sUnit,
  989. 'action' => $sAction,
  990. ]);
  991. if (!$sDesc) {
  992. $oDb->getOne("SELECT `description` FROM `sys_alerts_desc` WHERE `unit` LIKE '{%}' AND `action` = :action LIMIT 1", [
  993. 'action' => $sAction,
  994. ]);
  995. }
  996. return $sDesc;
  997. }
  998. }
  999. /** @} */