BxAdsModule.php 109 KB

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