BxDolAIQuery.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. static public function getAssistantObject($iId)
  53. {
  54. $oDb = BxDolDb::getInstance();
  55. $aAssistant = $oDb->getRow("SELECT * FROM `sys_agents_assistants` WHERE `id` = :id", ['id' => $iId]);
  56. if(!$aAssistant || !is_array($aAssistant))
  57. return false;
  58. return $aAssistant;
  59. }
  60. public function getModelsBy($aParams = [])
  61. {
  62. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  63. $sWhereClause = "";
  64. switch($aParams['sample']) {
  65. case 'id':
  66. $aMethod['name'] = 'getRow';
  67. $aMethod['params'][1] = [
  68. 'id' => $aParams['id']
  69. ];
  70. $sWhereClause .= " AND `id`=:id";
  71. break;
  72. case 'all_pairs':
  73. $aMethod['name'] = 'getPairs';
  74. $aMethod['params'][1] = 'id';
  75. $aMethod['params'][2] = 'title';
  76. $aMethod['params'][3] = [];
  77. if(isset($aParams['for_asst'])) {
  78. $aMethod['params'][3]['for_asst'] = $aParams['for_asst'];
  79. $sWhereClause .= " AND `for_asst`=:for_asst";
  80. }
  81. if(isset($aParams['active'])) {
  82. $aMethod['params'][3]['active'] = $aParams['active'];
  83. $sWhereClause .= " AND `active`=:active";
  84. }
  85. if(isset($aParams['hidden'])) {
  86. $aMethod['params'][3]['hidden'] = $aParams['hidden'];
  87. $sWhereClause .= " AND `hidden`=:hidden";
  88. }
  89. break;
  90. }
  91. $aMethod['params'][0] = "SELECT *
  92. FROM `sys_agents_models`
  93. WHERE 1" . $sWhereClause;
  94. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  95. }
  96. public function getAutomatorsBy($aParams = [])
  97. {
  98. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  99. $sSelectClause = "`taa`.*";
  100. $sJoinClause = $sWhereClause = "";
  101. switch($aParams['sample']) {
  102. case 'id':
  103. $aMethod['name'] = 'getRow';
  104. $aMethod['params'][1] = [
  105. 'id' => $aParams['id']
  106. ];
  107. $sWhereClause .= " AND `taa`.`id`=:id";
  108. break;
  109. case 'id_full':
  110. $aMethod['name'] = 'getRow';
  111. $aMethod['params'][1] = [
  112. 'id' => $aParams['id']
  113. ];
  114. $sSelectClause .= ", `tam`.`name` AS `model_name`, `tam`.`title` AS `model_title`, `tam`.`key` AS `model_key`, `tam`.`params` AS `model_params`";
  115. $sJoinClause .= " LEFT JOIN `sys_agents_models` AS `tam` ON `taa`.`model_id`=`tam`.`id`";
  116. $sWhereClause .= " AND `taa`.`id`=:id";
  117. break;
  118. case 'events':
  119. $aMethod['params'][1] = [
  120. 'type' => BX_DOL_AI_AUTOMATOR_EVENT,
  121. 'alert_unit' => $aParams['alert_unit'],
  122. 'alert_action' => $aParams['alert_action']
  123. ];
  124. $sWhereClause .= " AND `taa`.`type`=:type AND `taa`.`alert_unit`=:alert_unit AND `taa`.`alert_action`=:alert_action";
  125. if(isset($aParams['active'])) {
  126. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  127. $sWhereClause .= " AND `taa`.`active`=:active";
  128. }
  129. break;
  130. case 'schedulers':
  131. $aMethod['params'][1] = [
  132. 'type' => BX_DOL_AI_AUTOMATOR_SCHEDULER,
  133. ];
  134. $sWhereClause .= " AND `taa`.`type`=:type";
  135. if(isset($aParams['active'])) {
  136. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  137. $sWhereClause .= " AND `taa`.`active`=:active";
  138. }
  139. break;
  140. case 'webhooks':
  141. $aMethod['params'][1] = [
  142. 'type' => BX_DOL_AI_AUTOMATOR_WEBHOOK,
  143. ];
  144. $sWhereClause .= " AND `taa`.`type`=:type";
  145. if(isset($aParams['provider_id'])) {
  146. $aMethod['params'][1]['provider_id'] = (int)$aParams['provider_id'];
  147. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `tap` ON `taa`.`id`=`tap`.`automator_id`";
  148. $sWhereClause .= " AND `tap`.`provider_id`=:provider_id";
  149. }
  150. if(isset($aParams['active'])) {
  151. $aMethod['params'][1]['active'] = (int)$aParams['active'];
  152. $sWhereClause .= " AND `taa`.`active`=:active";
  153. }
  154. break;
  155. case 'providers_by_id_pairs':
  156. $aMethod['name'] = 'getPairs';
  157. $aMethod['params'][1] = 'id';
  158. $aMethod['params'][2] = 'provider_id';
  159. $aMethod['params'][3] = [
  160. 'id' => $aParams['id']
  161. ];
  162. $sSelectClause = "`taap`.`id`, `taap`.`provider_id`";
  163. $sJoinClause = "INNER JOIN `sys_agents_automators_providers` AS `taap` ON `taa`.`id`=`taap`.`automator_id`";
  164. $sWhereClause = " AND `taa`.`id`=:id";
  165. break;
  166. case 'helpers_by_id_pairs':
  167. $aMethod['name'] = 'getPairs';
  168. $aMethod['params'][1] = 'id';
  169. $aMethod['params'][2] = 'helper_id';
  170. $aMethod['params'][3] = [
  171. 'id' => $aParams['id']
  172. ];
  173. $sSelectClause = "`taah`.`id`, `taah`.`helper_id`";
  174. $sJoinClause = "INNER JOIN `sys_agents_automators_helpers` AS `taah` ON `taa`.`id`=`taah`.`automator_id`";
  175. $sWhereClause = " AND `taa`.`id`=:id";
  176. break;
  177. case 'assistants_by_id_pairs':
  178. $aMethod['name'] = 'getPairs';
  179. $aMethod['params'][1] = 'id';
  180. $aMethod['params'][2] = 'assistant_id';
  181. $aMethod['params'][3] = [
  182. 'id' => $aParams['id']
  183. ];
  184. $sSelectClause = "`taaa`.`id`, `taaa`.`assistant_id`";
  185. $sJoinClause = "INNER JOIN `sys_agents_automators_assistants` AS `taaa` ON `taa`.`id`=`taaa`.`automator_id`";
  186. $sWhereClause = " AND `taa`.`id`=:id";
  187. break;
  188. }
  189. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  190. FROM `sys_agents_automators` AS `taa` " . $sJoinClause . "
  191. WHERE 1" . $sWhereClause;
  192. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  193. }
  194. public function updateAutomators($aSetClause, $aWhereClause)
  195. {
  196. if(empty($aSetClause) || empty($aWhereClause))
  197. return false;
  198. return (int)$this->query("UPDATE `sys_agents_automators` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  199. }
  200. public function insertAutomatorProvider($aParamsSet)
  201. {
  202. if(empty($aParamsSet))
  203. return false;
  204. return (int)$this->query("INSERT INTO `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  205. }
  206. public function updateAutomatorProvider($aParamsSet, $aParamsWhere)
  207. {
  208. if(empty($aParamsSet) || empty($aParamsWhere))
  209. return false;
  210. return $this->query("UPDATE `sys_agents_automators_providers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  211. }
  212. public function deleteAutomatorProviders($aParamsWhere)
  213. {
  214. if(empty($aParamsWhere))
  215. return false;
  216. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  217. }
  218. public function deleteAutomatorProvidersById($mixedId)
  219. {
  220. if(!is_array($mixedId))
  221. $mixedId = [$mixedId];
  222. return (int)$this->query("DELETE FROM `sys_agents_automators_providers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  223. }
  224. public function insertAutomatorHelper($aParamsSet)
  225. {
  226. if(empty($aParamsSet))
  227. return false;
  228. return (int)$this->query("INSERT INTO `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  229. }
  230. public function updateAutomatorHelper($aParamsSet, $aParamsWhere)
  231. {
  232. if(empty($aParamsSet) || empty($aParamsWhere))
  233. return false;
  234. return $this->query("UPDATE `sys_agents_automators_helpers` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  235. }
  236. public function deleteAutomatorHelpers($aParamsWhere)
  237. {
  238. if(empty($aParamsWhere))
  239. return false;
  240. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  241. }
  242. public function deleteAutomatorHelpersById($mixedId)
  243. {
  244. if(!is_array($mixedId))
  245. $mixedId = [$mixedId];
  246. return (int)$this->query("DELETE FROM `sys_agents_automators_helpers` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  247. }
  248. public function insertAutomatorAssistant($aParamsSet)
  249. {
  250. if(empty($aParamsSet))
  251. return false;
  252. return (int)$this->query("INSERT INTO `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  253. }
  254. public function updateAutomatorAssistant($aParamsSet, $aParamsWhere)
  255. {
  256. if(empty($aParamsSet) || empty($aParamsWhere))
  257. return false;
  258. return $this->query("UPDATE `sys_agents_automators_assistants` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  259. }
  260. public function deleteAutomatorAssistants($aParamsWhere)
  261. {
  262. if(empty($aParamsWhere))
  263. return false;
  264. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE " . $this->arrayToSQL($aParamsWhere)) > 0;
  265. }
  266. public function deleteAutomatorAssistantsById($mixedId)
  267. {
  268. if(!is_array($mixedId))
  269. $mixedId = [$mixedId];
  270. return (int)$this->query("DELETE FROM `sys_agents_automators_assistants` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  271. }
  272. public function getProviderTypesBy($aParams = [])
  273. {
  274. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  275. $sWhereClause = "";
  276. switch($aParams['sample']) {
  277. case 'id':
  278. $aMethod['name'] = 'getRow';
  279. $aMethod['params'][1] = [
  280. 'id' => $aParams['id']
  281. ];
  282. $sWhereClause .= " AND `id`=:id";
  283. break;
  284. case 'name':
  285. $aMethod['name'] = 'getRow';
  286. $aMethod['params'][1] = [
  287. 'name' => $aParams['name']
  288. ];
  289. $sWhereClause .= " AND `name`=:name";
  290. break;
  291. case 'all_pairs':
  292. $aMethod['name'] = 'getPairs';
  293. $aMethod['params'][1] = 'id';
  294. $aMethod['params'][2] = 'title';
  295. if(isset($aParams['active'])) {
  296. $aMethod['params'][3] = [
  297. 'active' => $aParams['active']
  298. ];
  299. $sWhereClause = " AND `active`=:active";
  300. }
  301. break;
  302. }
  303. $aMethod['params'][0] = "SELECT *
  304. FROM `sys_agents_provider_types`
  305. WHERE 1" . $sWhereClause;
  306. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  307. }
  308. public function getProviderOptionsBy($aParams = [])
  309. {
  310. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  311. $sWhereClause = "";
  312. switch($aParams['sample']) {
  313. case 'id':
  314. $aMethod['name'] = 'getRow';
  315. $aMethod['params'][1] = [
  316. 'id' => $aParams['id']
  317. ];
  318. $sWhereClause .= " AND `id`=:id";
  319. break;
  320. case 'provider_type_id':
  321. $aMethod['params'][1] = [
  322. 'provider_type_id' => $aParams['provider_type_id']
  323. ];
  324. $sWhereClause .= " AND `provider_type_id`=:provider_type_id";
  325. break;
  326. }
  327. $aMethod['params'][0] = "SELECT *
  328. FROM `sys_agents_provider_options`
  329. WHERE 1" . $sWhereClause;
  330. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  331. }
  332. public function getProvidersBy($aParams = [])
  333. {
  334. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  335. $sSelectClause = "`tp`.*";
  336. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  337. switch($aParams['sample']) {
  338. case 'id':
  339. $aMethod['name'] = 'getRow';
  340. $aMethod['params'][1] = [
  341. 'id' => $aParams['id']
  342. ];
  343. $sWhereClause = " AND `tp`.`id`=:id";
  344. break;
  345. case 'ids':
  346. $sSelectClause .= ", `tpt`.`name` AS `type_name`";
  347. $sJoinClause = "INNER JOIN `sys_agents_provider_types` AS `tpt` ON `tp`.`type_id`=`tpt`.`id`";
  348. $sWhereClause = " AND `tp`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  349. break;
  350. case 'options_by_id':
  351. $aMethod['params'][1] = [
  352. 'id' => $aParams['id']
  353. ];
  354. $sSelectClause = "`tpo`.`id`, `tpo`.`name`, `tpo`.`type`, `tpo`.`title`, `tpo`.`description`, `tpv`.`value`";
  355. $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`";
  356. $sWhereClause = " AND `tp`.`id`=:id";
  357. break;
  358. case 'all_pairs':
  359. $aMethod['name'] = 'getPairs';
  360. $aMethod['params'][1] = 'id';
  361. $aMethod['params'][2] = 'name';
  362. if(isset($aParams['active'])) {
  363. $aMethod['params'][3] = [
  364. 'active' => $aParams['active']
  365. ];
  366. $sWhereClause = " AND `tp`.`active`=:active";
  367. }
  368. break;
  369. }
  370. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  371. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  372. $aMethod['params'][0] = "SELECT
  373. " . $sSelectClause . "
  374. FROM `sys_agents_providers` AS `tp` " . $sJoinClause . "
  375. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  376. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  377. }
  378. public function insertProviderValue($aParamsSet)
  379. {
  380. if(empty($aParamsSet) || !is_array($aParamsSet) || !isset($aParamsSet['value']))
  381. return false;
  382. return (int)$this->query("INSERT INTO `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " ON DUPLICATE KEY UPDATE `value`=:value", [
  383. 'value' => $aParamsSet['value']
  384. ]) > 0 ? (int)$this->lastId() : false;
  385. }
  386. public function updateProviderValue($aParamsSet, $aParamsWhere)
  387. {
  388. if(empty($aParamsSet))
  389. return false;
  390. $sWhereClause = "1";
  391. if(!empty($aParamsWhere) && is_array($aParamsWhere))
  392. $sWhereClause = $this->arrayToSQL($aParamsWhere, ' AND ');
  393. return $this->query("UPDATE `sys_agents_providers_values` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause) !== false;
  394. }
  395. public function deleteProviderValues($aParamsWhere)
  396. {
  397. if(empty($aParamsWhere))
  398. return false;
  399. return (int)$this->query("DELETE FROM `sys_agents_providers_values` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  400. }
  401. public function getHelpersBy($aParams = [])
  402. {
  403. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  404. $sSelectClause = "`th`.*";
  405. $sJoinClause = $sWhereClause = "";
  406. switch($aParams['sample']) {
  407. case 'id':
  408. $aMethod['name'] = 'getRow';
  409. $aMethod['params'][1] = [
  410. 'id' => $aParams['id']
  411. ];
  412. $sWhereClause .= " AND `th`.`id`=:id";
  413. break;
  414. case 'name':
  415. $aMethod['name'] = 'getRow';
  416. $aMethod['params'][1] = [
  417. 'name' => $aParams['name']
  418. ];
  419. $sWhereClause .= " AND `th`.`name`=:name";
  420. break;
  421. case 'ids':
  422. $sWhereClause = " AND `th`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  423. break;
  424. case 'all_pairs':
  425. $aMethod['name'] = 'getPairs';
  426. $aMethod['params'][1] = 'id';
  427. $aMethod['params'][2] = 'name';
  428. if(isset($aParams['active'])) {
  429. $aMethod['params'][3] = [
  430. 'active' => $aParams['active']
  431. ];
  432. $sWhereClause = " AND `th`.`active`=:active";
  433. }
  434. break;
  435. }
  436. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  437. FROM `sys_agents_helpers` AS `th` " . $sJoinClause . "
  438. WHERE 1" . $sWhereClause;
  439. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  440. }
  441. public function getAssistantsBy($aParams = [])
  442. {
  443. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  444. $sSelectClause = "`ta`.*";
  445. $sJoinClause = $sWhereClause = "";
  446. switch($aParams['sample']) {
  447. case 'id':
  448. $aMethod['name'] = 'getRow';
  449. $aMethod['params'][1] = [
  450. 'id' => $aParams['id']
  451. ];
  452. $sWhereClause .= " AND `ta`.`id`=:id";
  453. break;
  454. case 'name':
  455. $aMethod['name'] = 'getRow';
  456. $aMethod['params'][1] = [
  457. 'name' => $aParams['name']
  458. ];
  459. $sWhereClause .= " AND `ta`.`name`=:name";
  460. break;
  461. case 'ids':
  462. $sWhereClause = " AND `ta`.`id` IN (" . $this->implode_escape($aParams['ids']) . ")";
  463. break;
  464. case 'all_pairs':
  465. $aMethod['name'] = 'getPairs';
  466. $aMethod['params'][1] = 'id';
  467. $aMethod['params'][2] = 'name';
  468. $aMethod['params'][3] = [];
  469. if(isset($aParams['active'])) {
  470. $aMethod['params'][3]['active'] = $aParams['active'];
  471. $sWhereClause .= " AND `ta`.`active`=:active";
  472. }
  473. if(isset($aParams['hidden'])) {
  474. $aMethod['params'][3]['hidden'] = $aParams['hidden'];
  475. $sWhereClause .= " AND `ta`.`hidden`=:hidden";
  476. }
  477. break;
  478. }
  479. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  480. FROM `sys_agents_assistants` AS `ta` " . $sJoinClause . "
  481. WHERE 1" . $sWhereClause;
  482. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  483. }
  484. public function updateAssistants($aSetClause, $aWhereClause)
  485. {
  486. if(empty($aSetClause) || empty($aWhereClause))
  487. return false;
  488. return (int)$this->query("UPDATE `sys_agents_assistants` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  489. }
  490. public function getChatsBy($aParams = [])
  491. {
  492. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  493. $sSelectClause = "`tac`.*";
  494. $sJoinClause = $sWhereClause = "";
  495. switch($aParams['sample']) {
  496. case 'id':
  497. $aMethod['name'] = 'getRow';
  498. $aMethod['params'][1] = [
  499. 'id' => $aParams['id']
  500. ];
  501. $sWhereClause .= " AND `tac`.`id`=:id";
  502. break;
  503. case 'name':
  504. $aMethod['name'] = 'getRow';
  505. $aMethod['params'][1] = [
  506. 'name' => $aParams['name']
  507. ];
  508. $sWhereClause .= " AND `tac`.`name`=:name";
  509. break;
  510. case 'assistant_id':
  511. $aMethod['params'][1] = [
  512. 'assistant_id' => $aParams['assistant_id']
  513. ];
  514. $sWhereClause .= " AND `tac`.`assistant_id`=:assistant_id";
  515. break;
  516. case 'type':
  517. $aMethod['params'][1] = [
  518. 'type' => $aParams['type']
  519. ];
  520. $sWhereClause .= " AND `tac`.`type`=:type";
  521. if(isset($aParams['lifetime']) && (int)$aParams['lifetime'] > 0) {
  522. $aMethod['params'][1]['lifetime'] = (int)$aParams['lifetime'];
  523. $sWhereClause .= " AND (UNIX_TIMESTAMP() - `tac`.`added`) >= :lifetime";
  524. }
  525. break;
  526. }
  527. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  528. FROM `sys_agents_assistants_chats` AS `tac` " . $sJoinClause . "
  529. WHERE 1" . $sWhereClause;
  530. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  531. }
  532. public function insertChat($aParamsSet)
  533. {
  534. if(empty($aParamsSet) || !is_array($aParamsSet))
  535. return false;
  536. return (int)$this->query("INSERT INTO `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  537. }
  538. public function updateChats($aSetClause, $aWhereClause)
  539. {
  540. if(empty($aSetClause) || empty($aWhereClause))
  541. return false;
  542. return (int)$this->query("UPDATE `sys_agents_assistants_chats` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  543. }
  544. public function deleteChats($aParamsWhere)
  545. {
  546. if(empty($aParamsWhere))
  547. return false;
  548. return (int)$this->query("DELETE FROM `sys_agents_assistants_chats` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  549. }
  550. public function getFilesBy($aParams = [])
  551. {
  552. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  553. $sSelectClause = "`taf`.*";
  554. $sJoinClause = $sWhereClause = "";
  555. switch($aParams['sample']) {
  556. case 'id':
  557. $aMethod['name'] = 'getRow';
  558. $aMethod['params'][1] = [
  559. 'id' => $aParams['id']
  560. ];
  561. $sWhereClause .= " AND `taf`.`id`=:id";
  562. break;
  563. case 'assistant_id':
  564. $aMethod['params'][1] = [
  565. 'assistant_id' => $aParams['assistant_id']
  566. ];
  567. $sWhereClause .= " AND `taf`.`assistant_id`=:assistant_id";
  568. if(!empty($aParams['name'])) {
  569. $aMethod['name'] = 'getRow';
  570. $aMethod['params'][1]['name'] = $aParams['name'];
  571. $sWhereClause .= " AND `taf`.`name`=:name";
  572. }
  573. break;
  574. }
  575. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  576. FROM `sys_agents_assistants_files` AS `taf` " . $sJoinClause . "
  577. WHERE 1" . $sWhereClause;
  578. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  579. }
  580. public function insertFile($aParamsSet)
  581. {
  582. if(empty($aParamsSet) || !is_array($aParamsSet))
  583. return false;
  584. return (int)$this->query("INSERT INTO `sys_agents_assistants_files` SET " . $this->arrayToSQL($aParamsSet)) > 0 ? (int)$this->lastId() : false;
  585. }
  586. public function updateFiles($aSetClause, $aWhereClause)
  587. {
  588. if(empty($aSetClause) || empty($aWhereClause))
  589. return false;
  590. return (int)$this->query("UPDATE `sys_agents_assistants_files` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  591. }
  592. public function deleteFiles($aParamsWhere)
  593. {
  594. if(empty($aParamsWhere))
  595. return false;
  596. return (int)$this->query("DELETE FROM `sys_agents_assistants_files` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  597. }
  598. }
  599. /** @} */