BxAdsModule.php 108 KB

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