BxAdsDb.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 Ads Ads
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxAdsDb extends BxBaseModTextDb
  15. {
  16. function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. function getEntriesBy($aParams = array())
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  24. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  25. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.*";
  26. switch($aParams['type']) {
  27. case 'purchased':
  28. $aMethod['params'][1] = array(
  29. 'buyer_id' => $aParams['buyer_id'],
  30. );
  31. $sJoinClause .= " INNER JOIN `" . $CNF['TABLE_LICENSES'] . "` ON `" . $CNF['TABLE_ENTRIES'] . "`.`id`=`" . $CNF['TABLE_LICENSES'] . "`.`entry_id` AND `" . $CNF['TABLE_LICENSES'] . "`.`profile_id`=:buyer_id";
  32. if(isset($aParams['count']) && $aParams['count'] === true) {
  33. $aMethod['name'] = 'getOne';
  34. $sSelectClause = "COUNT(`" . $CNF['TABLE_ENTRIES'] . "`.`id`)";
  35. }
  36. break;
  37. case 'shipped':
  38. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_SHIPPED'] . "`<>0 AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_RECEIVED'] . "`=0";
  39. if(!empty($aParams['seller_id'])) {
  40. $aMethod['params'][1] = array(
  41. 'seller_id' => $aParams['seller_id']
  42. );
  43. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_AUTHOR'] . "`=:seller_id";
  44. }
  45. if(!empty($aParams['buyer_id'])) {
  46. $aMethod['params'][1] = array(
  47. 'buyer_id' => $aParams['buyer_id'],
  48. 'status' => BX_ADS_OFFER_STATUS_ACCEPTED
  49. );
  50. $sJoinClause .= " INNER JOIN `" . $CNF['TABLE_OFFERS'] . "` ON `" . $CNF['TABLE_ENTRIES'] . "`.`id`=`" . $CNF['TABLE_OFFERS'] . "`.`content_id` AND `" . $CNF['TABLE_OFFERS'] . "`.`author_id`=:buyer_id AND `" . $CNF['TABLE_OFFERS'] . "`.`status`=:status";
  51. }
  52. if(isset($aParams['count']) && $aParams['count'] === true) {
  53. $aMethod['name'] = 'getOne';
  54. $sSelectClause = "COUNT(`" . $CNF['TABLE_ENTRIES'] . "`.`id`)";
  55. }
  56. break;
  57. case 'expired':
  58. $aMethod['params'][1]['days'] = 86400 * (int)$aParams['days'];
  59. $sWhereClause .= " AND UNIX_TIMESTAMP() - `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_CHANGED'] . "` > :days";
  60. break;
  61. default:
  62. return parent::getEntriesBy($aParams);
  63. }
  64. if(!empty($sOrderClause))
  65. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  66. if(!empty($sLimitClause))
  67. $sLimitClause = 'LIMIT ' . $sLimitClause;
  68. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  69. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  70. }
  71. public function insertCategoryType($aParamsSet)
  72. {
  73. $CNF = &$this->_oConfig->CNF;
  74. if(empty($aParamsSet))
  75. return 0;
  76. if((int)$this->query("INSERT INTO `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` SET " . $this->arrayToSQL($aParamsSet)) <= 0)
  77. return 0;
  78. return (int)$this->lastId();
  79. }
  80. public function deleteCategoryType($aParamsWhere)
  81. {
  82. $CNF = &$this->_oConfig->CNF;
  83. if(empty($aParamsWhere))
  84. return false;
  85. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  86. }
  87. public function getCategoryTypes($aParams = array())
  88. {
  89. $CNF = &$this->_oConfig->CNF;
  90. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  91. $sSelectClause = "`tct`.*";
  92. $sWhereClause = $sOrderClause = "";
  93. switch($aParams['type']) {
  94. case 'id':
  95. $aMethod['name'] = 'getRow';
  96. $aMethod['params'][1] = array(
  97. 'id' => $aParams['id']
  98. );
  99. $sWhereClause = " AND `tct`.`id`=:id";
  100. break;
  101. case 'all':
  102. break;
  103. }
  104. if(!empty($sOrderClause))
  105. $sOrderClause = "ORDER BY " . $sOrderClause;
  106. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  107. FROM `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` AS `tct`
  108. WHERE 1" . $sWhereClause . " " . $sOrderClause;
  109. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  110. }
  111. public function updateCategory($aParamsSet, $aParamsWhere)
  112. {
  113. $CNF = &$this->_oConfig->CNF;
  114. if(empty($aParamsSet) || empty($aParamsWhere))
  115. return false;
  116. return $this->query("UPDATE `" . $CNF['TABLE_CATEGORIES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) !== false;
  117. }
  118. public function getCategories($aParams = array())
  119. {
  120. $CNF = &$this->_oConfig->CNF;
  121. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  122. $sSelectClause = "`tc`.*";
  123. $sJoinClause = $sWhereClause = $sGroupClause = "";
  124. $sOrderClause = "`tc`.`order` ASC";
  125. switch($aParams['type']) {
  126. case 'id':
  127. $aMethod['name'] = 'getRow';
  128. $aMethod['params'][1] = array(
  129. 'id' => $aParams['id']
  130. );
  131. $sWhereClause = " AND `tc`.`id`=:id";
  132. break;
  133. case 'id_full':
  134. $aMethod['name'] = 'getRow';
  135. $aMethod['params'][1] = array(
  136. 'id' => $aParams['id']
  137. );
  138. $sSelectClause .= ", `tct`.`name` AS `type_name`, `tct`.`display_add` AS `type_display_add`, `tct`.`display_edit` AS `type_display_edit`, `tct`.`display_view` AS `type_display_view`";
  139. $sJoinClause = " LEFT JOIN `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` AS `tct` ON `tc`.`type`=`tct`.`id`";
  140. $sWhereClause = " AND `tc`.`id`=:id";
  141. break;
  142. case 'parent_id':
  143. $aMethod['params'][1] = array(
  144. 'parent_id' => $aParams['parent_id']
  145. );
  146. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  147. if(isset($aParams['with_content']) && $aParams['with_content'] === true)
  148. $sWhereClause .= " AND `tc`.`items`>0";
  149. if(isset($aParams['active']) && $aParams['active'] === true)
  150. $sWhereClause .= " AND `tc`.`active`=1";
  151. break;
  152. case 'parent_id_count':
  153. $aMethod['name'] = 'getOne';
  154. $aMethod['params'][1] = array(
  155. 'parent_id' => $aParams['parent_id']
  156. );
  157. $sSelectClause = "COUNT(`tc`.`id`)";
  158. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  159. if(isset($aParams['active']) && $aParams['active'] === true)
  160. $sWhereClause .= " AND `tc`.`active`=1";
  161. break;
  162. case 'parent_id_order':
  163. $aMethod['name'] = 'getOne';
  164. $aMethod['params'][1] = array(
  165. 'parent_id' => $aParams['parent_id']
  166. );
  167. $sSelectClause = "MAX(`tc`.`order`)";
  168. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  169. break;
  170. case 'collect_stats':
  171. $aMethod['params'][1] = [];
  172. $aStatusActive = $this->_oConfig->getActiveStatus();
  173. $aStatusActiveAdmin = $this->_oConfig->getActiveStatusAdmin();
  174. $sCountClause = "SELECT COUNT(`te`.`id`) FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te` INNER JOIN `sys_profiles` AS `p` ON (`p`.`id` = `te`.`" . $CNF['FIELD_AUTHOR'] . "` AND `p`.`status` = 'active') WHERE `tc`.`id`=`te`.`" . $CNF['FIELD_CATEGORY'] . "` AND `te`.`" . $CNF['FIELD_STATUS'] . "` IN (" . $this->implode_escape($aStatusActive) . ") AND `te`.`" . $CNF['FIELD_STATUS_ADMIN'] . "` IN (" . $this->implode_escape($aStatusActiveAdmin) . ") AND (`te`.`" . $CNF['FIELD_ALLOW_VIEW_TO'] . "`=" . BX_DOL_PG_ALL . " OR `te`.`" . $CNF['FIELD_ALLOW_VIEW_TO'] . "`<0)";
  175. $sSelectClause = "`tc`.`id`, (" . $sCountClause . ") AS `count`";
  176. if(isset($aParams['category_id'])) {
  177. $aMethod['params'][1]['category_id'] = $aParams['category_id'];
  178. $sWhereClause = " AND `tc`.`id`=:category_id";
  179. }
  180. break;
  181. }
  182. if(!empty($sGroupClause))
  183. $sGroupClause = "GROUP BY " . $sGroupClause;
  184. if(!empty($sOrderClause))
  185. $sOrderClause = "ORDER BY " . $sOrderClause;
  186. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  187. FROM `" . $CNF['TABLE_CATEGORIES'] . "` AS `tc`" . $sJoinClause . "
  188. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  189. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  190. }
  191. public function getDisplays($sDisplayPrefix = '', $mixedDisplayType = '')
  192. {
  193. $sWhereClause = "";
  194. $aBindings = array(
  195. 'display_prefix' => '%' . (!empty($sDisplayPrefix) ? $sDisplayPrefix : $this->_oConfig->getName()) . '%'
  196. );
  197. if(!empty($mixedDisplayType)) {
  198. if(is_string($mixedDisplayType)) {
  199. $sWhereClause = " AND `display_name` LIKE :display_type";
  200. $aBindings['display_type'] = '%' . $mixedDisplayType . '%';
  201. }
  202. else if(is_array($mixedDisplayType)) {
  203. $aWhereClauseOr = array();
  204. foreach($mixedDisplayType as $iIndex => $sValue) {
  205. $aWhereClauseOr[] = "`display_name` LIKE :display_type_" . $iIndex;
  206. $aBindings['display_type_' . $iIndex] = '%' . $sValue . '%';
  207. }
  208. $sWhereClause = " AND (" . implode(" OR ", $aWhereClauseOr) . ")";
  209. }
  210. }
  211. return $this->getAll("SELECT * FROM `sys_form_displays` WHERE `display_name` LIKE :display_prefix" . $sWhereClause, $aBindings);
  212. }
  213. public function cloneDisplay($sDisplayName, $sNewDisplayName, $sNewDisplayTitle)
  214. {
  215. $aDisplay = $this->getRow("SELECT * FROM `sys_form_displays` WHERE `display_name`=:display_name", array('display_name' => $sDisplayName));
  216. if(empty($aDisplay) || !is_array($aDisplay))
  217. return false;
  218. unset($aDisplay['id']);
  219. $aDisplay['display_name'] = $sNewDisplayName;
  220. $aDisplay['title'] = $sNewDisplayTitle;
  221. if((int)$this->query("INSERT INTO `sys_form_displays` SET " . $this->arrayToSQL($aDisplay)) <= 0)
  222. return false;
  223. $iNewDisplayId = (int)$this->lastId();
  224. if((int)$this->query("INSERT INTO `sys_form_display_inputs` SELECT NULL, '" . $sNewDisplayName . "', `input_name`, `visible_for_levels`, `active`, `order` FROM `sys_form_display_inputs` WHERE `display_name`=:display_name AND `active`='1'", array('display_name' => $sDisplayName)) <= 0)
  225. return false;
  226. return true;
  227. }
  228. public function isInterested($iEntryId, $iProfileId)
  229. {
  230. $CNF = &$this->_oConfig->CNF;
  231. return (int)$this->getOne("SELECT `id` FROM `" . $CNF['TABLE_INTERESTED_TRACK'] . "` WHERE `entry_id`=:entry_id AND `profile_id`=:profile_id LIMIT 1", array(
  232. 'entry_id' => $iEntryId,
  233. 'profile_id' => $iProfileId
  234. )) > 0;
  235. }
  236. public function getInterested($aParams)
  237. {
  238. $CNF = &$this->_oConfig->CNF;
  239. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  240. $sSelectClause = "`tit`.*";
  241. $sJoinClause = $sWhereClause = $sGroupClause = "";
  242. $sOrderClause = "`tit`.`id` ASC";
  243. switch($aParams['type']) {
  244. case 'id':
  245. $aMethod['name'] = 'getRow';
  246. $aMethod['params'][1] = array(
  247. 'id' => $aParams['id']
  248. );
  249. $sWhereClause = " AND `tit`.`id`=:id";
  250. break;
  251. }
  252. if(!empty($sGroupClause))
  253. $sGroupClause = "GROUP BY " . $sGroupClause;
  254. if(!empty($sOrderClause))
  255. $sOrderClause = "ORDER BY " . $sOrderClause;
  256. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  257. FROM `" . $CNF['TABLE_INTERESTED_TRACK'] . "` AS `tit`" . $sJoinClause . "
  258. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  259. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  260. }
  261. public function insertInterested($aParamsSet)
  262. {
  263. $CNF = &$this->_oConfig->CNF;
  264. if(empty($aParamsSet))
  265. return 0;
  266. $sSetClause = $this->arrayToSQL($aParamsSet);
  267. if(!isset($aParamsSet['date']))
  268. $sSetClause .= ", `date`=UNIX_TIMESTAMP()";
  269. if((int)$this->query("INSERT INTO `" . $CNF['TABLE_INTERESTED_TRACK'] . "` SET " . $sSetClause) <= 0)
  270. return 0;
  271. return (int)$this->lastId();
  272. }
  273. public function registerLicense($iProfileId, $iEntryId, $iCount, $sOrder, $sLicense)
  274. {
  275. $CNF = &$this->_oConfig->CNF;
  276. $aQueryParams = array(
  277. 'profile_id' => $iProfileId,
  278. 'entry_id' => $iEntryId,
  279. 'count' => $iCount,
  280. 'order' => $sOrder,
  281. 'license' => $sLicense
  282. );
  283. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_LICENSES'] . "` SET " . $this->arrayToSQL($aQueryParams) . ", `added`=UNIX_TIMESTAMP()") > 0;
  284. }
  285. public function unregisterLicense($iProfileId, $iEntryId, $sOrder, $sLicense)
  286. {
  287. $CNF = &$this->_oConfig->CNF;
  288. $sWhereClause = "`profile_id`=:profile_id AND `entry_id`=:entry_id AND `order`=:order AND `license`=:license";
  289. $aWhereBindings = array(
  290. 'profile_id' => $iProfileId,
  291. 'entry_id' => $iEntryId,
  292. 'order' => $sOrder,
  293. 'license' => $sLicense
  294. );
  295. //--- Move to deleted licenses table with 'refund' as reason.
  296. $sQuery = "INSERT IGNORE INTO `" . $CNF['TABLE_LICENSES_DELETED'] . "` SELECT *, 'refund' AS `reason`, UNIX_TIMESTAMP() AS `deleted` FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . $sWhereClause;
  297. $this->query($sQuery, $aWhereBindings);
  298. $sQuery = "DELETE FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . $sWhereClause;
  299. return $this->query($sQuery, $aWhereBindings) !== false;
  300. }
  301. public function getLicense($aParams = array())
  302. {
  303. $CNF = &$this->_oConfig->CNF;
  304. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  305. $sSelectClause = "`tl`.*";
  306. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  307. switch($aParams['type']) {
  308. case 'id':
  309. $aMethod['name'] = 'getRow';
  310. $aMethod['params'][1] = array(
  311. 'id' => $aParams['id']
  312. );
  313. $sWhereClause = " AND `tl`.`id`=:id";
  314. break;
  315. case 'entry_id':
  316. $aMethod['params'][1] = array(
  317. 'entry_id' => $aParams['entry_id']
  318. );
  319. $sWhereClause = " AND `tl`.`entry_id`=:entry_id";
  320. $sOrderClause = "`tl`.`added` DESC";
  321. if(isset($aParams['newest']) && $aParams['newest'] === true) {
  322. $aMethod['name'] = 'getRow';
  323. $sLimitClause = "1";
  324. }
  325. break;
  326. case 'has_by':
  327. $aMethod['name'] = "getOne";
  328. $aMethod['params'][1] = array(
  329. 'profile_id' => $aParams['profile_id'],
  330. 'entry_id' => $aParams['entry_id']
  331. );
  332. $sSelectClause = "`tl`.`id`";
  333. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND `tl`.`entry_id`=:entry_id";
  334. if(!empty($aParams['order'])) {
  335. $aMethod['params'][1]['order'] = $aParams['order'];
  336. $sWhereClause .= " AND `tl`.`order`=:order";
  337. }
  338. $sLimitClause = "1";
  339. break;
  340. }
  341. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  342. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  343. $aMethod['params'][0] = "SELECT
  344. " . $sSelectClause . "
  345. FROM `" . $this->_oConfig->CNF['TABLE_LICENSES'] . "` AS `tl`" . $sJoinClause . "
  346. WHERE 1" . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  347. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  348. }
  349. public function updateLicense($aSet, $aWhere)
  350. {
  351. $CNF = &$this->_oConfig->CNF;
  352. return (int)$this->query("UPDATE `" . $CNF['TABLE_LICENSES'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  353. }
  354. public function deleteLicense($aWhere)
  355. {
  356. $CNF = &$this->_oConfig->CNF;
  357. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  358. }
  359. public function hasLicense($iProfileId, $iEntryId)
  360. {
  361. return (int)$this->getLicense(array(
  362. 'type' => 'has_by',
  363. 'profile_id' => $iProfileId,
  364. 'entry_id' => $iEntryId
  365. )) > 0;
  366. }
  367. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  368. {
  369. $CNF = &$this->_oConfig->CNF;
  370. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  371. if($aSearchParam['operator'] != 'between')
  372. continue;
  373. if(!is_array($aSearchParam['value']) || count($aSearchParam['value']) != 2)
  374. continue;
  375. foreach($aSearchParam['value'] as $iIndex => $sValue) {
  376. switch($sSearchParam) {
  377. case $CNF['FIELD_PRICE']:
  378. $sValue = (float)$sValue;
  379. break;
  380. case $CNF['FIELD_YEAR']:
  381. $sValue = (int)$sValue;
  382. break;
  383. }
  384. $aParams['search_params'][$sSearchParam]['value'][$iIndex] = $sValue;
  385. }
  386. }
  387. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  388. }
  389. public function getOffersBy($aParams = array())
  390. {
  391. $CNF = &$this->_oConfig->CNF;
  392. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  393. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  394. $sSelectClause = "`to`.*";
  395. if(!empty($aParams['type']))
  396. switch($aParams['type']) {
  397. case 'quantity_reserved':
  398. $aMethod['name'] = 'getOne';
  399. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  400. $aMethod['params'][1]['status'] = BX_ADS_OFFER_STATUS_ACCEPTED;
  401. $sSelectClause = "SUM(`to`.`quantity`)";
  402. $sWhereClause = " AND `to`.`content_id`=:content_id AND `to`.`status`=:status";
  403. break;
  404. case 'id':
  405. $aMethod['name'] = 'getRow';
  406. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  407. $sWhereClause = " AND `to`.`id`=:id";
  408. break;
  409. case 'content_id':
  410. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  411. $sWhereClause = " AND `to`.`content_id`=:content_id";
  412. if(isset($aParams['count']) && $aParams['count'] === true) {
  413. $aMethod['name'] = 'getOne';
  414. $sSelectClause = "COUNT(`to`.`id`)";
  415. }
  416. else if(isset($aParams['highest']) && $aParams['highest'] === true) {
  417. $aMethod['name'] = 'getRow';
  418. $sOrderClause = "`to`.`amount` DESC";
  419. $sLimitClause = 1;
  420. }
  421. break;
  422. case 'author_id':
  423. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  424. $sWhereClause = " AND `to`.`author_id`=:author_id";
  425. if(isset($aParams['count']) && $aParams['count'] === true) {
  426. $aMethod['name'] = 'getOne';
  427. $sSelectClause = "COUNT(`to`.`id`)";
  428. }
  429. break;
  430. case 'accepted':
  431. $aMethod['name'] = 'getRow';
  432. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  433. $aMethod['params'][1]['status'] = BX_ADS_OFFER_STATUS_ACCEPTED;
  434. $sWhereClause = " AND `to`.`content_id`=:content_id AND `to`.`status`=:status";
  435. break;
  436. case 'content_author_id':
  437. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  438. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `te` ON `to`.`content_id`=`te`.`id`";
  439. $sWhereClause = " AND `te`.`author`=:author_id";
  440. if(!empty($aParams['status'])) {
  441. $aMethod['params'][1]['status'] = $aParams['status'];
  442. $sWhereClause .= " AND `to`.`status`=:status";
  443. }
  444. if(isset($aParams['count']) && $aParams['count'] === true) {
  445. $aMethod['name'] = 'getOne';
  446. $sSelectClause = "COUNT(`to`.`id`)";
  447. }
  448. break;
  449. case 'content_and_author_ids':
  450. if(!isset($aParams['all']) || $aParams['all'] !== true)
  451. $aMethod['name'] = 'getRow';
  452. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  453. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  454. $sWhereClause = " AND `to`.`content_id`=:content_id AND `to`.`author_id`=:author_id";
  455. if(!empty($aParams['status'])) {
  456. $aMethod['params'][1]['status'] = $aParams['status'];
  457. $sWhereClause .= " AND `to`.`status`=:status";
  458. }
  459. $sOrderClause = "`to`.`added` DESC";
  460. break;
  461. case 'expired':
  462. $aMethod['params'][1]['status'] = BX_ADS_OFFER_STATUS_AWAITING;
  463. $aMethod['params'][1]['hours'] = 3600 * (int)$aParams['hours'];
  464. $sWhereClause .= " AND `to`.`" . $CNF['FIELD_OFR_STATUS'] . "`=:status AND UNIX_TIMESTAMP() - `to`.`" . $CNF['FIELD_OFR_ADDED'] . "` > :hours";
  465. break;
  466. }
  467. if(!empty($sGroupClause))
  468. $sGroupClause = ' GROUP BY ' . $sGroupClause;
  469. if(!empty($sOrderClause))
  470. $sOrderClause = ' ORDER BY ' . $sOrderClause;
  471. if(!empty($sLimitClause))
  472. $sLimitClause = ' LIMIT ' . $sLimitClause;
  473. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_OFFERS'] . "` AS `to` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sGroupClause . $sOrderClause . $sLimitClause;
  474. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  475. }
  476. public function insertOffer($aSet)
  477. {
  478. $CNF = &$this->_oConfig->CNF;
  479. if(!isset($aSet[$CNF['FIELD_OFR_ADDED']]))
  480. $aSet[$CNF['FIELD_OFR_ADDED']] = time();
  481. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_OFFERS'] . "` SET " . $this->arrayToSQL($aSet)) > 0 ? $this->lastId() : false;
  482. }
  483. public function updateOffer($aSet, $aWhere)
  484. {
  485. $CNF = &$this->_oConfig->CNF;
  486. if(!isset($aSet[$CNF['FIELD_OFR_CHANGED']]))
  487. $aSet[$CNF['FIELD_OFR_CHANGED']] = time();
  488. return (int)$this->query("UPDATE `" . $CNF['TABLE_OFFERS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  489. }
  490. public function deleteOffer($aWhere)
  491. {
  492. $CNF = &$this->_oConfig->CNF;
  493. if(empty($aWhere) || !is_array($aWhere))
  494. return false;
  495. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_OFFERS'] . "` WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  496. }
  497. }
  498. /** @} */