BxAdsDb.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 'expired':
  28. $aMethod['params'][1]['days'] = 86400 * (int)$aParams['days'];
  29. $sWhereClause .= " AND UNIX_TIMESTAMP() - `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_CHANGED'] . "` > :days";
  30. break;
  31. default:
  32. return parent::getEntriesBy($aParams);
  33. }
  34. if(!empty($sOrderClause))
  35. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  36. if(!empty($sLimitClause))
  37. $sLimitClause = 'LIMIT ' . $sLimitClause;
  38. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  39. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  40. }
  41. public function insertCategoryType($aParamsSet)
  42. {
  43. $CNF = &$this->_oConfig->CNF;
  44. if(empty($aParamsSet))
  45. return 0;
  46. if((int)$this->query("INSERT INTO `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` SET " . $this->arrayToSQL($aParamsSet)) <= 0)
  47. return 0;
  48. return (int)$this->lastId();
  49. }
  50. public function deleteCategoryType($aParamsWhere)
  51. {
  52. $CNF = &$this->_oConfig->CNF;
  53. if(empty($aParamsWhere))
  54. return false;
  55. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  56. }
  57. public function getCategoryTypes($aParams = array())
  58. {
  59. $CNF = &$this->_oConfig->CNF;
  60. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  61. $sSelectClause = "`tct`.*";
  62. $sWhereClause = $sOrderClause = "";
  63. switch($aParams['type']) {
  64. case 'id':
  65. $aMethod['name'] = 'getRow';
  66. $aMethod['params'][1] = array(
  67. 'id' => $aParams['id']
  68. );
  69. $sWhereClause = " AND `tct`.`id`=:id";
  70. break;
  71. case 'all':
  72. break;
  73. }
  74. if(!empty($sOrderClause))
  75. $sOrderClause = "ORDER BY " . $sOrderClause;
  76. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  77. FROM `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` AS `tct`
  78. WHERE 1" . $sWhereClause . " " . $sOrderClause;
  79. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  80. }
  81. public function updateCategory($aParamsSet, $aParamsWhere)
  82. {
  83. $CNF = &$this->_oConfig->CNF;
  84. if(empty($aParamsSet) || empty($aParamsWhere))
  85. return false;
  86. return $this->query("UPDATE `" . $CNF['TABLE_CATEGORIES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) !== false;
  87. }
  88. public function getCategories($aParams = array())
  89. {
  90. $CNF = &$this->_oConfig->CNF;
  91. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  92. $sSelectClause = "`tc`.*";
  93. $sJoinClause = $sWhereClause = $sGroupClause = "";
  94. $sOrderClause = "`tc`.`order` ASC";
  95. switch($aParams['type']) {
  96. case 'id':
  97. $aMethod['name'] = 'getRow';
  98. $aMethod['params'][1] = array(
  99. 'id' => $aParams['id']
  100. );
  101. $sWhereClause = " AND `tc`.`id`=:id";
  102. break;
  103. case 'id_full':
  104. $aMethod['name'] = 'getRow';
  105. $aMethod['params'][1] = array(
  106. 'id' => $aParams['id']
  107. );
  108. $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`";
  109. $sJoinClause = " LEFT JOIN `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` AS `tct` ON `tc`.`type`=`tct`.`id`";
  110. $sWhereClause = " AND `tc`.`id`=:id";
  111. break;
  112. case 'parent_id':
  113. $aMethod['params'][1] = array(
  114. 'parent_id' => $aParams['parent_id']
  115. );
  116. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  117. if(isset($aParams['with_content']) && $aParams['with_content'] === true)
  118. $sWhereClause .= " AND `tc`.`items`>0";
  119. break;
  120. case 'parent_id_count':
  121. $aMethod['name'] = 'getOne';
  122. $aMethod['params'][1] = array(
  123. 'parent_id' => $aParams['parent_id']
  124. );
  125. $sSelectClause = "COUNT(`tc`.`id`)";
  126. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  127. break;
  128. case 'parent_id_order':
  129. $aMethod['name'] = 'getOne';
  130. $aMethod['params'][1] = array(
  131. 'parent_id' => $aParams['parent_id']
  132. );
  133. $sSelectClause = "MAX(`tc`.`order`)";
  134. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  135. break;
  136. case 'collect_stats':
  137. $sSelectClause = "`tc`.`id`, COUNT(`te`.`id`) AS `count`";
  138. $sJoinClause = $this->prepareAsString(" LEFT JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `te` ON `tc`.`id`=`te`.`category` AND `te`.`status`='active' AND `te`.`status_admin`='active' AND (`te`.`allow_view_to`=? OR `te`.`allow_view_to`<0)", BX_DOL_PG_ALL);
  139. $sGroupClause = "`tc`.`id`";
  140. break;
  141. }
  142. if(!empty($sGroupClause))
  143. $sGroupClause = "GROUP BY " . $sGroupClause;
  144. if(!empty($sOrderClause))
  145. $sOrderClause = "ORDER BY " . $sOrderClause;
  146. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  147. FROM `" . $CNF['TABLE_CATEGORIES'] . "` AS `tc`" . $sJoinClause . "
  148. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  149. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  150. }
  151. public function getDisplays($sDisplayPrefix = '', $mixedDisplayType = '')
  152. {
  153. $sWhereClause = "";
  154. $aBindings = array(
  155. 'display_prefix' => '%' . (!empty($sDisplayPrefix) ? $sDisplayPrefix : $this->_oConfig->getName()) . '%'
  156. );
  157. if(!empty($mixedDisplayType)) {
  158. if(is_string($mixedDisplayType)) {
  159. $sWhereClause = " AND `display_name` LIKE :display_type";
  160. $aBindings['display_type'] = '%' . $mixedDisplayType . '%';
  161. }
  162. else if(is_array($mixedDisplayType)) {
  163. $aWhereClauseOr = array();
  164. foreach($mixedDisplayType as $iIndex => $sValue) {
  165. $aWhereClauseOr[] = "`display_name` LIKE :display_type_" . $iIndex;
  166. $aBindings['display_type_' . $iIndex] = '%' . $sValue . '%';
  167. }
  168. $sWhereClause = " AND (" . implode(" OR ", $aWhereClauseOr) . ")";
  169. }
  170. }
  171. return $this->getAll("SELECT * FROM `sys_form_displays` WHERE `display_name` LIKE :display_prefix" . $sWhereClause, $aBindings);
  172. }
  173. public function cloneDisplay($sDisplayName, $sNewDisplayName, $sNewDisplayTitle)
  174. {
  175. $aDisplay = $this->getRow("SELECT * FROM `sys_form_displays` WHERE `display_name`=:display_name", array('display_name' => $sDisplayName));
  176. if(empty($aDisplay) || !is_array($aDisplay))
  177. return false;
  178. unset($aDisplay['id']);
  179. $aDisplay['display_name'] = $sNewDisplayName;
  180. $aDisplay['title'] = $sNewDisplayTitle;
  181. if((int)$this->query("INSERT INTO `sys_form_displays` SET " . $this->arrayToSQL($aDisplay)) <= 0)
  182. return false;
  183. $iNewDisplayId = (int)$this->lastId();
  184. 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)
  185. return false;
  186. return true;
  187. }
  188. public function isInterested($iEntryId, $iProfileId)
  189. {
  190. $CNF = &$this->_oConfig->CNF;
  191. return (int)$this->getOne("SELECT `id` FROM `" . $CNF['TABLE_INTERESTED_TRACK'] . "` WHERE `entry_id`=:entry_id AND `profile_id`=:profile_id LIMIT 1", array(
  192. 'entry_id' => $iEntryId,
  193. 'profile_id' => $iProfileId
  194. )) > 0;
  195. }
  196. public function getInterested($aParams)
  197. {
  198. $CNF = &$this->_oConfig->CNF;
  199. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  200. $sSelectClause = "`tit`.*";
  201. $sJoinClause = $sWhereClause = $sGroupClause = "";
  202. $sOrderClause = "`tit`.`id` ASC";
  203. switch($aParams['type']) {
  204. case 'id':
  205. $aMethod['name'] = 'getRow';
  206. $aMethod['params'][1] = array(
  207. 'id' => $aParams['id']
  208. );
  209. $sWhereClause = " AND `tit`.`id`=:id";
  210. break;
  211. }
  212. if(!empty($sGroupClause))
  213. $sGroupClause = "GROUP BY " . $sGroupClause;
  214. if(!empty($sOrderClause))
  215. $sOrderClause = "ORDER BY " . $sOrderClause;
  216. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  217. FROM `" . $CNF['TABLE_INTERESTED_TRACK'] . "` AS `tit`" . $sJoinClause . "
  218. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  219. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  220. }
  221. public function insertInterested($aParamsSet)
  222. {
  223. $CNF = &$this->_oConfig->CNF;
  224. if(empty($aParamsSet))
  225. return 0;
  226. $sSetClause = $this->arrayToSQL($aParamsSet);
  227. if(!isset($aParamsSet['date']))
  228. $sSetClause .= ", `date`=UNIX_TIMESTAMP()";
  229. if((int)$this->query("INSERT INTO `" . $CNF['TABLE_INTERESTED_TRACK'] . "` SET " . $sSetClause) <= 0)
  230. return 0;
  231. return (int)$this->lastId();
  232. }
  233. public function registerLicense($iProfileId, $iEntryId, $iCount, $sOrder, $sLicense)
  234. {
  235. $CNF = &$this->_oConfig->CNF;
  236. $aQueryParams = array(
  237. 'profile_id' => $iProfileId,
  238. 'entry_id' => $iEntryId,
  239. 'count' => $iCount,
  240. 'order' => $sOrder,
  241. 'license' => $sLicense
  242. );
  243. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_LICENSES'] . "` SET " . $this->arrayToSQL($aQueryParams) . ", `added`=UNIX_TIMESTAMP()") > 0;
  244. }
  245. public function unregisterLicense($iProfileId, $iEntryId, $sOrder, $sLicense)
  246. {
  247. $CNF = &$this->_oConfig->CNF;
  248. $sWhereClause = "`profile_id`=:profile_id AND `entry_id`=:entry_id AND `order`=:order AND `license`=:license";
  249. $aWhereBindings = array(
  250. 'profile_id' => $iProfileId,
  251. 'entry_id' => $iEntryId,
  252. 'order' => $sOrder,
  253. 'license' => $sLicense
  254. );
  255. //--- Move to deleted licenses table with 'refund' as reason.
  256. $sQuery = "INSERT IGNORE INTO `" . $CNF['TABLE_LICENSES_DELETED'] . "` SELECT *, 'refund' AS `reason`, UNIX_TIMESTAMP() AS `deleted` FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . $sWhereClause;
  257. $this->query($sQuery, $aWhereBindings);
  258. $sQuery = "DELETE FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . $sWhereClause;
  259. return $this->query($sQuery, $aWhereBindings) !== false;
  260. }
  261. public function updateLicense($aSet, $aWhere)
  262. {
  263. $CNF = &$this->_oConfig->CNF;
  264. return (int)$this->query("UPDATE `" . $CNF['TABLE_LICENSES'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  265. }
  266. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  267. {
  268. $CNF = &$this->_oConfig->CNF;
  269. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  270. if($aSearchParam['operator'] != 'between')
  271. continue;
  272. if(!is_array($aSearchParam['value']) || count($aSearchParam['value']) != 2)
  273. continue;
  274. foreach($aSearchParam['value'] as $iIndex => $sValue) {
  275. switch($sSearchParam) {
  276. case $CNF['FIELD_PRICE']:
  277. $sValue = (float)$sValue;
  278. break;
  279. case $CNF['FIELD_YEAR']:
  280. $sValue = (int)$sValue;
  281. break;
  282. }
  283. $aParams['search_params'][$sSearchParam]['value'][$iIndex] = $sValue;
  284. }
  285. }
  286. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  287. }
  288. public function getOffersBy($aParams = array())
  289. {
  290. $CNF = &$this->_oConfig->CNF;
  291. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  292. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  293. $sSelectClause = "`to`.*";
  294. if(!empty($aParams['type']))
  295. switch($aParams['type']) {
  296. case 'id':
  297. $aMethod['name'] = 'getRow';
  298. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  299. $sWhereClause = " AND `to`.`id`=:id";
  300. break;
  301. case 'content_author_id':
  302. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  303. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `te` ON `to`.`content_id`=`te`.`id`";
  304. $sWhereClause = " AND `te`.`author`=:author_id";
  305. if(!empty($aParams['status'])) {
  306. $aMethod['params'][1]['status'] = $aParams['status'];
  307. $sWhereClause .= " AND `to`.`status`=:status";
  308. }
  309. if(isset($aParams['count']) && $aParams['count'] === true) {
  310. $aMethod['name'] = 'getOne';
  311. $sSelectClause = "COUNT(`to`.`id`)";
  312. }
  313. break;
  314. case 'content_and_author_ids':
  315. if(!isset($aParams['all']) || $aParams['all'] !== true)
  316. $aMethod['name'] = 'getRow';
  317. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  318. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  319. $sWhereClause = " AND `to`.`content_id`=:content_id AND `to`.`author_id`=:author_id";
  320. if(!empty($aParams['status'])) {
  321. $aMethod['params'][1]['status'] = $aParams['status'];
  322. $sWhereClause .= " AND `to`.`status`=:status";
  323. }
  324. $sOrderClause = "`to`.`added` DESC";
  325. break;
  326. }
  327. if(!empty($sGroupClause))
  328. $sGroupClause = ' GROUP BY ' . $sGroupClause;
  329. if(!empty($sOrderClause))
  330. $sOrderClause = ' ORDER BY ' . $sOrderClause;
  331. if(!empty($sLimitClause))
  332. $sLimitClause = ' LIMIT ' . $sLimitClause;
  333. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_OFFERS'] . "` AS `to` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sGroupClause . $sOrderClause . $sLimitClause;
  334. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  335. }
  336. public function updateOffer($aSet, $aWhere)
  337. {
  338. $CNF = &$this->_oConfig->CNF;
  339. if(!isset($aSet[$CNF['FIELD_OFR_CHANGED']]))
  340. $aSet[$CNF['FIELD_OFR_CHANGED']] = time();
  341. return (int)$this->query("UPDATE `" . $CNF['TABLE_OFFERS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  342. }
  343. }
  344. /** @} */