BxBaseUploaderHTML5.php 4.7 KB

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