BxAdsDb.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getSourcesDetailsForm()
  21. {
  22. $sQuery = "SELECT
  23. `ts`.`id` AS `source_id`,
  24. `ts`.`name` AS `source_name`,
  25. `ts`.`caption` AS `source_caption`,
  26. `ts`.`description` AS `source_description`,
  27. `ts`.`option_prefix` AS `source_option_prefix`,
  28. `tso`.`id` AS `id`,
  29. `tso`.`name` AS `name`,
  30. `tso`.`type` AS `type`,
  31. `tso`.`caption` AS `caption`,
  32. `tso`.`description` AS `description`,
  33. `tso`.`extra` AS `extra`,
  34. `tso`.`check_type` AS `check_type`,
  35. `tso`.`check_params` AS `check_params`,
  36. `tso`.`check_error` AS `check_error`
  37. FROM `" . $this->_sPrefix . "sources` AS `ts`
  38. LEFT JOIN `" . $this->_sPrefix . "sources_options` AS `tso` ON `ts`.`id`=`tso`.`source_id`
  39. WHERE `ts`.`active`='1'
  40. ORDER BY `ts`.`order` ASC, `tso`.`order` ASC";
  41. return $this->getAll($sQuery);
  42. }
  43. public function getSources($aParams = [])
  44. {
  45. $CNF = &$this->_oConfig->CNF;
  46. $aMethod = array('name' => 'getAll', 'params' => [0 => 'query']);
  47. $sWhereClause = "";
  48. switch($aParams['sample']) {
  49. case 'by_name':
  50. $aMethod['name'] = 'getRow';
  51. $aMethod['params'][1] = array(
  52. 'name' => $aParams['name']
  53. );
  54. $sWhereClause = " AND `ts`.`name`=:name";
  55. break;
  56. case 'all':
  57. $aMethod['name'] = 'getAllWithKey';
  58. $aMethod['params'][1] = 'name';
  59. if(!empty($aParams['active']))
  60. $sWhereClause = " AND `ts`.`active`='1'";
  61. break;
  62. }
  63. $aMethod['params'][0] = "SELECT
  64. `ts`.`id` AS `id`,
  65. `ts`.`name` AS `name`,
  66. `ts`.`caption` AS `caption`,
  67. `ts`.`description` AS `description`,
  68. `ts`.`option_prefix` AS `option_prefix`,
  69. `ts`.`class_name` AS `class_name`,
  70. `ts`.`class_file` AS `class_file`
  71. FROM `" . $CNF['TABLE_SOURCES'] . "` AS `ts`
  72. WHERE 1" . $sWhereClause;
  73. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  74. }
  75. public function getOption($aParams)
  76. {
  77. $CNF = &$this->_oConfig->CNF;
  78. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  79. $sSelectClause = "`tso`.*";
  80. $sJoinClause = $sWhereClause = "";
  81. switch($aParams['sample']) {
  82. case 'by_pid_and_name':
  83. $aMethod['name'] = 'getRow';
  84. $aMethod['params'][1] = [
  85. 'source_id' => $aParams['source_id'],
  86. 'name' => $aParams['name'],
  87. ];
  88. $sWhereClause = " AND `tso`.`source_id`=:source_id AND `tso`.`name`=:name";
  89. break;
  90. }
  91. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_SOURCES_OPTIONS'] . "` AS `tso` " . $sJoinClause . " WHERE 1" . $sWhereClause;
  92. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  93. }
  94. public function getSourcesOptions($iProfileId = 0, $iSourceId = 0)
  95. {
  96. $CNF = &$this->_oConfig->CNF;
  97. $aBinding = [
  98. 'profile_id' => $iProfileId
  99. ];
  100. if(empty($iProfileId) && empty($iSourceId))
  101. return $this->getAll("SELECT `id`, `name`, `type` FROM `" . $CNF['TABLE_SOURCES_OPTIONS'] . "`");
  102. $sWhereAddon = "";
  103. if(!empty($iSourceId)) {
  104. $aBinding['source_id'] = $iSourceId;
  105. $sWhereAddon = " AND `tso`.`source_id`=:source_id";
  106. }
  107. $sQuery = "SELECT
  108. `tso`.`name` AS `name`,
  109. `tsov`.`value` AS `value`
  110. FROM `" . $CNF['TABLE_SOURCES_OPTIONS'] . "` AS `tso`
  111. LEFT JOIN `" . $CNF['TABLE_SOURCES_OPTIONS_VALUES'] . "` AS `tsov` ON `tso`.`id`=`tsov`.`option_id`
  112. WHERE 1" . $sWhereAddon . " AND `tsov`.`profile_id`=:profile_id ORDER BY `tso`.`order`";
  113. return $this->getAllWithKey($sQuery, 'name', $aBinding);
  114. }
  115. public function updateSourceOption($iProfileId, $iOptionId, $sValue)
  116. {
  117. $CNF = &$this->_oConfig->CNF;
  118. return $this->query("INSERT INTO `" . $CNF['TABLE_SOURCES_OPTIONS_VALUES'] . "` SET `profile_id`=:profile_id, `option_id`=:option_id, `value`=:value ON DUPLICATE KEY UPDATE `value`=:value", [
  119. 'profile_id' => $iProfileId,
  120. 'option_id' => $iOptionId,
  121. 'value' => $sValue
  122. ]);
  123. }
  124. function getEntriesBy($aParams = array())
  125. {
  126. $CNF = &$this->_oConfig->CNF;
  127. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  128. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  129. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.*";
  130. switch($aParams['type']) {
  131. case 'purchased':
  132. $aMethod['params'][1] = array(
  133. 'buyer_id' => $aParams['buyer_id'],
  134. );
  135. $sJoinClause .= " INNER JOIN `" . $CNF['TABLE_LICENSES'] . "` ON `" . $CNF['TABLE_ENTRIES'] . "`.`id`=`" . $CNF['TABLE_LICENSES'] . "`.`entry_id` AND `" . $CNF['TABLE_LICENSES'] . "`.`profile_id`=:buyer_id";
  136. if(isset($aParams['count']) && $aParams['count'] === true) {
  137. $aMethod['name'] = 'getOne';
  138. $sSelectClause = "COUNT(`" . $CNF['TABLE_ENTRIES'] . "`.`id`)";
  139. }
  140. break;
  141. case 'shipped':
  142. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_SHIPPED'] . "`<>0 AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_RECEIVED'] . "`=0";
  143. if(!empty($aParams['seller_id'])) {
  144. $aMethod['params'][1] = array(
  145. 'seller_id' => $aParams['seller_id']
  146. );
  147. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_AUTHOR'] . "`=:seller_id";
  148. }
  149. if(!empty($aParams['buyer_id'])) {
  150. $aMethod['params'][1] = array(
  151. 'buyer_id' => $aParams['buyer_id'],
  152. 'status' => BX_ADS_OFFER_STATUS_ACCEPTED
  153. );
  154. $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";
  155. }
  156. if(isset($aParams['count']) && $aParams['count'] === true) {
  157. $aMethod['name'] = 'getOne';
  158. $sSelectClause = "COUNT(`" . $CNF['TABLE_ENTRIES'] . "`.`id`)";
  159. }
  160. break;
  161. case 'expired':
  162. $aMethod['params'][1]['days'] = 86400 * (int)$aParams['days'];
  163. $sWhereClause .= " AND UNIX_TIMESTAMP() - `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_CHANGED'] . "` > :days";
  164. break;
  165. case 'promotion':
  166. $aMethod['name'] = 'getPairs';
  167. $aMethod['params'][1] = 'id';
  168. $aMethod['params'][2] = 'weight';
  169. $aMethod['params'][3] = [
  170. 'today' => $this->_oConfig->getDay(),
  171. 'promotion_cpm' => $this->_oConfig->getPromotionCpm(),
  172. ];
  173. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "` AS `id`, CAST(`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_DAILY'] . "` AS UNSIGNED) AS `weight`";
  174. $sJoinClause .= " LEFT JOIN `" . $CNF['TABLE_PROMO_TRACKER'] . "` AS `tpt` ON `tpt`.`entry_id`=`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "` AND `tpt`.`date` = :today";
  175. //--- Check Total Budget
  176. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_TOTAL'] . "` <> 0 AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_TOTAL'] . "` > (:promotion_cpm * `" . $CNF['TABLE_ENTRIES'] . "`.`impressions`)/1000";
  177. //--- Check Daily Budget
  178. $sWhereClause .= " AND (ISNULL(`tpt`.`impressions`) OR `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_DAILY'] . "` > (:promotion_cpm * `tpt`.`impressions`)/1000)";
  179. if(!empty($aParams['seg_viewer']) && is_array($aParams['seg_viewer'])) {
  180. $sWhereSubclause = "1";
  181. if(!empty($aParams['seg_viewer']['gender'])) {
  182. $aMethod['params'][3]['seg_gender'] = $aParams['seg_viewer']['gender'];
  183. $sWhereSubclause .= " AND IF(`seg_gender` <> 0, `seg_gender` & :seg_gender, 1)";
  184. }
  185. if(!empty($aParams['seg_viewer']['age'])) {
  186. $aMethod['params'][3]['seg_age'] = $aParams['seg_viewer']['age'];
  187. $sWhereSubclause .= " AND IF(`seg_age_min` <> 0 AND `seg_age_max` <> 0 AND `seg_age_min` <= `seg_age_max`, `seg_age_min` <= :seg_age AND `seg_age_max` >= :seg_age, 1)";
  188. }
  189. if(!empty($aParams['seg_viewer']['country'])) {
  190. $aMethod['params'][3]['seg_country'] = $aParams['seg_viewer']['country'];
  191. $sWhereSubclause .= " AND IF(`seg_country` <> '', `seg_country` = :seg_country, 1)";
  192. }
  193. if(!empty($aParams['seg_viewer']['tags'])) {
  194. $sTags = "0";
  195. foreach($aParams['seg_viewer']['tags'] as $sTag) {
  196. $aMethod['params'][3]['seg_tag_' . $sTag] = '%' . $sTag . '%';
  197. $sTags .= " OR LOWER(`" . $CNF['FIELD_TAGS'] . "`) LIKE :seg_tag_" . $sTag;
  198. }
  199. $sWhereSubclause .= " AND IF(`seg_tags` <> 0, " . $sTags . ", 1)";
  200. }
  201. if(!empty($sWhereSubclause))
  202. $sWhereClause .= " AND IF(`seg` = 1, " . $sWhereSubclause . ", 1)";
  203. }
  204. //echo $sWhereClause; exit;
  205. $sOrderClause = "";
  206. break;
  207. default:
  208. return parent::getEntriesBy($aParams);
  209. }
  210. if(!empty($sOrderClause))
  211. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  212. if(!empty($sLimitClause))
  213. $sLimitClause = 'LIMIT ' . $sLimitClause;
  214. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  215. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  216. }
  217. public function insertCategoryType($aParamsSet)
  218. {
  219. $CNF = &$this->_oConfig->CNF;
  220. if(empty($aParamsSet))
  221. return 0;
  222. if((int)$this->query("INSERT INTO `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` SET " . $this->arrayToSQL($aParamsSet)) <= 0)
  223. return 0;
  224. return (int)$this->lastId();
  225. }
  226. public function deleteCategoryType($aParamsWhere)
  227. {
  228. $CNF = &$this->_oConfig->CNF;
  229. if(empty($aParamsWhere))
  230. return false;
  231. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  232. }
  233. public function getCategoryTypes($aParams = array())
  234. {
  235. $CNF = &$this->_oConfig->CNF;
  236. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  237. $sSelectClause = "`tct`.*";
  238. $sWhereClause = $sOrderClause = "";
  239. switch($aParams['type']) {
  240. case 'id':
  241. $aMethod['name'] = 'getRow';
  242. $aMethod['params'][1] = array(
  243. 'id' => $aParams['id']
  244. );
  245. $sWhereClause = " AND `tct`.`id`=:id";
  246. break;
  247. case 'all':
  248. break;
  249. }
  250. if(!empty($sOrderClause))
  251. $sOrderClause = "ORDER BY " . $sOrderClause;
  252. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  253. FROM `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` AS `tct`
  254. WHERE 1" . $sWhereClause . " " . $sOrderClause;
  255. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  256. }
  257. public function updateCategory($aParamsSet, $aParamsWhere)
  258. {
  259. $CNF = &$this->_oConfig->CNF;
  260. if(empty($aParamsSet) || empty($aParamsWhere))
  261. return false;
  262. return $this->query("UPDATE `" . $CNF['TABLE_CATEGORIES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) !== false;
  263. }
  264. public function getCategories($aParams = array())
  265. {
  266. $CNF = &$this->_oConfig->CNF;
  267. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  268. $sSelectClause = "`tc`.*";
  269. $sJoinClause = $sWhereClause = $sGroupClause = "";
  270. $sOrderClause = "`tc`.`order` ASC";
  271. switch($aParams['type']) {
  272. case 'id':
  273. $aMethod['name'] = 'getRow';
  274. $aMethod['params'][1] = array(
  275. 'id' => $aParams['id']
  276. );
  277. $sWhereClause = " AND `tc`.`id`=:id";
  278. break;
  279. case 'id_full':
  280. $aMethod['name'] = 'getRow';
  281. $aMethod['params'][1] = array(
  282. 'id' => $aParams['id']
  283. );
  284. $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`";
  285. $sJoinClause = " LEFT JOIN `" . $CNF['TABLE_CATEGORIES_TYPES'] . "` AS `tct` ON `tc`.`type`=`tct`.`id`";
  286. $sWhereClause = " AND `tc`.`id`=:id";
  287. break;
  288. case 'parent_id':
  289. $aMethod['params'][1] = array(
  290. 'parent_id' => $aParams['parent_id']
  291. );
  292. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  293. if(isset($aParams['with_content']) && $aParams['with_content'] === true)
  294. $sWhereClause .= " AND `tc`.`items`>0";
  295. if(isset($aParams['active']) && $aParams['active'] === true)
  296. $sWhereClause .= " AND `tc`.`active`=1";
  297. break;
  298. case 'parent_id_count':
  299. $aMethod['name'] = 'getOne';
  300. $aMethod['params'][1] = array(
  301. 'parent_id' => $aParams['parent_id']
  302. );
  303. $sSelectClause = "COUNT(`tc`.`id`)";
  304. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  305. if(isset($aParams['active']) && $aParams['active'] === true)
  306. $sWhereClause .= " AND `tc`.`active`=1";
  307. break;
  308. case 'parent_id_order':
  309. $aMethod['name'] = 'getOne';
  310. $aMethod['params'][1] = array(
  311. 'parent_id' => $aParams['parent_id']
  312. );
  313. $sSelectClause = "MAX(`tc`.`order`)";
  314. $sWhereClause = " AND `tc`.`parent_id`=:parent_id";
  315. break;
  316. case 'collect_stats':
  317. $aMethod['params'][1] = [];
  318. $aStatusActive = $this->_oConfig->getActiveStatus();
  319. $aStatusActiveAdmin = $this->_oConfig->getActiveStatusAdmin();
  320. $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)";
  321. $sSelectClause = "`tc`.`id`, (" . $sCountClause . ") AS `count`";
  322. if(isset($aParams['category_id'])) {
  323. $aMethod['params'][1]['category_id'] = $aParams['category_id'];
  324. $sWhereClause = " AND `tc`.`id`=:category_id";
  325. }
  326. break;
  327. }
  328. if(!empty($sGroupClause))
  329. $sGroupClause = "GROUP BY " . $sGroupClause;
  330. if(!empty($sOrderClause))
  331. $sOrderClause = "ORDER BY " . $sOrderClause;
  332. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  333. FROM `" . $CNF['TABLE_CATEGORIES'] . "` AS `tc`" . $sJoinClause . "
  334. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  335. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  336. }
  337. public function getDisplays($sDisplayPrefix = '', $mixedDisplayType = '')
  338. {
  339. $sWhereClause = "";
  340. $aBindings = array(
  341. 'display_prefix' => '%' . (!empty($sDisplayPrefix) ? $sDisplayPrefix : $this->_oConfig->getName()) . '%'
  342. );
  343. if(!empty($mixedDisplayType)) {
  344. if(is_string($mixedDisplayType)) {
  345. $sWhereClause = " AND `display_name` LIKE :display_type";
  346. $aBindings['display_type'] = '%' . $mixedDisplayType . '%';
  347. }
  348. else if(is_array($mixedDisplayType)) {
  349. $aWhereClauseOr = array();
  350. foreach($mixedDisplayType as $iIndex => $sValue) {
  351. $aWhereClauseOr[] = "`display_name` LIKE :display_type_" . $iIndex;
  352. $aBindings['display_type_' . $iIndex] = '%' . $sValue . '%';
  353. }
  354. $sWhereClause = " AND (" . implode(" OR ", $aWhereClauseOr) . ")";
  355. }
  356. }
  357. return $this->getAll("SELECT * FROM `sys_form_displays` WHERE `display_name` LIKE :display_prefix" . $sWhereClause, $aBindings);
  358. }
  359. public function cloneDisplay($sDisplayName, $sNewDisplayName, $sNewDisplayTitle)
  360. {
  361. $aDisplay = $this->getRow("SELECT * FROM `sys_form_displays` WHERE `display_name`=:display_name", array('display_name' => $sDisplayName));
  362. if(empty($aDisplay) || !is_array($aDisplay))
  363. return false;
  364. unset($aDisplay['id']);
  365. $aDisplay['display_name'] = $sNewDisplayName;
  366. $aDisplay['title'] = $sNewDisplayTitle;
  367. if((int)$this->query("INSERT INTO `sys_form_displays` SET " . $this->arrayToSQL($aDisplay)) <= 0)
  368. return false;
  369. $iNewDisplayId = (int)$this->lastId();
  370. 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)
  371. return false;
  372. return true;
  373. }
  374. public function isInterested($iEntryId, $iProfileId)
  375. {
  376. $CNF = &$this->_oConfig->CNF;
  377. return (int)$this->getOne("SELECT `id` FROM `" . $CNF['TABLE_INTERESTED_TRACK'] . "` WHERE `entry_id`=:entry_id AND `profile_id`=:profile_id LIMIT 1", array(
  378. 'entry_id' => $iEntryId,
  379. 'profile_id' => $iProfileId
  380. )) > 0;
  381. }
  382. public function getInterested($aParams)
  383. {
  384. $CNF = &$this->_oConfig->CNF;
  385. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  386. $sSelectClause = "`tit`.*";
  387. $sJoinClause = $sWhereClause = $sGroupClause = "";
  388. $sOrderClause = "`tit`.`id` ASC";
  389. switch($aParams['type']) {
  390. case 'id':
  391. $aMethod['name'] = 'getRow';
  392. $aMethod['params'][1] = array(
  393. 'id' => $aParams['id']
  394. );
  395. $sWhereClause = " AND `tit`.`id`=:id";
  396. break;
  397. }
  398. if(!empty($sGroupClause))
  399. $sGroupClause = "GROUP BY " . $sGroupClause;
  400. if(!empty($sOrderClause))
  401. $sOrderClause = "ORDER BY " . $sOrderClause;
  402. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  403. FROM `" . $CNF['TABLE_INTERESTED_TRACK'] . "` AS `tit`" . $sJoinClause . "
  404. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause;
  405. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  406. }
  407. public function insertInterested($aParamsSet)
  408. {
  409. $CNF = &$this->_oConfig->CNF;
  410. if(empty($aParamsSet))
  411. return 0;
  412. $sSetClause = $this->arrayToSQL($aParamsSet);
  413. if(!isset($aParamsSet['date']))
  414. $sSetClause .= ", `date`=UNIX_TIMESTAMP()";
  415. if((int)$this->query("INSERT INTO `" . $CNF['TABLE_INTERESTED_TRACK'] . "` SET " . $sSetClause) <= 0)
  416. return 0;
  417. return (int)$this->lastId();
  418. }
  419. public function getCommodity($aParams = [])
  420. {
  421. $CNF = &$this->_oConfig->CNF;
  422. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  423. $sSelectClause = "`tc`.*";
  424. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  425. switch($aParams['sample']) {
  426. case 'id':
  427. $aMethod['name'] = 'getRow';
  428. $aMethod['params'][1] = [
  429. 'id' => $aParams['id']
  430. ];
  431. $sWhereClause = " AND `tc`.`id`=:id";
  432. break;
  433. case 'id_with_entry':
  434. $aMethod['name'] = 'getRow';
  435. $aMethod['params'][1] = [
  436. 'id' => $aParams['id']
  437. ];
  438. $aFields = $this->getFields($CNF['TABLE_ENTRIES']);
  439. $sFields = implode(", ", array_map(function($sValue) {
  440. return "`te`.`{$sValue}` AS `entry_{$sValue}`";
  441. }, $aFields['original']));
  442. $sSelectClause = "`tc`.`id`, `tc`.`type`, `tc`.`amount`, `tc`.`added`, " . $sFields;
  443. $sJoinClause = " INNER JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `te` ON `tc`.`entry_id`=`te`.`id`";
  444. $sWhereClause = " AND `tc`.`id`=:id";
  445. break;
  446. case 'entry_id':
  447. $aMethod['params'][1] = [
  448. 'entry_id' => $aParams['entry_id']
  449. ];
  450. $sWhereClause = " AND `tc`.`entry_id`=:entry_id";
  451. if(!empty($aParams['type'])) {
  452. $aMethod['params'][1]['type'] = $aParams['type'];
  453. $sWhereClause .= " AND `tc`.`type`=:type";
  454. if(isset($aParams['latest']) && $aParams['latest'] === true) {
  455. $aMethod['name'] = 'getRow';
  456. $sOrderClause = "`tc`.`added` DESC";
  457. $sLimitClause = "1";
  458. }
  459. if(isset($aParams['unpaid']) && $aParams['unpaid'] === true) {
  460. $aMethod['name'] = 'getRow';
  461. $sJoinClause = " LEFT JOIN `" . $CNF['TABLE_PROMO_LICENSES'] . "` AS `tp` ON `tc`.`id`=`tp`.`commodity_id`";
  462. $sWhereClause .= " AND ISNULL(`tp`.`commodity_id`)";
  463. $sLimitClause = "1";
  464. }
  465. }
  466. break;
  467. case 'entry_author':
  468. $aMethod['params'][1] = [
  469. 'author' => $aParams['author']
  470. ];
  471. $sJoinClause = " INNER JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `te` ON `tc`.`entry_id`=`te`.`id`";
  472. $sWhereClause = " AND `te`.`" . $CNF['FIELD_AUTHOR'] . "`=:author";
  473. if(!empty($aParams['type'])) {
  474. $aMethod['params'][1]['type'] = $aParams['type'];
  475. $sWhereClause .= " AND `tc`.`type`=:type";
  476. }
  477. break;
  478. }
  479. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  480. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  481. $aMethod['params'][0] = "SELECT
  482. " . $sSelectClause . "
  483. FROM `" . $CNF['TABLE_COMMODITIES'] . "` AS `tc`" . $sJoinClause . "
  484. WHERE 1" . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  485. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  486. }
  487. public function insertCommodity($iEntryId, $sType, $fAmount)
  488. {
  489. $CNF = &$this->_oConfig->CNF;
  490. $aQueryParams = [
  491. 'entry_id' => $iEntryId,
  492. 'type' => $sType,
  493. 'amount' => $fAmount
  494. ];
  495. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_COMMODITIES'] . "` SET " . $this->arrayToSQL($aQueryParams) . ", `added`=UNIX_TIMESTAMP()") > 0;
  496. }
  497. public function updateCommodity($aSet, $aWhere)
  498. {
  499. $CNF = &$this->_oConfig->CNF;
  500. return (int)$this->query("UPDATE `" . $CNF['TABLE_COMMODITIES'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  501. }
  502. public function deleteCommodity($aWhere)
  503. {
  504. $CNF = &$this->_oConfig->CNF;
  505. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_COMMODITIES'] . "` WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  506. }
  507. public function registerLicense($iProfileId, $iEntryId, $iCount, $sOrder, $sLicense)
  508. {
  509. $CNF = &$this->_oConfig->CNF;
  510. $aQueryParams = array(
  511. 'profile_id' => $iProfileId,
  512. 'entry_id' => $iEntryId,
  513. 'count' => $iCount,
  514. 'order' => $sOrder,
  515. 'license' => $sLicense
  516. );
  517. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_LICENSES'] . "` SET " . $this->arrayToSQL($aQueryParams) . ", `added`=UNIX_TIMESTAMP()") > 0;
  518. }
  519. public function unregisterLicense($iProfileId, $iEntryId, $sOrder, $sLicense)
  520. {
  521. $CNF = &$this->_oConfig->CNF;
  522. $sWhereClause = "`profile_id`=:profile_id AND `entry_id`=:entry_id AND `order`=:order AND `license`=:license";
  523. $aWhereBindings = array(
  524. 'profile_id' => $iProfileId,
  525. 'entry_id' => $iEntryId,
  526. 'order' => $sOrder,
  527. 'license' => $sLicense
  528. );
  529. //--- Move to deleted licenses table with 'refund' as reason.
  530. $sQuery = "INSERT IGNORE INTO `" . $CNF['TABLE_LICENSES_DELETED'] . "` SELECT *, 'refund' AS `reason`, UNIX_TIMESTAMP() AS `deleted` FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . $sWhereClause;
  531. $this->query($sQuery, $aWhereBindings);
  532. $sQuery = "DELETE FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . $sWhereClause;
  533. return $this->query($sQuery, $aWhereBindings) !== false;
  534. }
  535. public function getLicense($aParams = array())
  536. {
  537. $CNF = &$this->_oConfig->CNF;
  538. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  539. $sSelectClause = "`tl`.*";
  540. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  541. switch($aParams['type']) {
  542. case 'id':
  543. $aMethod['name'] = 'getRow';
  544. $aMethod['params'][1] = array(
  545. 'id' => $aParams['id']
  546. );
  547. $sWhereClause = " AND `tl`.`id`=:id";
  548. break;
  549. case 'entry_id':
  550. $aMethod['params'][1] = array(
  551. 'entry_id' => $aParams['entry_id']
  552. );
  553. $sWhereClause = " AND `tl`.`entry_id`=:entry_id";
  554. $sOrderClause = "`tl`.`added` DESC";
  555. if(isset($aParams['newest']) && $aParams['newest'] === true) {
  556. $aMethod['name'] = 'getRow';
  557. $sLimitClause = "1";
  558. }
  559. break;
  560. case 'entry_id_income':
  561. $aMethod['name'] = "getOne";
  562. $aMethod['params'][1] = [
  563. 'entry_id' => $aParams['entry_id']
  564. ];
  565. $sSelectClause = "SUM(`te`.`" . $CNF['FIELD_PRICE'] . "` * `tl`.`count`)";
  566. $sWhereClause = " AND `tl`.`entry_id`=:entry_id";
  567. $sJoinClause = " INNER JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `te` ON `tl`.`entry_id`=`te`.`" . $CNF['FIELD_ID'] . "`";
  568. $sGroupClause = "`tl`.`entry_id`";
  569. break;
  570. case 'has_by':
  571. $aMethod['name'] = "getOne";
  572. $aMethod['params'][1] = array(
  573. 'profile_id' => $aParams['profile_id'],
  574. 'entry_id' => $aParams['entry_id']
  575. );
  576. $sSelectClause = "`tl`.`id`";
  577. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND `tl`.`entry_id`=:entry_id";
  578. if(!empty($aParams['order'])) {
  579. $aMethod['params'][1]['order'] = $aParams['order'];
  580. $sWhereClause .= " AND `tl`.`order`=:order";
  581. }
  582. $sLimitClause = "1";
  583. break;
  584. }
  585. $sGroupClause = !empty($sGroupClause) ? "GROUP BY " . $sGroupClause : $sGroupClause;
  586. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  587. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  588. $aMethod['params'][0] = "SELECT
  589. " . $sSelectClause . "
  590. FROM `" . $this->_oConfig->CNF['TABLE_LICENSES'] . "` AS `tl`" . $sJoinClause . "
  591. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  592. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  593. }
  594. public function updateLicense($aSet, $aWhere)
  595. {
  596. $CNF = &$this->_oConfig->CNF;
  597. return (int)$this->query("UPDATE `" . $CNF['TABLE_LICENSES'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  598. }
  599. public function deleteLicense($aWhere)
  600. {
  601. $CNF = &$this->_oConfig->CNF;
  602. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_LICENSES'] . "` WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  603. }
  604. public function hasLicense($iProfileId, $iEntryId)
  605. {
  606. return (int)$this->getLicense(array(
  607. 'type' => 'has_by',
  608. 'profile_id' => $iProfileId,
  609. 'entry_id' => $iEntryId
  610. )) > 0;
  611. }
  612. public function registerPromotion($iProfileId, $iCommodityId, $iEntryId, $fAmount, $sOrder, $sLicense)
  613. {
  614. $CNF = &$this->_oConfig->CNF;
  615. $aQueryParams = [
  616. 'profile_id' => $iProfileId,
  617. 'commodity_id' => $iCommodityId,
  618. 'entry_id' => $iEntryId,
  619. 'amount' => $fAmount,
  620. 'order' => $sOrder,
  621. 'license' => $sLicense
  622. ];
  623. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_PROMO_LICENSES'] . "` SET " . $this->arrayToSQL($aQueryParams) . ", `added`=UNIX_TIMESTAMP()") > 0;
  624. }
  625. public function unregisterPromotion($iProfileId, $iCommodityId, $iEntryId, $sOrder, $sLicense)
  626. {
  627. $CNF = &$this->_oConfig->CNF;
  628. $aWhereBindings = [
  629. 'profile_id' => $iProfileId,
  630. 'commodity_id' => $iCommodityId,
  631. 'entry_id' => $iEntryId,
  632. 'order' => $sOrder,
  633. 'license' => $sLicense
  634. ];
  635. $sWhereClause = "`profile_id`=:profile_id AND `entry_id`=:entry_id AND `order`=:order AND `license`=:license";
  636. //--- Move to Deleted Promotions table with 'refund' as reason.
  637. $sQuery = "INSERT IGNORE INTO `" . $CNF['TABLE_PROMO_LICENSES_DELETED'] . "` SELECT *, 'refund' AS `reason`, UNIX_TIMESTAMP() AS `deleted` FROM `" . $CNF['TABLE_PROMO_LICENSES'] . "` WHERE " . $sWhereClause;
  638. $this->query($sQuery, $aWhereBindings);
  639. $sQuery = "DELETE FROM `" . $CNF['TABLE_PROMO_LICENSES'] . "` WHERE " . $sWhereClause;
  640. return $this->query($sQuery, $aWhereBindings) !== false;
  641. }
  642. public function getPromotionLicense($aParams = [])
  643. {
  644. $CNF = &$this->_oConfig->CNF;
  645. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  646. $sSelectClause = "`tl`.*";
  647. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  648. switch($aParams['type']) {
  649. case 'id':
  650. $aMethod['name'] = 'getRow';
  651. $aMethod['params'][1] = [
  652. 'id' => $aParams['id']
  653. ];
  654. $sWhereClause = " AND `tl`.`id`=:id";
  655. break;
  656. case 'entry_id_outcome':
  657. $aMethod['name'] = "getOne";
  658. $aMethod['params'][1] = [
  659. 'entry_id' => $aParams['entry_id']
  660. ];
  661. $sSelectClause = "SUM(`tl`.`amount`)";
  662. $sWhereClause = " AND `tl`.`entry_id`=:entry_id";
  663. $sGroupClause = "`tl`.`entry_id`";
  664. break;
  665. }
  666. $sGroupClause = !empty($sGroupClause) ? "GROUP BY " . $sGroupClause : $sGroupClause;
  667. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  668. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  669. $aMethod['params'][0] = "SELECT
  670. " . $sSelectClause . "
  671. FROM `" . $CNF['TABLE_PROMO_LICENSES'] . "` AS `tl`" . $sJoinClause . "
  672. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  673. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  674. }
  675. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  676. {
  677. $CNF = &$this->_oConfig->CNF;
  678. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  679. if($aSearchParam['operator'] != 'between')
  680. continue;
  681. if(!is_array($aSearchParam['value']) || count($aSearchParam['value']) != 2)
  682. continue;
  683. foreach($aSearchParam['value'] as $iIndex => $sValue) {
  684. switch($sSearchParam) {
  685. case $CNF['FIELD_PRICE']:
  686. $sValue = (float)$sValue;
  687. break;
  688. case $CNF['FIELD_YEAR']:
  689. $sValue = (int)$sValue;
  690. break;
  691. }
  692. $aParams['search_params'][$sSearchParam]['value'][$iIndex] = $sValue;
  693. }
  694. }
  695. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  696. }
  697. public function getOffersBy($aParams = array())
  698. {
  699. $CNF = &$this->_oConfig->CNF;
  700. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  701. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  702. $sSelectClause = "`to`.*";
  703. if(!empty($aParams['type']))
  704. switch($aParams['type']) {
  705. case 'quantity_reserved':
  706. $aMethod['name'] = 'getOne';
  707. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  708. $aMethod['params'][1]['status'] = BX_ADS_OFFER_STATUS_ACCEPTED;
  709. $sSelectClause = "SUM(`to`.`quantity`)";
  710. $sWhereClause = " AND `to`.`content_id`=:content_id AND `to`.`status`=:status";
  711. break;
  712. case 'id':
  713. $aMethod['name'] = 'getRow';
  714. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  715. $sWhereClause = " AND `to`.`id`=:id";
  716. break;
  717. case 'content_id':
  718. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  719. $sWhereClause = " AND `to`.`content_id`=:content_id";
  720. if(isset($aParams['count']) && $aParams['count'] === true) {
  721. $aMethod['name'] = 'getOne';
  722. $sSelectClause = "COUNT(`to`.`id`)";
  723. }
  724. else if(isset($aParams['highest']) && $aParams['highest'] === true) {
  725. $aMethod['name'] = 'getRow';
  726. $sOrderClause = "`to`.`amount` DESC";
  727. $sLimitClause = 1;
  728. }
  729. break;
  730. case 'author_id':
  731. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  732. $sWhereClause = " AND `to`.`author_id`=:author_id";
  733. if(isset($aParams['count']) && $aParams['count'] === true) {
  734. $aMethod['name'] = 'getOne';
  735. $sSelectClause = "COUNT(`to`.`id`)";
  736. }
  737. break;
  738. case 'accepted':
  739. $aMethod['name'] = 'getRow';
  740. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  741. $aMethod['params'][1]['status'] = BX_ADS_OFFER_STATUS_ACCEPTED;
  742. $sWhereClause = " AND `to`.`content_id`=:content_id AND `to`.`status`=:status";
  743. break;
  744. case 'content_author_id':
  745. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  746. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_ENTRIES'] . "` AS `te` ON `to`.`content_id`=`te`.`id`";
  747. $sWhereClause = " AND `te`.`author`=:author_id";
  748. if(!empty($aParams['status'])) {
  749. $aMethod['params'][1]['status'] = $aParams['status'];
  750. $sWhereClause .= " AND `to`.`status`=:status";
  751. }
  752. if(isset($aParams['count']) && $aParams['count'] === true) {
  753. $aMethod['name'] = 'getOne';
  754. $sSelectClause = "COUNT(`to`.`id`)";
  755. }
  756. break;
  757. case 'content_and_author_ids':
  758. if(!isset($aParams['all']) || $aParams['all'] !== true)
  759. $aMethod['name'] = 'getRow';
  760. $aMethod['params'][1]['content_id'] = (int)$aParams['content_id'];
  761. $aMethod['params'][1]['author_id'] = (int)$aParams['author_id'];
  762. $sWhereClause = " AND `to`.`content_id`=:content_id AND `to`.`author_id`=:author_id";
  763. if(!empty($aParams['status'])) {
  764. $aMethod['params'][1]['status'] = $aParams['status'];
  765. $sWhereClause .= " AND `to`.`status`=:status";
  766. }
  767. $sOrderClause = "`to`.`added` DESC";
  768. break;
  769. case 'expired':
  770. $aMethod['params'][1]['status'] = BX_ADS_OFFER_STATUS_AWAITING;
  771. $aMethod['params'][1]['hours'] = 3600 * (int)$aParams['hours'];
  772. $sWhereClause .= " AND `to`.`" . $CNF['FIELD_OFR_STATUS'] . "`=:status AND UNIX_TIMESTAMP() - `to`.`" . $CNF['FIELD_OFR_ADDED'] . "` > :hours";
  773. break;
  774. }
  775. if(!empty($sGroupClause))
  776. $sGroupClause = ' GROUP BY ' . $sGroupClause;
  777. if(!empty($sOrderClause))
  778. $sOrderClause = ' ORDER BY ' . $sOrderClause;
  779. if(!empty($sLimitClause))
  780. $sLimitClause = ' LIMIT ' . $sLimitClause;
  781. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_OFFERS'] . "` AS `to` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sGroupClause . $sOrderClause . $sLimitClause;
  782. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  783. }
  784. public function insertOffer($aSet)
  785. {
  786. $CNF = &$this->_oConfig->CNF;
  787. if(!isset($aSet[$CNF['FIELD_OFR_ADDED']]))
  788. $aSet[$CNF['FIELD_OFR_ADDED']] = time();
  789. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_OFFERS'] . "` SET " . $this->arrayToSQL($aSet)) > 0 ? $this->lastId() : false;
  790. }
  791. public function updateOffer($aSet, $aWhere)
  792. {
  793. $CNF = &$this->_oConfig->CNF;
  794. if(!isset($aSet[$CNF['FIELD_OFR_CHANGED']]))
  795. $aSet[$CNF['FIELD_OFR_CHANGED']] = time();
  796. return (int)$this->query("UPDATE `" . $CNF['TABLE_OFFERS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  797. }
  798. public function deleteOffer($aWhere)
  799. {
  800. $CNF = &$this->_oConfig->CNF;
  801. if(empty($aWhere) || !is_array($aWhere))
  802. return false;
  803. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_OFFERS'] . "` WHERE " . (!empty($aWhere) ? $this->arrayToSQL($aWhere, ' AND ') : "1")) > 0;
  804. }
  805. public function getFromPromotionTracker($aParams)
  806. {
  807. $CNF = &$this->_oConfig->CNF;
  808. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query', 1 => []]];
  809. $sSelectClause = "`tpt`.*";
  810. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  811. switch($aParams['sample']) {
  812. case 'impressions_by_entry_id':
  813. $aMethod['name'] = 'getOne';
  814. $aMethod['params'][1]['entry_id'] = (int)$aParams['entry_id'];
  815. $sSelectClause = "SUM(`tpt`.`impressions`)";
  816. $sWhereClause = "AND `tpt`.`entry_id`=:entry_id";
  817. $sGroupClause = "`tpt`.`entry_id`";
  818. break;
  819. }
  820. if(!empty($sGroupClause))
  821. $sGroupClause = ' GROUP BY ' . $sGroupClause;
  822. if(!empty($sOrderClause))
  823. $sOrderClause = ' ORDER BY ' . $sOrderClause;
  824. if(!empty($sLimitClause))
  825. $sLimitClause = ' LIMIT ' . $sLimitClause;
  826. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_PROMO_TRACKER'] . "` AS `tpt` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sGroupClause . $sOrderClause . $sLimitClause;
  827. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  828. }
  829. public function updatePromotionTracker($iEntryId, $sCounter, $iCounter = 1)
  830. {
  831. $CNF = &$this->_oConfig->CNF;
  832. if($this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `" . $sCounter . "`=`" . $sCounter . "`+:counter WHERE `" . $CNF['FIELD_ID'] ."`=:id", ['counter' => $iCounter, 'id' => $iEntryId]) === false)
  833. return false;
  834. $iDate = $this->_oConfig->getDay();
  835. return $this->query("INSERT INTO `" . $CNF['TABLE_PROMO_TRACKER'] . "` (`entry_id`, `date`, `" . $sCounter . "`) VALUES (:entry_id, :date, :counter) ON DUPLICATE KEY UPDATE `" . $sCounter . "`=`" . $sCounter . "`+:counter", [
  836. 'entry_id' => $iEntryId,
  837. 'date' => $iDate,
  838. 'counter' => $iCounter
  839. ]) !== false;
  840. }
  841. }
  842. /** @} */