BxTasksDb.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. * @defgroup Tasks Tasks
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. /*
  11. * Module database queries
  12. */
  13. class BxTasksDb extends BxBaseModTextDb
  14. {
  15. protected $_aSqlMarkers;
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. $this->_aSqlMarkers = [
  20. 'logged_pid' => bx_get_logged_profile_id()
  21. ];
  22. }
  23. public function getContexts($aParams = [])
  24. {
  25. $CNF = &$this->_oConfig->CNF;
  26. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  27. $sSelectClause = '`tc`.*';
  28. $sJoinClause = $sWhereClause = $sOrderClause = '';
  29. if(!empty($aParams))
  30. switch($aParams['sample']) {
  31. case 'id':
  32. $aMethod['name'] = 'getRow';
  33. $aMethod['params'][1] = [
  34. 'id' => $aParams['id']
  35. ];
  36. $sWhereClause = "AND `tc`.`id` = :id";
  37. break;
  38. }
  39. if(!empty($sOrderClause))
  40. $sOrderClause = "ORDER BY " . $sOrderClause;
  41. $aMethod['params'][0] = "SELECT
  42. " . $sSelectClause . "
  43. FROM `" . $CNF['TABLE_CONTEXTS'] . "` AS `tc` " . $sJoinClause . "
  44. WHERE 1 " . $sWhereClause . " " . $sOrderClause;
  45. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  46. }
  47. public function getContextRepository($iId)
  48. {
  49. $aContext = $this->getContexts([
  50. 'sample' => 'id',
  51. 'id' => $iId
  52. ]);
  53. return $aContext && is_array($aContext) ? $aContext : false;
  54. }
  55. public function getLists ($iContextId = 0)
  56. {
  57. $CNF = &$this->_oConfig->CNF;
  58. $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_LISTS'] . "` WHERE `context_id` = ?", $iContextId);
  59. return $this->getAll($sQuery);
  60. }
  61. public function getList ($iId = 0)
  62. {
  63. $CNF = &$this->_oConfig->CNF;
  64. $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_LISTS'] . "` WHERE `id` = ?", $iId);
  65. return $this->getRow($sQuery);
  66. }
  67. public function deleteList ($iId = 0)
  68. {
  69. $CNF = &$this->_oConfig->CNF;
  70. $sQuery = $this->prepare ("DELETE FROM `" . $CNF['TABLE_LISTS'] . "` WHERE `id` = ?", $iId);
  71. $this->query($sQuery);
  72. $sQuery = $this->prepare ("DELETE FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_TASKLIST'] . "` = ?", $iId);
  73. $this->query($sQuery);
  74. }
  75. public function getTasks ($iContextId = 0, $iListId = 0, $aParams = [])
  76. {
  77. $CNF = &$this->_oConfig->CNF;
  78. $aBindings = [];
  79. $sSelectClause = "`te`.*";
  80. $sJoinClause = $sWhereClause = "";
  81. if(($sField = $CNF['FIELD_ALLOW_VIEW_TO']) && $iContextId) {
  82. $aBindings[$sField] = -$iContextId;
  83. $sWhereClause .= " AND `te`.`" . $sField . "` = :" . $sField;
  84. }
  85. if(($sField = $CNF['FIELD_TASKLIST']) && $iListId) {
  86. $aBindings[$sField] = $iListId;
  87. $sWhereClause .= " AND `te`.`" . $sField . "` = :" . $sField;
  88. }
  89. if(($iFilter = (int)($aParams['filter'] ?? 0)) && ($aFilter = $this->getFilterById($iFilter))) {
  90. if(($aFltJoin = $aFilter['conditions']['join'] ?? false) && ($sFltJoin = $this->_getSqlJoinFrom($aFltJoin)) != '')
  91. $sJoinClause .= " " . $sFltJoin;
  92. if(($aFltWhere = $aFilter['conditions']['where'] ?? false) && ($sFltWhere = $this->_getSqlWhereFrom($aFltWhere)) != '')
  93. $sWhereClause .= " AND " . $sFltWhere;
  94. }
  95. if(($iProfileId = $aParams['for_profile'] ?? false)) {
  96. $aBindings['profile_id'] = (int)$iProfileId;
  97. $sJoinClause .= " LEFT JOIN `" . $CNF['TABLE_ASSIGNMENTS'] . "` AS `ta` ON `te`.`id`=`ta`.`content`";
  98. $sWhereClause .= " AND (`te`.`" . $CNF['FIELD_AUTHOR'] . "`=:profile_id OR `ta`.`initiator`=:profile_id)";
  99. }
  100. if(($oCf = BxDolContentFilter::getInstance()) && $oCf->isEnabled())
  101. $sWhereClause .= $oCf->getSQLParts('te', $CNF['FIELD_CF']);
  102. if(($aParams['with_stats'] ?? false)) {
  103. $sSelectClause .= $this->prepareAsString(", (SELECT SUM(`value`) FROM `" . $CNF['TABLE_TIME_TRACK'] . "` WHERE `object_id`=`te`.`id` AND `author_id`=?) AS `time`, `tt`.`sum` AS `time_total`", bx_get_logged_profile_id());
  104. $sJoinClause .= " LEFT JOIN `" . $CNF['TABLE_TIME'] . "` AS `tt` ON `te`.`id`=`tt`.`object_id`";
  105. }
  106. return $this->getAll("SELECT DISTINCT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te`" . $sJoinClause . " WHERE 1" . $sWhereClause, $aBindings);
  107. }
  108. public function getEntriesByDate($sDateFrom, $sDateTo, $aSQLPart = array())
  109. {
  110. // validate input data
  111. if (false === ($oDateFrom = date_create($sDateFrom, new DateTimeZone('UTC'))))
  112. return array();
  113. if (false === ($oDateTo = date_create($sDateTo, new DateTimeZone('UTC'))))
  114. return array();
  115. if ($oDateFrom > $oDateTo)
  116. return array();
  117. // increase start and end date to cover timezones
  118. $oDateFrom = $oDateFrom->sub(new DateInterval("P1D"));
  119. $oDateTo = $oDateTo->add(new DateInterval("P1D"));
  120. // look throught all days in the interval
  121. $oDateIter = clone($oDateFrom);
  122. $aEntries = array();
  123. while ($oDateIter->format('Y-m-d') != $oDateTo->format('Y-m-d')) {
  124. $oDateMin = date_create($oDateIter->format('Y-m-d') . '00:00:00', new DateTimeZone('UTC'));
  125. $oDateMax = date_create($oDateIter->format('Y-m-d') . '23:59:59', new DateTimeZone('UTC'));
  126. // get all events for the specific day
  127. $oDateMonthBegin = date_create($oDateIter->format('Y-m-01'), new DateTimeZone('UTC'));
  128. $iWeekOfMonth = $oDateIter->format('W') - $oDateMonthBegin->format('W') + 1;
  129. $aBindings = array(
  130. 'timestamp_min' => $oDateMin->getTimestamp(),
  131. 'timestamp_max' => $oDateMax->getTimestamp(),
  132. );
  133. $sWhere = isset($aSQLPart['where']) ? $aSQLPart['where'] : '';
  134. $a = $this->getAll("SELECT DISTINCT `bx_tasks_tasks`.`id`, `bx_tasks_tasks`.`title` AS `title`, `bx_tasks_tasks`.`due_date`
  135. FROM `bx_tasks_tasks`
  136. WHERE (`bx_tasks_tasks`.`due_date` >= :timestamp_min AND `bx_tasks_tasks`.`due_date` <= :timestamp_max ) $sWhere
  137. ", $aBindings);
  138. // prepare variables for each event
  139. $sCurrentDay = $oDateIter->format('Y-m-d');
  140. foreach ($a as $k => $r) {
  141. $oDateStart = new DateTime();
  142. $oDateStart->setTimestamp($r['due_date']);
  143. $oDateStart->setTimezone(new DateTimeZone('UTC'));
  144. $oDateEnd = new DateTime();
  145. $oDateEnd->setTimestamp($r['due_date']);
  146. $oDateEnd->setTimezone(new DateTimeZone('UTC'));
  147. $oDuration = $oDateStart->diff($oDateEnd);
  148. $sHoursStart = $oDateStart->format('H:i:s');
  149. $oStart = date_create($sCurrentDay . ' ' . $sHoursStart, new DateTimeZone('UTC'));
  150. $oEnd = $oStart ? clone($oStart) : null;
  151. $oEnd = $oEnd ? $oEnd->add($oDuration) : null;
  152. $a[$k]['start'] = $oStart ? $oStart->format('Y-m-d') : 0;
  153. $a[$k]['start_utc'] = $oStart ? $oStart->getTimestamp() : 0;
  154. $a[$k]['url'] = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_VIEW_ENTRY'] . '&id=' . $r['id']));
  155. }
  156. // merge with all other events
  157. $aEntries = array_merge($aEntries, $a);
  158. // go to the next day
  159. $oDateIter = $oDateIter->add(new DateInterval("P1D"));
  160. }
  161. return $aEntries;
  162. }
  163. public function expireEntries()
  164. {
  165. $CNF = $this->_oConfig->CNF;
  166. $aResult = $this->getColumn("SELECT `id` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_DUE_DATE'] . "` < UNIX_TIMESTAMP() AND `" . $CNF['FIELD_EXPIRED'] . "` = '0'");
  167. if(empty($aResult) || !is_array($aResult))
  168. return false;
  169. return count($aResult) == (int)$this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_EXPIRED'] . "` = '1' WHERE `id` IN (" . $this->implode_escape($aResult) . ")") ? $aResult : false;
  170. }
  171. public function getContextsIdsByType($sType, $iProfileId)
  172. {
  173. $CNF = $this->_oConfig->CNF;
  174. //--- Add own tasks
  175. $sQuery = '(SELECT `tp`.`id` FROM `' . $CNF['TABLE_ENTRIES'] . '` AS `tt` INNER JOIN `sys_profiles` AS `tp` ON ABS(`tt`.`allow_view_to`)=`tp`.`id` AND `tp`.`type`=:type WHERE `tt`.`author`=:author GROUP BY `tp`.`id`)';
  176. //--- Add assigned tasks
  177. $oAssignments = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION']);
  178. $aSql = $oAssignments->getConnectedContentAsSQLParts('tt', 'id', $iProfileId);
  179. if($aSql && ($sJoin = $aSql['join'] ?? false))
  180. $sQuery .= ' UNION (SELECT `tp`.`id` FROM `' . $CNF['TABLE_ENTRIES'] . '` AS `tt` INNER JOIN `sys_profiles` AS `tp` ON ABS(`tt`.`allow_view_to`)=`tp`.`id` AND `tp`.`type`=:type ' . $sJoin . ' WHERE 1)';
  181. return $this->getColumn($sQuery, [
  182. 'type' => $sType,
  183. 'author' => $iProfileId
  184. ]);
  185. }
  186. public function getFilters($aParams = [])
  187. {
  188. $CNF = &$this->_oConfig->CNF;
  189. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  190. $sSelectClause = '`tf`.*';
  191. $sJoinClause = $sWhereClause = '';
  192. $sOrderClause = '`tf`.`added` ASC, `tf`.`permanent` ASC';
  193. if(!empty($aParams))
  194. switch($aParams['sample']) {
  195. case 'id':
  196. $aMethod['name'] = 'getRow';
  197. $aMethod['params'][1] = [
  198. 'id' => $aParams['id']
  199. ];
  200. $sWhereClause = "AND `tf`.`id` = :id";
  201. break;
  202. case 'author':
  203. $aMethod['params'][1] = [
  204. 'author' => $aParams['author']
  205. ];
  206. $sWhereClause = "AND `tf`.`author` = :author";
  207. if(isset($aParams['active']) && $aParams['active'] === true)
  208. $sWhereClause .= " AND `tf`.`active` <> '0'";
  209. break;
  210. case 'active':
  211. $sWhereClause .= " AND `tf`.`active` <> '0'";
  212. if(($iContextId = $aParams['context_id'] ?? false) !== false) {
  213. $aMethod['params'][1]['context_id'] = $iContextId;
  214. $sWhereClause .= " AND `tf`.`context_id` = :context_id";
  215. }
  216. if(($iAuthor = $aParams['author'] ?? false) !== false) {
  217. $aMethod['params'][1]['author'] = $iAuthor;
  218. $sWhereClause .= " AND `tf`.`author` = :author";
  219. }
  220. if(($iPermanent = $aParams['permanent'] ?? false) !== false) {
  221. $aMethod['params'][1]['permanent'] = $iPermanent;
  222. $sWhereClause .= " AND `tf`.`permanent` = :permanent";
  223. }
  224. break;
  225. }
  226. if(!empty($sOrderClause))
  227. $sOrderClause = "ORDER BY " . $sOrderClause;
  228. $aMethod['params'][0] = "SELECT
  229. " . $sSelectClause . "
  230. FROM `" . $CNF['TABLE_FILTERS'] . "` AS `tf` " . $sJoinClause . "
  231. WHERE 1 " . $sWhereClause . " " . $sOrderClause;
  232. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  233. }
  234. public function getFilterById($iId)
  235. {
  236. $aFilter = $this->getFilters(['sample' => 'id', 'id' => $iId]);
  237. if(!$aFilter || !is_array($aFilter))
  238. return false;
  239. if(($sK = 'conditions') && $aFilter[$sK])
  240. $aFilter[$sK] = json_decode($aFilter[$sK], true);
  241. return $aFilter;
  242. }
  243. public function cleanFilters($iContextId, $iAuthor, $iLifetime = 3600)
  244. {
  245. $CNF = &$this->_oConfig->CNF;
  246. $this->query("DELETE FROM `" . $CNF['TABLE_FILTERS'] . "` WHERE `context_id` = :context_id AND `author` = :author AND UNIX_TIMESTAMP() - `added` > :lifetime AND `permanent` = '0'", [
  247. 'context_id' => $iContextId,
  248. 'author' => $iAuthor,
  249. 'lifetime' => $iLifetime
  250. ]);
  251. }
  252. public function getTimeTracks($aParams = [])
  253. {
  254. $CNF = &$this->_oConfig->CNF;
  255. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  256. $sSelectClause = '`ttt`.*';
  257. $sJoinClause = $sWhereClause = $sOrderClause = '';
  258. if(!empty($aParams))
  259. switch($aParams['sample']) {
  260. case 'id':
  261. $aMethod['name'] = 'getRow';
  262. $aMethod['params'][1] = [
  263. 'id' => $aParams['id']
  264. ];
  265. $sWhereClause = "AND `ttt`.`id` = :id";
  266. break;
  267. case 'assignees':
  268. $aMethod['name'] = 'getColumn';
  269. $sSelectClause = 'DISTINCT `ttt`.`author_id`';
  270. break;
  271. }
  272. if(!empty($sOrderClause))
  273. $sOrderClause = "ORDER BY " . $sOrderClause;
  274. $aMethod['params'][0] = "SELECT
  275. " . $sSelectClause . "
  276. FROM `" . $CNF['TABLE_TIME_TRACK'] . "` AS `ttt` " . $sJoinClause . "
  277. WHERE 1 " . $sWhereClause . " " . $sOrderClause;
  278. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  279. }
  280. public function insertTimeTrack($aParamsSet)
  281. {
  282. $CNF = &$this->_oConfig->CNF;
  283. if(empty($aParamsSet))
  284. return false;
  285. return $this->query("INSERT INTO `" . $CNF['TABLE_TIME_TRACK'] . "` SET " . $this->arrayToSQL($aParamsSet)) ? $this->lastId() : false;
  286. }
  287. public function getTimers($aParams = [])
  288. {
  289. $CNF = &$this->_oConfig->CNF;
  290. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  291. $sSelectClause = '`tt`.*';
  292. $sJoinClause = $sWhereClause = $sOrderClause = '';
  293. if(!empty($aParams))
  294. switch($aParams['sample']) {
  295. case 'id':
  296. $aMethod['name'] = 'getRow';
  297. $aMethod['params'][1] = [
  298. 'id' => $aParams['id']
  299. ];
  300. $sWhereClause = "AND `tt`.`id` = :id";
  301. break;
  302. case 'content_profile_ids':
  303. $aMethod['name'] = 'getRow';
  304. $aMethod['params'][1] = [
  305. 'content_id' => $aParams['content_id'],
  306. 'profile_id' => $aParams['profile_id']
  307. ];
  308. $sWhereClause = "AND `tt`.`content_id` = :content_id AND `tt`.`profile_id` = :profile_id";
  309. break;
  310. case 'profile_id':
  311. $aMethod['params'][1] = [
  312. 'profile_id' => $aParams['profile_id']
  313. ];
  314. $sWhereClause = "AND `tt`.`profile_id` = :profile_id";
  315. if(isset($aParams['active']) && $aParams['active'] === true) {
  316. $aMethod['name'] = 'getRow';
  317. $sWhereClause .= " AND `tt`.`started` <> '0'";
  318. }
  319. break;
  320. }
  321. if(!empty($sOrderClause))
  322. $sOrderClause = "ORDER BY " . $sOrderClause;
  323. $aMethod['params'][0] = "SELECT
  324. " . $sSelectClause . "
  325. FROM `" . $CNF['TABLE_TIMERS'] . "` AS `tt` " . $sJoinClause . "
  326. WHERE 1 " . $sWhereClause . " " . $sOrderClause;
  327. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  328. }
  329. public function isTimerStarted($iObjectId, $iAuthorId)
  330. {
  331. $aTimer = $this->getTimers([
  332. 'sample' => 'content_profile_ids',
  333. 'content_id' => $iObjectId,
  334. 'profile_id' => $iAuthorId
  335. ]);
  336. return ($aTimer['started'] ?? 0) != 0;
  337. }
  338. public function insertTimer($aParamsSet)
  339. {
  340. $CNF = &$this->_oConfig->CNF;
  341. if(empty($aParamsSet))
  342. return false;
  343. return $this->query("INSERT INTO `" . $CNF['TABLE_TIMERS'] . "` SET " . $this->arrayToSQL($aParamsSet)) ? $this->lastId() : false;
  344. }
  345. public function updateTimer($aParamsSet, $aParamsWhere)
  346. {
  347. $CNF = &$this->_oConfig->CNF;
  348. if(empty($aParamsSet) || empty($aParamsWhere))
  349. return false;
  350. return $this->query("UPDATE `" . $CNF['TABLE_TIMERS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  351. }
  352. public function deleteTimer($aParamsWhere)
  353. {
  354. $CNF = &$this->_oConfig->CNF;
  355. if(empty($aParamsWhere))
  356. return false;
  357. return $this->query("DELETE FROM `" . $CNF['TABLE_TIMERS'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  358. }
  359. public function getPreLists($aParams = [])
  360. {
  361. $CNF = &$this->_oConfig->CNF;
  362. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  363. $sSelectClause = '`tpl`.*';
  364. $sJoinClause = $sWhereClause = $sOrderClause = '';
  365. if(!empty($aParams))
  366. switch($aParams['sample']) {
  367. case 'name':
  368. $aMethod['name'] = 'getRow';
  369. $aMethod['params'][1] = [
  370. 'name' => $aParams['name']
  371. ];
  372. $sWhereClause = "AND `tpl`.`name` = :name";
  373. break;
  374. }
  375. if(!empty($sOrderClause))
  376. $sOrderClause = "ORDER BY " . $sOrderClause;
  377. $aMethod['params'][0] = "SELECT
  378. " . $sSelectClause . "
  379. FROM `" . $CNF['TABLE_PRE_LISTS'] . "` AS `tpl` " . $sJoinClause . "
  380. WHERE 1 " . $sWhereClause . " " . $sOrderClause;
  381. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  382. }
  383. public function getPreValues($aParams = [])
  384. {
  385. $CNF = &$this->_oConfig->CNF;
  386. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  387. $sSelectClause = '`tpv`.*';
  388. $sJoinClause = $sWhereClause = $sOrderClause = '';
  389. if(!empty($aParams))
  390. switch($aParams['sample']) {
  391. case 'value_to_use':
  392. $aMethod['name'] = 'getOne';
  393. $aMethod['params'][1] = [
  394. 'context_id' => $aParams['context_id'],
  395. 'list' => $aParams['list']
  396. ];
  397. $sSelectClause = "IF(ISNULL(MAX(`tpv`.`value`)), 0, MAX(`tpv`.`value`)) + 1";
  398. $sWhereClause = "AND `tpv`.`context_id` = :context_id AND `tpv`.`list` = :list";
  399. break;
  400. case 'id':
  401. $aMethod['name'] = 'getRow';
  402. $aMethod['params'][1] = [
  403. 'id' => $aParams['id']
  404. ];
  405. $sWhereClause = "AND `tpv`.`id` = :id";
  406. break;
  407. case 'context_list_value':
  408. $aMethod['name'] = 'getRow';
  409. $aMethod['params'][1] = [
  410. 'context_id' => $aParams['context_id'],
  411. 'list' => $aParams['list'],
  412. 'value' => $aParams['value']
  413. ];
  414. $sWhereClause = "AND `tpv`.`context_id` = :context_id AND `tpv`.`list` = :list AND `tpv`.`value` = :value";
  415. break;
  416. case 'context_list':
  417. $aMethod['name'] = 'getAllWithKey';
  418. $aMethod['params'][1] = 'value';
  419. $aMethod['params'][2] = [
  420. 'context_id' => $aParams['context_id'],
  421. 'list' => $aParams['list']
  422. ];
  423. $sWhereClause = "AND `tpv`.`context_id` = :context_id AND `tpv`.`list` = :list";
  424. $sOrderClause = "`tpv`.`order` ASC";
  425. break;
  426. }
  427. if(!empty($sOrderClause))
  428. $sOrderClause = "ORDER BY " . $sOrderClause;
  429. $aMethod['params'][0] = "SELECT
  430. " . $sSelectClause . "
  431. FROM `" . $CNF['TABLE_PRE_VALUES'] . "` AS `tpv` " . $sJoinClause . "
  432. WHERE 1 " . $sWhereClause . " " . $sOrderClause;
  433. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  434. }
  435. /**
  436. * Legend:
  437. * grp - group
  438. * cnd - condition
  439. * cnds - conditions
  440. *
  441. * t - table
  442. * f - field
  443. * o - operation
  444. * v - value
  445. * j - joining type (INNER, LEFT)
  446. *
  447. * Addons:
  448. * +a - alias, like ta - table alias
  449. * +j - join, like tj - table join or fj - field join
  450. * +m - main, like tm - table main or fm - field main
  451. */
  452. protected function _getSqlJoinFrom($aCnd)
  453. {
  454. return $this->_getSqlClauseFrom('join', $aCnd);
  455. }
  456. protected function _getSqlJoinFromGroup($aGrp)
  457. {
  458. $sResult = "";
  459. if(empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  460. return $sResult;
  461. $sOprGrp = " ";
  462. foreach($aGrp['cnds'] as $aCnd)
  463. if(($sResultCnd = $this->_getSqlJoinFromCondition($aCnd)) != '')
  464. $sResult .= $sOprGrp . $sResultCnd;
  465. return trim($sResult, $sOprGrp);
  466. }
  467. protected function _getSqlJoinFromCondition($aCnd)
  468. {
  469. $sResult = "";
  470. if(!$this->_isJoinCondition($aCnd))
  471. return $sResult;
  472. $sFldJ = "`" . ($aCnd['taj'] ?? $aCnd['tj']) . "`.`" . $aCnd['fj'] . "`";
  473. $sFldM = "`" . $aCnd['fm'] . "`";
  474. if(($sTblAlsM = $aCnd['tam'] ?? ($aCnd['tm'] ?? false)))
  475. $sFldM = "`" . $sTblAlsM . "`." . $sFldM;
  476. return $aCnd['j'] . " JOIN `" . $aCnd['tj'] . "`" . (($sTblAlsJ = $aCnd['taj'] ?? false) ? " AS `" . $sTblAlsJ . "`" : "") . " ON (" . $sFldM . " = " . $sFldJ . ")";
  477. }
  478. protected function _getSqlWhereFrom($aCnd)
  479. {
  480. return $this->_getSqlClauseFrom('where', $aCnd);
  481. }
  482. protected function _getSqlWhereFromGroup($aGrp)
  483. {
  484. $sResult = "";
  485. if(!isset($aGrp['o'], $aGrp['cnds']) || empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  486. return $sResult;
  487. $sOprGrp = " " . $aGrp['o'] . " ";
  488. foreach($aGrp['cnds'] as $aCnd)
  489. if(($sResultCnd = $this->_getSqlWhereFrom($aCnd)) != '')
  490. $sResult .= $sOprGrp . $sResultCnd;
  491. if(($sResult = trim($sResult, $sOprGrp)) != '')
  492. $sResult = "(" . $sResult . ")";
  493. return $sResult;
  494. }
  495. protected function _getSqlWhereFromCondition($aCnd)
  496. {
  497. $sResult = "";
  498. if(!$this->_isWhereCondition($aCnd))
  499. return $sResult;
  500. if(($sMethod = '_getSqlWhereFromCondition' . bx_gen_method_name($aCnd['f'])) && method_exists($this, $sMethod)) {
  501. $mixedResult = $this->$sMethod($aCnd);
  502. if(!$this->_isWhereCondition($mixedResult))
  503. return $mixedResult;
  504. $aCnd = $mixedResult;
  505. }
  506. $sFld = "`" . $aCnd['f'] . "`";
  507. if(($sTbl = $aCnd['t'] ?? ''))
  508. $sFld = "`" . $sTbl . "`." . $sFld;
  509. switch($aCnd['o']) {
  510. case 'IN':
  511. if(empty($aCnd['v']) || !is_array($aCnd['v']))
  512. break;
  513. $sResult .= $sFld . " IN (" . $this->implode_escape($aCnd['v']) . ")";
  514. break;
  515. case 'LIKE':
  516. $sResult .= $sFld . " LIKE " . $this->escape('%' . $aCnd['v'] . '%');
  517. break;
  518. default:
  519. $sResult .= $sFld . " " . $aCnd['o'] . " " . $this->escape($aCnd['v']);
  520. }
  521. return $sResult;
  522. }
  523. protected function _isJoinCondition($mixedCnd)
  524. {
  525. return is_array($mixedCnd) && isset($mixedCnd['j'], $mixedCnd['tj'], $mixedCnd['fj'], $mixedCnd['fm']);
  526. }
  527. protected function _isWhereCondition($mixedCnd)
  528. {
  529. return is_array($mixedCnd) && isset($mixedCnd['f'], $mixedCnd['v'], $mixedCnd['o']);
  530. }
  531. protected function _getSqlClauseFrom($sType, $aCnd)
  532. {
  533. $sMethod = '';
  534. if(($aCnd['grp'] ?? false) === true)
  535. $sMethod = 'Group';
  536. else if(($aCnd['cnd'] ?? false) === true)
  537. $sMethod = 'Condition';
  538. if(!$sMethod || !method_exists($this, ($sMethod = '_getSql' . bx_gen_method_name($sType) . 'From' . $sMethod)))
  539. return '';
  540. $sResult = $this->$sMethod($aCnd);
  541. if($sResult && $this->_aSqlMarkers)
  542. $sResult = bx_replace_markers($sResult, $this->_aSqlMarkers);
  543. return $sResult;
  544. }
  545. }
  546. /** @} */