BxDolAIQuery.php 25 KB

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