1
0

BxBaseUploaderHTML5.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Upload files using AJAX uploader with multiple files selection support (without flash),
  11. * it works in Firefox and WebKit(Safari, Chrome) browsers only, but has fallback for other browsers (IE, Opera).
  12. * @see BxDolUploader
  13. */
  14. class BxBaseUploaderHTML5 extends BxDolUploader
  15. {
  16. protected $_sDivId; ///< div id where upload button will be placed
  17. protected $_sFocusDivId;
  18. protected $_sProgressDivId;
  19. public function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
  20. {
  21. parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate);
  22. $this->_sDivId = 'bx-form-input-files-' . $sUniqId . '-div-' . $this->_aObject['object'];
  23. $this->_sFocusDivId = 'bx-form-input-files-' . $sUniqId . '-focus-' . $this->_aObject['object'];
  24. $this->_sProgressDivId = 'bx-form-input-files-' . $sUniqId . '-progress';
  25. $this->_sButtonTemplate = 'uploader_button_html5.html';
  26. $this->_sJsTemplate = 'uploader_button_html5_js.html';
  27. $this->_sUploaderFormTemplate = 'uploader_form_html5.html';
  28. $this->addJs([
  29. 'filepond/filepond.min.js',
  30. 'filepond/filepond-plugin-image-preview.min.js',
  31. 'filepond/filepond-plugin-image-transform.min.js',
  32. 'filepond/filepond-plugin-image-crop.min.js',
  33. 'filepond/filepond-plugin-image-resize.min.js',
  34. 'filepond/filepond-plugin-file-validate-size.min.js',
  35. 'filepond/filepond-plugin-file-validate-type.js',
  36. ]);
  37. $this->addCss([
  38. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'filepond/|filepond.min.css',
  39. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'filepond/|filepond-plugin-image-preview.min.css',
  40. ]);
  41. }
  42. /**
  43. * Get uploader button title
  44. */
  45. public function getUploaderButtonTitle($mixed = false)
  46. {
  47. if (is_string($mixed))
  48. return $mixed;
  49. elseif (is_array($mixed) && isset($mixed['HTML5']))
  50. return $mixed['HTML5'];
  51. else
  52. return _t('_sys_uploader_html5_button_name');
  53. }
  54. /**
  55. * Show uploader button.
  56. * @return HTML string
  57. */
  58. public function getUploaderButton($aParams = array())
  59. {
  60. $iResizeWidth = (int)getParam('client_image_resize_width');
  61. $iResizeHeight = (int)getParam('client_image_resize_height');
  62. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  63. $iContentId = $aParams['content_id'];
  64. $isPrivate = $aParams['storage_private'];
  65. $aParams = array_merge(
  66. $aParams,
  67. [
  68. 'form_container_id' => $this->_sFormContainerId,
  69. 'errors_container_id' => $this->_sErrorsContainerId,
  70. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  71. 'restrictions_text' => $this->getRestrictionsText(),
  72. 'div_id' => $this->_sDivId,
  73. 'focus_div_id' => $this->_sFocusDivId,
  74. 'progress_div_id' => $this->_sProgressDivId,
  75. 'content_id' => $iContentId,
  76. 'storage_private' => $isPrivate,
  77. 'max_file_size' => $this->getMaxUploadFileSize(),
  78. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  79. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  80. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  81. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  82. ]
  83. );
  84. return $this->_oTemplate->parseHtmlByName(isset($aParams['button_template']) ? $aParams['button_template'] : $this->_sButtonTemplate, $aParams);
  85. }
  86. public function getUploaderJsParams(){
  87. $iResizeWidth = (int)getParam('client_image_resize_width');
  88. $iResizeHeight = (int)getParam('client_image_resize_height');
  89. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  90. return [
  91. 'max_file_size' => $this->getMaxUploadFileSize(),
  92. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  93. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  94. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  95. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  96. ];
  97. }
  98. /**
  99. * Show uploader form.
  100. * @return HTML string
  101. */
  102. public function getUploaderForm($isMultiple = true, $iContentId = false, $isPrivate = true)
  103. {
  104. $iResizeWidth = (int)getParam('client_image_resize_width');
  105. $iResizeHeight = (int)getParam('client_image_resize_height');
  106. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  107. return $this->_oTemplate->parseHtmlByName($this->_sUploaderFormTemplate, array(
  108. 'form_container_id' => $this->_sFormContainerId,
  109. 'errors_container_id' => $this->_sErrorsContainerId,
  110. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  111. 'restrictions_text' => $this->getRestrictionsText(),
  112. 'div_id' => $this->_sDivId,
  113. 'focus_div_id' => $this->_sFocusDivId,
  114. 'content_id' => $iContentId,
  115. 'storage_private' => $isPrivate,
  116. 'max_file_size' => $this->getMaxUploadFileSize(),
  117. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  118. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  119. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  120. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  121. ));
  122. }
  123. /**
  124. * Handle uploads here.
  125. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  126. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  127. */
  128. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false, $bPrivate = true)
  129. {
  130. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  131. if (!$isMultiple)
  132. $this->deleteGhostsForProfile($iProfileId, $iContentId);
  133. if (bx_get('file')) {
  134. $iId = $oStorage->storeFileFromXhr(bx_get('file'), $bPrivate, $iProfileId, $iContentId);
  135. }
  136. else {
  137. $iId = $oStorage->storeFileFromForm($_FILES['file'], $bPrivate, $iProfileId, $iContentId);
  138. }
  139. if ($iId) {
  140. $aResponse = array ('success' => 1);
  141. } else {
  142. $this->appendUploadErrorMessage(_t('_sys_uploader_err_msg', isset($_FILES['file']['name']) ? $_FILES['file']['name'] : bx_get('file'), $oStorage->getErrorString()));
  143. $aResponse = array ('error' => $this->getUploadErrorMessages());
  144. }
  145. echo htmlspecialchars(json_encode($aResponse), ENT_NOQUOTES);
  146. }
  147. }
  148. /** @} */