BxAdsModule.php 94 KB

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