BxAdsModule.php 109 KB

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