BxDolAIQuery.php 27 KB

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