BxAdsModule.php 89 KB

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