BxAdsModule.php 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470
  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. define('BX_ADS_STATUS_OFFER', 'offer');
  12. define('BX_ADS_STATUS_SOLD', 'sold');
  13. define('BX_ADS_STATUS_ADMIN_UNPAID', 'unpaid');
  14. define('BX_ADS_COMMODITY_TYPE_PRODUCT', 'product');
  15. define('BX_ADS_COMMODITY_TYPE_PROMOTION', 'promotion');
  16. define('BX_ADS_OFFER_STATUS_ACCEPTED', 'accepted');
  17. define('BX_ADS_OFFER_STATUS_AWAITING', 'awaiting');
  18. define('BX_ADS_OFFER_STATUS_DECLINED', 'declined');
  19. define('BX_ADS_OFFER_STATUS_CANCELED', 'canceled');
  20. define('BX_ADS_OFFER_STATUS_PAID', 'paid');
  21. /**
  22. * Ads module
  23. */
  24. class BxAdsModule extends BxBaseModTextModule
  25. {
  26. protected $_aOfferStatuses;
  27. function __construct(&$aModule)
  28. {
  29. parent::__construct($aModule);
  30. $this->_oConfig->init($this->_oDb);
  31. $CNF = &$this->_oConfig->CNF;
  32. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  33. $CNF['FIELD_CATEGORY_VIEW'],
  34. $CNF['FIELD_CATEGORY_SELECT']
  35. ));
  36. $this->_aOfferStatuses = array(
  37. BX_ADS_OFFER_STATUS_ACCEPTED,
  38. BX_ADS_OFFER_STATUS_AWAITING,
  39. BX_ADS_OFFER_STATUS_DECLINED
  40. );
  41. }
  42. public function decodeDataApi ($aData, $bExtended = false)
  43. {
  44. $CNF = $this->_oConfig->CNF;
  45. $aResult = parent::decodeDataApi($aData, $bExtended);
  46. $aResult[$CNF['FIELD_PRICE']] = $aData[$CNF['FIELD_PRICE']];
  47. $aResult[$CNF['FIELD_QUANTITY']] = $aData[$CNF['FIELD_QUANTITY']];
  48. $aResult[$CNF['FIELD_NOTES_PURCHASED']] = $aData[$CNF['FIELD_NOTES_PURCHASED']];
  49. return $aResult;
  50. }
  51. public function actionLoadEntryFromSource()
  52. {
  53. $sSourceType = bx_process_url_param(bx_process_input(bx_get('source_type')));
  54. $sSource = bx_process_url_param(bx_process_input(bx_get('source')));
  55. if(empty($sSourceType) || empty($sSource))
  56. return echoJson(['code' => 1]);
  57. $aEntry = $this->serviceLoadEntryFromSource($sSourceType, $sSource);
  58. if(empty($aEntry) || !is_array($aEntry))
  59. return echoJson(['code' => 2]);
  60. return echoJson([
  61. 'code' => 0,
  62. 'fields' => $aEntry
  63. ]);
  64. }
  65. public function actionGetCategoryForm()
  66. {
  67. if(($iCategory = bx_get('category')) === false || (int)$iCategory == 0)
  68. return echoJson(['msg' => _t('_bx_ads_form_entry_input_category_select_err')]);
  69. return echoJson([
  70. 'eval' => $this->_oConfig->getJsObject('form') . '.onSelectCategory(oData)',
  71. 'content' => $this->serviceGetCreatePostForm([
  72. 'absolute_action_url' => true,
  73. 'dynamic_mode' => true
  74. ])
  75. ]);
  76. }
  77. public function actionCheckName()
  78. {
  79. $CNF = &$this->_oConfig->CNF;
  80. $iId = (int)bx_get($CNF['FIELD_ID']);
  81. $sTitle = bx_process_input(bx_get($CNF['FIELD_TITLE']));
  82. echoJson($this->serviceCheckName($sTitle, $iId));
  83. }
  84. public function actionInterested()
  85. {
  86. $CNF = &$this->_oConfig->CNF;
  87. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  88. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  89. if(empty($aContentInfo) || !is_array($aContentInfo))
  90. return echoJson(array());
  91. $iViewer = bx_get_logged_profile_id();
  92. $oViewer = BxDolProfile::getInstance($iViewer);
  93. if(!$oViewer)
  94. return echoJson(array());
  95. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  96. if($iContentAuthor == $iViewer)
  97. return echoJson(array('msg' => _t('_bx_ads_txt_err_your_own')));
  98. if($this->_oDb->isInterested($iContentId, $iViewer))
  99. return echoJson(array('msg' => _t('_bx_ads_txt_err_duplicate')));
  100. $iInterestId = $this->_oDb->insertInterested(array('entry_id' => $iContentId, 'profile_id' => $iViewer));
  101. if(!$iInterestId)
  102. return echoJson(array('msg' => _t('_bx_ads_txt_err_cannot_perform_action')));
  103. bx_alert($this->getName(), 'doInterest', $iContentId, $iViewer, array(
  104. 'subobject_id' => $iInterestId,
  105. 'subobject_author_id' => $iViewer,
  106. 'object_author_id' => $iContentAuthor
  107. ));
  108. if(getParam($CNF['PARAM_USE_IIN']) == 'on')
  109. sendMailTemplate($CNF['ETEMPLATE_INTERESTED'], 0, $iContentAuthor, array(
  110. 'viewer_name' => $oViewer->getDisplayName(),
  111. 'viewer_url' => $oViewer->getUrl(),
  112. 'ad_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  113. 'ad_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', array(
  114. 'i' => $CNF['URI_VIEW_ENTRY'],
  115. $CNF['FIELD_ID'] => $iContentId
  116. )))
  117. ));
  118. return echoJson(array('msg' => _t('_bx_ads_txt_msg_author_notified')));
  119. }
  120. public function actionShow()
  121. {
  122. return echoJson($this->_actionChangeStatus(BX_BASE_MOD_TEXT_STATUS_ACTIVE));
  123. }
  124. public function actionHide()
  125. {
  126. return echoJson($this->_actionChangeStatus(BX_BASE_MOD_TEXT_STATUS_HIDDEN));
  127. }
  128. public function actionMakeOffer()
  129. {
  130. $CNF = &$this->_oConfig->CNF;
  131. $sJsObject = $this->_oConfig->getJsObject('entry');
  132. $iAuthorId = bx_get_logged_profile_id();
  133. $iContentId = bx_process_input(bx_get('content_id'), BX_DATA_INT);
  134. if(empty($iAuthorId))
  135. return echoJson(['code' => 1, 'eval' => 'window.open("' . BxDolPermalinks::getInstance()->permalink('page.php?i=login') . '", "_self");']);
  136. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  137. if(empty($aContentInfo) || !is_array($aContentInfo))
  138. return echoJson([]);
  139. if(($mixedCheck = $this->checkAllowedMakeOffer($aContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  140. return echoJson(['code' => 2, 'msg' => $mixedCheck]);
  141. $aOffer = $this->_oDb->getOffersBy([
  142. 'type' => 'content_and_author_ids',
  143. 'content_id' => $iContentId,
  144. 'author_id' => $iAuthorId,
  145. 'status' => BX_ADS_OFFER_STATUS_AWAITING
  146. ]);
  147. if(!empty($aOffer) && is_array($aOffer))
  148. return echoJson(['code' => 3, 'msg' => _t('_bx_ads_txt_err_duplicate')]);
  149. $aOffer = $this->_oDb->getOffersBy([
  150. 'type' => 'content_and_author_ids',
  151. 'content_id' => $iContentId,
  152. 'author_id' => $iAuthorId,
  153. 'status' => BX_ADS_OFFER_STATUS_ACCEPTED
  154. ]);
  155. if(!empty($aOffer) && is_array($aOffer))
  156. return echoJson(['code' => 4, 'msg' => _t('_bx_ads_txt_err_offer_accepted')]);
  157. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_OFFER'], $CNF['OBJECT_FORM_OFFER_DISPLAY_ADD']);
  158. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . bx_append_url_params($this->_oConfig->getBaseUri() . 'make_offer', ['content_id' => $iContentId]);
  159. $oForm->initChecker();
  160. if($oForm->isSubmittedAndValid()) {
  161. $iQuantity = (int)$oForm->getCleanValue($CNF['FIELD_OFR_QUANTITY']);
  162. $iQuantityAvailable = $this->getAvailableQuantity($aContentInfo);
  163. if($iQuantity > $iQuantityAvailable)
  164. return echoJson(['code' => 5, 'msg' => _t('_bx_ads_txt_err_offer_wrong_quantity', $iQuantityAvailable)]);
  165. $aValToAdd = ['content_id' => $iContentId, 'author_id' => $iAuthorId];
  166. if(($iId = (int)$oForm->insert($aValToAdd)) != 0) {
  167. $this->checkAllowedMakeOffer($aContentInfo, true);
  168. $this->onOfferAdded($iId, $aResult);
  169. $aResult = ['code' => 0, 'msg' => _t('_bx_ads_txt_msg_offer_added'), 'eval' => $sJsObject . '.onMakeOffer(oData);', 'id' => $iId];
  170. }
  171. else
  172. $aResult = ['code' => 6, 'msg' => _t('_bx_ads_txt_err_cannot_perform_action')];
  173. return echoJson($aResult);
  174. }
  175. $sContent = BxTemplFunctions::getInstance()->transBox($this->_oConfig->getHtmlIds('offer_popup'), $this->_oTemplate->parseHtmlByName('offer_popup.html', array(
  176. 'form_id' => $oForm->getId(),
  177. 'form' => $oForm->getCode(true)
  178. )));
  179. return echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false, 'removeOnClose' => true))));
  180. }
  181. public function actionViewOffer()
  182. {
  183. $CNF = &$this->_oConfig->CNF;
  184. $iAuthorId = bx_get_logged_profile_id();
  185. $iOfferId = bx_process_input(bx_get('id'), BX_DATA_INT);
  186. $aOffer = $this->_oDb->getOffersBy(array('type' => 'id', 'id' => $iOfferId));
  187. if(empty($aOffer) || !is_array($aOffer))
  188. return echoJson(array());
  189. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_OFFER'], $CNF['OBJECT_FORM_OFFER_DISPLAY_VIEW']);
  190. $oForm->initChecker($aOffer);
  191. $sContent = BxTemplFunctions::getInstance()->transBox($this->_oConfig->getHtmlIds('offer_popup'), $this->_oTemplate->parseHtmlByName('offer_popup.html', array(
  192. 'form_id' => $oForm->getId(),
  193. 'form' => $oForm->getCode(true)
  194. )));
  195. return echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => true, 'removeOnClose' => true))));
  196. }
  197. public function actionAcceptOffer()
  198. {
  199. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  200. if(($mixedResult = $this->offerAccept($iId)) !== true)
  201. return echoJson(['msg' => $mixedResult !== false ? $mixedResult : _t('_bx_ads_txt_err_cannot_perform_action')]);
  202. return echoJson(['msg' => _t('_bx_ads_txt_msg_offer_accepted'), 'reload' => 1]);
  203. }
  204. public function actionDeclineOffer()
  205. {
  206. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  207. if(!$this->offerDecline($iId))
  208. return echoJson(array('msg' => _t('_bx_ads_txt_err_cannot_perform_action')));
  209. return echoJson(array('msg' => _t('_bx_ads_txt_msg_offer_declined'), 'reload' => 1));
  210. }
  211. public function actionCancelOffer()
  212. {
  213. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  214. if(!$this->offerCancel($iId))
  215. return echoJson(['msg' => _t('_bx_ads_txt_err_cannot_perform_action')]);
  216. return echoJson(['msg' => _t('_bx_ads_txt_msg_offer_canceled'), 'reload' => 1]);
  217. }
  218. public function actionPayOffer()
  219. {
  220. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  221. if(($mixedResult = $this->offerPay($iId)) !== false)
  222. return echoJson($mixedResult);
  223. return echoJson(array('msg' => _t('_bx_ads_txt_err_cannot_perform_action')));
  224. }
  225. public function actionShipped()
  226. {
  227. return echoJson($this->_actionMarkAs('shipped'));
  228. }
  229. public function actionReceived()
  230. {
  231. return echoJson($this->_actionMarkAs('received'));
  232. }
  233. public function actionRegisterImpression($iId)
  234. {
  235. echoJson($this->serviceRegisterImpression($iId));
  236. }
  237. public function actionRegisterClick($iId)
  238. {
  239. echoJson($this->serviceRegisterClick($iId));
  240. }
  241. public function serviceGetSafeServices()
  242. {
  243. return array_merge(parent::serviceGetSafeServices(), [
  244. 'IsSourcesAvaliable' => '',
  245. 'LoadEntryFromSource' => '',
  246. 'EntityReviews' => '',
  247. 'EntityReviewsRating' => '',
  248. 'CategoriesList' => '',
  249. 'BrowseCategory' => '',
  250. 'RegisterImpression' => '',
  251. 'RegisterClick' => ''
  252. ]);
  253. }
  254. public function serviceRegisterImpression($iId)
  255. {
  256. return ['code' => $this->_oDb->updatePromotionTracker($iId, 'impressions') ? 0 : 1];
  257. }
  258. public function serviceRegisterClick($iId)
  259. {
  260. $CNF = &$this->_oConfig->CNF;
  261. if(!$this->_oDb->updatePromotionTracker($iId, 'clicks'))
  262. return ['code' => 1];
  263. $sUrl = '';
  264. if($this->_oConfig->isSources()) {
  265. $aEntry = $this->_oDb->getContentInfoById($iId);
  266. if(!empty($aEntry) && is_array($aEntry) && !empty($aEntry[$CNF['FIELD_URL']]))
  267. $sUrl = $aEntry[$CNF['FIELD_URL']];
  268. }
  269. return [
  270. 'code' => 0,
  271. 'redirect' => !empty($sUrl) ? $sUrl : $this->_oConfig->getViewEntryUrl($iId)
  272. ];
  273. }
  274. public function serviceCheckName($sTitle, $iId = 0)
  275. {
  276. $CNF = &$this->_oConfig->CNF;
  277. if(empty($sTitle))
  278. return array();
  279. $sName = '';
  280. if(!empty($iId)) {
  281. $aEntry = $this->_oDb->getContentInfoById($iId);
  282. if(strcmp($sTitle, $aEntry[$CNF['FIELD_NAME']]) == 0)
  283. $sName = $sTitle;
  284. }
  285. return array(
  286. 'title' => $sTitle,
  287. 'name' => !empty($sName) ? $sName : $this->_oConfig->getEntryName($sTitle, $iId)
  288. );
  289. }
  290. public function serviceUpdateCategoriesStats($mixedContentInfo = false)
  291. {
  292. $aContentInfo = [];
  293. if(!empty($mixedContentInfo))
  294. $aContentInfo = !is_array($mixedContentInfo) ? $this->_oDb->getContentInfoById((int)$mixedContentInfo) : $mixedContentInfo;
  295. $iCategoryId = 0;
  296. if($aContentInfo && !empty($aContentInfo['category']))
  297. $iCategoryId = (int)$aContentInfo['category'];
  298. return $this->serviceUpdateCategoriesStatsByCategory($iCategoryId);
  299. }
  300. public function serviceUpdateCategoriesStatsByCategory($iCategoryId = 0)
  301. {
  302. $aParams = ['type' => 'collect_stats'];
  303. if($iCategoryId)
  304. $aParams['category_id'] = (int)$iCategoryId;
  305. $aStats = $this->_oDb->getCategories($aParams);
  306. if(empty($aStats) || !is_array($aStats))
  307. return true;
  308. $iUpdated = 0;
  309. foreach($aStats as $aStat)
  310. if($this->_oDb->updateCategory(array('items' => $aStat['count']), array('id' => $aStat['id'])))
  311. $iUpdated++;
  312. return count($aStats) == $iUpdated;
  313. }
  314. public function serviceGetCategoryOptions($iParentId, $bPleaseSelect = false)
  315. {
  316. $aValues = array();
  317. if($bPleaseSelect)
  318. $aValues[] = array('key' => '', 'value' => _t('_sys_please_select'));
  319. $this->_getCategoryOptions($iParentId, $aValues);
  320. return $aValues;
  321. }
  322. public function serviceGetSearchableFields($aInputsAdd = array())
  323. {
  324. $CNF = &$this->_oConfig->CNF;
  325. $aResult = parent::serviceGetSearchableFields(array_merge($aInputsAdd, $this->_getSearchableFields()));
  326. unset($aResult[$CNF['FIELD_NOTES_PURCHASED']]);
  327. unset($aResult[$CNF['FIELD_CATEGORY_VIEW']], $aResult[$CNF['FIELD_CATEGORY_SELECT']]);
  328. unset($aResult[$CNF['FIELD_PRICE']], $aResult[$CNF['FIELD_YEAR']]);
  329. return $aResult;
  330. }
  331. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  332. {
  333. $CNF = &$this->_oConfig->CNF;
  334. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  335. if(isset($aInputsAdd[$CNF['FIELD_CATEGORY']])) {
  336. $aInputsAdd[$CNF['FIELD_CATEGORY']]['type'] = 'select';
  337. $aInputsAdd[$CNF['FIELD_CATEGORY']]['values_src'] = BxDolService::getSerializedService($this->_oConfig->getName(), 'get_category_options', array(0));
  338. }
  339. if(isset($aInputsAdd[$CNF['FIELD_PRICE']])) {
  340. $aInputsAdd[$CNF['FIELD_PRICE']]['search_type'] = 'text_range';
  341. $aInputsAdd[$CNF['FIELD_PRICE']]['search_operator'] = 'between';
  342. }
  343. if(isset($aInputsAdd[$CNF['FIELD_YEAR']])) {
  344. $aInputsAdd[$CNF['FIELD_YEAR']]['search_type'] = 'text_range';
  345. $aInputsAdd[$CNF['FIELD_YEAR']]['search_operator'] = 'between';
  346. }
  347. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  348. }
  349. /**
  350. * Mark an ad as shipped/received by seller/bauer accordingly.
  351. * @param type $sAction - shipped/received action.
  352. * @param type $iContentId - an ad the action will be performed with.
  353. * @param integer $iProfileSrc - profile, who performed the action.
  354. * @param integer $iProfileDst - profile, the action is pointed on.
  355. * @return boolean - true or a string value with error message.
  356. */
  357. public function serviceMarkAs($sAction, $iContentId, $iProfileSrc = 0, $iProfileDst = 0)
  358. {
  359. $CNF = &$this->_oConfig->CNF;
  360. if(!$iProfileSrc)
  361. $iProfileSrc = bx_get_logged_profile_id();
  362. $sTxtError = '_bx_ads_txt_err_cannot_perform_action';
  363. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  364. $sMethodCheck = 'checkAllowedMark' . bx_gen_method_name($sAction) . 'ForProfile';
  365. if(!method_exists($this, $sMethodCheck))
  366. return _t($sTxtError);
  367. if(($mixedCheckResult = $this->$sMethodCheck($aContentInfo, false, $iProfileSrc)) !== CHECK_ACTION_RESULT_ALLOWED)
  368. return $mixedCheckResult;
  369. if((int)$this->_oDb->updateEntriesBy(array($CNF['FIELD_' . strtoupper($sAction)] => time()), array($CNF['FIELD_ID'] => $iContentId)) == 0)
  370. return _t($sTxtError);
  371. $this->$sMethodCheck($aContentInfo, true, $iProfileSrc);
  372. $sMethodOnResult = 'on' . bx_gen_method_name($sAction);
  373. if(method_exists($this, $sMethodOnResult))
  374. $this->$sMethodOnResult($aContentInfo, $iProfileSrc, $iProfileDst);
  375. return true;
  376. }
  377. public function serviceEntityCreate ($sParams = false)
  378. {
  379. $iCategory = is_numeric($sParams) ? (int)$sParams : 0;
  380. if(($sDisplay = $this->getCategoryDisplay('add', $iCategory)) !== false) {
  381. if(empty($sParams) || !is_array($sParams))
  382. $sParams = array();
  383. $sParams['display'] = $sDisplay;
  384. }
  385. if($iCategory)
  386. BxDolSession::getInstance()->setValue($sDisplay . '_category', $iCategory);
  387. return parent::serviceEntityCreate($sParams);
  388. }
  389. public function serviceEntityEditBudget ($iContentId = 0, $sDisplay = false)
  390. {
  391. $CNF = &$this->_oConfig->CNF;
  392. return parent::serviceEntityEdit($iContentId, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_BUDGET']);
  393. }
  394. /**
  395. * @page service Service Calls
  396. * @section bx_ads Ads
  397. * @subsection bx_ads-page_blocks Page Blocks
  398. * @subsubsection bx_ads-entity_reviews entity_reviews
  399. *
  400. * @code bx_srv('bx_ads', 'entity_reviews', [...]); @endcode
  401. *
  402. * Get reviews for particular content
  403. * @param $iContentId content ID
  404. *
  405. * @see BxAdsModule::serviceEntityReviews
  406. */
  407. /**
  408. * @ref bx_ads-entity_reviews "entity_reviews"
  409. */
  410. public function serviceEntityReviews($iContentId = 0)
  411. {
  412. $CNF = &$this->_oConfig->CNF;
  413. if(empty($CNF['OBJECT_REVIEWS']))
  414. return false;
  415. return $this->_entityComments($CNF['OBJECT_REVIEWS'], $iContentId);
  416. }
  417. /**
  418. * @page service Service Calls
  419. * @section bx_ads Ads
  420. * @subsection bx_ads-page_blocks Page Blocks
  421. * @subsubsection bx_ads-entity_reviews_rating entity_reviews_rating
  422. *
  423. * @code bx_srv('bx_ads', 'entity_reviews_rating', [...]); @endcode
  424. *
  425. * Get reviews rating for particular content
  426. * @param $iContentId content ID
  427. *
  428. * @see BxAdsModule::serviceEntityReviewsRating
  429. */
  430. /**
  431. * @ref bx_ads-entity_reviews_rating "entity_reviews_rating"
  432. */
  433. public function serviceEntityReviewsRating($iContentId = 0)
  434. {
  435. $CNF = &$this->_oConfig->CNF;
  436. if(empty($CNF['OBJECT_REVIEWS']))
  437. return false;
  438. if(!$iContentId)
  439. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  440. if(!$iContentId)
  441. return false;
  442. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_REVIEWS'], $iContentId);
  443. if (!$oCmts || !$oCmts->isEnabled())
  444. return false;
  445. return $oCmts->getRatingBlock(array('in_designbox' => false));
  446. }
  447. public function serviceEntityPromotionGrowth($iContentId = 0)
  448. {
  449. return $this->_serviceTemplateFunc('entryPromotionGrowth', $iContentId);
  450. }
  451. public function serviceEntityPromotionSummary($iContentId = 0)
  452. {
  453. return $this->_serviceTemplateFunc('entryPromotionSummary', $iContentId);
  454. }
  455. public function serviceEntityPromotionRoi($iContentId = 0)
  456. {
  457. return $this->_serviceTemplateFunc('entryPromotionRoi', $iContentId);
  458. }
  459. /**
  460. * @page service Service Calls
  461. * @section bx_ads Ads
  462. * @subsection bx_ads-page_blocks Page Blocks
  463. * @subsubsection bx_ads-categories_list categories_list
  464. *
  465. * @code bx_srv('bx_ads', 'categories_list', [...]); @endcode
  466. *
  467. * Get reviews rating for particular content
  468. * @param $aParams additional params array, such as 'show_empty'
  469. *
  470. * @see BxAdsModule::serviceCategoriesList
  471. */
  472. /**
  473. * @ref bx_ads-categories_list "categories_list"
  474. */
  475. public function serviceCategoriesList($aParams = array())
  476. {
  477. if(!isset($aParams['show_empty']))
  478. $aParams['show_empty'] = true;
  479. return $this->_oTemplate->categoriesList($aParams);
  480. }
  481. /**
  482. * @page service Service Calls
  483. * @section bx_ads Ads
  484. * @subsection bx_ads-browse Browse
  485. * @subsubsection bx_ads-browse_category browse_category
  486. *
  487. * @code bx_srv('bx_ads', 'browse_category', [...]); @endcode
  488. *
  489. * Get reviews rating for particular content
  490. * @param $iCategoryId category ID
  491. * @param $aParams additional params array, such as empty_message, ajax_paginate, etc
  492. *
  493. * @see BxAdsModule::serviceBrowseCategory
  494. */
  495. /**
  496. * @ref bx_ads-browse_category "browse_category"
  497. */
  498. public function serviceBrowseCategory($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true, $aParams = [])
  499. {
  500. $mixedResult = parent::serviceBrowseCategory($sUnitView, $bEmptyMessage, $bAjaxPaginate, $aParams);
  501. if(empty($mixedResult['content']))
  502. return $mixedResult;
  503. $aCategory = $this->_oDb->getCategories([
  504. 'type' => 'id',
  505. 'id' => !empty($aParams['category']) ? (int)$aParams['category'] : bx_process_input(bx_get('category'), BX_DATA_INT)
  506. ]);
  507. if(!empty($aCategory['title']))
  508. $mixedResult['title'] = _t('_bx_ads_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  509. return $mixedResult;
  510. }
  511. public function serviceGetNotificationsData()
  512. {
  513. $sModule = $this->_aModule['name'];
  514. $sEventPrivacy = $sModule . '_allow_view_event_to';
  515. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  516. $sEventPrivacy = '';
  517. $aResult = parent::serviceGetNotificationsData();
  518. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  519. array('group' => $sModule . '_interest', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doInterest', 'module_name' => $sModule, 'module_method' => 'get_notifications_interest', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  520. array('group' => $sModule . '_paid', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'license_register', 'module_name' => $sModule, 'module_method' => 'get_notifications_license_register', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  521. //---> To Buyer
  522. array('group' => $sModule . '_shipped', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'shipped', 'module_name' => $sModule, 'module_method' => 'get_notifications_shipped', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  523. //<---
  524. array('group' => $sModule . '_received', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'received', 'module_name' => $sModule, 'module_method' => 'get_notifications_received', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  525. array('group' => $sModule . '_offer_added', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'offer_added', 'module_name' => $sModule, 'module_method' => 'get_notifications_offer_added', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  526. //---> To Offerer
  527. array('group' => $sModule . '_offer_accepted', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'offer_accepted', 'module_name' => $sModule, 'module_method' => 'get_notifications_offer_accepted', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  528. array('group' => $sModule . '_offer_declined', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'offer_declined', 'module_name' => $sModule, 'module_method' => 'get_notifications_offer_declined', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  529. //<---
  530. array('group' => $sModule . '_offer_canceled', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'offer_canceled', 'module_name' => $sModule, 'module_method' => 'get_notifications_offer_canceled', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy)
  531. ));
  532. $aResult['settings'] = array_merge($aResult['settings'], array(
  533. array('group' => 'interest', 'unit' => $sModule, 'action' => 'doInterest', 'types' => array('personal')),
  534. array('group' => 'usage', 'unit' => $sModule, 'action' => 'license_register', 'types' => array('personal')),
  535. array('group' => 'usage', 'unit' => $sModule, 'action' => 'shipped', 'types' => array('personal')),
  536. array('group' => 'usage', 'unit' => $sModule, 'action' => 'received', 'types' => array('personal')),
  537. array('group' => 'usage', 'unit' => $sModule, 'action' => 'offer_added', 'types' => array('personal')),
  538. array('group' => 'usage', 'unit' => $sModule, 'action' => 'offer_accepted', 'types' => array('personal')),
  539. array('group' => 'usage', 'unit' => $sModule, 'action' => 'offer_declined', 'types' => array('personal')),
  540. array('group' => 'usage', 'unit' => $sModule, 'action' => 'offer_canceled', 'types' => array('personal')),
  541. ));
  542. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  543. array('unit' => $sModule, 'action' => 'doInterest'),
  544. array('unit' => $sModule, 'action' => 'license_register'),
  545. array('unit' => $sModule, 'action' => 'shipped'),
  546. array('unit' => $sModule, 'action' => 'received'),
  547. array('unit' => $sModule, 'action' => 'offer_added'),
  548. array('unit' => $sModule, 'action' => 'offer_accepted'),
  549. array('unit' => $sModule, 'action' => 'offer_declined'),
  550. array('unit' => $sModule, 'action' => 'offer_canceled'),
  551. ));
  552. return $aResult;
  553. }
  554. public function serviceGetNotificationsInsertData($oAlert, $aHandler, $aDataItems)
  555. {
  556. $CNF = &$this->_oConfig->CNF;
  557. $aResult = [];
  558. switch($oAlert->sAction) {
  559. case 'license_register':
  560. $aContentInfo = $this->_oDb->getContentInfoById($oAlert->aExtras['product_id']);
  561. if(empty($aContentInfo) || !is_array($aContentInfo)) {
  562. $aResult = $aDataItems;
  563. break;
  564. }
  565. foreach($aDataItems as $aDataItem) {
  566. $aDataItem = array_merge($aDataItem, [
  567. 'owner_id' => $oAlert->aExtras['profile_id'],
  568. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  569. 'object_owner_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  570. 'object_privacy_view' => BX_DOL_PG_ALL
  571. ]);
  572. $aResult[] = $aDataItem;
  573. }
  574. break;
  575. case 'shipped':
  576. foreach($aDataItems as $aDataItem) {
  577. $aLicense = $this->_oDb->getLicense(['type' => 'entry_id', 'entry_id' => $aDataItem['object_id'], 'newest' => true]);
  578. if(!empty($aLicense) && is_array($aLicense))
  579. $aDataItem['object_owner_id'] = $aLicense['profile_id'];
  580. $aResult[] = $aDataItem;
  581. }
  582. break;
  583. case 'offer_added':
  584. case 'offer_canceled':
  585. foreach($aDataItems as $aDataItem) {
  586. $aDataItem['object_owner_id'] = $oAlert->aExtras['object_author_id'];
  587. $aResult[] = $aDataItem;
  588. }
  589. break;
  590. case 'offer_accepted':
  591. case 'offer_declined':
  592. foreach($aDataItems as $aDataItem) {
  593. $aDataItem['object_owner_id'] = $oAlert->aExtras['offer_author_id'];
  594. $aResult[] = $aDataItem;
  595. }
  596. break;
  597. default:
  598. $aResult = $aDataItems;
  599. }
  600. return $aResult;
  601. }
  602. public function serviceGetNotificationsInterest($aEvent)
  603. {
  604. $CNF = &$this->_oConfig->CNF;
  605. $iContentId = (int)$aEvent['object_id'];
  606. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  607. if(empty($aContentInfo) || !is_array($aContentInfo))
  608. return [];
  609. $iInterestedId = (int)$aEvent['subobject_id'];
  610. $aInterestedInfo = $this->_oDb->getInterested(['type' => 'id', 'id' => $iInterestedId]);
  611. if(empty($aInterestedInfo) || !is_array($aInterestedInfo))
  612. return [];
  613. if(($oInterestedProfile = BxDolProfile::getInstance($aInterestedInfo['profile_id'])) !== false)
  614. $sEntryUrl = $oInterestedProfile->getUrl();
  615. else
  616. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId), '{bx_url_root}');
  617. return [
  618. 'entry_sample' => $CNF['T']['txt_sample_single'],
  619. 'entry_url' => $sEntryUrl,
  620. 'entry_caption' => $aContentInfo[$CNF['FIELD_TITLE']],
  621. 'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  622. 'subentry_sample' => $CNF['T']['txt_sample_interest_single'],
  623. 'subentry_url' => '',
  624. 'lang_key' => '_bx_ads_txt_ntfs_subobject_interested', //may be empty or not specified. In this case the default one from Notification module will be used.
  625. ];
  626. }
  627. public function serviceGetNotificationsLicenseRegister($aEvent)
  628. {
  629. return $this->_serviceGetNotificationsByEntryAndAction($aEvent, 'license_register');
  630. }
  631. public function serviceGetNotificationsShipped($aEvent)
  632. {
  633. $aResult = $this->_serviceGetNotificationsByEntryAndAction($aEvent, 'shipped');
  634. $aResult['entry_author'] = $aEvent['object_owner_id'];
  635. return $aResult;
  636. }
  637. public function serviceGetNotificationsReceived($aEvent)
  638. {
  639. return $this->_serviceGetNotificationsByEntryAndAction($aEvent, 'received');
  640. }
  641. public function serviceGetNotificationsOfferAdded($aEvent)
  642. {
  643. return $this->_serviceGetNotificationsByOfferAndAction($aEvent, 'offer_added');
  644. }
  645. public function serviceGetNotificationsOfferAccepted($aEvent)
  646. {
  647. $aResult = $this->_serviceGetNotificationsByOfferAndAction($aEvent, 'offer_accepted');
  648. $aResult['entry_author'] = $aEvent['object_owner_id'];
  649. return $aResult;
  650. }
  651. public function serviceGetNotificationsOfferDeclined($aEvent)
  652. {
  653. $aResult = $this->_serviceGetNotificationsByOfferAndAction($aEvent, 'offer_declined');
  654. $aResult['entry_author'] = $aEvent['object_owner_id'];
  655. return $aResult;
  656. }
  657. public function serviceGetNotificationsOfferCanceled($aEvent)
  658. {
  659. return $this->_serviceGetNotificationsByOfferAndAction($aEvent, 'offer_canceled');
  660. }
  661. protected function _serviceGetNotificationsByEntryAndAction($aEvent, $sAction)
  662. {
  663. $CNF = &$this->_oConfig->CNF;
  664. $iContentId = (int)$aEvent['object_id'];
  665. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  666. if(empty($aContentInfo) || !is_array($aContentInfo))
  667. return array();
  668. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId), '{bx_url_root}');
  669. return array(
  670. 'entry_sample' => $CNF['T']['txt_sample_single'],
  671. 'entry_url' => $sEntryUrl,
  672. 'entry_caption' => $aContentInfo[$CNF['FIELD_TITLE']],
  673. 'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  674. 'subentry_sample' => '',
  675. 'subentry_url' => '',
  676. 'lang_key' => '_bx_ads_txt_ntfs_object_' . $sAction, //may be empty or not specified. In this case the default one from Notification module will be used.
  677. );
  678. }
  679. protected function _serviceGetNotificationsByOfferAndAction($aEvent, $sAction)
  680. {
  681. $CNF = &$this->_oConfig->CNF;
  682. $iOfferId = (int)$aEvent['object_id'];
  683. $aOfferInfo = $this->_oDb->getOffersBy(['type' => 'id', 'id' => $iOfferId]);
  684. if(empty($aOfferInfo) || !is_array($aOfferInfo))
  685. return array();
  686. $iContentId = $aOfferInfo[$CNF['FIELD_OFR_CONTENT']];
  687. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  688. if(empty($aContentInfo) || !is_array($aContentInfo))
  689. return array();
  690. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId), '{bx_url_root}');
  691. return array(
  692. 'object_id' => $iContentId,
  693. 'entry_sample' => $CNF['T']['txt_sample_single'],
  694. 'entry_url' => $sEntryUrl,
  695. 'entry_caption' => $aContentInfo[$CNF['FIELD_TITLE']],
  696. 'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  697. 'subentry_sample' => '',
  698. 'subentry_url' => '',
  699. 'lang_key' => '_bx_ads_txt_ntfs_' . $sAction, //may be empty or not specified. In this case the default one from Notification module will be used.
  700. );
  701. }
  702. /**
  703. * @page service Service Calls
  704. * @section bx_ads Ads
  705. * @subsection bx_ads-page_blocks Page Blocks
  706. * @subsubsection bx_ads-block_licenses block_licenses
  707. *
  708. * @code bx_srv('bx_ads', 'block_licenses', [...]); @endcode
  709. *
  710. * Get page block with a list of licenses purchased by currently logged member.
  711. *
  712. * @return an array describing a block to display on the site or false if there is no enough input data. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  713. *
  714. * @see BxAdsModule::serviceBlockLicenses
  715. */
  716. /**
  717. * @ref bx_ads-block_licenses "block_licenses"
  718. */
  719. public function serviceBlockLicenses()
  720. {
  721. return $this->_getBlockLicenses();
  722. }
  723. /**
  724. * @page service Service Calls
  725. * @section bx_ads Ads
  726. * @subsection bx_ads-page_blocks Page Blocks
  727. * @subsubsection bx_ads-block_licenses_administration block_licenses_administration
  728. *
  729. * @code bx_srv('bx_ads', 'block_licenses_administration', [...]); @endcode
  730. *
  731. * Get page block with a list of all licenses. It's needed for moderators/administrators.
  732. *
  733. * @return an array describing a block to display on the site or false if there is no enough input data. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  734. *
  735. * @see BxAdsModule::serviceBlockLicensesAdministration
  736. */
  737. /**
  738. * @ref bx_ads-block_licenses "block_licenses"
  739. */
  740. public function serviceBlockLicensesAdministration()
  741. {
  742. return $this->_getBlockLicenses('administration');
  743. }
  744. /**
  745. * @page service Service Calls
  746. * @section bx_ads Ads
  747. * @subsection bx_ads-page_blocks Page Blocks
  748. * @subsubsection bx_ads-block_licenses_note block_licenses_note
  749. *
  750. * @code bx_srv('bx_ads', 'block_licenses_note', [...]); @endcode
  751. *
  752. * Get page block with a notice for licenses usage.
  753. *
  754. * @return HTML string with block content to display on the site.
  755. *
  756. * @see BxAdsModule::serviceBlockLicensesNote
  757. */
  758. /**
  759. * @ref bx_ads-block_licenses_note "block_licenses_note"
  760. */
  761. public function serviceBlockLicensesNote()
  762. {
  763. return MsgBox(_t('_bx_ads_page_block_content_licenses_note'));
  764. }
  765. /**
  766. * @page service Service Calls
  767. * @section bx_ads Ads
  768. * @subsection bx_ads-payments Payments
  769. * @subsubsection bx_ads-get_payment_data get_payment_data
  770. *
  771. * @code bx_srv('bx_ads', 'get_payment_data', [...]); @endcode
  772. *
  773. * Get an array with module's description. Is needed for payments processing module.
  774. *
  775. * @return an array with module's description.
  776. *
  777. * @see BxAdsModule::serviceGetPaymentData
  778. */
  779. /**
  780. * @ref bx_ads-get_payment_data "get_payment_data"
  781. */
  782. public function serviceGetPaymentData()
  783. {
  784. $CNF = &$this->_oConfig->CNF;
  785. $oPermalink = BxDolPermalinks::getInstance();
  786. $aResult = $this->_aModule;
  787. $aResult['url_browse_order_common'] = bx_absolute_url($oPermalink->permalink($CNF['URL_LICENSES_COMMON'], array('filter' => '{order}')));
  788. $aResult['url_browse_order_administration'] = bx_absolute_url($oPermalink->permalink($CNF['URL_LICENSES_ADMINISTRATION'], array('filter' => '{order}')));
  789. return $aResult;
  790. }
  791. /**
  792. * @page service Service Calls
  793. * @section bx_ads Ads
  794. * @subsection bx_ads-payments Payments
  795. * @subsubsection bx_ads-get_cart_item get_cart_item
  796. *
  797. * @code bx_srv('bx_ads', 'get_cart_item', [...]); @endcode
  798. *
  799. * Get an array with prodict's description. Is used in Shopping Cart in payments processing module.
  800. *
  801. * @param $mixedItemId product's ID or Unique Name.
  802. * @return an array with prodict's description. Empty array is returned if something is wrong.
  803. *
  804. * @see BxAdsModule::serviceGetCartItem
  805. */
  806. /**
  807. * @ref bx_ads-get_cart_item "get_cart_item"
  808. */
  809. public function serviceGetCartItem($mixedItemId, $iClientId = 0)
  810. {
  811. $CNF = &$this->_oConfig->CNF;
  812. if(!$mixedItemId || !is_numeric($mixedItemId))
  813. return [];
  814. $aCommodity = $this->_oDb->getCommodity(['sample' => 'id_with_entry', 'id' => (int)$mixedItemId]);
  815. if(empty($aCommodity) || !is_array($aCommodity))
  816. return [];
  817. $iItemId = (int)$aCommodity['id'];
  818. $sItemType = $aCommodity['type'];
  819. $iItemAuthor = 0;
  820. $fItemPrice = 0;
  821. switch($sItemType) {
  822. case BX_ADS_COMMODITY_TYPE_PRODUCT:
  823. $iItemAuthor = $aCommodity['entry_' . $CNF['FIELD_AUTHOR']];
  824. $fItemPrice = (float)$aCommodity['entry_' . $CNF['FIELD_PRICE']];
  825. if($this->_oConfig->isAuction() && (int)$aCommodity['entry_' . $CNF['FIELD_AUCTION']] != 0) {
  826. $aOffer = $this->_oDb->getOffersBy([
  827. 'type' => 'content_and_author_ids',
  828. 'content_id' => $iItemId,
  829. 'author_id' => $iClientId,
  830. 'status' => BX_ADS_OFFER_STATUS_ACCEPTED
  831. ]);
  832. if(!empty($aOffer) && is_array($aOffer))
  833. $fItemPrice = (float)$aOffer[$CNF['FIELD_OFR_AMOUNT']];
  834. }
  835. break;
  836. case BX_ADS_COMMODITY_TYPE_PROMOTION:
  837. $iItemAuthor = BxDolPayments::getInstance()->getOption('site_admin');
  838. $fItemPrice = (float)$aCommodity['amount'];
  839. break;
  840. }
  841. if(!$iClientId)
  842. $iClientId = bx_get_logged_profile_id();
  843. return [
  844. 'id' => $iItemId,
  845. 'author_id' => $iItemAuthor,
  846. 'name' => $aCommodity['entry_' . $CNF['FIELD_NAME']] . '-' . $sItemType,
  847. 'title' => $aCommodity['entry_' . $CNF['FIELD_TITLE']],
  848. 'description' => $sItemDescription = _t($CNF['T']['txt_cd_ct_' . $sItemType]),
  849. 'url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'], ['id' => $aCommodity['entry_' . $CNF['FIELD_ID']]])),
  850. 'price_single' => $fItemPrice,
  851. 'price_recurring' => '',
  852. 'period_recurring' => 0,
  853. 'period_unit_recurring' => '',
  854. 'trial_recurring' => ''
  855. ];
  856. }
  857. /**
  858. * @page service Service Calls
  859. * @section bx_ads Ads
  860. * @subsection bx_ads-payments Payments
  861. * @subsubsection bx_ads-get_cart_items get_cart_items
  862. *
  863. * @code bx_srv('bx_ads', 'get_cart_items', [...]); @endcode
  864. *
  865. * Get an array with prodicts' descriptions by seller. Is used in Manual Order Processing in payments processing module.
  866. *
  867. * @param $iSellerId seller ID.
  868. * @return an array with prodicts' descriptions. Empty array is returned if something is wrong or seller doesn't have any products.
  869. *
  870. * @see BxAdsModule::serviceGetCartItems
  871. */
  872. /**
  873. * @ref bx_ads-get_cart_items "get_cart_items"
  874. */
  875. public function serviceGetCartItems($iSellerId)
  876. {
  877. $CNF = &$this->_oConfig->CNF;
  878. $iSellerId = (int)$iSellerId;
  879. if(empty($iSellerId))
  880. return [];
  881. $aItems = $this->_oDb->getCommodity(['sample' => 'entry_author', 'author' => $iSellerId, 'type' => BX_ADS_COMMODITY_TYPE_PRODUCT]);
  882. $aResult = [];
  883. foreach($aItems as $aItem) {
  884. $aItemInfo = $this->_oDb->getContentInfoById((int)$aItem['entry_id']);
  885. if(empty($aItemInfo) || !is_array($aItemInfo) || $aItemInfo[$CNF['FIELD_STATUS']] != BX_BASE_MOD_TEXT_STATUS_ACTIVE || empty($aItemInfo[$CNF['FIELD_QUANTITY']]))
  886. continue;
  887. $aResult[] = $this->serviceGetCartItem($aItem['id']);
  888. }
  889. return $aResult;
  890. }
  891. /**
  892. * @page service Service Calls
  893. * @section bx_ads Ads
  894. * @subsection bx_ads-payments Payments
  895. * @subsubsection bx_ads-authorize_cart_item authorize_cart_item
  896. *
  897. * @code bx_srv('bx_ads', 'authorize_cart_item', [...]); @endcode
  898. *
  899. * Authorize the order to process a single time payment inside the Ads module in future. Is called with payment processing module after the order was authorized there.
  900. *
  901. * @param $iClientId client ID.
  902. * @param $iSellerId seller ID
  903. * @param $iItemId item ID.
  904. * @param $iItemCount the number of purchased items.
  905. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  906. * @return an array with authorize prodict's description. Empty array is returned if something is wrong.
  907. *
  908. * @see BxAdsModule::serviceAuthorizeCartItem
  909. */
  910. /**
  911. * @ref bx_ads-authorize_cart_item "authorize_cart_item"
  912. */
  913. public function serviceAuthorizeCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  914. {
  915. $CNF = &$this->_oConfig->CNF;
  916. $aItem = $this->serviceGetCartItem($iItemId);
  917. if(empty($aItem) || !is_array($aItem))
  918. return [];
  919. $aCommodity = $this->_oDb->getCommodity(['sample' => 'id_with_entry', 'id' => (int)$iItemId]);
  920. if(empty($aCommodity) || !is_array($aCommodity))
  921. return [];
  922. bx_alert($this->getName(), 'order_authorize', 0, false, [
  923. 'id' => $aCommodity['id'],
  924. 'type' => $aCommodity['type'],
  925. 'product_id' => $aCommodity['entry_id'],
  926. 'profile_id' => $iClientId,
  927. 'order' => $sOrder,
  928. 'count' => $iItemCount
  929. ]);
  930. return $aItem;
  931. }
  932. /**
  933. * @page service Service Calls
  934. * @section bx_ads Ads
  935. * @subsection bx_ads-payments Payments
  936. * @subsubsection bx_ads-register_cart_item register_cart_item
  937. *
  938. * @code bx_srv('bx_ads', 'register_cart_item', [...]); @endcode
  939. *
  940. * Register a processed single time payment inside the Ads module. Is called with payment processing module after the payment was registered there.
  941. *
  942. * @param $iClientId client ID.
  943. * @param $iSellerId seller ID
  944. * @param $iItemId item ID.
  945. * @param $iItemCount the number of purchased items.
  946. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  947. * @param $sLicense license number genereted with payment processing module for internal usage
  948. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  949. *
  950. * @see BxAdsModule::serviceRegisterCartItem
  951. */
  952. /**
  953. * @ref bx_ads-register_cart_item "register_cart_item"
  954. */
  955. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  956. {
  957. $aCommodity = $this->_oDb->getCommodity(['sample' => 'id', 'id' => (int)$iItemId]);
  958. if(empty($aCommodity) || !is_array($aCommodity))
  959. return [];
  960. $sMethod = '_registerCartItem' . bx_gen_method_name($aCommodity['type']);
  961. if(!method_exists($this, $sMethod))
  962. return [];
  963. return $this->$sMethod($iClientId, $iSellerId, $aCommodity, $iItemCount, $sOrder, $sLicense);
  964. }
  965. protected function _registerCartItemProduct($iClientId, $iSellerId, $aCommodity, $iItemCount, $sOrder, $sLicense)
  966. {
  967. $CNF = &$this->_oConfig->CNF;
  968. $sModule = $this->getName();
  969. $aItem = $this->serviceGetCartItem($aCommodity['id']);
  970. if(empty($aItem) || !is_array($aItem))
  971. return [];
  972. $aEntry = $this->_oDb->getContentInfoById($aCommodity['entry_id']);
  973. if(empty($aEntry) || !is_array($aEntry))
  974. return [];
  975. $iEntryId = (int)$aCommodity['entry_id'];
  976. $iEntryQnt = (int)$aEntry[$CNF['FIELD_QUANTITY']];
  977. if(($iEntryQnt - $iItemCount) < 0 || !$this->_oDb->registerLicense($iClientId, $iEntryId, $iItemCount, $sOrder, $sLicense))
  978. return [];
  979. $aOffer = $this->_oDb->getOffersBy([
  980. 'type' => 'content_and_author_ids',
  981. 'content_id' => $iEntryId,
  982. 'author_id' => $iClientId,
  983. 'status' => BX_ADS_OFFER_STATUS_ACCEPTED
  984. ]);
  985. if(!empty($aOffer) && is_array($aOffer))
  986. $this->_oDb->updateOffer([$CNF['FIELD_OFR_STATUS'] => BX_ADS_OFFER_STATUS_PAID], [$CNF['FIELD_OFR_ID'] => $aOffer[$CNF['FIELD_OFR_ID']]]);
  987. $iEntryQnt -= $iItemCount;
  988. $bEntrySold = $iEntryQnt == 0;
  989. $aUpdate = [
  990. $CNF['FIELD_QUANTITY'] => $iEntryQnt
  991. ];
  992. if($bEntrySold)
  993. $aUpdate = array_merge($aUpdate, [
  994. $CNF['FIELD_STATUS'] => BX_ADS_STATUS_SOLD,
  995. $CNF['FIELD_SOLD'] => time()
  996. ]);
  997. $this->_oDb->updateEntriesBy($aUpdate, [$CNF['FIELD_ID'] => $iEntryId]);
  998. bx_alert($sModule, 'license_register', 0, false, [
  999. 'id' => $aCommodity['id'],
  1000. 'type' => $aCommodity['type'],
  1001. 'product_id' => $iEntryId,
  1002. 'profile_id' => $iClientId,
  1003. 'order' => $sOrder,
  1004. 'license' => $sLicense,
  1005. 'count' => $iItemCount
  1006. ]);
  1007. if($bEntrySold) {
  1008. $aParams = $this->_alertParams($aEntry);
  1009. bx_alert($sModule, 'sold', $iEntryId, false, $aParams);
  1010. }
  1011. $oAuthor = BxDolProfile::getInstanceMagic($aEntry[$CNF['FIELD_AUTHOR']]);
  1012. $oClient = BxDolProfile::getInstanceMagic($iClientId);
  1013. $oSeller = BxDolProfile::getInstanceMagic($iSellerId);
  1014. $sSellerUrl = $oSeller->getUrl();
  1015. $sSellerName = $oSeller->getDisplayName();
  1016. $sNote = $aEntry[$CNF['FIELD_NOTES_PURCHASED']];
  1017. if(empty($sNote))
  1018. $sNote = _t('_bx_ads_txt_purchased_note', $sSellerUrl, $sSellerName);
  1019. $sEmailTemplate = $CNF['ETEMPLATE_PURCHASED'];
  1020. $aEmailParams = [
  1021. 'client_name' => $oClient->getDisplayName(),
  1022. 'entry_name' => $aEntry[$CNF['FIELD_TITLE']],
  1023. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'], ['id' => $iEntryId])),
  1024. 'author_url' => $oAuthor->getUrl(),
  1025. 'author_name' => $oAuthor->getDisplayName(),
  1026. 'vendor_url' => $sSellerUrl,
  1027. 'vendor_name' => $sSellerName,
  1028. 'count' => (int)$iItemCount,
  1029. 'license' => $sLicense,
  1030. 'notes' => $sNote,
  1031. ];
  1032. $bCancel = false;
  1033. bx_alert($sModule, 'license_register_notif', 0, false, [
  1034. 'entry_id' => $iEntryId,
  1035. 'order' => $sOrder,
  1036. 'recipient_id' => &$iClientId,
  1037. 'email_template' => &$sEmailTemplate,
  1038. 'email_params' => &$aEmailParams,
  1039. 'cancel' => &$bCancel
  1040. ]);
  1041. if(!$bCancel)
  1042. sendMailTemplate($sEmailTemplate, 0, $iClientId, $aEmailParams);
  1043. return $aItem;
  1044. }
  1045. protected function _registerCartItemPromotion($iClientId, $iSellerId, $aCommodity, $iItemCount, $sOrder, $sLicense)
  1046. {
  1047. $CNF = &$this->_oConfig->CNF;
  1048. $aItem = $this->serviceGetCartItem($aCommodity['id']);
  1049. if(empty($aItem) || !is_array($aItem))
  1050. return [];
  1051. $aEntry = $this->_oDb->getContentInfoById($aCommodity['entry_id']);
  1052. if(empty($aEntry) || !is_array($aEntry))
  1053. return [];
  1054. $iEntryId = (int)$aCommodity['entry_id'];
  1055. if(!$this->_oDb->registerPromotion($aEntry[$CNF['FIELD_AUTHOR']], $aCommodity['id'], $iEntryId, $aCommodity['amount'], $sOrder, $sLicense))
  1056. return [];
  1057. if($aEntry[$CNF['FIELD_STATUS_ADMIN']] == BX_ADS_STATUS_ADMIN_UNPAID) {
  1058. $aUpdate[$CNF['FIELD_STATUS_ADMIN']] = BX_BASE_MOD_GENERAL_STATUS_ACTIVE;
  1059. if(!$this->_isModerator() && !$this->_oConfig->isAutoApproveEnabled())
  1060. $aUpdate[$CNF['FIELD_STATUS_ADMIN']] = BX_BASE_MOD_GENERAL_STATUS_PENDING;
  1061. $this->_oDb->updateEntriesBy($aUpdate, [$CNF['FIELD_ID'] => $iEntryId]);
  1062. }
  1063. bx_alert($this->getName(), 'promotion_register', 0, false, [
  1064. 'id' => $aCommodity['id'],
  1065. 'type' => $aCommodity['type'],
  1066. 'product_id' => $iEntryId,
  1067. 'profile_id' => $iClientId,
  1068. 'amount' => $aCommodity['amount'],
  1069. 'order' => $sOrder,
  1070. 'license' => $sLicense,
  1071. ]);
  1072. return $aItem;
  1073. }
  1074. /**
  1075. * @page service Service Calls
  1076. * @section bx_ads Ads
  1077. * @subsection bx_ads-payments Payments
  1078. * @subsubsection bx_ads-authorize_subscription_item authorize_subscription_item
  1079. *
  1080. * @code bx_srv('bx_ads', 'authorize_subscription_item', [...]); @endcode
  1081. *
  1082. * Authorize the order to process a subscription (recurring payment) inside the Ads module in future. Is called with payment processing module after the order for subscription was authorized there.
  1083. *
  1084. * @param $iClientId client ID.
  1085. * @param $iSellerId seller ID
  1086. * @param $iItemId item ID.
  1087. * @param $iItemCount the number of purchased items.
  1088. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1089. * @return an array with authorize prodict's description. Empty array is returned if something is wrong.
  1090. *
  1091. * @see BxAdsModule::serviceAuthorizeSubscriptionItem
  1092. */
  1093. /**
  1094. * @ref bx_ads-authorize_subscription_item "authorize_subscription_item"
  1095. */
  1096. public function serviceAuthorizeSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  1097. {
  1098. return [];
  1099. }
  1100. /**
  1101. * @page service Service Calls
  1102. * @section bx_ads Ads
  1103. * @subsection bx_ads-payments Payments
  1104. * @subsubsection bx_ads-register_subscription_item register_subscription_item
  1105. *
  1106. * @code bx_srv('bx_ads', 'register_subscription_item', [...]); @endcode
  1107. *
  1108. * Register a processed subscription (recurring payment) inside the Ads module. Is called with payment processing module after the subscription was registered there.
  1109. *
  1110. * @param $iClientId client ID.
  1111. * @param $iSellerId seller ID
  1112. * @param $iItemId item ID.
  1113. * @param $iItemCount the number of purchased items.
  1114. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1115. * @param $sLicense license number genereted with payment processing module for internal usage
  1116. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  1117. *
  1118. * @see BxAdsModule::serviceRegisterSubscriptionItem
  1119. */
  1120. /**
  1121. * @ref bx_ads-register_subscription_item "register_subscription_item"
  1122. */
  1123. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1124. {
  1125. return [];
  1126. }
  1127. /**
  1128. * @page service Service Calls
  1129. * @section bx_ads Ads
  1130. * @subsection bx_ads-payments Payments
  1131. * @subsubsection bx_ads-reregister_cart_item reregister_cart_item
  1132. *
  1133. * @code bx_srv('bx_ads', 'reregister_cart_item', [...]); @endcode
  1134. *
  1135. * Reregister a single time payment inside the Ads module. Is called with payment processing module after the payment was reregistered there.
  1136. *
  1137. * @param $iClientId client ID.
  1138. * @param $iSellerId seller ID
  1139. * @param $iItemIdOld old item ID.
  1140. * @param $iItemIdNew new item ID.
  1141. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1142. * @return an array with purchased prodict's description. Empty array is returned if something is wrong.
  1143. *
  1144. * @see BxAdsModule::serviceReregisterCartItem
  1145. */
  1146. /**
  1147. * @ref bx_ads-reregister_cart_item "reregister_cart_item"
  1148. */
  1149. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  1150. {
  1151. return [];
  1152. }
  1153. /**
  1154. * @page service Service Calls
  1155. * @section bx_ads Ads
  1156. * @subsection bx_ads-payments Payments
  1157. * @subsubsection bx_ads-reregister_subscription_item reregister_subscription_item
  1158. *
  1159. * @code bx_srv('bx_ads', 'reregister_subscription_item', [...]); @endcode
  1160. *
  1161. * Reregister a subscription (recurring payment) inside the Ads module. Is called with payment processing module after the subscription was reregistered there.
  1162. *
  1163. * @param $iClientId client ID.
  1164. * @param $iSellerId seller ID
  1165. * @param $iItemIdOld old item ID.
  1166. * @param $iItemIdNew new item ID.
  1167. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1168. * @return an array with subscribed prodict's description. Empty array is returned if something is wrong.
  1169. *
  1170. * @see BxAdsModule::serviceReregisterSubscriptionItem
  1171. */
  1172. /**
  1173. * @ref bx_ads-reregister_subscription_item "reregister_subscription_item"
  1174. */
  1175. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  1176. {
  1177. return [];
  1178. }
  1179. /**
  1180. * @page service Service Calls
  1181. * @section bx_ads Ads
  1182. * @subsection bx_ads-payments Payments
  1183. * @subsubsection bx_ads-unregister_cart_item unregister_cart_item
  1184. *
  1185. * @code bx_srv('bx_ads', 'unregister_cart_item', [...]); @endcode
  1186. *
  1187. * Unregister an earlier processed single time payment inside the Ads module. Is called with payment processing module after the payment was unregistered there.
  1188. *
  1189. * @param $iClientId client ID.
  1190. * @param $iSellerId seller ID
  1191. * @param $iItemId item ID.
  1192. * @param $iItemCount the number of purchased items.
  1193. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1194. * @param $sLicense license number genereted with payment processing module for internal usage
  1195. * @return boolean value determining where the payment was unregistered or not.
  1196. *
  1197. * @see BxAdsModule::serviceUnregisterCartItem
  1198. */
  1199. /**
  1200. * @ref bx_ads-unregister_cart_item "unregister_cart_item"
  1201. */
  1202. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1203. {
  1204. $aCommodity = $this->_oDb->getCommodity(['sample' => 'id', 'id' => (int)$iItemId]);
  1205. if(empty($aCommodity) || !is_array($aCommodity))
  1206. return [];
  1207. $sMethod = '_unregisterCartItem' . bx_gen_method_name($aCommodity['type']);
  1208. if(!method_exists($this, $sMethod))
  1209. return [];
  1210. return $this->$sMethod($iClientId, $iSellerId, $aCommodity, $iItemCount, $sOrder, $sLicense);
  1211. }
  1212. protected function _unregisterCartItemProduct($iClientId, $iSellerId, $aCommodity, $iItemCount, $sOrder, $sLicense)
  1213. {
  1214. $iEntryId = (int)$aCommodity['entry_id'];
  1215. if(!$this->_oDb->unregisterLicense($iClientId, $iEntryId, $sOrder, $sLicense))
  1216. return false;
  1217. bx_alert($this->getName(), 'license_unregister', 0, false, [
  1218. 'id' => $aCommodity['id'],
  1219. 'type' => $aCommodity['type'],
  1220. 'product_id' => $iEntryId,
  1221. 'profile_id' => $iClientId,
  1222. 'order' => $sOrder,
  1223. 'license' => $sLicense,
  1224. 'count' => $iItemCount
  1225. ]);
  1226. return true;
  1227. }
  1228. protected function _unregisterCartItemPromotion($iClientId, $iSellerId, $aCommodity, $iItemCount, $sOrder, $sLicense)
  1229. {
  1230. $CNF = &$this->_oConfig->CNF;
  1231. $aEntry = $this->_oDb->getContentInfoById($aCommodity['entry_id']);
  1232. if(empty($aEntry) || !is_array($aEntry))
  1233. return false;
  1234. $iEntryId = (int)$aCommodity['entry_id'];
  1235. if(!$this->_oDb->unregisterPromotion($aEntry[$CNF['FIELD_AUTHOR']], $aCommodity['id'], $iEntryId, $sOrder, $sLicense))
  1236. return false;
  1237. bx_alert($this->getName(), 'promotion_unregister', 0, false, [
  1238. 'id' => $aCommodity['id'],
  1239. 'type' => $aCommodity['type'],
  1240. 'product_id' => $iEntryId,
  1241. 'profile_id' => $iClientId,
  1242. 'amount' => $aCommodity['amount'],
  1243. 'order' => $sOrder,
  1244. 'license' => $sLicense
  1245. ]);
  1246. return true;
  1247. }
  1248. /**
  1249. * @page service Service Calls
  1250. * @section bx_ads Ads
  1251. * @subsection bx_ads-payments Payments
  1252. * @subsubsection bx_ads-unregister_subscription_item unregister_subscription_item
  1253. *
  1254. * @code bx_srv('bx_ads', 'unregister_subscription_item', [...]); @endcode
  1255. *
  1256. * Unregister an earlier processed subscription (recurring payment) inside the Ads module. Is called with payment processing module after the subscription was unregistered there.
  1257. *
  1258. * @param $iClientId client ID.
  1259. * @param $iSellerId seller ID
  1260. * @param $iItemId item ID.
  1261. * @param $iItemCount the number of purchased items.
  1262. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1263. * @param $sLicense license number genereted with payment processing module for internal usage
  1264. * @return boolean value determining where the subscription was unregistered or not.
  1265. *
  1266. * @see BxAdsModule::serviceUnregisterSubscriptionItem
  1267. */
  1268. /**
  1269. * @ref bx_ads-unregister_subscription_item "unregister_subscription_item"
  1270. */
  1271. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  1272. {
  1273. return true;
  1274. }
  1275. /**
  1276. * @page service Service Calls
  1277. * @section bx_ads Ads
  1278. * @subsection bx_ads-payments Payments
  1279. * @subsubsection bx_ads-cancel_subscription_item cancel_subscription_item
  1280. *
  1281. * @code bx_srv('bx_ads', 'cancel_subscription_item', [...]); @endcode
  1282. *
  1283. * Cancel an earlier processed subscription (recurring payment) inside the Ads module. Is called with payment processing module after the subscription was canceled there.
  1284. *
  1285. * @param $iClientId client ID.
  1286. * @param $iSellerId seller ID
  1287. * @param $iItemId item ID.
  1288. * @param $iItemCount the number of purchased items.
  1289. * @param $sOrder order number received from payment provider (PayPal, Stripe, etc)
  1290. * @return boolean value determining where the subscription was canceled or not.
  1291. *
  1292. * @see BxAdsModule::serviceCancelSubscriptionItem
  1293. */
  1294. /**
  1295. * @ref bx_ads-cancel_subscription_item "cancel_subscription_item"
  1296. */
  1297. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  1298. {
  1299. return true;
  1300. }
  1301. public function serviceGetOffersCount($sStatus = '')
  1302. {
  1303. if(!$this->_oConfig->isAuction())
  1304. return 0;
  1305. if(empty($sStatus) || !in_array($sStatus, $this->_aOfferStatuses))
  1306. $sStatus = BX_ADS_OFFER_STATUS_AWAITING;
  1307. $iUserId = bx_get_logged_profile_id();
  1308. return $this->_oDb->getOffersBy(array('type' => 'content_author_id', 'author_id' => $iUserId, 'status' => $sStatus, 'count' => true));
  1309. }
  1310. public function serviceGetLiveUpdates($sStatus, $aMenuItemParent, $aMenuItemChild, $iCount = 0)
  1311. {
  1312. if(!in_array($sStatus, $this->_aOfferStatuses))
  1313. return false;
  1314. $iUserId = bx_get_logged_profile_id();
  1315. $iCountNew = $this->_oDb->getOffersBy(array('type' => 'content_author_id', 'author_id' => $iUserId, 'status' => $sStatus, 'count' => true));
  1316. if($iCountNew == $iCount)
  1317. return false;
  1318. return array(
  1319. 'count' => $iCountNew, // required
  1320. 'method' => 'bx_menu_show_live_update(oData)', // required
  1321. 'data' => array(
  1322. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  1323. 'content' => '{count}'
  1324. )),
  1325. 'mi_parent' => $aMenuItemParent,
  1326. 'mi_child' => $aMenuItemChild
  1327. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  1328. );
  1329. }
  1330. public function serviceOffers()
  1331. {
  1332. $CNF = &$this->_oConfig->CNF;
  1333. if(!$this->_oConfig->isAuction())
  1334. return '';
  1335. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_OFFERS_ALL']);
  1336. if(!$oGrid)
  1337. return '';
  1338. return $oGrid->getCode();
  1339. }
  1340. public function serviceEntityOffers($iContentId = 0)
  1341. {
  1342. $CNF = &$this->_oConfig->CNF;
  1343. if(!$this->_oConfig->isAuction())
  1344. return '';
  1345. $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_OFFERS']);
  1346. if(!$oGrid)
  1347. return '';
  1348. if(empty($iContentId) && ($_iContentId = bx_get('id')) !== false)
  1349. $iContentId = (int)$_iContentId;
  1350. $oGrid->setContentId($iContentId);
  1351. return $oGrid->getCode();
  1352. }
  1353. public function serviceEntityOfferAccepted($iContentId = 0)
  1354. {
  1355. $CNF = &$this->_oConfig->CNF;
  1356. if(!$this->_oConfig->isAuction())
  1357. return '';
  1358. $iUserId = bx_get_logged_profile_id();
  1359. if(empty($iContentId) && ($_iContentId = bx_get('id')) !== false)
  1360. $iContentId = (int)$_iContentId;
  1361. $aContent = $this->_oDb->getContentInfoById($iContentId);
  1362. if(empty($aContent) || !is_array($aContent) || $aContent[$CNF['FIELD_QUANTITY']] <= 0)
  1363. return '';
  1364. $aOffer = $this->_oDb->getOffersBy(array(
  1365. 'type' => 'content_and_author_ids',
  1366. 'content_id' => $iContentId,
  1367. 'author_id' => $iUserId,
  1368. 'status' => BX_ADS_OFFER_STATUS_ACCEPTED
  1369. ));
  1370. if(empty($aOffer) || !is_array($aOffer))
  1371. return '';
  1372. return $this->_oTemplate->entryOfferAccepted($iUserId, $aContent, $aOffer);
  1373. }
  1374. public function serviceIsSourcesAvaliable()
  1375. {
  1376. return $this->_oConfig->isSources();
  1377. }
  1378. public function serviceGetSources($iAuthorId)
  1379. {
  1380. $aSources = $this->_oDb->getSources(['sample' => 'all']);
  1381. $aOptions = $this->_oDb->getSourcesOptions($iAuthorId);
  1382. $aResult = [];
  1383. foreach($aSources as $sSource => $aSource) {
  1384. if(!isset($aOptions[$aSource['option_prefix'] . 'active']) || $aOptions[$aSource['option_prefix'] . 'active']['value'] != 'on')
  1385. continue;
  1386. foreach($aOptions as $sName => $aOption)
  1387. if(strpos($sName, $aSource['option_prefix']) !== false)
  1388. $aSource['options'][$sName] = $aOption;
  1389. $aResult[$sSource] = $aSource;
  1390. }
  1391. return $aResult;
  1392. }
  1393. public function serviceGetSource($iAuthorId, $sAuthorSource = '')
  1394. {
  1395. $aSourcesOptions = $this->serviceGetSources($iAuthorId);
  1396. if(empty($aSourcesOptions) || !is_array($aSourcesOptions))
  1397. return false;
  1398. $mixedResult = false;
  1399. if(!empty($sAuthorSource)) {
  1400. if(!empty($aSourcesOptions[$sAuthorSource]) && is_array($aSourcesOptions[$sAuthorSource]))
  1401. $mixedResult = $aSourcesOptions[$sAuthorSource];
  1402. }
  1403. else
  1404. $mixedResult = array_shift($aSourcesOptions);
  1405. return $mixedResult;
  1406. }
  1407. public function serviceLoadEntryFromSource($sSourceType, $sSource)
  1408. {
  1409. $iProfileId = bx_get_logged_profile_id();
  1410. $oSource = $this->getObjectSource($sSourceType, $iProfileId);
  1411. if(!$oSource)
  1412. return [];
  1413. return $oSource->getEntry($sSource);
  1414. }
  1415. public function serviceBlockSourcesDetails($iProfileId = 0)
  1416. {
  1417. $CNF = &$this->_oConfig->CNF;
  1418. if(!isLogged())
  1419. return [
  1420. 'content' => MsgBox(_t('_Access denied'))
  1421. ];
  1422. $iProfileId = !empty($iProfileId) ? $iProfileId : bx_get_logged_profile_id();
  1423. $oForm = BxTemplFormView::getObjectInstance($CNF['OBJECT_FORM_SOURCES_DETAILS'], $CNF['OBJECT_FORM_SOURCES_DETAILS_DISPLAY_EDIT']);
  1424. $oForm->setProfileId($iProfileId);
  1425. $oForm->initChecker();
  1426. if($oForm->isSubmitted()) {
  1427. if($oForm->isValid()) {
  1428. $aOptions = $this->_oDb->getSourcesOptions();
  1429. foreach($aOptions as $aOption) {
  1430. $sValue = bx_get($aOption['name']) !== false ? bx_get($aOption['name']) : '';
  1431. $this->_oDb->updateSourceOption($iProfileId, $aOption['id'], bx_process_input($sValue));
  1432. }
  1433. header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_SOURCES'])));
  1434. return;
  1435. }
  1436. else
  1437. foreach($oForm->aInputs as $aInput)
  1438. if(!empty($aInput['error']) && !empty($aInput['attrs']['bx-data-source'])) {
  1439. $sSourceBlock = 'source_' . (int)$aInput['attrs']['bx-data-source'] . '_begin';
  1440. if(!empty($oForm->aInputs[$sSourceBlock]))
  1441. $oForm->aInputs[$sSourceBlock]['collapsed'] = false;
  1442. }
  1443. }
  1444. return [
  1445. 'content' => $oForm->getCode()
  1446. ];
  1447. }
  1448. public function isEntryActive($aContentInfo)
  1449. {
  1450. $CNF = &$this->_oConfig->CNF;
  1451. $iViewer = bx_get_logged_profile_id();
  1452. $bModerator = $this->_isModerator();
  1453. $mixedResult = null;
  1454. bx_alert($this->getName(), 'is_entry_active', 0, 0, ['viewer_id' => $iViewer, 'is_moderator' => $bModerator, 'content_info' => $aContentInfo, 'override_result' => &$mixedResult]);
  1455. if($mixedResult !== null)
  1456. return $mixedResult;
  1457. if($aContentInfo[$CNF['FIELD_AUTHOR']] == $iViewer || $bModerator)
  1458. return true;
  1459. if(isset($CNF['FIELD_STATUS']) && !in_array($aContentInfo[$CNF['FIELD_STATUS']], array(BX_BASE_MOD_TEXT_STATUS_ACTIVE, BX_ADS_STATUS_OFFER, BX_ADS_STATUS_SOLD)))
  1460. return false;
  1461. if(isset($CNF['FIELD_STATUS_ADMIN']) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  1462. return false;
  1463. return true;
  1464. }
  1465. public function isAuction($aContentInfo)
  1466. {
  1467. $CNF = &$this->_oConfig->CNF;
  1468. return $this->_oConfig->isAuction() && (int)$aContentInfo[$CNF['FIELD_AUCTION']] != 0;
  1469. }
  1470. public function isAllowedMakeOffer($mixedContent, $isPerformAction = false)
  1471. {
  1472. return $this->checkAllowedMakeOffer($mixedContent, $isPerformAction) === CHECK_ACTION_RESULT_ALLOWED;
  1473. }
  1474. public function checkAllowedMakeOffer($mixedContent, $isPerformAction = false)
  1475. {
  1476. $CNF = &$this->_oConfig->CNF;
  1477. $sTxtError = '_sys_txt_access_denied';
  1478. $iProfileId = bx_get_logged_profile_id();
  1479. if(!is_array($mixedContent))
  1480. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1481. if(empty($mixedContent) || !is_array($mixedContent))
  1482. return _t($sTxtError);
  1483. if(!$this->isAuction($mixedContent) || (int)$mixedContent[$CNF['FIELD_QUANTITY']] <= 0)
  1484. return _t($sTxtError);
  1485. if($mixedContent[$CNF['FIELD_AUTHOR']] == $iProfileId)
  1486. return _t($sTxtError);
  1487. $aCheck = checkActionModule($iProfileId, 'make offer', $this->getName(), $isPerformAction);
  1488. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1489. return $aCheck[CHECK_ACTION_MESSAGE];
  1490. return CHECK_ACTION_RESULT_ALLOWED;
  1491. }
  1492. public function isAllowedViewOffers($mixedContent, $isPerformAction = false)
  1493. {
  1494. return $this->checkAllowedViewOffers($mixedContent, $isPerformAction) === CHECK_ACTION_RESULT_ALLOWED;
  1495. }
  1496. public function checkAllowedViewOffers($mixedContent, $isPerformAction = false)
  1497. {
  1498. $CNF = &$this->_oConfig->CNF;
  1499. $sTxtError = '_sys_txt_access_denied';
  1500. if(!is_array($mixedContent))
  1501. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1502. if(empty($mixedContent) || !is_array($mixedContent))
  1503. return _t($sTxtError);
  1504. if(!$this->isAuction($mixedContent))
  1505. return _t($sTxtError);
  1506. if($this->_isModerator())
  1507. return CHECK_ACTION_RESULT_ALLOWED;
  1508. if($mixedContent[$CNF['FIELD_AUTHOR']] == bx_get_logged_profile_id())
  1509. return CHECK_ACTION_RESULT_ALLOWED;
  1510. return _t($sTxtError);
  1511. }
  1512. public function isAllowedMarkShipped($mixedContent, $isPerformAction = false)
  1513. {
  1514. return $this->checkAllowedMarkShipped($mixedContent, $isPerformAction) === CHECK_ACTION_RESULT_ALLOWED;
  1515. }
  1516. public function checkAllowedMarkShipped($mixedContent, $isPerformAction = false)
  1517. {
  1518. return $this->checkAllowedMarkShippedForProfile($mixedContent, $isPerformAction);
  1519. }
  1520. public function checkAllowedMarkShippedForProfile($mixedContent, $isPerformAction = false, $iProfileId = false)
  1521. {
  1522. $CNF = &$this->_oConfig->CNF;
  1523. $sTxtError = '_sys_txt_access_denied';
  1524. if(!$iProfileId)
  1525. $iProfileId = bx_get_logged_profile_id();
  1526. if(!is_array($mixedContent))
  1527. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1528. if(empty($mixedContent) || !is_array($mixedContent) || !$this->isSingle($mixedContent))
  1529. return _t($sTxtError);
  1530. if((int)$mixedContent[$CNF['FIELD_SHIPPED']] != 0 || $mixedContent[$CNF['FIELD_STATUS']] != BX_ADS_STATUS_SOLD || $mixedContent[$CNF['FIELD_AUTHOR']] != $iProfileId)
  1531. return _t($sTxtError);
  1532. return CHECK_ACTION_RESULT_ALLOWED;
  1533. }
  1534. public function isAllowedMarkReceived($mixedContent, $isPerformAction = false)
  1535. {
  1536. return $this->checkAllowedMarkReceived($mixedContent, $isPerformAction) === CHECK_ACTION_RESULT_ALLOWED;
  1537. }
  1538. public function checkAllowedMarkReceived($mixedContent, $isPerformAction = false)
  1539. {
  1540. return $this->checkAllowedMarkReceivedForProfile($mixedContent, $isPerformAction);
  1541. }
  1542. public function checkAllowedMarkReceivedForProfile($mixedContent, $isPerformAction = false, $iProfileId = false)
  1543. {
  1544. $CNF = &$this->_oConfig->CNF;
  1545. $sTxtError = '_sys_txt_access_denied';
  1546. if(!$iProfileId)
  1547. $iProfileId = bx_get_logged_profile_id();
  1548. if(!is_array($mixedContent))
  1549. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1550. if(empty($mixedContent) || !is_array($mixedContent) || !$this->isSingle($mixedContent))
  1551. return _t($sTxtError);
  1552. if((int)$mixedContent[$CNF['FIELD_RECEIVED']] != 0 || !$this->_oDb->hasLicense($iProfileId, $mixedContent[$CNF['FIELD_ID']]))
  1553. return _t($sTxtError);
  1554. return CHECK_ACTION_RESULT_ALLOWED;
  1555. }
  1556. public function onPublished($iContentId)
  1557. {
  1558. $this->serviceUpdateCategoriesStats($iContentId);
  1559. parent::onPublished($iContentId);
  1560. }
  1561. public function onOfferAdded($iOfferId, &$aResult)
  1562. {
  1563. $CNF = &$this->_oConfig->CNF;
  1564. $aOfferInfo = $this->_oDb->getOffersBy(array('type' => 'id', 'id' => $iOfferId));
  1565. if(empty($aOfferInfo) || !is_array($aOfferInfo))
  1566. return;
  1567. $iContentId = (int)$aOfferInfo[$CNF['FIELD_OFR_CONTENT']];
  1568. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1569. if(empty($aContentInfo) || !is_array($aContentInfo))
  1570. return;
  1571. $iViewer = bx_get_logged_profile_id();
  1572. $oViewer = BxDolProfile::getInstanceMagic($iViewer);
  1573. if(getParam($CNF['PARAM_USE_IIN']) == 'on')
  1574. sendMailTemplate($CNF['ETEMPLATE_OFFER_ADDED'], 0, $aContentInfo[$CNF['FIELD_AUTHOR']], array(
  1575. 'viewer_name' => $oViewer->getDisplayName(),
  1576. 'viewer_url' => $oViewer->getUrl(),
  1577. 'entry_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  1578. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', array(
  1579. 'i' => $CNF['URI_VIEW_ENTRY'],
  1580. $CNF['FIELD_ID'] => $iContentId
  1581. )))
  1582. ));
  1583. $aParams = $this->_alertParamsOffer($aContentInfo, $aOfferInfo);
  1584. $aParams['override_result'] = &$aResult;
  1585. bx_alert($this->getName(), 'offer_added', $iOfferId, $aOfferInfo[$CNF['FIELD_OFR_AUTHOR']], $aParams);
  1586. }
  1587. public function onOfferAccepted($iOfferId)
  1588. {
  1589. $CNF = &$this->_oConfig->CNF;
  1590. $aOfferInfo = $this->_oDb->getOffersBy(array('type' => 'id', 'id' => $iOfferId));
  1591. if(empty($aOfferInfo) || !is_array($aOfferInfo))
  1592. return;
  1593. $iContentId = (int)$aOfferInfo[$CNF['FIELD_OFR_CONTENT']];
  1594. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1595. if(empty($aContentInfo) || !is_array($aContentInfo))
  1596. return;
  1597. if($aContentInfo[$CNF['FIELD_STATUS']] != BX_ADS_STATUS_OFFER && ($this->isSingle($aContentInfo) || $this->getAvailableQuantity($aContentInfo) <= 0))
  1598. if(!$this->_oDb->updateEntriesBy([$CNF['FIELD_STATUS'] => BX_ADS_STATUS_OFFER], [$CNF['FIELD_ID'] => $iContentId]))
  1599. return;
  1600. $this->serviceUpdateCategoriesStats($iContentId);
  1601. $iOfferer = (int)$aOfferInfo[$CNF['FIELD_OFR_AUTHOR']];
  1602. $oOfferer = BxDolProfile::getInstanceMagic($iOfferer);
  1603. if(getParam($CNF['PARAM_USE_IIN']) == 'on')
  1604. sendMailTemplate($CNF['ETEMPLATE_OFFER_ACCEPTED'], 0, $iOfferer, array(
  1605. 'offerer_name' => $oOfferer->getDisplayName(),
  1606. 'offerer_url' => $oOfferer->getUrl(),
  1607. 'entry_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  1608. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', array(
  1609. 'i' => $CNF['URI_VIEW_ENTRY'],
  1610. $CNF['FIELD_ID'] => $iContentId
  1611. )))
  1612. ));
  1613. $aParams = $this->_alertParamsOffer($aContentInfo, $aOfferInfo);
  1614. bx_alert($this->getName(), 'offer_accepted', $iOfferId, $aContentInfo[$CNF['FIELD_AUTHOR']], $aParams);
  1615. }
  1616. public function onOfferDeclined($iOfferId)
  1617. {
  1618. $CNF = &$this->_oConfig->CNF;
  1619. $aOfferInfo = $this->_oDb->getOffersBy(array('type' => 'id', 'id' => $iOfferId));
  1620. if(empty($aOfferInfo) || !is_array($aOfferInfo))
  1621. return;
  1622. $iContentId = (int)$aOfferInfo[$CNF['FIELD_OFR_CONTENT']];
  1623. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1624. if(empty($aContentInfo) || !is_array($aContentInfo))
  1625. return;
  1626. $iOfferer = (int)$aOfferInfo[$CNF['FIELD_OFR_AUTHOR']];
  1627. $oOfferer = BxDolProfile::getInstanceMagic($iOfferer);
  1628. if(getParam($CNF['PARAM_USE_IIN']) == 'on')
  1629. sendMailTemplate($CNF['ETEMPLATE_OFFER_DECLINED'], 0, $iOfferer, array(
  1630. 'offerer_name' => $oOfferer->getDisplayName(),
  1631. 'offerer_url' => $oOfferer->getUrl(),
  1632. 'entry_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  1633. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', array(
  1634. 'i' => $CNF['URI_VIEW_ENTRY'],
  1635. $CNF['FIELD_ID'] => $iContentId
  1636. )))
  1637. ));
  1638. $aParams = $this->_alertParamsOffer($aContentInfo, $aOfferInfo);
  1639. bx_alert($this->getName(), 'offer_declined', $iOfferId, $aContentInfo[$CNF['FIELD_AUTHOR']], $aParams);
  1640. }
  1641. public function onOfferCanceled($iOfferId)
  1642. {
  1643. $CNF = &$this->_oConfig->CNF;
  1644. $aOfferInfo = $this->_oDb->getOffersBy(['type' => 'id', 'id' => $iOfferId]);
  1645. if(empty($aOfferInfo) || !is_array($aOfferInfo))
  1646. return;
  1647. $iContentId = (int)$aOfferInfo[$CNF['FIELD_OFR_CONTENT']];
  1648. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1649. if(empty($aContentInfo) || !is_array($aContentInfo))
  1650. return;
  1651. if(!$this->_oDb->updateEntriesBy([$CNF['FIELD_STATUS'] => BX_BASE_MOD_TEXT_STATUS_ACTIVE], [$CNF['FIELD_ID'] => $iContentId]))
  1652. return;
  1653. $this->serviceUpdateCategoriesStats($iContentId);
  1654. $iOfferer = (int)$aOfferInfo[$CNF['FIELD_OFR_AUTHOR']];
  1655. $oOfferer = BxDolProfile::getInstanceMagic($iOfferer);
  1656. if(getParam($CNF['PARAM_USE_IIN']) == 'on')
  1657. sendMailTemplate($CNF['ETEMPLATE_OFFER_CANCELED'], 0, (int)$aContentInfo[$CNF['FIELD_AUTHOR']], [
  1658. 'offerer_name' => $oOfferer->getDisplayName(),
  1659. 'offerer_url' => $oOfferer->getUrl(),
  1660. 'entry_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  1661. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', [
  1662. 'i' => $CNF['URI_VIEW_ENTRY'],
  1663. $CNF['FIELD_ID'] => $iContentId
  1664. ]))
  1665. ]);
  1666. $aParams = $this->_alertParamsOffer($aContentInfo, $aOfferInfo);
  1667. bx_alert($this->getName(), 'offer_canceled', $iOfferId, $aOfferInfo[$CNF['FIELD_OFR_AUTHOR']], $aParams);
  1668. }
  1669. /**
  1670. * Common methods.
  1671. */
  1672. public function onShipped($mixedContent, $iProfileSrc = 0, $iProfileDst = 0)
  1673. {
  1674. $CNF = &$this->_oConfig->CNF;
  1675. if(!is_array($mixedContent))
  1676. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1677. $iContentId = (int)$mixedContent[$CNF['FIELD_ID']];
  1678. if(empty($iProfileSrc))
  1679. $iProfileSrc = (int)$mixedContent[$CNF['FIELD_AUTHOR']];
  1680. $oProfileSrc = BxDolProfile::getInstanceMagic($iProfileSrc);
  1681. $aOffer = $this->_oDb->getOffersBy(array('type' => 'accepted', 'content_id' => $iContentId));
  1682. $bOffer = !empty($aOffer) && is_array($aOffer);
  1683. if(empty($iProfileDst) && $bOffer)
  1684. $iProfileDst = (int)$aOffer[$CNF['FIELD_OFR_AUTHOR']];
  1685. if(!empty($oProfileSrc) && !empty($iProfileDst) && getParam($CNF['PARAM_USE_IIN']) == 'on')
  1686. sendMailTemplate($CNF['ETEMPLATE_SHIPPED'], 0, $iProfileDst, array(
  1687. 'vendor_name' => $oProfileSrc->getDisplayName(),
  1688. 'vendor_url' => $oProfileSrc->getUrl(),
  1689. 'entry_name' => $mixedContent[$CNF['FIELD_TITLE']],
  1690. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', array(
  1691. 'i' => $CNF['URI_VIEW_ENTRY'],
  1692. $CNF['FIELD_ID'] => $iContentId
  1693. )))
  1694. ));
  1695. $aParams = $this->_alertParams($mixedContent);
  1696. $aParams = array_merge($aParams, array(
  1697. 'profile_src' => $iProfileSrc,
  1698. 'profile_dst' => $iProfileDst,
  1699. 'offer_id' => $bOffer ? (int)$aOffer[$CNF['FIELD_OFR_ID']] : 0,
  1700. ));
  1701. bx_alert($this->getName(), 'shipped', $iContentId, false, $aParams);
  1702. }
  1703. public function onReceived($mixedContent, $iProfileSrc = 0, $iProfileDst = 0)
  1704. {
  1705. $CNF = &$this->_oConfig->CNF;
  1706. if(!is_array($mixedContent))
  1707. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1708. $iContentId = (int)$mixedContent[$CNF['FIELD_ID']];
  1709. if(empty($iProfileDst))
  1710. $iProfileDst = (int)$mixedContent[$CNF['FIELD_AUTHOR']];
  1711. $aOffer = $this->_oDb->getOffersBy(array('type' => 'accepted', 'content_id' => $iContentId));
  1712. $bOffer = !empty($aOffer) && is_array($aOffer);
  1713. if(empty($iProfileSrc) && $bOffer)
  1714. $iProfileSrc = (int)$aOffer[$CNF['FIELD_OFR_AUTHOR']];
  1715. $oProfileSrc = BxDolProfile::getInstanceMagic($iProfileSrc);
  1716. if(!empty($iProfileSrc) && !empty($iProfileDst) && getParam($CNF['PARAM_USE_IIN']) == 'on')
  1717. sendMailTemplate($CNF['ETEMPLATE_RECEIVED'], 0, $iProfileDst, array(
  1718. 'client_name' => $oProfileSrc->getDisplayName(),
  1719. 'client_url' => $oProfileSrc->getUrl(),
  1720. 'entry_name' => $mixedContent[$CNF['FIELD_TITLE']],
  1721. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', array(
  1722. 'i' => $CNF['URI_VIEW_ENTRY'],
  1723. $CNF['FIELD_ID'] => $iContentId
  1724. )))
  1725. ));
  1726. $aParams = $this->_alertParams($mixedContent);
  1727. $aParams = array_merge($aParams, array(
  1728. 'profile_src' => $iProfileSrc,
  1729. 'profile_dst' => $iProfileDst,
  1730. 'offer_id' => $bOffer ? (int)$aOffer[$CNF['FIELD_OFR_ID']] : 0,
  1731. ));
  1732. bx_alert($this->getName(), 'received', $iContentId, false, $aParams);
  1733. }
  1734. public function getObjectSource($sSource, $iProfileId = 0)
  1735. {
  1736. $aSource = $this->_oDb->getSources(array('sample' => 'by_name', 'name' => $sSource));
  1737. if(empty($aSource) || !is_array($aSource) || empty($aSource['class_name']))
  1738. return false;
  1739. if(!empty($iProfileId)) {
  1740. $aSource['author'] = (int)$iProfileId;
  1741. $aSource['options'] = $this->_oDb->getSourcesOptions((int)$iProfileId, $aSource['id']);
  1742. }
  1743. $sClassPath = !empty($aSource['class_file']) ? BX_DIRECTORY_PATH_ROOT . $aSource['class_file'] : $this->_oConfig->getClassPath() . $aSource['class_name'] . '.php';
  1744. if(!file_exists($sClassPath))
  1745. return false;
  1746. require_once($sClassPath);
  1747. return new $aSource['class_name']($this->_iProfileId, $aSource, $this);
  1748. }
  1749. public function offerAccept($iId)
  1750. {
  1751. $CNF = &$this->_oConfig->CNF;
  1752. $aOfferInfo = $this->_oDb->getOffersBy(['type' => 'id', 'id' => $iId]);
  1753. if(empty($aOfferInfo) || !is_array($aOfferInfo))
  1754. return false;
  1755. $iContentId = (int)$aOfferInfo[$CNF['FIELD_OFR_CONTENT']];
  1756. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1757. if(empty($aContentInfo) || !is_array($aContentInfo))
  1758. return false;
  1759. $iQuantity = (int)$aOfferInfo[$CNF['FIELD_OFR_QUANTITY']];
  1760. $iQuantityAvailable = $this->getAvailableQuantity($aContentInfo);
  1761. if($iQuantity > $iQuantityAvailable)
  1762. return _t('_bx_ads_txt_err_offer_wrong_quantity', $iQuantityAvailable);
  1763. if(!$this->_oDb->updateOffer([$CNF['FIELD_OFR_STATUS'] => BX_ADS_OFFER_STATUS_ACCEPTED], [$CNF['FIELD_OFR_ID'] => $iId]))
  1764. return false;
  1765. $this->onOfferAccepted($iId);
  1766. return true;
  1767. }
  1768. public function offerDecline($iId)
  1769. {
  1770. $CNF = &$this->_oConfig->CNF;
  1771. if(!$this->_oDb->updateOffer([$CNF['FIELD_OFR_STATUS'] => BX_ADS_OFFER_STATUS_DECLINED], [$CNF['FIELD_OFR_ID'] => $iId]))
  1772. return false;
  1773. $this->onOfferDeclined($iId);
  1774. return true;
  1775. }
  1776. public function offerCancel($iId)
  1777. {
  1778. $CNF = &$this->_oConfig->CNF;
  1779. if(!$this->_oDb->updateOffer([$CNF['FIELD_OFR_STATUS'] => BX_ADS_OFFER_STATUS_CANCELED], [$CNF['FIELD_OFR_ID'] => $iId]))
  1780. return false;
  1781. $this->onOfferCanceled($iId);
  1782. return true;
  1783. }
  1784. public function offerPay($iId)
  1785. {
  1786. $CNF = &$this->_oConfig->CNF;
  1787. $aOffer = $this->_oDb->getOffersBy(array('type' => 'id', 'id' => $iId));
  1788. if(empty($aOffer) || !is_array($aOffer))
  1789. return false;
  1790. $iContent = (int)$aOffer[$CNF['FIELD_OFR_CONTENT']];
  1791. $aContent = $this->_oDb->getContentInfoById($iContent);
  1792. $iContentAuthor = (int)$aContent[$CNF['FIELD_AUTHOR']];
  1793. $aReturn = array(
  1794. 'redirect' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php', array(
  1795. 'i' => $CNF['URI_VIEW_ENTRY'],
  1796. $CNF['FIELD_ID'] => $aOffer[$CNF['FIELD_OFR_CONTENT']]
  1797. )))
  1798. );
  1799. $oPayments = BxDolPayments::getInstance();
  1800. $aResult = $oPayments->addToCart($iContentAuthor, $this->getName(), $iContent, $aOffer[$CNF['FIELD_OFR_QUANTITY']]);
  1801. if(!empty($aResult) && is_array($aResult)) {
  1802. if(!empty($aResult['message']))
  1803. $aReturn['msg'] = $aResult['message'];
  1804. if(isset($aResult['code']) && (int)$aResult['code'] == 0)
  1805. $aReturn['redirect'] = $oPayments->getCartUrl($iContentAuthor);
  1806. }
  1807. return $aReturn;
  1808. }
  1809. public function processMetasAdd($iContentId)
  1810. {
  1811. if(!parent::processMetasAdd($iContentId))
  1812. return false;
  1813. $CNF = &$this->_oConfig->CNF;
  1814. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  1815. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1816. $aCategoryTypes = $this->_oDb->getCategoryTypes(array('type' => 'all'));
  1817. foreach($aCategoryTypes as $aCategoryType)
  1818. $oMetatags->metaAddAuto($iContentId, $aContentInfo, $CNF, $aCategoryType['display_add']);
  1819. return true;
  1820. }
  1821. public function processMetasEdit($iContentId, $oForm)
  1822. {
  1823. if(!parent::processMetasEdit($iContentId, $oForm))
  1824. return false;
  1825. $CNF = &$this->_oConfig->CNF;
  1826. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  1827. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1828. $aCategoryTypes = $this->_oDb->getCategoryTypes(array('type' => 'all'));
  1829. foreach($aCategoryTypes as $aCategoryType)
  1830. $oMetatags->metaAddAuto($iContentId, $aContentInfo, $CNF, $aCategoryType['display_edit']);
  1831. return true;
  1832. }
  1833. public function getCategoryDisplay($sDisplayType, $iCategory = 0)
  1834. {
  1835. if(empty($iCategory) && bx_get('category') !== false)
  1836. $iCategory = (int)bx_get('category');
  1837. if(empty($iCategory))
  1838. return false;
  1839. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  1840. $sKey = 'type_display_' . $sDisplayType;
  1841. if(empty($aCategory[$sKey]))
  1842. return false;
  1843. return $aCategory[$sKey];
  1844. }
  1845. public function isSingle($mixedContent)
  1846. {
  1847. $CNF = &$this->_oConfig->CNF;
  1848. if(!is_array($mixedContent))
  1849. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1850. return (int)$mixedContent[$CNF['FIELD_SINGLE']] == 1;
  1851. }
  1852. public function getAvailableQuantity($mixedContent)
  1853. {
  1854. $CNF = &$this->_oConfig->CNF;
  1855. if(!is_array($mixedContent))
  1856. $mixedContent = $this->_oDb->getContentInfoById((int)$mixedContent);
  1857. if(!$this->_oConfig->isAuction() || (int)$mixedContent[$CNF['FIELD_AUCTION']] == 0)
  1858. return (int)$mixedContent[$CNF['FIELD_QUANTITY']];
  1859. $iQuantity = (int)$this->_oDb->getOffersBy([
  1860. 'type' => 'quantity_reserved',
  1861. 'content_id' => $mixedContent[$CNF['FIELD_ID']],
  1862. 'status' => BX_ADS_OFFER_STATUS_ACCEPTED
  1863. ]);
  1864. return (int)$mixedContent[$CNF['FIELD_QUANTITY']] - $iQuantity;
  1865. }
  1866. /**
  1867. * Internal methods.
  1868. */
  1869. protected function _actionMarkAs($sAction)
  1870. {
  1871. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  1872. if(($mixedResult = $this->serviceMarkAs($sAction, $iContentId)) !== true)
  1873. return array('msg' => $mixedResult);
  1874. return array(
  1875. 'reload' => 1
  1876. );
  1877. }
  1878. protected function _actionChangeStatus($sStatus)
  1879. {
  1880. $CNF = &$this->_oConfig->CNF;
  1881. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  1882. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  1883. if(($mixedResult = $this->checkAllowedEdit($aContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  1884. return array('msg' => $mixedResult);
  1885. if(!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => $sStatus), array($CNF['FIELD_ID'] => $iContentId)))
  1886. return array('msg' => _t('_bx_ads_txt_err_cannot_perform_action'));
  1887. $this->serviceUpdateCategoriesStats($aContentInfo);
  1888. return array(
  1889. 'reload' => 1
  1890. );
  1891. }
  1892. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  1893. {
  1894. $CNF = &$this->_oConfig->CNF;
  1895. $mixedContent = $this->_getContent($iContentId, true);
  1896. if($mixedContent === false)
  1897. return false;
  1898. list($iContentId, $aContentInfo) = $mixedContent;
  1899. $sDisplayType = false;
  1900. switch($sFormMethod) {
  1901. case 'editDataForm':
  1902. if($sDisplay === false)
  1903. $sDisplayType = 'edit';
  1904. break;
  1905. case 'viewDataForm':
  1906. case 'viewDataEntry':
  1907. $sDisplayType = 'view';
  1908. break;
  1909. }
  1910. if($sDisplayType !== false && ($sDisplayNew = $this->getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  1911. $sDisplay = $sDisplayNew;
  1912. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  1913. }
  1914. protected function _getCategoryOptions($iParentId, &$aValues)
  1915. {
  1916. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId, 'active' => true));
  1917. foreach($aCategories as $aCategory) {
  1918. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  1919. $this->_getCategoryOptions($aCategory['id'], $aValues);
  1920. }
  1921. }
  1922. protected function _getSearchableFields($mixedDisplayType = '')
  1923. {
  1924. $CNF = &$this->_oConfig->CNF;
  1925. if(empty($mixedDisplayType))
  1926. $mixedDisplayType = array('add', 'edit');
  1927. $aResult = array();
  1928. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  1929. foreach($aDisplays as $aDisplay) {
  1930. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  1931. continue;
  1932. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  1933. if(!$oForm)
  1934. continue;
  1935. $aResult = array_merge($aResult, $oForm->aInputs);
  1936. }
  1937. return $aResult;
  1938. }
  1939. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  1940. {
  1941. $CNF = &$this->_oConfig->CNF;
  1942. $bDynamic = isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true;
  1943. $sCategory = '';
  1944. if(!empty($CNF['FIELD_CATEGORY']) && !empty($aContentInfo[$CNF['FIELD_CATEGORY']])) {
  1945. $iCategory = (int)$aContentInfo[$CNF['FIELD_CATEGORY']];
  1946. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategory));
  1947. $sCategory = _t($aCategory['title']);
  1948. $sCategoryLink = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES'], array($CNF['GET_PARAM_CATEGORY'] => $iCategory)));
  1949. }
  1950. $sPrice = _t('_bx_ads_txt_free');
  1951. if(!empty($CNF['FIELD_PRICE']) && !empty($aContentInfo[$CNF['FIELD_PRICE']]))
  1952. $sPrice = _t_format_currency_ext((float)$aContentInfo[$CNF['FIELD_PRICE']], [
  1953. 'sign' => BxDolPayments::getInstance()->getCurrencySign((int)$aContentInfo[$CNF['FIELD_AUTHOR']])
  1954. ]);
  1955. $sInclude = $this->_oTemplate->addCss(array('timeline.css'), $bDynamic);
  1956. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  1957. if(bx_is_api()){
  1958. $aResult['price'] = $sPrice;
  1959. $aResult['category_title'] = $sCategory;
  1960. }
  1961. else{
  1962. $aResult['text'] = $this->_oTemplate->parseHtmlByName('timeline_post_text.html', array(
  1963. 'category_link' => $sCategoryLink,
  1964. 'category_title' => $sCategory,
  1965. 'category_title_attr' => bx_html_attribute($sCategory),
  1966. 'price' => $sPrice,
  1967. 'text' => $aResult['text']
  1968. )) . ($bDynamic ? $sInclude : '');
  1969. }
  1970. return $aResult;
  1971. }
  1972. protected function _getBlockLicenses($sType = '')
  1973. {
  1974. $CNF = &$this->_oConfig->CNF;
  1975. $sGrid = $CNF['OBJECT_GRID_LICENSES' . (!empty($sType) ? '_' . strtoupper($sType) : '')];
  1976. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  1977. if(!$oGrid)
  1978. return '';
  1979. $this->_oDb->updateLicense(array('new' => 0), array('profile_id' => bx_get_logged_profile_id(), 'new' => 1));
  1980. $this->_oTemplate->addJs(array('licenses.js'));
  1981. return array(
  1982. 'content' => $this->_oTemplate->getJsCode('licenses', array('sObjNameGrid' => $sGrid)) . $oGrid->getCode(),
  1983. 'menu' => $CNF['OBJECT_MENU_LICENSES']
  1984. );
  1985. }
  1986. protected function _alertParamsOffer($aContentInfo, $aOfferInfo)
  1987. {
  1988. $CNF = &$this->_oConfig->CNF;
  1989. $aParams = array(
  1990. 'object_id' => (int)$aContentInfo[$CNF['FIELD_ID']],
  1991. 'object_author_id' => (int)$aContentInfo[$CNF['FIELD_AUTHOR']],
  1992. 'offer_id' => (int)$aOfferInfo[$CNF['FIELD_OFR_ID']],
  1993. 'offer_author_id' => (int)$aOfferInfo[$CNF['FIELD_OFR_AUTHOR']],
  1994. );
  1995. if(isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]))
  1996. $aParams['privacy_view'] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  1997. return $aParams;
  1998. }
  1999. }
  2000. /** @} */