BxBaseUploaderHTML5.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 $_sUploaderFormTemplate = 'uploader_form_html5.html';
  18. function __construct ($aObject, $sStorageObject, $sUniqId, $oTemplate)
  19. {
  20. parent::__construct($aObject, $sStorageObject, $sUniqId, $oTemplate);
  21. $this->_sDivId = 'bx-form-input-files-' . $sUniqId . '-div-' . $this->_aObject['object'];
  22. $this->_sFocusDivId = 'bx-form-input-files-' . $sUniqId . '-focus-' . $this->_aObject['object'];
  23. $this->_sButtonTemplate = 'uploader_button_html5.html';
  24. }
  25. /**
  26. * add necessary js, css files and js translations
  27. */
  28. public function addCssJs ($bDynamic = false)
  29. {
  30. $s = parent::addCssJs ($bDynamic);
  31. $aJs = [
  32. 'filepond/filepond.min.js',
  33. 'filepond/filepond-plugin-image-preview.min.js',
  34. 'filepond/filepond-plugin-image-transform.min.js',
  35. 'filepond/filepond-plugin-image-crop.min.js',
  36. 'filepond/filepond-plugin-image-resize.min.js',
  37. 'filepond/filepond-plugin-file-validate-size.min.js',
  38. 'filepond/filepond-plugin-file-validate-type.js',
  39. ];
  40. if($bDynamic)
  41. $s .= $this->_oTemplate->addJsPreloadedWrapped($aJs);
  42. else
  43. $this->_oTemplate->addJs($aJs);
  44. $s .= $this->_oTemplate->addCss([
  45. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'filepond/|filepond.min.css',
  46. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'filepond/|filepond-plugin-image-preview.min.css',
  47. ], $bDynamic);
  48. return $bDynamic ? $s : '';
  49. }
  50. /**
  51. * Get uploader button title
  52. */
  53. public function getUploaderButtonTitle($mixed = false)
  54. {
  55. if (is_string($mixed))
  56. return $mixed;
  57. elseif (is_array($mixed) && isset($mixed['HTML5']))
  58. return $mixed['HTML5'];
  59. else
  60. return _t('_sys_uploader_html5_button_name');
  61. }
  62. /**
  63. * Show uploader form.
  64. * @return HTML string
  65. */
  66. public function getUploaderForm($isMultiple = true, $iContentId = false, $isPrivate = true)
  67. {
  68. $iResizeWidth = (int)getParam('client_image_resize_width');
  69. $iResizeHeight = (int)getParam('client_image_resize_height');
  70. $sAcceptedFiles = $this->getAcceptedFilesExtensions();
  71. return $this->_oTemplate->parseHtmlByName($this->_sUploaderFormTemplate, array(
  72. 'form_container_id' => $this->_sFormContainerId,
  73. 'errors_container_id' => $this->_sErrorsContainerId,
  74. 'uploader_instance_name' => $this->getNameJsInstanceUploader(),
  75. 'restrictions_text' => $this->getRestrictionsText(),
  76. 'div_id' => $this->_sDivId,
  77. 'focus_div_id' => $this->_sFocusDivId,
  78. 'content_id' => $iContentId,
  79. 'storage_private' => $isPrivate,
  80. 'max_file_size' => $this->getMaxUploadFileSize(),
  81. 'accepted_files' => null === $sAcceptedFiles ? 'null' : "'" . bx_js_string($sAcceptedFiles) . "'",
  82. 'resize_width' => $iResizeWidth ? $iResizeWidth : 'null',
  83. 'resize_height' => $iResizeHeight ? $iResizeHeight : 'null',
  84. 'resize_method' => $iResizeWidth && $iResizeHeight ? "'contain'" : 'null',
  85. ));
  86. }
  87. /**
  88. * Handle uploads here.
  89. * @param $mixedFiles as usual $_FILES['some_name'] array, but maybe some other params depending on the uploader
  90. * @return nothing, but if some files failed to upload, the actual error message can be determined by calling BxDolUploader::getUploadErrorMessages()
  91. */
  92. public function handleUploads ($iProfileId, $mixedFiles, $isMultiple = true, $iContentId = false, $bPrivate = true)
  93. {
  94. $oStorage = BxDolStorage::getObjectInstance($this->_sStorageObject);
  95. if (!$isMultiple)
  96. $this->deleteGhostsForProfile($iProfileId, $iContentId);
  97. if (bx_get('file')) {
  98. $iId = $oStorage->storeFileFromXhr(bx_get('file'), $bPrivate, $iProfileId, $iContentId);
  99. }
  100. else {
  101. $iId = $oStorage->storeFileFromForm($_FILES['file'], $bPrivate, $iProfileId, $iContentId);
  102. }
  103. if ($iId) {
  104. $aResponse = array ('success' => 1);
  105. } else {
  106. $this->appendUploadErrorMessage(_t('_sys_uploader_err_msg', isset($_FILES['file']['name']) ? $_FILES['file']['name'] : bx_get('file'), $oStorage->getErrorString()));
  107. $aResponse = array ('error' => $this->getUploadErrorMessages());
  108. }
  109. echo htmlspecialchars(json_encode($aResponse), ENT_NOQUOTES);
  110. }
  111. }
  112. /** @} */