BxSnipcartModule.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 Snipcart Snipcart
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_SNIPCART_MODE_TEST', 'test');
  12. define('BX_SNIPCART_MODE_LIVE', 'live');
  13. /**
  14. * Snipcart module
  15. */
  16. class BxSnipcartModule extends BxBaseModTextModule
  17. {
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. $CNF = &$this->_oConfig->CNF;
  22. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  23. $CNF['FIELD_PRICE'],
  24. $CNF['FIELD_WEIGHT']
  25. ));
  26. }
  27. public function getSettings($iProfileId = 0)
  28. {
  29. if(empty($iProfileId))
  30. $iProfileId = bx_get_logged_profile_id();
  31. $aSettings = $this->_oDb->getSettings(array('type' => 'author', 'author' => $iProfileId));
  32. if(empty($aSettings) || !is_array($aSettings))
  33. return array();
  34. return $aSettings;
  35. }
  36. /**
  37. * @page service Service Calls
  38. * @section bx_snipcart Snipcart
  39. * @subsection bx_snipcart-other Other
  40. * @subsubsection bx_snipcart-get_searchable_fields get_searchable_fields
  41. *
  42. * @code bx_srv('bx_snipcart', 'get_searchable_fields', [...]); @endcode
  43. *
  44. * Get a list of searchable fields. Is used in common search engine.
  45. *
  46. * @return an array with a list of searchable fields.
  47. *
  48. * @see BxSnipcartModule::serviceGetSearchableFields
  49. */
  50. /**
  51. * @ref bx_snipcart-get_searchable_fields "get_searchable_fields"
  52. */
  53. public function serviceGetSearchableFields ($aInputsAdd = array())
  54. {
  55. $CNF = &$this->_oConfig->CNF;
  56. $aResult = parent::serviceGetSearchableFields($aInputsAdd);
  57. unset($aResult[$CNF['FIELD_PRICE']], $aResult[$CNF['FIELD_WEIGHT']]);
  58. return $aResult;
  59. }
  60. /**
  61. * @page service Service Calls
  62. * @section bx_snipcart Snipcart
  63. * @subsection bx_snipcart-integration Integration
  64. * @subsubsection bx_snipcart-include_css_js include_css_js
  65. *
  66. * @code bx_srv('bx_snipcart', 'include_css_js', [...]); @endcode
  67. *
  68. * Get integration code with all necessary CSS and JS includes.
  69. *
  70. * @return HTML sting with integration code to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. Empty string is returned if there is no enough input data.
  71. *
  72. * @see BxSnipcartModule::serviceIncludeCssJs
  73. */
  74. /**
  75. * @ref bx_snipcart-include_css_js "include_css_js"
  76. */
  77. public function serviceIncludeCssJs($iProfileId = 0)
  78. {
  79. $iProfileId = !empty($iProfileId) ? $iProfileId : $this->_iProfileId;
  80. if(empty($iProfileId))
  81. return '';
  82. return $this->_oTemplate->getSctInclude($iProfileId);
  83. }
  84. /**
  85. * @page service Service Calls
  86. * @section bx_snipcart Snipcart
  87. * @subsection bx_snipcart-page_blocks Page Blocks
  88. * @subsubsection bx_snipcart-entity_create entity_create
  89. *
  90. * @code bx_srv('bx_snipcart', 'entity_create', [...]); @endcode
  91. *
  92. * Get page block with product creation form or an error message if something wasn't configured correctly.
  93. *
  94. * @param $sDisplay form display name to use
  95. * @return HTML string with block content to display on the site, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  96. *
  97. * @see BxSnipcartModule::serviceEntityCreate
  98. */
  99. /**
  100. * @ref bx_snipcart-entity_create "entity_create"
  101. */
  102. public function serviceEntityCreate ($sDisplay = false)
  103. {
  104. $CNF = &$this->_oConfig->CNF;
  105. $aSettings = $this->_oDb->getSettings(array('type' => 'author', 'author' => $this->_iProfileId));
  106. if(empty($aSettings) || !is_array($aSettings))
  107. return MsgBox(_t('_bx_snipcart_err_not_configured', BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_SETTINGS'])));
  108. return parent::serviceEntityCreate($sDisplay);
  109. }
  110. /**
  111. * @page service Service Calls
  112. * @section bx_snipcart Snipcart
  113. * @subsection bx_snipcart-page_blocks Page Blocks
  114. * @subsubsection bx_snipcart-settings settings
  115. *
  116. * @code bx_srv('bx_snipcart', 'settings', [...]); @endcode
  117. *
  118. * Get page block with configuration settings form.
  119. *
  120. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  121. *
  122. * @see BxSnipcartModule::serviceSettings
  123. */
  124. /**
  125. * @ref bx_snipcart-settings "settings"
  126. */
  127. public function serviceSettings()
  128. {
  129. $CNF = &$this->_oConfig->CNF;
  130. if(!$this->_iProfileId)
  131. return MsgBox(_t('_Access denied'));
  132. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_SETTINGS'], $CNF['OBJECT_FORM_SETTINGS_DISPLAY_EDIT'], $this->_oTemplate);
  133. if(!$oForm)
  134. return MsgBox(_t('_sys_txt_error_occured'));
  135. $aSettings = $this->_oDb->getSettings(array('type' => 'author', 'author' => $this->_iProfileId));
  136. $bSettings = !empty($aSettings) && is_array($aSettings);
  137. $sResultTimer = 0;
  138. $sResultMessage = '';
  139. $oForm->initChecker($aSettings);
  140. if($oForm->isSubmittedAndValid()) {
  141. if(!$bSettings) {
  142. if((int)$oForm->insert(array('author' => $this->_iProfileId)) > 0) {
  143. $sResultTimer = 3;
  144. $sResultMessage = '_bx_snipcart_msg_save_settings';
  145. }
  146. else
  147. $sResultMessage = '_bx_snipcart_err_save_settings';
  148. }
  149. else {
  150. if($oForm->update($aSettings['id']) !== false) {
  151. $sResultTimer = 3;
  152. $sResultMessage = '_bx_snipcart_msg_save_settings';
  153. }
  154. else
  155. $sResultMessage = '_bx_snipcart_err_save_settings';
  156. }
  157. }
  158. return array(
  159. 'content' => (!empty($sResultMessage) ? MsgBox(_t($sResultMessage), $sResultTimer) : '') . $oForm->getCode()
  160. );
  161. }
  162. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  163. {
  164. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  165. $aResult['raw'] = $this->_oTemplate->getBuyButton($aContentInfo);
  166. if(isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true)
  167. $aResult['raw'] .= $this->_oTemplate->addCss(array('timeline.css'), true);
  168. return $aResult;
  169. }
  170. }
  171. /** @} */