BxMarketFormEntry.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 Market Market
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit entry form
  13. */
  14. class BxMarketFormEntry extends BxBaseModTextFormEntry
  15. {
  16. public function __construct($aInfo, $oTemplate = false)
  17. {
  18. $this->MODULE = 'bx_market';
  19. parent::__construct($aInfo, $oTemplate);
  20. $CNF = &$this->_oModule->_oConfig->CNF;
  21. $iProfileId = bx_get_logged_profile_id();
  22. if(isset($this->aInputs[$CNF['FIELD_TITLE']], $this->aInputs[$CNF['FIELD_NAME']])) {
  23. $sJsObject = $this->_oModule->_oConfig->getJsObject('form');
  24. $aMask = array('mask' => "javascript:%s.checkName(this, '%s', '%s');", $sJsObject, $CNF['FIELD_TITLE'], $CNF['FIELD_NAME']);
  25. if($this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'] && bx_get('id') !== false) {
  26. $aMask['mask'] = "javascript:%s.checkName(this, '%s', '%s', %d);";
  27. $aMask[] = (int)bx_get('id');
  28. }
  29. $sOnBlur = call_user_func_array('sprintf', array_values($aMask));
  30. $this->aInputs[$CNF['FIELD_TITLE']]['attrs']['onblur'] = $sOnBlur;
  31. $this->aInputs[$CNF['FIELD_NAME']]['attrs']['onblur'] = $sOnBlur;
  32. }
  33. if(isset($this->aInputs[$CNF['FIELD_COVER_RAW']]))
  34. $this->aInputs[$CNF['FIELD_COVER_RAW']]['code'] = 1;
  35. if(isset($this->aInputs[$CNF['FIELD_FILE']])) {
  36. $this->aInputs[$CNF['FIELD_FILE']]['storage_object'] = $CNF['OBJECT_STORAGE_FILES'];
  37. $this->aInputs[$CNF['FIELD_FILE']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_FILE']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_FILE']]['value']) : $CNF['OBJECT_UPLOADERS'];
  38. $this->aInputs[$CNF['FIELD_FILE']]['images_transcoder'] = '';
  39. $this->aInputs[$CNF['FIELD_FILE']]['storage_private'] = 1;
  40. $this->aInputs[$CNF['FIELD_FILE']]['multiple'] = true;
  41. $this->aInputs[$CNF['FIELD_FILE']]['content_id'] = 0;
  42. $this->aInputs[$CNF['FIELD_FILE']]['ghost_template'] = '';
  43. }
  44. $bRecurring = $this->_oModule->_oDb->getParam($CNF['OPTION_ENABLE_RECURRING']) == 'on';
  45. if(!$bRecurring) {
  46. $this->aInputs[$CNF['FIELD_DURATION_RECURRING']]['type'] = 'hidden';
  47. $this->aInputs[$CNF['FIELD_PRICE_RECURRING']]['type'] = 'hidden';
  48. $this->aInputs[$CNF['FIELD_PRICE_RECURRING']]['value'] = 0;
  49. unset($this->aInputs[$CNF['FIELD_HEADER_BEG_RECURRING']]);
  50. unset($this->aInputs[$CNF['FIELD_HEADER_END_RECURRING']]);
  51. }
  52. $oPayment = BxDolPayments::getInstance();
  53. if(isset($this->aInputs[$CNF['FIELD_WARNING_SINGLE']])) {
  54. if(!$oPayment->isAcceptingPayments($iProfileId, BX_PAYMENT_TYPE_SINGLE))
  55. $this->aInputs[$CNF['FIELD_WARNING_SINGLE']]['value'] = MsgBox(_t($this->aInputs[$CNF['FIELD_WARNING_SINGLE']]['value'], $oPayment->getDetailsUrl()));
  56. else
  57. unset($this->aInputs[$CNF['FIELD_WARNING_SINGLE']]);
  58. }
  59. if(isset($this->aInputs[$CNF['FIELD_WARNING_RECURRING']])) {
  60. if($bRecurring && !$oPayment->isAcceptingPayments($iProfileId, BX_PAYMENT_TYPE_RECURRING))
  61. $this->aInputs[$CNF['FIELD_WARNING_RECURRING']]['value'] = MsgBox(_t($this->aInputs[$CNF['FIELD_WARNING_RECURRING']]['value'], $oPayment->getDetailsUrl()));
  62. else
  63. unset($this->aInputs[$CNF['FIELD_WARNING_RECURRING']]);
  64. }
  65. if(isset($this->aInputs[$CNF['FIELD_ALLOW_PURCHASE_TO']]))
  66. $this->aInputs[$CNF['FIELD_ALLOW_PURCHASE_TO']] = BxDolPrivacy::getGroupChooser($CNF['OBJECT_PRIVACY_PURCHASE']);
  67. $aDynamicGroups = array(
  68. array ('key' => '', 'value' => '----'),
  69. array ('key' => 'c', 'value' => _t('_bx_market_privacy_group_customers'))
  70. );
  71. if(isset($this->aInputs[$CNF['FIELD_ALLOW_COMMENT_TO']])) {
  72. $this->aInputs[$CNF['FIELD_ALLOW_COMMENT_TO']] = BxDolPrivacy::getGroupChooser($CNF['OBJECT_PRIVACY_COMMENT'], $iProfileId, array('dynamic_groups' => $aDynamicGroups));
  73. $this->aInputs[$CNF['FIELD_ALLOW_COMMENT_TO']]['db']['pass'] = 'Xss';
  74. }
  75. if(isset($this->aInputs[$CNF['FIELD_ALLOW_VOTE_TO']])) {
  76. $this->aInputs[$CNF['FIELD_ALLOW_VOTE_TO']] = BxDolPrivacy::getGroupChooser($CNF['OBJECT_PRIVACY_VOTE'], $iProfileId, array('dynamic_groups' => $aDynamicGroups));
  77. $this->aInputs[$CNF['FIELD_ALLOW_VOTE_TO']]['db']['pass'] = 'Xss';
  78. }
  79. if(isset($this->aInputs[$CNF['FIELD_SUBENTRIES']]) && $this->_oModule->checkAllowedSetSubentries() !== CHECK_ACTION_RESULT_ALLOWED)
  80. unset($this->aInputs[$CNF['FIELD_SUBENTRIES']]);
  81. }
  82. function getCode($bDynamicMode = false)
  83. {
  84. $sCss = $this->_oModule->_oTemplate->addCss([BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css'], $bDynamicMode);
  85. $sJs = $this->_oModule->_oTemplate->addJs([
  86. 'codemirror/codemirror.min.js',
  87. 'form.js'
  88. ], $bDynamicMode);
  89. $sCode = '';
  90. if($bDynamicMode)
  91. $sCode .= $sCss . $sJs;
  92. $sCode .= $this->_oModule->_oTemplate->getJsCode('form');
  93. $sCode .= parent::getCode($bDynamicMode);
  94. return $sCode;
  95. }
  96. function initChecker ($aValues = array (), $aSpecificValues = array())
  97. {
  98. $CNF = &$this->_oModule->_oConfig->CNF;
  99. if(isset($this->aInputs[$CNF['FIELD_SUBENTRIES']]) && !empty($aValues[$CNF['FIELD_ID']])) {
  100. $oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTION_SUBENTRIES']);
  101. if($oConnection)
  102. $this->aInputs[$CNF['FIELD_SUBENTRIES']]['value'] = $oConnection->getConnectedContent((int)$aValues[$CNF['FIELD_ID']]);
  103. }
  104. return parent::initChecker($aValues, $aSpecificValues);
  105. }
  106. public function insert ($aValsToAdd = array(), $isIgnore = false)
  107. {
  108. $CNF = &$this->_oModule->_oConfig->CNF;
  109. if(isset($this->aInputs[$CNF['FIELD_NAME']])) {
  110. $sName = BxDolForm::getSubmittedValue($CNF['FIELD_NAME'], $this->aFormAttrs['method'], $this->_aSpecificValues);
  111. $sName = $this->_oModule->_oConfig->getEntryName($sName);
  112. BxDolForm::setSubmittedValue($CNF['FIELD_NAME'], $sName, $this->aFormAttrs['method'], $this->_aSpecificValues);
  113. }
  114. if($this->_oModule->checkAllowedSetCover() === CHECK_ACTION_RESULT_ALLOWED) {
  115. $aCover = isset($_POST[$CNF['FIELD_COVER']]) ? bx_process_input ($_POST[$CNF['FIELD_COVER']], BX_DATA_INT) : false;
  116. $aValsToAdd[$CNF['FIELD_COVER']] = 0;
  117. if(!empty($aCover) && is_array($aCover) && ($iFileCover = array_pop($aCover)))
  118. $aValsToAdd[$CNF['FIELD_COVER']] = $iFileCover;
  119. }
  120. $aPackage = bx_process_input(bx_get($CNF['FIELD_PACKAGE']), BX_DATA_INT);
  121. $aValsToAdd[$CNF['FIELD_PACKAGE']] = 0;
  122. if(!empty($aPackage) && is_array($aPackage) && ($iFilePackage = array_pop($aPackage)))
  123. $aValsToAdd[$CNF['FIELD_PACKAGE']] = $iFilePackage;
  124. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  125. if(!empty($iContentId))
  126. $this->processFiles($CNF['FIELD_FILE'], $iContentId, true, $CNF['OBJECT_STORAGE_FILES']);
  127. return $iContentId;
  128. }
  129. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  130. {
  131. $CNF = &$this->_oModule->_oConfig->CNF;
  132. $sNameKey = $CNF['FIELD_NAME'];
  133. if(isset($this->aInputs[$sNameKey])) {
  134. $sName = $this->getCleanValue($sNameKey);
  135. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  136. if($aContentInfo[$sNameKey] != $sName) {
  137. $sName = $this->_oModule->_oConfig->getEntryName($sName);
  138. BxDolForm::setSubmittedValue($sNameKey, $sName, $this->aFormAttrs['method'], $this->_aSpecificValues);
  139. }
  140. }
  141. if($this->_oModule->checkAllowedSetCover() === CHECK_ACTION_RESULT_ALLOWED) {
  142. $aCover = bx_process_input (bx_get($CNF['FIELD_COVER']), BX_DATA_INT);
  143. $aValsToAdd[$CNF['FIELD_COVER']] = 0;
  144. if (!empty($aCover) && is_array($aCover) && ($iFileCover = array_pop($aCover)))
  145. $aValsToAdd[$CNF['FIELD_COVER']] = $iFileCover;
  146. }
  147. $aPackage = bx_process_input(bx_get($CNF['FIELD_PACKAGE']), BX_DATA_INT);
  148. $aValsToAdd[$CNF['FIELD_PACKAGE']] = 0;
  149. if(!empty($aPackage) && is_array($aPackage) && ($iFilePackage = array_pop($aPackage)))
  150. $aValsToAdd[$CNF['FIELD_PACKAGE']] = $iFilePackage;
  151. $iResult = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  152. $this->processFiles($CNF['FIELD_FILE'], $iContentId, false, $CNF['OBJECT_STORAGE_FILES']);
  153. return $iResult;
  154. }
  155. function delete ($iContentId, $aContentInfo = array())
  156. {
  157. $CNF = &$this->_oModule->_oConfig->CNF;
  158. $bResult = parent::delete($iContentId, $aContentInfo);
  159. if(!$bResult)
  160. return $bResult;
  161. // delete associated files
  162. if (!empty($CNF['OBJECT_STORAGE_FILES'])) {
  163. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  164. if($oStorage)
  165. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  166. }
  167. // delete associations
  168. $bResult &= $this->_oModule->_oDb->deassociatePhotoWithContent($iContentId, 0);
  169. $bResult &= $this->_oModule->_oDb->deassociateFileWithContent($iContentId, 0);
  170. return $bResult;
  171. }
  172. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  173. {
  174. parent::_associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField);
  175. $sStorage = $oStorage->getObject();
  176. switch($sStorage) {
  177. case $this->_oModule->_oConfig->CNF['OBJECT_STORAGE']:
  178. $this->_oModule->_oDb->associatePhotoWithContent($iContentId, $iFileId, $this->getCleanValue('title-' . $iFileId));
  179. break;
  180. case $this->_oModule->_oConfig->CNF['OBJECT_STORAGE_FILES']:
  181. $aParams = array(
  182. 'type' => $this->getCleanValue('type-' . $iFileId)
  183. );
  184. switch ($aParams['type']) {
  185. case BX_MARKET_FILE_TYPE_VERSION:
  186. $aParams = array_merge($aParams, array(
  187. 'version' => $this->getCleanValue('version-' . $iFileId),
  188. ));
  189. break;
  190. case BX_MARKET_FILE_TYPE_UPDATE:
  191. $aParams = array_merge($aParams, array(
  192. 'version' => $this->getCleanValue('version-from-' . $iFileId),
  193. 'version_to' => $this->getCleanValue('version-to-' . $iFileId),
  194. ));
  195. break;
  196. }
  197. $this->_oModule->_oDb->associateFileWithContent($iContentId, $iFileId, $aParams);
  198. break;
  199. }
  200. }
  201. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  202. {
  203. $CNF = &$this->_oModule->_oConfig->CNF;
  204. $aResult = parent::_getPhotoGhostTmplVars($aContentInfo);
  205. $aResult = array_merge($aResult, array(
  206. 'cover_id' => isset($aContentInfo[$CNF['FIELD_COVER']]) ? $aContentInfo[$CNF['FIELD_COVER']] : 0,
  207. 'bx_if:set_cover' => array (
  208. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetCover(),
  209. 'content' => array (
  210. 'name_cover' => $CNF['FIELD_COVER'],
  211. ),
  212. ),
  213. ));
  214. return $aResult;
  215. }
  216. protected function _getFileGhostTmplVars($aContentInfo = array())
  217. {
  218. $CNF = &$this->_oModule->_oConfig->CNF;
  219. $aResult = parent::_getFileGhostTmplVars($aContentInfo);
  220. $aResult = array_merge($aResult, array(
  221. 'thumb_id' => isset($aContentInfo[$CNF['FIELD_PACKAGE']]) ? $aContentInfo[$CNF['FIELD_PACKAGE']] : 0,
  222. 'bx_if:set_thumb' => array (
  223. 'condition' => true,
  224. 'content' => array(
  225. 'name_thumb' => $CNF['FIELD_PACKAGE'],
  226. ),
  227. ),
  228. ));
  229. return $aResult;
  230. }
  231. function genInputPrice(&$aInput)
  232. {
  233. $CNF = &$this->_oModule->_oConfig->CNF;
  234. if(!isset($aInput['value_currency'])) {
  235. $iAuthorId = 0;
  236. if(!empty($this->_iContentId)) {
  237. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($this->_iContentId);
  238. if(!empty($aContentInfo) || is_array($aContentInfo))
  239. $iAuthorId = $aContentInfo[$CNF['FIELD_AUTHOR']];
  240. }
  241. else
  242. $iAuthorId = bx_get_logged_profile_id();
  243. $aInput['value_currency'] = BxDolPayments::getInstance()->getCurrencyCode($iAuthorId);
  244. }
  245. return parent::genInputPrice($aInput);
  246. }
  247. protected function genCustomInputSubentries ($aInput)
  248. {
  249. $CNF = &$this->_oModule->_oConfig->CNF;
  250. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'get_subentries';
  251. $aInput['placeholder'] = _t('_bx_market_form_entry_input_subentries_placeholder');
  252. $sVals = '';
  253. if(!empty($aInput['value']) && is_array($aInput['value'])) {
  254. foreach($aInput['value'] as $iValue) {
  255. $iValue = (int)$iValue;
  256. if(!$iValue)
  257. continue;
  258. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iValue);
  259. if(empty($aContentInfo) || !is_array($aContentInfo))
  260. continue;
  261. $sVals .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . $aContentInfo[$CNF['FIELD_TITLE']] . '<input type="hidden" name="' . $aInput['name'] . '[]" value="' . $iValue . '" /></b>';
  262. }
  263. $sVals = trim($sVals, ',');
  264. }
  265. $aInput['value'] = $sVals;
  266. return $this->genCustomInputUsernamesSuggestions($aInput);
  267. }
  268. }
  269. /** @} */